当我将环境从Windows 10 更改为 Ubuntu 时,某些功能无法正常工作。像那个功能一样截图
请帮我找到解决方案,在 Ubuntu 上截图。在 Ubuntu 上截屏时,我认为图片是元素的错误位置。谢谢
为 Windows 10 工作
public void captureElementScreenshot(WebElement element, String fileName) {
//Capture entire page screenshot as buffer.
//Used TakesScreenshot, OutputType Interface of selenium and File class of java to capture screenshot of entire page.
File screen = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
//Used selenium getSize() method to get height and width of element.
//Retrieve width of element.
int ImageWidth = element.getSize().getWidth();
//Retrieve height of element.
int ImageHeight = element.getSize().getHeight();
//Used selenium Point class to get x y coordinates of Image element.
//get location(x y coordinates) of the element.
Point point = element.getLocation();
int xcord = point.getX();
int ycord = point.getY();
//Reading full image screenshot.
BufferedImage img = null;
try {
img = ImageIO.read(screen);
BufferedImage dest = img.getSubimage(xcord, ycord, ImageWidth, ImageHeight);
ImageIO.write(dest, "png", screen);
//Used FileUtils class of apache.commons.io.
//save Image screenshot In D: drive.
FileUtils.copyFile(screen, new File(System.getProperty("user.dir") + "//src//test//screenshot//" + fileName + ".png"));
System.out.println("Success save file " + fileName);
} catch (IOException e) {
e.printStackTrace();
}
//cut Image using height, width and x y coordinates parameters.
}
还有这个配置浏览器
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "//src//test//browser//chromedriver");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
options.addArguments("force-device-scale-factor=0.75");
options.addArguments("high-dpi-support=0.75");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(capabilities);
driver.manage().window().maximize();