Skip to content
Jakub Raczek edited this page Mar 8, 2021 · 2 revisions

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