捕获Selenium Webdriver中并行执行测试用例的截图

时间:2013-11-22 06:39:14

标签: java selenium selenium-webdriver selenium-grid

我使用页面对象模型(POM)设计模式和 Selenium Webdriver 。我想在 Catch 块中 断言失败 时抓住屏幕截图,如下所示

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 Capture是否可以在并行执行中运行?

帮助我提供屏幕截图捕获方法并在 Catch 块中调用

2 个答案:

答案 0 :(得分:0)

正如praveen所提到的,你可以在catch块中获取截图。如果您在网格中运行,则需要扩充Remotewebdriver。

创建一个名为driver的webdriver对象,并将远程驱动程序扩充到本地驱动器。

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

RDriver是你的remoteWebDriver。

希望这会对你有所帮助

答案 1 :(得分: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");

}

和ScreenShot方法如下所示用于顺序执行并行执行

       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:此处使用的ScreenShot Capture是驱动程序级别而不是桌面级别