Thursday 9 March 2017

Extent Report for selenium

Extent report gives you online HTML Report with screenshot.

You need to have Extent report jar with you ,It also need freemarker jar, below is Jar URL,
https://jar-download.com/?detail_search=g%3A%22com.relevantcodes%22+AND+a%3A%22extentreports%22&search_type=av&a=extentreports
Below is code for extent report,


public class HomeVerification extends BaseClass {

//extent report initializatiion
       ExtentReports report;
       ExtentTest test;
      
       @Before
       public void setBrowser() {
             
              //u need to write broser launch selenium code
      
              startBrowser("chrome");
                System.out.println("browser launchead");
       }

       @Test
       public void login() throws InterruptedException, IOException {
             
             
              //report path
              report = new ExtentReports("C:\\Users\\ylondhe\\reports\\automationreport.html"true);
              //report = new ExtentReports("D:\\automationreport.html", true);
                           test = report.startTest("Verify application Home screen");
             
              driver.get("TestingURL");
              test.log(LogStatus.INFO"URL Launched started");
              String actualTitle = driver.getTitle();
              test.log(LogStatus.INFO"page title is " + actualTitle );
             
              //Thread.sleep(30000);
              (new WebDriverWait(driver, 30))
                              .until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='headertext']")));
              List<WebElement> list = driver.findElements(By.xpath("//*[@id='navitemlist']/li[1]/a/span1"));
       //     Assert.assertTrue("Text not found!", list.size() > 0);
              System.out.println(list.size());
             
       if(list.size()>0){
          test.log(LogStatus.PASS"Element is Present");
          System.out.println("element present");
          
       }
       else{
      
       //fail scenario, capture image code
          
          File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
                     FileUtils.copyFile(scrFile,
                                  new File("D:\\img.jpg"));
                     String image = test
                                  .addScreenCapture("D:\\img.jpg");
                     test.log(LogStatus.FAIL"verify logo of the application"image);
       }
              Thread.sleep(5000);

       }

       @After
       public void closeDriver() {
              driver.close();
              report.endTest(test);
              report.flush();
       }
}