Java 任何人都可以解释 Selenium 中的 Screenshot 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21877839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:13:09  来源:igfitidea点击:

Can any one explain Screenshot in Selenium?

javaseleniumselenium-webdriver

提问by ChanGan

WebDriver driver = new FirefoxDriver();

driver.get("http://www.google.com/");

File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

// Now you can do whatever you need to do with it, for example copy somewhere
FileUtils.copyFile(scrFile, new File("c:\tmp\screenshot.png"));

Can any tell me that

谁能告诉我

File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE) 

getScreenShotAsis the method in the TakesScreenshotInterface......

getScreenShotAsTakesScreenshot接口中的方法......

(TakesScreenshot)driver, What it refers to??? can you please explain little bit?

(TakesScreenshot)driver,它指的是什么???你能解释一下吗?

采纳答案by Petr Jane?ek

The WebDriverinterface does not contain the getScreenshotAs()method, because it is possible to have a webdriver unable of taking screenshots - for example the in-memory drivers that don't render the page at all, like HtmlUnitDriver.

WebDriver接口不包含该getScreenshotAs()方法,因为可能有一个 webdriver 无法截取屏幕截图 - 例如根本不呈现页面的内存驱动程序,如HtmlUnitDriver.

In order to have the method, the driver must implement the TakesScreenshotinterface which makes it capable to ... well ... take screenshots.

为了拥有该方法,驱动程序必须实现TakesScreenshot接口,使其能够……好吧……截取屏幕截图。

Therefore, you must tell the program somehow that you want to take a screenshot and that you are absolutely sure you can do so. That's what the (TakesScreenshot)driverpart is for. In Java, it's called castingand it literally translates to "I know that this driverinstance is able to take a screenshot, please cast it to TakesScreenshottype."

因此,您必须以某种方式告诉程序您想要截取屏幕截图,并且您绝对确定可以这样做。这就是(TakesScreenshot)driver零件的用途。在 Java 中,它被称为强制转换,它的字面意思是“我知道此实例能够截取屏幕截图,请强制转换输入”。driverTakesScreenshot

If your cast succeeds, everything is fine and the driverobject will be cast at run-time to an instance of TakesScreenshot. If your cast fails, however, you'll get a ClassCastExcepionat run-time.

如果您的转换成功,则一切正常,driver对象将在运行时转换为TakesScreenshot. 但是,如果您的演员表失败,您将ClassCastExcepion在运行时得到一个。

Some examples:

一些例子:

// We already know this is ok, because FirefoxDriver implements (IS-A) TakesScreenshot.
WebDriver driver = new FirefoxDriver();
TakesScreenshot screenshottingDriver = (TakesScreenshot)driver;

// This will fail at run-time, because HtmlUnitDriver does not implement TakesScreenshot;
WebDriver driver = new HtmlUnitDriver();
TakesScreenshot screenshottingDriver = (TakesScreenshot)driver;

// You can use the `instanceof` operator to check:
if (driver instanceof TakesScreenshot) {
    // we can be sure we can take screenshots, the cast will be safe
    ((TakesScreenshot)driver).getScreenshotAs(...);
}

回答by rafaborrego

As you may read hereit indicates that the driver can take a screenshot. It is necessary to do the casting because the WebDriverinterface does not contain the getScreenshotAsmethod although it is implemented by most of the classes that implement that interface like FirefoxDriver.

正如您在此处阅读的那样,它表示驱动程序可以截取屏幕截图。有必要进行转换,因为WebDriver接口不包含getScreenshotAs方法,尽管它由大多数实现该接口的类(如FirefoxDriver )实现