是否可以使用Selenium WebDriver进行截图?
(注:不含硒遥控器)
是否可以使用Selenium WebDriver进行截图?
(注:不含硒遥控器)
当前回答
Java
在附加TestName和Timestamp的情况下为Selenium中的失败捕获屏幕截图的方法。
public class Screenshot{
final static String ESCAPE_PROPERTY = "org.uncommons.reportng.escape-output";
public static String imgname = null;
/*
* Method to Capture Screenshot for the failures in Selenium with TestName and Timestamp appended.
*/
public static void getSnapShot(WebDriver wb, String testcaseName) throws Exception {
try {
String imgpath = System.getProperty("user.dir").concat("\\Screenshot\\"+testcaseName);
File f = new File(imgpath);
if(!f.exists()) {
f.mkdir();
}
Date d = new Date();
SimpleDateFormat sd = new SimpleDateFormat("dd_MM_yy_HH_mm_ss_a");
String timestamp = sd.format(d);
imgname = imgpath + "\\" + timestamp + ".png";
// Snapshot code
TakesScreenshot snpobj = ((TakesScreenshot)wb);
File srcfile = snpobj.getScreenshotAs(OutputType.FILE);
File destFile = new File(imgname);
FileUtils.copyFile(srcfile, destFile);
}
catch(Exception e) {
e.getMessage();
}
}
其他回答
Java
String yourfilepath = "E:\\username\\Selenium_Workspace\\foldername";
// Take a snapshort
File snapshort_file = ((TakesScreenshot) mWebDriver)
.getScreenshotAs(OutputType.FILE);
// Copy the file into folder
FileUtils.copyFile(snapshort_file, new File(yourfilepath));
Java
我解决了这个问题。你可以扩展RemoteWebDriver,给它所有的接口,它的代理驱动程序实现:
WebDriver augmentedDriver = new Augmenter().augment(driver);
((TakesScreenshot)augmentedDriver).getScreenshotAs(...); // It works this way
是的,可以通过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();
使用c#和MSTestframework, 这里我创建了一个静态方法。采取了一个路径,我可以将图像存储为jpeg格式。为了更清楚,我用当前正在执行的测试用例和失败的日期和时间来命名这些屏幕截图。
/// <summary>
/// This method is used to screen shot where test method failed
/// </summary>
/// <param name="testCase">TestCaseName</param>
public static void Capture(string testCase)
{
try
{
StringBuilder path = new StringBuilder("C:/Logs/Screenshot/");
Constant.screenshot = ((ITakesScreenshot)Constant.browser).GetScreenshot();
string fileName = path.Append(string.Format(testCase + "-at-{0:yyyy-MM dd_hh-mm-ss}.jpeg", DateTime.Now)).ToString();
Constant.screenshot.SaveAsFile(fileName, ScreenshotImageFormat.Jpeg);
}
catch (Exception e)
{
File.AppendAllText("C:/Logs/FailedTestCasesLogs.txt", "\nCOULD NOT CAPTURE THE SCREENSHOT!\n");
Log(e);
}
}
Ruby
require 'rubygems'
require 'selenium-webdriver'
driver = Selenium::WebDriver.for :ie
driver.get "https://www.google.com"
driver.save_screenshot("./screen.png")
更多的文件类型和选项可用,您可以在文件takes_screenshot.rb中看到它们。