Wednesday, May 18, 2022

Selenium Java - Shadow DOM, TestNG, Properties File, Extent Reports, Screenshot

 package selenium;

import java.io.File;

import java.io.FileReader;

import java.io.IOException;

import java. text.SimpleDateFormat;

import java.util.Date;

import java.util.Properties;

import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;

import org.openqa.selenium.By;

import org.openqa.selenium.OutputType;

import org.openqa.selenium.TakesScreenshot;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.ITestResult;

import org.testng.annotations.AfterMethod;

import org.testng.annotations.AfterTest;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.Test;

import com.relevantcodes.extentreports.ExtentReports;

import com.relevantcodes.extentreports.ExtentTest;

import com.relevantcodes.extentreports.LogStatus;

import io.github.sukgu.Shadow;



public class Job_Failure_Login_Page_NG {

public WebDriver driver;

public ExtentReports extent;

public ExtentTest extentTest; 



@BeforeTest

public void setExtent(){

  extent = new ExtentReports(System.getProperty("user.dir")+"/test output/result/LoginPageReport.html", true); 

//To add system or environment info by using the add system info method.

extent.addSystemInfo("User Name", "Suriya");

extent.addSystemInfo("Environment", "Automation Testing");

extent.addSystemInfo("Application","Job"); 

extent.addSystemInfo("Test Scenario","Login Functionality");


}


@AfterTest

public void endReport(){

extent.flush(); 

      //extent.close(); 

}



public static String getScreenshot(WebDriver driver, String screenshotName) throws IOException{

String dateName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());

TakesScreenshot ts = (TakesScreenshot) driver;

File source = ts.getScreenshotAs(OutputType.FILE);

// after execution, you could see a folder "FailedTestsScreenshots"

// under src folder

String destination = System.getProperty("user.dir") + "/FailedTestsScreenshots/" + screenshotName + dateName  + ".png";

File finalDestination = new File(destination);

FileUtils.copyFile(source, finalDestination);

return destination;

}


@BeforeMethod

public void setup() throws InterruptedException, IOException {

System.setProperty("webdriver.chrome.driver","C:\\Selenium\\chromedriver\\chromedriver.exe");

driver = new ChromeDriver(); 

driver.manage().window().maximize();

driver.manage().deleteAllCookies();

driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

driver.get("URL");

Thread.sleep(3000);



}

  @Test(enabled=true,priority=0)

  public void user_and_password_correct() throws InterruptedException, IOException {

  extentTest = extent.startTest("Enter correct username and password");

  Shadow shadow = new Shadow(driver);

  WebElement element = shadow.findElement("amplify-form-section");

  //File Read

  FileReader reader=new FileReader("datafile.properties");

  Properties p=new Properties();  

  p.load(reader);

  

  extentTest.log(LogStatus.INFO, "Enter valid Username");

  element.findElement(By.id("username")).sendKeys(p.getProperty("username"));

  extentTest.log(LogStatus.INFO, "Enter valid Password");

  element.findElement(By.id("password")).sendKeys(p.getProperty("password"));

 

  extentTest.log(LogStatus.INFO, "Click on the SIGN IN button "); 

  WebElement signin = element.findElement(By.cssSelector("[class=button][type=submit]"));

  signin.submit();

  extentTest.log(LogStatus.INFO, "Home Page Open");

  Thread.sleep(3000);

  driver.findElement(By.linkText("LogOut")).click();

  Thread.sleep(5000);

   

 }


  
  @AfterMethod

public void Down(ITestResult result) throws IOException{


      if(result.getStatus()==ITestResult.FAILURE){

       extentTest.log(LogStatus.FAIL, "TEST CASE FAILED IS "+result.getName()); //to add name in extent report

extentTest.log(LogStatus.FAIL, "TEST CASE FAILED IS "+result.getThrowable()); //to add error/exception in extent report

            String screenshotPath = getScreenshot(driver, result.getName());

extentTest.log(LogStatus.FAIL, extentTest.addScreenCapture(screenshotPath)); //to add screenshot in extent report

//extentTest.log(LogStatus.FAIL, extentTest.addScreencast(screenshotPath)); 
    //to add screencast/video in extent report

}

else if(result.getStatus()==ITestResult.SKIP){

extentTest.log(LogStatus.SKIP, "Test Case SKIPPED IS " + result.getName());

}

else if(result.getStatus()==ITestResult.SUCCESS){

extentTest.log(LogStatus.PASS, "Test Case PASSED IS " + result.getName());


}

extent.endTest(extentTest); //ending test and ends the current test and prepare to create html report

driver.quit();

}

}

    

No comments:

Post a Comment