【问题标题】:How are Selenium screenshots handled with multiple instances on Grid?Selenium 屏幕截图如何处理 Grid 上的多个实例?
【发布时间】:2013-08-31 20:40:32
【问题描述】:

如何处理在 Grid 上运行的多个实例的 Selenium 屏幕截图?假设我有一个 Grid Hub 驱动一个 Grid 节点,在一台 Node 机器上同时运行 3 个 Firefox 浏览器,我如何从 3 个节点线程中的每个线程中获取 3 个不同的屏幕截图?

例如,将这段代码 sn-p 用于单线程测试:

RemoteWebDriver driver;
driver = new RemoteWebDriver(new URL("http://127.1/wd/hub"), DesiredCapabilities
    .firefox() );
driver.get( "http://www.google.com/" );
WebDriver augmentedDriver = new Augmenter().augment(driver);
File screenshot = (TakesScreenshot)augmentedDriver.getScreenshotAs(OutputType
    .FILE);
System.out.println( "Page title is: " + driver.getTitle() );
System.out.println( "Screenshot is located at: " + screenshot.getAbsolutePath());
assertTrue( "Page did not contain string.", driver.getSource().contains( 
    "search") );
driver.quit();

【问题讨论】:

    标签: selenium webdriver selenium-webdriver selenium-grid


    【解决方案1】:

    它绝对可以正常工作。

    屏幕截图实际上是该特定驱动程序实例的图像,而不是遗传桌面图像。您不会在每个屏幕截图中看到多个浏览器

    【讨论】:

      【解决方案2】:

      首先,Selenium/WebDriver/Selenium Grid 不会为您处理多线程,它的底层测试框架(TestNG/JUnit/Cucumber 等)会处理它。 WebDriver 不是线程安全的,如果您正在并行运行测试,则需要确保您的代码是线程安全的。

      回到您的问题,您编写的代码将覆盖同一个屏幕截图文件。您需要将文件复制到其他名称不同的地方。我建议您在屏幕截图文件前面加上毫秒精度的时间戳,然后复制屏幕截图文件。这样,您将拥有三个不同浏览器实例的三个独特的不同屏幕截图。这在过去对我有用。

      File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
      String file_name = "screenshot_"+ Add system_time with millisecond precision 
      FileUtils.copyFile(scrFile, new File(file_name));
      

      【讨论】:

      • 我有一个 Grid 节点同时运行 3 个 Firefox 浏览器。如何获取线程名称/ID,以便我可以这样命名屏幕截图:[thread]-[dateamp]-[errorId].jpg ???
      • 我认为像 YYYY-MM-DD-HH-MM-SS-mm 这样的日期格式就足够了。如果你愿意,你可以像这样获得线程 id - Thread.currentThread().getId()
      • @RobbieWareham - 这听起来是个好主意。不好调查。
      • 谢谢。本月晚些时候,在我仔细考虑之后,我将在我的“maven-testng-selenium2”GitHub 存储库中实现它:github.com/djangofan。感谢您的想法。
      【解决方案3】:

      这是我的实用程序代码中的一个 sn-p,它工作得非常好

        String path = null;
      try {
              File source = ((TakesScreenshot)
                      driver).getScreenshotAs(OutputType.FILE);
              Calendar currentDate = Calendar.getInstance();
              SimpleDateFormat formatter = new SimpleDateFormat(
                      "yyyy/MMM/dd HH:mm:ss");
              String dateN = formatter.format(currentDate.getTime()).replace("/","_");
              String dateNow = dateN.replace(":","_");
              String snapShotDirectory = Files.screenShotDirectory  + dateNow;
      
              File f = new File(snapShotDirectory);
              if(f.mkdir()){
              path = f.getAbsolutePath() + "/" + source.getName();
              FileUtils.copyFile(source, new File(path)); 
              }
          }
          catch(IOException e) {
              path = "Failed to capture screenshot: " + e.getMessage();
          }
      

      你可以试试看。

      【讨论】:

      • 我有一个 Grid 节点同时运行 3 个 Firefox 浏览器。如何获取线程名称/ID,以便我可以这样命名屏幕截图:[thread]-[dateamp]-[errorId].jpg ???
      • 我认为时间戳是足够的信息。但是,您可以获得线程 ID。作为 Thread.currentThread().getId()
      • @AbhishekSingh 我注意到你在抄袭我的 cmets :)
      【解决方案4】:

      捕获您需要将文件复制到其他位置并使用不同名称的屏幕截图。下面的代码对你有帮助。

      创建任何名称的方法。我在这里创建 captureScreenshot 方法。

       public static void captureScreenshot(String path) throws IOException{
          try{
              File scrFile= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
      
              FileUtils.copyFile(scrFile, new File(path) );
          }
          catch (Exception e){
              System.out.println("Failed to capture screenshot");
         }
      
      }
      

      然后在您要截屏的方法中使用此方法。请参考以下代码行。这里我使用系统当前时间(以毫秒为单位)来保存多个不同名称的图像。

      captureScreenshot("././screenshots/loginerror_" + System.currentTimeMillis()+".jpg");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-10-13
        • 2016-02-12
        • 1970-01-01
        • 1970-01-01
        • 2018-03-22
        • 2018-01-29
        • 2012-04-06
        • 2013-05-27
        相关资源
        最近更新 更多