質問

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

役に立ちましたか?

解決 2

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

他のヒント

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top