【问题标题】:Capturing Screenshot in Parallel Execution of TestCases in Selenium Webdriver在 Selenium Webdriver 中并行执行测试用例时截屏
【发布时间】:2013-12-06 23:09:45
【问题描述】:

我正在使用页面对象模型 (POM) 设计模式和 Selenium Webdriver。我想在 Catch 块中 assertion failed 时捕获屏幕截图,如下所示

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


        }

屏幕截图捕获是否在并行执行中工作?

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

【问题讨论】:

  • 用Praveen提到的方法并行执行不行吗?
  • 屏幕截图可以并行执行,使用/不使用基于您正在使用的 POM 自动化架构的网格
  • 我在整个白色背景页面的四分之一部分获取屏幕截图。有什么方法可以让 ScreenShot 适合整个页面?

标签: java selenium selenium-webdriver selenium-grid


【解决方案1】:
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");

}

截图方法如下,顺序执行并行执行

       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 :这里使用的屏幕截图捕获是驱动程序级别而不是桌面级别

【讨论】:

    【解决方案2】:

    正如 praveen 所说,您可以在 catch 块中获取屏幕截图。如果您在网格中运行,则需要增强 Remotewebdriver。

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

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

    RDriver 是你的 remoteWebDriver。

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2019-10-03
      • 1970-01-01
      • 1970-01-01
      • 2014-05-21
      • 1970-01-01
      • 2021-05-23
      • 2022-08-16
      • 2014-10-07
      • 2012-11-23
      相关资源
      最近更新 更多