Reputation: 642
Hi All,
I have written the below code to capture the screen shot of a whole webpage.
But I'm able to capture only the partial/visible part of the webpage displayed not the whole webpage. Please suggest.
Im Using:
Selenium WebDriver Version: 3.0.0-beta3
Firefox Version: 49.0.1
OS: Win10
package Selenium_WebDriver_Part1;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Quikr {
public static WebDriver driver=null;
@Test
public void loginTest() throws InterruptedException, IOException{
System.setProperty("webdriver.gecko.driver","C:\\Eclipse\\Drivers\\geckodriver.exe");
driver = new FirefoxDriver();
driver.get("http://www.quikr.com/");
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
takeScreenShot("LoginPage");
}
public static void takeScreenShot(String fileName) throws IOException{
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy hh mm ss");
String time = dateFormat.format(now);
String resDir = System.getProperty("user.dir")+"\\"+time;
File resFolder = new File(resDir);
resFolder.mkdir();
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(resDir+"\\"+fileName+".jpeg"));
}
}
Upvotes: 2
Views: 1158
Reputation: 41
I am using selenium 3.0.0 beta4 Firefox v.43.0.1 OS Windows7 instead of System.setProperty("webdriver.gecko.driver","C:\Eclipse\Drivers\geckodriver.exe"); use following line System.setProperty("webdriver.firefox.marionette","D:\Testing\WebDriver\geckodriver.exe");
Upvotes: 2
Reputation: 338
My best advice is to use the third party image processing library Ashot and it almost resolves most of such problems as well as provides image comparison etc. Once you imported AShot use the below code to get the entire screenshot of a webpage.
public byte[] takeScreenshot(){
byte[] img = null;
Screenshot screenshot= new AShot()
.shootingStrategy(ShootingStrategies.viewportPasting(100))
.takeScreenshot(driver);
BufferedImage image = screenshot.getImage();
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write( image, "jpg", baos );
baos.flush();
byte[] imageInByte = baos.toByteArray();
baos.close();
img = imageInByte;
InputStream in = new ByteArrayInputStream(imageInByte);
BufferedImage bImageFromConvert = ImageIO.read(in);
URL location = CodeLocations.codeLocationFromClass(this.getClass());
String path = location.toString().substring(location.toString().indexOf("file:/")+5,location.toString().length());
ImageIO.write(bImageFromConvert, "jpg", new File(
path+"/screenshot.jpg"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return img;
}
Try AShot and share your thoughts
Upvotes: 1
Reputation: 247
In order to capture the entire page screenshot you need to include an additional line of code before the ((TakesScreenshot)driver).getScreenshotAs... statement.
driver = new Augmenter().augment(driver);
Upvotes: 0
Reputation: 387
This is how it is supposed to work.If you want to capture the entire web page then you'll have to zoom out the entire content into visible area and then take the screenshot.
Upvotes: 0