我正在测试类似于Paint的GWT Web应用程序。在这里,有一个我正在研究的测试用例,我必须使用Selenium Grid在另一台机器上进行测试。除此之外,我正在使用TestNG并行执行它们,在2个浏览器上测试这个测试用例。现在,我在这个测试用例中面临的问题是,我必须截取屏幕截图,然后将其保存在我当前工作目录中创建的文件夹中。但是第二个浏览器在测试期间截取的屏幕截图更新了第一个浏览器截取的屏幕截图,因为我只为这两个浏览器提供了一个类(待测试)。
我想要的只是我能够截取屏幕截图并在我的本地目录中使用不同的名称保存它们。我在互联网上搜索了很多,但没有一种方法适合我,因此,我对如何做到这一点很困惑。
同时,这是我的代码。
首先,有一个浏览器类,我已经给出了节点地址以及浏览器的功能。
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的初学者,所以我很困惑如何解决我的问题。请帮忙。
答案 0 :(得分:1)
使用其他名称保存实际图像文件。您可以使用下面给出的会话ID或浏览器名称进行存储。
方法1: 只需替换以下代码
即可File fileActual = new File("./src/Images/actualDrawing.png");
与
String sessionId = ((RemoteWebDriver) driver).getSessionId().toString();
File fileActual = new File("./src/Images/actualDrawing"+sessionId +".png");
方法2: 或者替换为
Capabilities cap = ((RemoteWebDriver) driver).getCapabilities();
String browserName = cap.getBrowserName().toLowerCase();
File fileActual = new File("./src/Images/actualDrawing"+browserName+".png");
它可能对你有帮助。