ashot icon indicating copy to clipboard operation
ashot copied to clipboard

Only Current Screen is captured in Chrome Driver

Open shahankit783 opened this issue 8 years ago • 38 comments

Hi AShot Team, When I run the below java code it runs properly,but it captures only current screen. The below part of screen is not captured by this code. please see the below code :+1:

System.setProperty("webdriver.chrome.driver", "D:\\selenium\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://www.bbc.com");
			Thread.sleep(5000);
			(new WebDriverWait(driver, 60)).until(new ExpectedCondition<WebElement>() {
				public WebElement apply(WebDriver newDriver) {
					return newDriver.findElement(By.cssSelector(".wsradio__title__link"));
				}
			});
final Screenshot screenshot = new AShot().coordsProvider(new WebDriverCoordsProvider()).takeScreenshot(driver);
final BufferedImage image = screenshot.getImage();
ImageIO.write(image, "PNG", new File("D:\\temp\\"+ "AShot_BBC_Entire.png"));
driver.quit();

Please provide the solution. It works perfectly in Firefox browser.

shahankit783 avatar Dec 19 '16 10:12 shahankit783

Hi, @shahankit783. For Chrome you should use

new AShot()
  .shootingStrategy(ShootingStrategies.viewportPasting(100))
  .takeScreenshot(webDriver);

because it takes only view port screenshot. Notice that you should use different AShot configurations for different browsers. For Firefox you don't need this strategy.

pazone avatar Dec 19 '16 16:12 pazone

Hi , @pazone

Thank you for reply. For chrome driver can you please suggest what can be the possible values of "ShootingStrategies" in this case

shahankit783 avatar Dec 20 '16 05:12 shahankit783

You can find the factory of such strategies in ShootingStrategies.java. In this case viewportPasting(100) - 100 - is a timeout interval between scrolling of viewport.

pazone avatar Dec 20 '16 08:12 pazone

For chrome driver, it is not taking full web-page screenshot

  • It overlaps body parts in screenshot.
  • Also, it takes 50-70% of screenshot horizontally.

I used

new AShot()
  .shootingStrategy(ShootingStrategies.viewportPasting(100))
  .takeScreenshot(webDriver);

I tried using two ways. Both produce same result in Chrome. For firefox, things are working perfectly using above way.

new AShot()
  .shootingStrategy(ShootingStrategies.viewportPasting(100))
  .takeScreenshot(webDriver, webDriver.findElement(By.xpath("//body"));

take anything, //body OR //div[@class='<element-class-name-for-complete-container']

sspgupta avatar Mar 05 '17 10:03 sspgupta

Hi Ashot Team,

I am trying to capture the full page screenshot in chrome using following code but its only taking part of it, like it is almost cutting 25% of right and 30% bottom part. Please see attached image which is produced from Ashot. However I have 2 more columns in right and one graph in bottom which is got cut. fullpagescreenshot

Please help.

javedansari1983 avatar Mar 20 '17 17:03 javedansari1983

Hello, @javedansari1983. Could you please provide your code when you are taking screenshot?

pazone avatar Mar 21 '17 15:03 pazone

Here is my code which I am using for chrome. However I have also tried with firefox (v46) but it just takes top part screenshot and paste 4 times below and create the screenshot.

public void takeScreenshot() throws IOException { System.setProperty("webdriver.chrome.driver", "C:\Javed_Projects\LongHorn\LongHorn\drivers\chromedriver.exe"); WebDriver myDriver = new ChromeDriver(); myDriver.manage().window().maximize(); myDriver.get("C:/Javed_Projects/LongHorn/LongHorn/src/test/java/JSon/index.html"); Screenshot fpScreenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(2000)).takeScreenshot(myDriver);

    ImageIO.write(fpScreenshot.getImage(),"PNG",new File("C:/Javed_Projects/LongHorn/LongHorn/CustomEmailReport/FullPageScreenshot.png"));    
    myDriver.quit();
}

Please Help.

javedansari1983 avatar Mar 22 '17 13:03 javedansari1983

Hey Ashot Team,

I have got the full page screenshot using firefox v46 without using any strategies. Please help me for chrome.

javedansari1983 avatar Mar 22 '17 13:03 javedansari1983

Hey Ashot Team,

Can you please tell the solution for chrome??

javedansari1983 avatar Mar 24 '17 16:03 javedansari1983

Hey Ashot team,

Unfortunately I din't get answer to my last comment which is getting proper screenshot with chrome.

However I am using firefox for now and got in to another problem when I am running this using jenkins.

It works fine when I run it using eclipse. But it fails to take correct screenshot when I am running the same project using jenkins. See the attached screenshot which is got produced running the project using jenkins.

noname

javedansari1983 avatar Mar 29 '17 16:03 javedansari1983

@javedansari1983 To be honest, I can not understand the problem. At least I should know your Selenium configuration on Jenkins. Obviously it differs of your local firefox. What is the actual problem in your attached screenshots?

pazone avatar Mar 30 '17 14:03 pazone

Thanks for responding.

I am using selenium 2.53 and jenkins 2.32.3.

The problem is that the html opens up as screenshot #1 in browser either I open it manually or I run the project through eclipse. However when I am invoking this project through Jenkins it produces screenshot#2.

Screenshot#1: unnamed

Screenshot#2: 9f888654-14cd-11e7-86dd-a92e5973055c

Please help.

javedansari1983 avatar Mar 30 '17 14:03 javedansari1983

@javedansari1983 Firefox on Jenkins has smaller window size. The bast approach is to make you window size predictable. Just try to set you preferred window size before your test driver.manage().window().setSize(new Dimension(x, y));

