0

I m using Page Object Model (POM) Design Pattern and Selenium Webdriver. I want to catch the screenshot when assertion fails in Catch block as shown below

try{
        WebElement element = driver.findElement(By.xpath("//html/body/table[2]/form/table/tbody/tr/td/font"));
        String strngAcc = element.getText();
        System.out.println(strngAcc);
        AssertJUnit.assertEquals("Account Information Created Successfully",strngAcc);

        }
        catch(WebDriverException ex) {


          // Take **Screen Shot** here by Calling ScreenShot Capture Method


        }

Does Screenshot Capture works in Parallel Execution ?

Help me in providing the screenshot capture method and calling the same in Catch block

Satish
  • 724
  • 3
  • 10
  • 22
  • Its already answered here http://stackoverflow.com/questions/12969967/how-to-make-selenium-take-a-picture-whenever-an-exception-is-thrown – Praveen Kumar Nov 22 '13 at 06:51
  • Does it not work in parallel execution with the method mentioned by Praveen? – Seanny123 Nov 22 '13 at 07:00
  • Screen Shot works in parallel execution with / without using grid based on the POM Automation Architechture you are using – Harshavardhan Konakanchi Nov 24 '13 at 04:08
  • i m getting ScreenShot in the Quarter Portion of the Entire White Background Page. Is there any way to get ScreenShot which fits on Entire Page ? – Satish Nov 25 '13 at 10:55

2 Answers2

0

As praveen mentioned, you can get the screenshots in catch block. In case if you are running in the grid, the Remotewebdriver need to be augmented.

Create a webdriver object called driver and augment the remote driver to the local drive.

WebDriver  driver= new Augmenter.augment( RDriver);
( (TakesScreenshot)driver ).getScreenshotAs( ... ); 

Where RDriver is your remoteWebDriver.

hope this would help you

Karthikeyan
  • 2,634
  • 5
  • 30
  • 51
0
try{
        WebElement element = driver.findElement(By.xpath("//html/body/table[2]/form/table/tbody/tr/td/font"));
        String strngAcc = element.getText();
        System.out.println(strngAcc);
        AssertJUnit.assertEquals("Account Information Created Successfully",strngAcc);

        }
        catch(WebDriverException ex) {


          // Take **Screen Shot** here by Calling ScreenShot Capture Method
          ScreenShot.takeScreenShot(driver, "AccountTest", "createAccount");

}

and ScreenShot Method is as below which Is used for Both Sequential Execution and Parallel Execution

       static Properties prop = null;
       public static void takeScreenShot(WebDriver driver, String className,
        String methodName) {

    try {

        prop = PropertiesLoader.getPropertiesLoader();
        File scrnsht=null;
        String isSequential = prop.getProperty("isSequential");
        if (isSequential.equalsIgnoreCase("true")) {
            scrnsht = ((TakesScreenshot) driver)
                    .getScreenshotAs(OutputType.FILE);


        } else {

            WebDriver augmentedDriver = new Augmenter().augment(driver);
            scrnsht = ((TakesScreenshot) augmentedDriver)
                    .getScreenshotAs(OutputType.FILE);

        }

            Calendar currentDate = Calendar.getInstance();
    SimpleDateFormat formatter = new SimpleDateFormat(
            "yyyy/MMM/dd HH:mm:ss");
    String date = formatter.format(currentDate.getTime()).replace(
            "/", "_");
    String dateFormatetd = dateN.replace(":", "_");
    FileUtils.copyFile(scrnsht, new File(
            "D:\\SeleniumScreenShots\\" + className + "\\"
                    + methodName + dateFormatted + ".png"));

P.S : The ScreenShot Capture Used here is Driver Level and Not the Desktop Level

Satish
  • 724
  • 3
  • 10
  • 22