Take screenshot in Selenium



Getscreenshot in Selenium with C#

public void Getscreenshot()
{
try
{
            dr.Navigate().GoToUrl("https://www.axismf.com");
            dr.FindElement(By.XPath("(//ion-button[@id='origin'])[2]")).Click();        
            //click on Login button            
            IWebElement elementPan = dr.FindElement(By.XPath("(//input[@name='pan'])[2]"));
            elementPan.SendKeys("India"); //enter india in PAn number

            IWebElement panError = dr.FindElement(By.XPath("//div[text()='Please enter a correct PAN']"));
            Thread.Sleep(2000);
            Assert.IsTrue(panError.Size != Size.Empty, "validation error is not displayed for PAN"); //Error is displayed

        }
        catch(ElementClickInterceptedException clickex)
        {
            Console.WriteLine(clickex.Message);
        }
        catch(NoSuchElementException nosuchex)
        {
            Console.WriteLine(nosuchex.Message);
        }
        catch(NoSuchWindowException ex)
        {
            ITakesScreenshot screenshotDriver = dr as ITakesScreenshot;
            Screenshot screenshot = screenshotDriver.GetScreenshot();
            // Creating UIScreenshot folder if not exists
            System.IO.Directory.CreateDirectory(Environment.CurrentDirectory + "/UIScreenshots/");
            string fileName = Environment.CurrentDirectory + "/UIScreenshots/" + "sampletestcase" + "_" + DateTime.Now.ToString("yyyy_MM_dd_hh_mm_ss") + ".png";
            screenshot.SaveAsFile(fileName, ScreenshotImageFormat.Png);
        }

    }
Tags: , ,
Leave a comment

Your email address will not be published. Required fields are marked *

Subscribe now

Receive weekly newsletter with educational materials, new courses, most popular posts, popular books and much more!

https://bridgejunks.com/ https://crownmakesense.com/ https://brithaniabookjudges.com/ https://hughesroyality.com/ https://rhythmholic.com/ https://bandar89.simnasfikpunhas.com/ https://www.100calshop.co.il/products/thailand/ https://myasociados.com/ https://solyser.com/ http://konfidence.cz/ https://muscadinepdx.com/ https://bandar89.parajesandinos.com.ve/ https://goremekoop.com/ https://oncoswisscenter.com/ https://www.turunclifehotel.com/bandar89/ https://www.houseofproducts.biz/ https://taimoormphotography.com/
BIJI18 BIJI18 BIJI18