Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)

When I take screenshots with ChromeDriver I get screens with the size of my viewport. 
When I take screenshots with FirefoxDriver I get what I want, which is a full-screen print of a website.

ChromeDriver is declared like this:

IWebDriver driver = new ChromeDriver();     

FirefoxDriver is declared like this:

IWebDriver driver = new FirefoxDriver();

Both drivers execute identical code:

driver.Manage().Window.Maximize();

driver.Navigate().GoToUrl(url);//url is a string variable

ITakesScreenshot screenshotDriver = driver as ITakesScreenshot;

Screenshot screenshot = screenshotDriver.GetScreenshot();

screenshot.SaveAsFile("c:/test.png", ImageFormat.Png);

ChromeDriver's test.png is of 1920x1099 resolution and contains only the browser viewport. 
FirefoxDriver's test.png is of 1903x16559 resolution and contains the whole page.

I know that GetScreenshot()  method doesn't return identical resolution sizes because it has slightly different implementations in IEDriver, FirefoxDriver, OperaDriver, ChromeDriver.

My questions are:

  1. Why is there such a difference between ChromeDriver's and FirefoxDriver's .GetScreenshot()method, even tho they use an identical interface (ITakesScreenshot)?

  2. Is there a way to make ChromeDriver's GetScreenshot() method returns the whole webpage screen instead of just the viewport?

1 Answer

0 votes
by (62.9k points)

We can't get the entire page screenshot with ChromeDriver2, we need to go for manual implementation. I have modified a method with is available in a blog which works fine with ChromeDriver.

 Use this method as follows:

 

private IWebDriver _driver = new ChromeDriver(CHROME_DRIVER_PATH);

screenshot.SaveAsFile(saveFileName, ImageFormat.Jpeg);

 

public Bitmap GetEntereScreenshot()

    {

 

        Bitmap stitchedImage = null;

        try

        {

            long totalwidth1 = (long)((IJavaScriptExecutor)_driver).ExecuteScript("return document.body.offsetWidth");//documentElement.scrollWidth");

 

            long totalHeight1 = (long)((IJavaScriptExecutor)_driver).ExecuteScript("return  document.body.parentNode.scrollHeight");

 

            int totalWidth = (int)totalwidth1;

            int totalHeight = (int)totalHeight1;

 

            // Get the Size of the Viewport

            long viewportWidth1 = (long)((IJavaScriptExecutor)_driver).ExecuteScript("return document.body.clientWidth");//documentElement.scrollWidth");

            long viewportHeight1 = (long)((IJavaScriptExecutor)_driver).ExecuteScript("return window.innerHeight");//documentElement.scrollWidth");

 

            int viewportWidth = (int)viewportWidth1;

            int viewportHeight = (int)viewportHeight1;

 

 

        // Split the Screen in multiple Rectangles

        List<Rectangle> rectangles = new List<Rectangle>();

        // Loop until the Total Height is reached

        for (int i = 0; i < totalHeight; i += viewportHeight)

        {

            int newHeight = viewportHeight;

            // Fix if the Height of the Element is too big

            if (i + viewportHeight > totalHeight)

            {

                newHeight = totalHeight - i;

            }

            // Loop until the Total Width is reached

            for (int ii = 0; ii < totalWidth; ii += viewportWidth)

            {

                int newWidth = viewportWidth;

                // Fix if the Width of the Element is too big

                if (ii + viewportWidth > totalWidth)

                {

                    newWidth = totalWidth - ii;

                }

 

                // Create and add the Rectangle

                Rectangle currRect = new Rectangle(ii, i, newWidth, newHeight);

                rectangles.Add(currRect);

            }

        }

 

        // Build the Image

        stitchedImage = new Bitmap(totalWidth, totalHeight);

        // Get all Screenshots and stitch them together

        Rectangle previous = Rectangle.Empty;

        foreach (var rectangle in rectangles)

        {

            // Calculate the Scrolling (if needed)

            if (previous != Rectangle.Empty)

            {

                int xDiff = rectangle.Right - previous.Right;

                int yDiff = rectangle.Bottom - previous.Bottom;

                // Scroll

                //selenium.RunScript(String.Format("window.scrollBy({0}, {1})", xDiff, yDiff));

                ((IJavaScriptExecutor)_driver).ExecuteScript(String.Format("window.scrollBy({0}, {1})", xDiff, yDiff));

                System.Threading.Thread.Sleep(200);

            }

 

            // Take Screenshot

            var screenshot = ((ITakesScreenshot)_driver).GetScreenshot();

 

            // Build an Image out of the Screenshot

            Image screenshotImage;

            using (MemoryStream memStream = new MemoryStream(screenshot.AsByteArray))

            {

                screenshotImage = Image.FromStream(memStream);

            }

 

            // Calculate the Source Rectangle

            Rectangle sourceRectangle = new Rectangle(viewportWidth - rectangle.Width, viewportHeight - rectangle.Height, rectangle.Width, rectangle.Height);

 

            // Copy the Image

            using (Graphics g = Graphics.FromImage(stitchedImage))

            {

                g.DrawImage(screenshotImage, rectangle, sourceRectangle, GraphicsUnit.Pixel);

            }

 

            // Set the Previous Rectangle

            previous = rectangle;

        }

        }

        catch (Exception ex)

        {

            // handle

        }

        return stitchedImage;

    }

If you are interested to learn Selenium on a much deeper level and want to become a professional in the testing domain, check out Intellipaat’s Selenium course

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...