【问题标题】:How to create a new file in every parallel test using TestNG + Selenium Grid?如何使用 TestNG + Selenium Grid 在每个并行测试中创建一个新文件?
【发布时间】:2018-03-06 08:18:23
【问题描述】:

我正在测试一个类似于 Paint 的 GWT Web 应用程序。在此,我正在处理一个测试用例,我必须使用 Selenium Grid 在另一台机器上对其进行测试。除此之外,我正在两个浏览器上测试这个测试用例,方法是使用 TestNG 并行执行它们。现在,我在这个测试用例中面临的问题是我必须截取屏幕截图,然后将其保存在我在当前工作目录中创建的文件夹中。但是第二个浏览器在测试期间截取的屏幕截图更新了第一个浏览器截取的屏幕截图,因为我只有两个浏览器的 1 个类(待测试)。

我想要的是能够截取屏幕截图并将它们以不同的名称保存在我的本地目录中。我在互联网上搜索了很多,但没有一种方法适合我,因此我对如何做到这一点感到困惑。

同时,这是我的代码。

首先,有一个浏览器类,我在其中给出了节点地址以及浏览器的功能。

package testNgPackage;

 public class Browser {
    //ThreadLocal will provide thread-safe tests
    protected ThreadLocal<RemoteWebDriver> threadLocal = null;
    @BeforeTest
    @Parameters("browser")
    public void setup(String browser) throws MalformedURLException{
        String nodeMachine = "http://xxx.xxx.xxx.xxx:5555/wd/hub"; //Providing the IP address of Node
        if(browser.equalsIgnoreCase("chrome")) {

            System.setProperty("webdriver.chrome.driver", ".src/Drivers/chromedriver.exe");
            DesiredCapabilities capability = null;
            capability = DesiredCapabilities.chrome();
            capability.setPlatform(Platform.VISTA);
            capability.setBrowserName("chrome");
            threadLocal = new ThreadLocal<RemoteWebDriver>();
            threadLocal.set(new RemoteWebDriver(new URL(nodeMachine), capability));
        }
        else if(browser.equalsIgnoreCase("firefox")) {

            System.setProperty("webdriver.gecko.driver", ".src/Drivers/geckodriver.exe");
            DesiredCapabilities capability = null;
            capability = DesiredCapabilities.firefox();
            capability.setPlatform(Platform.VISTA);
            capability.setBrowserName("firefox");
            threadLocal = new ThreadLocal<RemoteWebDriver>();
            threadLocal.set(new RemoteWebDriver(new URL(nodeMachine), capability));
        }

    }

    public WebDriver getDriver() {
        return threadLocal.get();
    }


    @AfterTest
    public void closeBrowser(){
        getDriver().close();
    }

}

然后,有一个TestNGClass,其中包含要测试的代码。

package testNgPackage;

public class TestNGClass extends Browser{

    @Test
    public void testCanvas() throws InterruptedException{
        getDriver().manage().window().maximize();
        getDriver().manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS) ;
        getDriver().get("XXXX"); //Here I have specified the URL

        WebDriverWait wait = new WebDriverWait(getDriver(), 10);
        wait.until(ExpectedConditions.titleContains("xxxxx"));
        wait.until(ExpectedConditions.elementToBeClickable(By.id("yyyyy")));
        Thread.sleep(3000);
        /* 
          After that, there is a code to click on certain elements and then take the screenshot of the canvas.
          I won't mention the code here because of the privacy concerns of the company. Along with that, it is useless and wastage of time to mention it here.
        */

        //Given below is the main code which describes how I've specified the file location of Images.

        File elementImage;
        File fileBase = new File("./src/Images/baseDrawing.png");
        File fileActual = new File("./src/Images/actualDrawing.png");
        try {
            elementImage = this.takeElementScreenshot(canvas,"png");
            FileUtils.copyFile(elementImage, fileActual);
        }catch(IOException e) {
            e.printStackTrace();
        }
        this.compareImage(fileBase,fileActual);
        Thread.sleep(6000);
    }


    public File takeElementScreenshot(WebElement element, String imageFormat) throws IOException{
        /*
             Code to take the Element Screenshot. The code in this function is correct and I'm not mentioning it here to reduce the reading time of the users.

         */
        }

    public void compareImage(File fileBase, File fileActual) {
        /* 
         Code to compare the images by comparing them pixel by pixel. Not mentioning it here.
         */

        }
}

这是 testng.xml 文件,我在其中指定了要与参数一起运行的测试名称。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
 <suite name="Parallel test suite" parallel="tests" thread-count="2">
    <test name="FirefoxTest">
       <parameter name="browser" value="Firefox" />
       <classes>
         <class name="testNgPackage.TestNGClass"/>
       </classes>
    </test>
    <test name="ChromeTest">
       <parameter name="browser" value="Chrome" />
       <classes>
         <class name="testNgPackage.TestNGClass"/>
       </classes>
    </test>
</suite>

由于我是 TestNG 的初学者,因此我很困惑如何解决我的问题。请帮忙。

【问题讨论】:

    标签: java selenium selenium-webdriver testng selenium-grid


    【解决方案1】:

    以不同的名称保存实际的图像文件。您可以使用会话 ID 或浏览器名称进行存储,如下所示。

    方法一: 只需替换以下代码

    File fileActual = new File("./src/Images/actualDrawing.png");
    

    String sessionId = ((RemoteWebDriver) driver).getSessionId().toString();
    File fileActual = new File("./src/Images/actualDrawing"+sessionId +".png");
    

    方法二: 或替换为

    Capabilities cap = ((RemoteWebDriver) driver).getCapabilities();
    String browserName = cap.getBrowserName().toLowerCase();
    File fileActual = new File("./src/Images/actualDrawing"+browserName+".png");
    

    也许对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-25
      • 2016-02-20
      • 1970-01-01
      • 2014-04-04
      • 1970-01-01
      • 2022-01-23
      • 2023-04-01
      • 1970-01-01
      相关资源
      最近更新 更多