我正在使用Selenium网格在不同的PC上执行GWT web应用程序的跨浏览器测试。我面临的问题是,对于不同的电脑,生成的屏幕截图(在测试期间拍摄)的大小不同,我想让我的代码通用,即设置一些默认的屏幕截图大小。这是我的代码,我用它拍摄屏幕截图,然后将生成的图像与本地存储在电脑上的图像进行比较。
首先,我将调用CallScreenshotAndCompareImage方法
参数(画布、类名)。
这里canvas是WebElement
在GWT应用程序中表示HTML5画布。
FileBase是本地存储在我的项目中的文件,fileActual是生成的屏幕截图。
public class Browser {
protected ThreadLocal<RemoteWebDriver> threadLocal = null;
String serverMachine = "xxx.xxx.xxx.xxx:xxx/hub";
@BeforeTest
@Parameters("browser")
public void setup(String browser) throws MalformedURLException{
if(browser.equalsIgnoreCase("chrome")) {
System.setProperty("webdriver.chrome.driver", ".src/Drivers/chromedriver.exe");
DesiredCapabilities capability = null;
capability = DesiredCapabilities.chrome();
capability.setPlatform(Platform.WINDOWS);
capability.setBrowserName("chrome");
createRemoteWebDriver(capability);
}
else if(browser.equalsIgnoreCase("firefox")) {
System.setProperty("webdriver.gecko.driver", ".src/Drivers/geckodriver.exe");
DesiredCapabilities capability = null;
capability = DesiredCapabilities.firefox();
capability.setPlatform(Platform.WINDOWS);
capability.setBrowserName("firefox");
createRemoteWebDriver(capability);
}
}
public void createRemoteWebDriver(DesiredCapabilities capability) throws MalformedURLException {
threadLocal = new ThreadLocal<RemoteWebDriver>();
threadLocal.set(new RemoteWebDriver(new URL(serverMachine), capability));
}
public WebDriver getDriver() {
return threadLocal.get();
}
public void CallScreenshotAndCompareImage(WebElement element, String className) throws IOException, InterruptedException {
File fileBase1 = new File("./src/Images/baseDrawing"+className+"Chrome.png");
File fileBase2 = new File("./src/Images/baseDrawing"+className+"Firefox.png");
Capabilities cap = ((RemoteWebDriver) getDriver()).getCapabilities();
String browserName = cap.getBrowserName().toLowerCase();
File fileActual = new File("./src/Images/actualDrawing"+className+browserName+".png");
File elementImage = this.takeElementScreenshot(element,"png");
FileUtils.copyFile(elementImage, fileActual);
if(browserName.equalsIgnoreCase("chrome"))
this.compareImage(fileBase1,fileActual);
else if(browserName.equalsIgnoreCase("firefox"))
this.compareImage(fileBase2,fileActual);
Thread.sleep(3000);
}
public File takeElementScreenshot(WebElement element, String imageFormat) throws IOException{
Point elementXY = element.getLocation();
int elementHeight = element.getSize().getHeight();
int elementWidth = element.getSize().getWidth();
Rectangle elementRectArea = new Rectangle(elementWidth,elementHeight);
WrapsDriver wrapsDriver = (WrapsDriver) element;
File pageImage = ((TakesScreenshot)wrapsDriver.getWrappedDriver()).getScreenshotAs(OutputType.FILE);
BufferedImage bufferedImage = ImageIO.read(pageImage);
BufferedImage elementImage = bufferedImage.getSubimage(elementXY.getX(), elementXY.getY(), elementRectArea.width, elementRectArea.height);
ImageIO.write(elementImage, imageFormat, pageImage);
return pageImage;
}
public void compareImage(File fileBase, File fileActual) {
try {
BufferedImage bufferedImage = ImageIO.read(fileBase);
DataBuffer dataBufferFirst = bufferedImage.getData().getDataBuffer();
int sizeFirst = dataBufferFirst.getSize();
BufferedImage bufferedImage2 = ImageIO.read(fileActual);
DataBuffer dataBufferSecond = bufferedImage2.getData().getDataBuffer();
int sizeSecond = dataBufferSecond.getSize();
int count=0;
Assert.assertEquals(sizeFirst, sizeSecond,"Size of Base Drawing and actual Drawing is not same");
if(sizeFirst == sizeSecond)
{
for(int i=0; i<sizeFirst; i++)
{
if(dataBufferFirst.getElem(i) != dataBufferSecond.getElem(i))
{
count++;
}
}
Assert.assertEquals(count, 0,"Both images are not same");
}
} catch (Exception e) {
Assert.fail("Failed to compare image files...!!!");
}
}
}
运行此代码后,当我比较基本(局部)图像和实际(生成)图像的属性时,这两个图像之间有一点差异。
基础图纸
:大小->253 KB,位深度->32,尺寸->1570 x 873像素
实际图纸
:大小->232 KB,位深度->24,尺寸->1570 x 873像素
这两幅图像的其他属性都是一样的。
我应该在代码中添加什么,以便从不同电脑生成的屏幕截图大小始终相同?