我有一個屏幕快照問題。當我捕捉屏幕時,它只需要可見屏幕。我想要捕捉整個頁面。下面是我的代碼。硒webdriver屏幕快照問題
WebDriver webDriver=getCurrentWebDriver();
WebDriverWaitUtility.waitForPageLoad(WebDriverGUIUtilityContants.WaitTime, webDriver);
WebDriverGUIUtility.captureScreenShot(webDriver);
我有一個屏幕快照問題。當我捕捉屏幕時,它只需要可見屏幕。我想要捕捉整個頁面。下面是我的代碼。硒webdriver屏幕快照問題
WebDriver webDriver=getCurrentWebDriver();
WebDriverWaitUtility.waitForPageLoad(WebDriverGUIUtilityContants.WaitTime, webDriver);
WebDriverGUIUtility.captureScreenShot(webDriver);
下面是硒與TestNG的代碼,以谷歌頁面截圖
public class ScreenShot {
public WebDriver d;
Logger log;
@Test
public void m1() throws Exception
{
try
{
d=new FirefoxDriver();
d.manage().window().maximize();
d.get("https://www.google.co.in/?gfe_rd=cr&ei=4caQV6fxNafnugTjpIGADg");
String pagetitle=d.getTitle();
log = Logger.getLogger(FirefoxDriver.class.getName());
log.info("logger is launched..");
log.info("Title name : "+pagetitle);
d.findElement(By.id("testing")).sendKeys("test");
d.findElement(By.cssSelector("input.gsfi")).sendKeys("gmail account");
}
catch(Exception e)
{
System.out.println("something happened, look into screenshot..");
screenShot();
}
}
public void screenShot() throws Exception
{
Files.deleteIfExists(Paths.get("G:\\"+"Test results.png"));
System.out.println("previous pics deleted...");
File scrFile = ((TakesScreenshot)d).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile,new File("G:\\"+"Test results.png"));
}
}
如果你使用Maven的,那麼你可以使用阿紹特來完成你的任務。爲此,您需要添加依賴於你的POM文件:
<dependency>
<groupId>ru.yandex.qatools.ashot</groupId>
<artifactId>ashot</artifactId>
<version>1.5.2</version>
</dependency>
,並使用代碼片段如下:
Screenshot screenshot = new AShot().shootingStrategy(new ViewportPastingStrategy(1000)).takeScreenshot(augmentedDriver);
ImageIO.write(screenshot.getImage(), "PNG", new File("d:\\tmp\\results.png"));
但是,如果你不使用Maven然後下載阿紹特罐子(版本:1.5.2)文件並將其添加到您的構建路徑。這裏是你的幫助鏈接: https://javalibs.com/artifact/ru.yandex.qatools.ashot/ashot
希望,這可能會幫助你。
hi老闆是什麼意思ViewportPastingStrategy(1000) – naveen
這是一個構造函數。可能它用於將分割的屏幕截圖合併到一個屏幕截圖中,並且該參數用於scrollTimeout。 –
@naveen,通常它發生在Chrome瀏覽器。 ChromeDriver能夠拍攝可見部分的屏幕截圖。因此,這裏的概念是使用Java腳本執行器在頁面中滾動並拍攝多個圖像,然後將它們組合成單個圖像。 FirefoxDriver能夠在沒有問題的情況下拍攝整個屏幕的圖像。這裏是一個例子
@Test(enabled=true)
public void screenShotExample() throws IOException{
//WebDriver driver = new FirefoxDriver();
System.setProperty("webdriver.chrome.driver", "yourpath to chromeDriver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.w3schools.com/");
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
driver.manage().window().maximize();
JavascriptExecutor jexec = (JavascriptExecutor)driver;
jexec.executeScript("window.scrollTo(0,0)"); // will scroll to (0,0) position
boolean isScrollBarPresent = (boolean)jexec.executeScript("return document.documentElement.scrollHeight>document.documentElement.clientHeight");
long scrollHeight = (long)jexec.executeScript("return document.documentElement.scrollHeight");
long clientHeight = (long)jexec.executeScript("return document.documentElement.clientHeight");
int fileIndex = 1;
if(driver instanceof ChromeDriver){
if(isScrollBarPresent){
while(scrollHeight > 0){
File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
org.apache.commons.io.FileUtils.copyFile(srcFile, new File("F://MyFile"+ fileIndex+".jpg"));
jexec.executeScript("window.scrollTo(0,"+clientHeight*fileIndex++ +")");
scrollHeight = scrollHeight - clientHeight;
}
}else{
File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
org.apache.commons.io.FileUtils.copyFile(srcFile, new File("F://MyFile"+ fileIndex+".jpg"));
}
}else{
File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
org.apache.commons.io.FileUtils.copyFile(srcFile, new File("F://MyFile"+ fileIndex+".jpg"));
}
// Combine all the .jpg file to single file
driver.close();
driver.quit();
}
要結合所有的圖像文件,你會發現一些幫助here。希望這會幫助你。
您正在使用哪個驅動程序(瀏覽器)? –
我使用mozilla – naveen