pazone avatar Mar 31 '17 10:03 pazone

@pazone

Hey, I am also facing same issue. Vertical scroll for full page screengrab is good, but horizontally, the image gets chopped and I see only 70-75% screen. When I take just the visible screen screenshot, I get proper width. This is my method for the full screen image:

` public static String captureFullScreen(String screengrabName, WebDriver driver) throws IOException {

	int veiwPortWidth = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width;
	System.out.println("veiwPortWidth: "+veiwPortWidth);
	
	Screenshot fpScreenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000))
			.takeScreenshot(driver);
	dest = screengrabFolder + screengrabName + ".png";
	File destination = new File(dest);
	ImageIO.write(fpScreenshot.getImage(), "PNG", destination);
	System.out.println("Screengrab link: " + dest);

	return dest;
}`

I am also attaching the image of the chopped view. fullscreengrab_accountdetailspage

jiggy-rc avatar Jul 14 '17 03:07 jiggy-rc

@jiggy-rc Hi. You can solve it by adjusting your window width. driver.manage().window().setSize(new Dimension([over100500])). This workaround will help right now. Also it is possible to create VerticalAndHorisontalPastingStrtegy. But it going to take some time. I'll try to create it but it's hard to predict when it will be done.

pazone avatar Jul 14 '17 13:07 pazone

@pazone ,

Thank you so much for the quick response, but when I used driver.manage().window().setSize(new Dimension(1000,725));

I got the attached :

fullscreengrab_accountdetailspage

which kinda is worst =/

jiggy-rc avatar Jul 14 '17 15:07 jiggy-rc

@jiggy-rc Try webDriverRule.getDriver().manage().window().maximize() or new Dimension(2000,725)

pazone avatar Jul 14 '17 15:07 pazone

@pazone ,

No luck. This is what I get:

When dimensions are new Dimension(2000,725)

fullscreengrab_accountdetailspage

When dimensions are new Dimension(2500,800)

fullscreengrab_accountdetailspage

jiggy-rc avatar Jul 14 '17 16:07 jiggy-rc

Use Firefox 46.0.1 and use firefox driver to take screenshot. This is what I am using and screenshot is coming properly for me. Try it and let me know. Code Snippet: import ru.yandex.qatools.ashot.AShot; import ru.yandex.qatools.ashot.Screenshot; WebDriver myFFDriver = new FirefoxDriver(); Screenshot fpScreenshot = new AShot().takeScreenshot(myFFDriver);

javedansari1983 avatar Jul 14 '17 18:07 javedansari1983

@javedansari1983 Cannot. Company wants to use chrome only. =/

jiggy-rc avatar Jul 14 '17 20:07 jiggy-rc

@jiggy-rc It looks like a problem with you Selenium grid configuration. For some reasons browser window size is limited and your page doesn't fit in it. Do you use Selenium grid?

pazone avatar Jul 17 '17 08:07 pazone

@pazone Nope. Just regular Selenium webdriver. And our page is sized dynamically. So it will fit in any size window. Window is getting re-sized correctly, but for any size, only 75% (horizontally) page is being captured.

jiggy-rc avatar Jul 18 '17 03:07 jiggy-rc

I had problem similar to one described above where either pasting startegy wasn't working correctly by producing screenshots where some parts overlap others or where width is incorrect. It was solved by switching from setting webdriver window size to maximize(); It seems that Ashot is not working correctly with drivers that are not maximized.

mpanin avatar Aug 23 '17 14:08 mpanin

@jiggy-rc Which OS are you using? And which DPR? If you have the retina display, you should set the dpr to AShot.

pazone avatar Aug 25 '17 12:08 pazone

Actually, since I've received notification from this thread: It's worth mentioning that you would receive incorrect screenshot if screen scaling is set to more than 100%, could be checked in Display settings in Windows.

mpanin avatar Aug 25 '17 12:08 mpanin

@pazone ,

Win7 Chrome Version 60.0.3112.101 (Official Build) (64-bit)

jiggy-rc avatar Aug 25 '17 18:08 jiggy-rc

Dear Ashot Team, I have tried below code but still the screenshot is taken only for visible part of the web page and not for the scrolled down page.. PLease help!!

driver.manage().window().setSize(new Dimension(1000,725));

Also tried: driver.manage().window().maximize();

doesnt work. I am using Chrome.

apathan123 avatar Sep 18 '17 15:09 apathan123

@apathan123 , for full screen, u can use this code. But the issue is only 75-80% of the hotizontal screen is captured (vertically 100% is captured).

` public static String captureFullScreen(String screengrabName, WebDriver driver) throws Exception {

	Thread.sleep(1500); // This wait is to pause the code till the page
						// completely loads before taking screengrab.

	driver.manage().window().setSize(new Dimension(1500, 1000));
	Screenshot fpScreenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000))
			.takeScreenshot(driver);
	dest = screengrabFolder + screengrabName + ".png";
	File destination = new File(dest);
	ImageIO.write(fpScreenshot.getImage(), "PNG", destination);
	System.out.println("Screengrab link: " + dest);

	driver.manage().window().maximize();

	return dest;
}

`

jiggy-rc avatar Sep 18 '17 15:09 jiggy-rc

@pazone , My system is Win7, 64 bit, ChromeVersion 60.0.3112.113 (Official Build) (64-bit) ... nothing fancy ... and I am still not getting 100% image horizontally.

jiggy-rc avatar Sep 18 '17 15:09 jiggy-rc

@jiggy-rc

It did not work. Actually viewportpasting is throwing an exception for me so had to remove that line. raised new issue for that error. #107

apathan123 avatar Sep 19 '17 10:09 apathan123