How to Capture Full page screenshot of using selenium WebDriver
Asked Answered
N

1

2

I have created a Selenium Full page screenshot test for Responsive tests using selenium. But if I am going to run against the SharePoint Site then it's not taking a full-page screenshot but If I'll give any particular public site to capture the screenshots.

Here is the code snippet:

public class resTest {
@Test(groups = {"Test1"})
    public void ResTest() throws InterruptedException, IOException {
        System.setProperty("webdriver.gecko.driver", "C:\\browserdriver\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        driver.get(getUrl());
        
        driver.manage().window().maximize();

        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30));

        //Enter email ID
        WebElement enterEmail = wait.until(ExpectedConditions.elementToBeClickable(By.name("loginfmt")));
        enterEmail.sendKeys(getUsername());

        WebElement clickNext = wait.until(ExpectedConditions.elementToBeClickable(By.className("win-button")));
        clickNext.click();

        //Enter Password
        WebElement SubmitPass = wait.until(ExpectedConditions.elementToBeClickable(By.name("passwd")));
        SubmitPass.sendKeys(getPassword());

        //Click on Sign In button
        WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@value='Sign in']")));
        element.click();

        WebElement afterNext = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@value='No']")));
        afterNext.click();

        Thread.sleep(9000);

        File src = ((FirefoxDriver)driver).getFullPageScreenshotAs(OutputType.FILE);
        FileHandler.copy(src, new File(System.getProperty("user.dir")+"/Res2/screen.png"));

    }
}
Namtar answered 12/5, 2022 at 9:48 Comment(0)
G
2

On a headless browser I use the following to get a screen of the full page

full_width  = driver.execute_script('return document.body.parentNode.scrollWidth')
full_height = driver.execute_script('return document.body.parentNode.scrollHeight')
driver.set_window_size(full_width, full_height)
driver.save_screenshot(path)

The advantage of a headless browser window is that you can resize it to whatever size you want, even if it is larger than your screen resolution. For example, if your screen is 1920x1080 and you resize the headless browser window to 1000x5000 and take a screenshot, then the image will be 1000x5000 pixels.

Groundwork answered 14/5, 2022 at 10:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.