是否可以使用Selenium WebDriver进行截图?
(注:不含硒遥控器)
是否可以使用Selenium WebDriver进行截图?
(注:不含硒遥控器)
当前回答
Java
我想我应该给出完整的解决方案,因为有两种不同的方法获得截图。一个来自本地浏览器,另一个来自远程浏览器。我甚至将图像嵌入到HTML报告中:
@After()
public void selenium_after_step(Scenario scenario) throws IOException, JSONException {
if (scenario.isFailed()){
scenario.write("Current URL = " + driver.getCurrentUrl() + "\n");
try{
driver.manage().window().maximize(); // Maximize window to get full screen for chrome
}
catch (org.openqa.selenium.WebDriverException e){
System.out.println(e.getMessage());
}
try {
if(isAlertPresent()){
Alert alert = getAlertIfPresent();
alert.accept();
}
byte[] screenshot;
if(false /*Remote Driver flow*/) { // Get a screenshot from the remote driver
Augmenter augmenter = new Augmenter();
TakesScreenshot ts = (TakesScreenshot) augmenter.augment(driver);
screenshot = ts.getScreenshotAs(OutputType.BYTES);
}
else { // Get a screenshot from the local driver
// Local webdriver user flow
screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
}
scenario.embed(screenshot, "image/png"); // Embed the image in reports
}
catch (WebDriverException wde) {
System.err.println(wde.getMessage());
}
catch (ClassCastException cce) {
cce.printStackTrace();
}
}
//seleniumCleanup();
}
其他回答
是的,可以通过Selenium WebDriver来截屏。我目前使用Chrome驱动来抓取网站图片。请参考以下方法captureScreenshot()。
您还可以添加对web驱动程序的限制,例如
使用无头版的网页浏览器 当页面加载时禁用通知 启动全屏等。
如果一个网站配备了警报框,你的网页驱动程序将无法捕捉屏幕截图,因为异常将被抛出。在这种情况下,你需要关闭警告框,然后获取截图。下面的代码片段关闭警报框。
public void captureScreenshot() throws InterruptedException, IOException {
System.out.println("Creating Chrome Driver");
// Set Chrome Driver
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
// Add arguments to Chrome Options
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("start-maximized");
chromeOptions.addArguments("--disable-gpu");
chromeOptions.addArguments("--start-fullscreen");
chromeOptions.addArguments("--disable-extensions");
chromeOptions.addArguments("--disable-popup-blocking");
chromeOptions.addArguments("--disable-notifications");
chromeOptions.addArguments("--window-size=1920,1080");
chromeOptions.addArguments("--no-sandbox");
chromeOptions.addArguments("--dns-prefetch-disable");
chromeOptions.addArguments("enable-automation");
chromeOptions.addArguments("disable-features=NetworkService");
WebDriver driver = new ChromeDriver(chromeOptions);
driver.get("https://www.google.com");
System.out.println("Wait a bit for the page to render");
TimeUnit.SECONDS.sleep(5);
System.out.println("Taking Screenshot");
File outputFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
String imageDetails = "D:\\Images";
File screenShot = new File(imageDetails).getAbsoluteFile();
FileUtils.copyFile(outputFile, screenShot);
System.out.println("Screenshot saved: {}" + imageDetails);
}
}
为了接受警报框和获取截图:
String alertText = alert.getText();
System.out.println("ERROR: (ALERT BOX DETECTED) - ALERT MSG : " + alertText);
alert.accept();
File outputFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
String imageDetails = "D://Images"
File screenShot = new File(imageDetails).getAbsoluteFile();
FileUtils.copyFile(outputFile, screenShot);
System.out.println("Screenshot saved: {}" + imageDetails);
driver.close();
Python
您可以使用Python web驱动程序从windows中捕获图像。使用下面的代码哪个页面需要捕获屏幕截图。
driver.save_screenshot('c:\foldername\filename.extension(png, jpeg)')
Java
我解决了这个问题。你可以扩展RemoteWebDriver,给它所有的接口,它的代理驱动程序实现:
WebDriver augmentedDriver = new Augmenter().augment(driver);
((TakesScreenshot)augmentedDriver).getScreenshotAs(...); // It works this way
Ruby(黄瓜)
After do |scenario|
if(scenario.failed?)
puts "after step is executed"
end
time = Time.now.strftime('%a_%e_%Y_%l_%m_%p_%M')
file_path = File.expand_path(File.dirname(__FILE__) + '/../../../../../mlife_screens_shot')+'/'+time +'.png'
page.driver.browser.save_screenshot file_path
end
Given /^snapshot$/ do
time = Time.now.strftime('%a_%e_%Y_%l_%m_%p_%M')
file_path = File.expand_path(File.dirname(__FILE__) + '/../../../../../mlife_screens_shot')+'/'+time +'.png'
page.driver.browser.save_screenshot file_path
end
硒化/ Java
以下是Selenide项目是如何做到这一点的,这比任何其他方式都要简单:
import static com.codeborne.selenide.Selenide.screenshot;
screenshot("my_file_name");
JUnit:
@Rule
public ScreenShooter makeScreenshotOnFailure =
ScreenShooter.failedTests().succeededTests();
美国小妞TestNG:
import com.codeborne.selenide.testng.ScreenShooter;
@Listeners({ ScreenShooter.class})