ExtentReport - Screenshot capture only in the case if webelement not found

78 Views Asked by At
@Beforemethod
public static String getScreenshot(WebDriver driver, String screenshotName) {
            String dateName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
            TakesScreenshot ts = (TakesScreenshot) driver;
            File src = ts.getScreenshotAs(OutputType.FILE);
            String path = System.getProperty("user.dir")+ "/FailedTestsScreenshots/"+ screenshotName + dateName + ".png";
            File destination = new File(path);
            
            try {
                FileUtils.copyFile(src, destination);
            }
            catch(IOException e){
                System.out.println("Capture Failed" +e.getMessage());
            }
            return path;
        }


@Test
    public void openWeb() throws IOException, InterruptedException {
        fis = new FileInputStream(System.getProperty("user.dir")+"\\resources\\config.properties");
        property = new Properties();
        property.load(fis);
        
        WebElement UsernameField = driver.findElement(By.name(property.getProperty("username_fieldName")));
        WebElement PasswordField = driver.findElement(By.name(property.getProperty("password_fieldName")));
        WebElement SubmitButton = driver.findElement(By.xpath(property.getProperty("submit_ButtonXpath")));
        
        
        //Test case 1 TC_1.01 - Opening the login page of the release page
        boolean usrfield = UsernameField.isDisplayed();
        boolean passfield = PasswordField.isDisplayed();
        
        extentTest = extent.startTest("TC_1.01 - Open the Login screen of the Release page for Admin");
        if(usrfield == true && passfield == true) {
            extentTest.log(LogStatus.PASS, "Login screen of the Release Page open for Admin");
            //extentTest.log(Status.PASS, MarkupHelper.createLabel("Login screen of the Release Page open for Admin", ExtentColor.GREEN));
        }
        else {
            extentTest.log(LogStatus.FAIL, "Login screen of the Release Page is not open for Admin");
        }
        
        Thread.sleep(2000);
        
        //Test case 2 TC_1.02 - Check for the language options
        //Saving element of language  drop down using its name
        WebElement Lang_Dropdown = driver.findElement(By.name(property.getProperty("language_DropDownName")));
        Lang_Dropdown.click();
        Lang_Dropdown.click();
        
        
        boolean Lang_list = Lang_Dropdown.isDisplayed();
        
        extentTest = extent.startTest("TC_1.02 - Check for the language options");
        if(Lang_list == true) {
            extentTest.log(LogStatus.PASS, "Dropdown list of multiple Languages display");
        }
        else {
            extentTest.log(LogStatus.FAIL, "Dropdown list of multiple Languages is not display");
        }
        Thread.sleep(2000);
        
        //Test case 3 TC_1.03 - Verify the Selection of a Language
        Select Lang = new Select(driver.findElement(By.name(property.getProperty("language_DropDownName"))));
        Lang.selectByVisibleText("SPA");
        
        //language is not getting selected by below line.
        //driver.findElement(By.xpath(property.getProperty("languageDropdown_ListXpath"))).click();
        
        extentTest = extent.startTest("TC_1.03 - Verify the selection of a language from the dropdown list");
        
        if(Lang_Dropdown.getText().contains("SPA")) {
            extentTest.log(LogStatus.PASS, "Selected language display in the dropdown field");
        }
        else {
            extentTest.log(LogStatus.FAIL, "Selected language is not display in the dropdown field");
        }

@AfterMethod
    public void tearDown(ITestResult result) {
        if(result.getStatus() == ITestResult.FAILURE) {
            extentTest.log(LogStatus.FAIL, "Test case failed" + result.getName());
            extentTest.log(LogStatus.FAIL, "Test case Failed" + result.getThrowable());
            
            String screenshotPath = TestCycle1.getScreenshot(driver, result.getName());
            extentTest.log(LogStatus.FAIL, extentTest.addScreenCapture(screenshotPath));
        }
        else if(result.getStatus() == ITestResult.SKIP) {
            extentTest.log(LogStatus.SKIP, "Test case skipped" + result.getName());
        }
        else if(result.getStatus() == ITestResult.SUCCESS) {
            extentTest.log(LogStatus.PASS, "Test case Passed" + result.getName());
        }
        
        extent.endTest(extentTest);
        driver.quit();
    }
    
    @AfterTest
    public void endReport() {
        extent.flush();
        //extent.close();
    }

When executing the above code, i am not getting any error, test is running successfully, with some fail testcases because of the implemented conditions but i am not getting any screenshots for this in my extent report. I am getting only one screenshot in the extent report when a test case fails due to element not found on the web page. So How to get all the screenshots for all the failed testcases either due to some conditions or due to element not found??? Any suggestions will be appreciated.

0

There are 0 best solutions below