如何使用 TestNG + Selenium Grid 在每个并行测试中创建一个新文件?
How to create a new file in every parallel test using TestNG + Selenium Grid?
我正在测试类似于画图的 GWT Web 应用程序。在这里,有一个我正在处理的测试用例,我必须使用 Selenium Grid 在不同的机器上测试它。除此之外,我通过使用 TestNG 并行执行它们在 2 个浏览器上测试这个测试用例。现在,我在这个测试用例中面临的问题是我必须截取屏幕截图,然后将其保存在我在当前工作目录中创建的本地文件夹中。但是第二个浏览器在测试期间截取的屏幕截图更新了第一个浏览器截取的屏幕截图,因为我只有 1 个 class(待测试)用于两个浏览器。
我想要的只是我能够截取屏幕截图并将它们以不同的名称保存在我的本地目录中。我在 Internet 上搜索了很多,但 none 的方法适合我,因此,我对如何执行此操作感到困惑。
同时,这是我的代码。
首先,有一个浏览器 class,我在其中提供了节点地址以及浏览器的功能。
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 的初学者,因此,我很困惑如何解决我的问题。请帮忙。
以不同的名称保存实际图像文件。您可以使用会话 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");
可能对你有帮助。
我正在测试类似于画图的 GWT Web 应用程序。在这里,有一个我正在处理的测试用例,我必须使用 Selenium Grid 在不同的机器上测试它。除此之外,我通过使用 TestNG 并行执行它们在 2 个浏览器上测试这个测试用例。现在,我在这个测试用例中面临的问题是我必须截取屏幕截图,然后将其保存在我在当前工作目录中创建的本地文件夹中。但是第二个浏览器在测试期间截取的屏幕截图更新了第一个浏览器截取的屏幕截图,因为我只有 1 个 class(待测试)用于两个浏览器。
我想要的只是我能够截取屏幕截图并将它们以不同的名称保存在我的本地目录中。我在 Internet 上搜索了很多,但 none 的方法适合我,因此,我对如何执行此操作感到困惑。
同时,这是我的代码。
首先,有一个浏览器 class,我在其中提供了节点地址以及浏览器的功能。
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 的初学者,因此,我很困惑如何解决我的问题。请帮忙。
以不同的名称保存实际图像文件。您可以使用会话 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");
可能对你有帮助。