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
Can any one explain Screenshot in Selenium?
提问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)
getScreenShotAs
is the method in the TakesScreenshot
Interface......
getScreenShotAs
是TakesScreenshot
接口中的方法......
(TakesScreenshot)driver
, What it refers to??? can you please explain little bit?
(TakesScreenshot)driver
,它指的是什么???你能解释一下吗?
采纳答案by Petr Jane?ek
The WebDriver
interface 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 TakesScreenshot
interface 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)driver
part is for. In Java, it's called castingand it literally translates to "I know that this driver
instance is able to take a screenshot, please cast it to TakesScreenshot
type."
因此,您必须以某种方式告诉程序您想要截取屏幕截图,并且您绝对确定可以这样做。这就是(TakesScreenshot)driver
零件的用途。在 Java 中,它被称为强制转换,它的字面意思是“我知道此实例能够截取屏幕截图,请强制转换为输入”。driver
TakesScreenshot
If your cast succeeds, everything is fine and the driver
object will be cast at run-time to an instance of TakesScreenshot
. If your cast fails, however, you'll get a ClassCastExcepion
at 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 )实现。