Skip to content

Screen shots full desktop selenium PageSource saving

Jakub Raczek edited this page Apr 21, 2020 · 4 revisions

Our framework allows you to take screenshots: full desktop, selenium full browser, screenshots of elements. It also allows you to save page source.

Screenshots can be taken automatically in case of test failure by setting it in app.config file:

<?xml version="1.0" encoding="utf-8"?>
...
    <!--Screenshots and logging-->
    <add key="FullDesktopScreenShotEnabled" value="true"/>
    <add key="SeleniumScreenShotEnabled" value="true"/>
    <add key="GetPageSourceEnabled" value="true"/>
...
</configuration>

Folder where screenshots will be saved is also set in in app.config file:

<?xml version="1.0" encoding="utf-8"?>
...
    <!--Downloaded files, screenshots and page source location-->
    <add key="UseCurrentDirectory" value="true"/>
    <add key="ScreenShotFolder" value="\TestOutput"/>
    <add key="PageSourceFolder" value="\TestOutput"/>
...
</configuration>

Visual Testing

Screenshots of elements can be saved to allow you images comparison with e.g baseline:

var el = this.Driver.GetElement(this.menu);
TakeScreenShot.TakeScreenShotOfElement(el, TestContext.CurrentContext.TestDirectory + BaseConfiguration.ScreenShotFolder, "MenuOutSideTheIFrame");

more details here

Screenshots of specific element within iframe can be taken by passing location of iframe to TakeScreenShotOfElement method:

var iFrame = this.Driver.GetElement(this.iframe);
int x = iFrame.Location.X;
int y = iFrame.Location.Y;
this.Driver.SwitchTo().Frame(0);
var el = this.Driver.GetElement(this.elelemtInIFrame);
TakeScreenShot.TakeScreenShotOfElement(x, y, el, TestContext.CurrentContext.TestDirectory + BaseConfiguration.ScreenShotFolder, "MenuOutSideTheIFrame");

more details here

Test example of comparing screen shot of elements using ImageMagick, more details here

        [Test]
        public void TakingScreehShotsOfElementInIFrameTest()
        {
            var internetPage = new InternetPage(this.DriverContext).OpenHomePage();
            internetPage.GoToIFramePage();
            
            IFramePage page = new IFramePage(this.DriverContext);
            var path = page.TakeScreenShotsOfTextInIFrame(TestContext.CurrentContext.TestDirectory + BaseConfiguration.ScreenShotFolder, "TextWithinIFrame" + BaseConfiguration.TestBrowser);
            var path2 = TestContext.CurrentContext.TestDirectory + BaseConfiguration.ScreenShotFolder + "\\TextWithinIFrameChromeError.png";
            bool flag = true;
            using (var img1 = new MagickImage(path))
            {
                using (var img2 = new MagickImage(path2))
                {
                    using (var imgDiff = new MagickImage())
                    {
                        img1.Compose = CompositeOperator.Src;
                        img1.Compare(img2, new ErrorMetric(), imgDiff);
                        flag = img1.Equals(img2);
                        imgDiff.Write(TestContext.CurrentContext.TestDirectory + BaseConfiguration.ScreenShotFolder + "\\" + BaseConfiguration.TestBrowser + "TextWithinIFrameDIFF.png");
                    }
                }
            }

            Assert.IsFalse(flag);
        }

Result of image comparison:

Expected result:

Actual result:

Result with marked differences:

Clone this wiki locally