How to use Screenshot class of WebDriverAPI package

Best WinAppDriver code snippet using WebDriverAPI.Screenshot

Screenshot.cs

Source:Screenshot.cs Github

copy

Full Screen

...21using OpenQA.Selenium.Appium.Windows;22namespace WebDriverAPI23{24 [TestClass]25 public class Screenshot : AlarmClockBase26 {27 [ClassInitialize]28 public static void ClassInitialize(TestContext context)29 {30 Setup(context);31 }32 [ClassCleanup]33 public static void ClassCleanup()34 {35 TearDown();36 }37 [TestMethod]38 public void GetElementScreenshot()39 {40 WindowsDriver<WindowsElement> desktopSession = null;41 try42 {43 // Locate the AlarmPivotItem element in Alarms & Clock app to be captured44 WindowsElement alarmPivotItem1 = session.FindElementByAccessibilityId(AlarmTabAutomationId);45 OpenQA.Selenium.Screenshot alarmPivotItemScreenshot1 = alarmPivotItem1.GetScreenshot(); 46 // Save the AlarmPivotItem screenshot capture locally on the machine running the test47 alarmPivotItemScreenshot1.SaveAsFile(@"ScreenshotAlarmPivotItem.png", ScreenshotImageFormat.Png);48 // Using the Desktop session, locate the same AlarmPivotItem element in Alarms & Clock app to be captured49 desktopSession = Utility.CreateNewSession(CommonTestSettings.DesktopAppId);50 WindowsElement alarmPivotItem2 = desktopSession.FindElementByAccessibilityId(AlarmTabAutomationId);51 OpenQA.Selenium.Screenshot alarmPivotItemScreenshot2 = alarmPivotItem2.GetScreenshot();52 // Using the Desktop session, locate the Alarms & Clock app top level window to be captured53 WindowsElement alarmsClockWindowTopWindow = desktopSession.FindElementByName("Alarms & Clock");54 OpenQA.Selenium.Screenshot alarmsClockWindowTopWindowScreenshot = alarmsClockWindowTopWindow.GetScreenshot();55 using (MemoryStream msScreenshot1 = new MemoryStream(alarmPivotItemScreenshot1.AsByteArray))56 using (MemoryStream msScreenshot2 = new MemoryStream(alarmPivotItemScreenshot2.AsByteArray))57 using (MemoryStream msScreenshot3 = new MemoryStream(alarmsClockWindowTopWindowScreenshot.AsByteArray))58 {59 // Verify that the element screenshot has a valid size60 Image screenshotImage1 = Image.FromStream(msScreenshot1);61 Assert.AreEqual(alarmPivotItem1.Size.Height, screenshotImage1.Height);62 Assert.AreEqual(alarmPivotItem1.Size.Width, screenshotImage1.Width);63 // Verify that the element screenshot captured using the Alarms & Clock session64 // is identical in size with the one taken using the desktop session65 Image screenshotImage2 = Image.FromStream(msScreenshot2);66 Assert.AreEqual(screenshotImage1.Height, screenshotImage2.Height);67 Assert.AreEqual(screenshotImage1.Width, screenshotImage2.Width);68 // Verify that the element screenshot is smaller in size compared to the application top level window69 Image screenshotImage3 = Image.FromStream(msScreenshot3);70 Assert.AreEqual(alarmsClockWindowTopWindow.Size.Height, screenshotImage3.Height);71 Assert.AreEqual(alarmsClockWindowTopWindow.Size.Width, screenshotImage3.Width);72 Assert.IsTrue(screenshotImage3.Height > screenshotImage1.Height);73 Assert.IsTrue(screenshotImage3.Width > screenshotImage1.Width);74 }75 }76 finally77 {78 if (desktopSession != null)79 {80 desktopSession.Quit();81 }82 }83 }84 [TestMethod]85 public void GetElementScreenshotError_NoSuchWindow()86 {87 try88 {89 var screenshot = Utility.GetOrphanedElement().GetScreenshot();90 Assert.Fail("Exception should have been thrown because there is no such window");91 }92 catch (InvalidOperationException exception)93 {94 Assert.AreEqual(ErrorStrings.NoSuchWindow, exception.Message);95 }96 }97 [TestMethod]98 public void GetElementScreenshotError_StaleElement()99 {100 try101 {102 var screenshot = GetStaleElement().GetScreenshot();103 Assert.Fail("Exception should have been thrown");104 }105 catch (InvalidOperationException exception)106 {107 Assert.AreEqual(ErrorStrings.StaleElementReference, exception.Message);108 }109 }110 [TestMethod]111 public void GetScreenshot()112 {113 WindowsDriver<WindowsElement> notepadSession = null;114 WindowsDriver<WindowsElement> desktopSession = null;115 try116 {117 // Launch and capture a screenshot of a maximized Notepad application. The steps below intentionally use118 // the Notepad application window to fully cover the Alarms & Clock application. This setup demonstrates119 // that capturing Alarms & Clock screenshot afterward will implicitly bring its window to foreground.120 notepadSession = Utility.CreateNewSession(CommonTestSettings.NotepadAppId);121 notepadSession.Manage().Window.Maximize();122 System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1));123 OpenQA.Selenium.Screenshot notepadScreenshot = notepadSession.GetScreenshot();124 // Capture a screenshot of Alarms & Clock application125 // This implicitly brings the application window to foreground126 OpenQA.Selenium.Screenshot alarmsClockScreenshot = session.GetScreenshot();127 // Save the application screenshot capture locally on the machine running the test128 alarmsClockScreenshot.SaveAsFile(@"ScreenshotAlarmsClockApplication.png", ScreenshotImageFormat.Png);129 // Capture the entire desktop using the Desktop session130 desktopSession = Utility.CreateNewSession(CommonTestSettings.DesktopAppId);131 OpenQA.Selenium.Screenshot desktopScreenshot = desktopSession.GetScreenshot();132 // Save the desktop screenshot capture locally on the machine running the test133 desktopScreenshot.SaveAsFile(@"ScreenshotDesktop.png", ScreenshotImageFormat.Png);134 using (MemoryStream msScreenshot1 = new MemoryStream(alarmsClockScreenshot.AsByteArray))135 using (MemoryStream msScreenshot2 = new MemoryStream(notepadScreenshot.AsByteArray))136 using (MemoryStream msScreenshot3 = new MemoryStream(desktopScreenshot.AsByteArray))137 {138 // Verify that the Alarms & Clock application screenshot has a valid size139 Image screenshotImage1 = Image.FromStream(msScreenshot1);140 Assert.AreEqual(session.Manage().Window.Size.Height, screenshotImage1.Height);141 Assert.AreEqual(session.Manage().Window.Size.Width, screenshotImage1.Width);142 // Verify that the maximized Notepad application screenshot has a valid size143 Image screenshotImage2 = Image.FromStream(msScreenshot2);144 Assert.AreEqual(notepadSession.Manage().Window.Size.Height, screenshotImage2.Height);145 Assert.AreEqual(notepadSession.Manage().Window.Size.Width, screenshotImage2.Width);146 // Verify that the application screenshot is smaller in size compared to the entire desktop147 Image screenshotImage3 = Image.FromStream(msScreenshot3);148 Assert.IsTrue(screenshotImage2.Height >= screenshotImage1.Height);149 Assert.IsTrue(screenshotImage2.Width >= screenshotImage1.Width);150 }151 }152 finally153 {154 if (notepadSession != null)155 {156 notepadSession.Quit();157 }158 if (desktopSession != null)159 {160 desktopSession.Quit();161 }162 }163 }164 [TestMethod]165 public void GetScreenshotError_NoSuchWindow()166 {167 try168 {169 Utility.GetOrphanedSession().GetScreenshot();170 Assert.Fail("Exception should have been thrown because there is no such window");171 }172 catch (InvalidOperationException exception)173 {174 Assert.AreEqual(ErrorStrings.NoSuchWindow, exception.Message);175 }176 }177 }178}...

Full Screen

Full Screen

WebElementExtensions.cs

Source:WebElementExtensions.cs Github

copy

Full Screen

...47 }48 public static ReadOnlyCollection<IWebElement> FindElementsByPartialLinkText(this IWebElement element, string tagName)49 {50 }51 public static OpenQA.Selenium.Screenshot GetScreenshot(this IWebElement element)52 {53 }54 }55}...

Full Screen

Full Screen

Screenshot

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using OpenQA.Selenium;7using OpenQA.Selenium.Firefox;8using OpenQA.Selenium.Support.UI;9using OpenQA.Selenium.Support.Extensions;10using System.Drawing.Imaging;11using System.Drawing;12using System.IO;13{14 {15 static void Main(string[] args)16 {17 IWebDriver driver = new FirefoxDriver();18 driver.Manage().Window.Maximize();19 driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));20 driver.TakeScreenshot().SaveAsFile("c:\\selenium\\google.png", ImageFormat.Png);21 driver.Quit();22 }23 }24}25using System;26using System.Collections.Generic;27using System.Linq;28using System.Text;29using System.Threading.Tasks;30using OpenQA.Selenium;31using OpenQA.Selenium.Firefox;32using System.Drawing.Imaging;33using System.Drawing;34using System.IO;35{36 {37 static void Main(string[] args)38 {39 IWebDriver driver = new FirefoxDriver();40 driver.Manage().Window.Maximize();41 driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));42 var screenshot = ((ITakesScreenshot)driver).GetScreenshot();43 screenshot.SaveAsFile("c:\\selenium\\google.png", ImageFormat.Png);44 driver.Quit();45 }46 }47}48using System;49using System.Collections.Generic;50using System.Linq;51using System.Text;52using System.Threading.Tasks;53using OpenQA.Selenium;54using OpenQA.Selenium.Firefox;55using System.Drawing.Imaging;56using System.Drawing;57using System.IO;58{59 {60 static void Main(string[] args)61 {62 IWebDriver driver = new FirefoxDriver();63 driver.Manage().Window.Maximize();64 driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));65 var screenshot = ((ITakesScreenshot)driver).GetScreenshot();66 screenshot.SaveAsFile("c:\\selenium\\google.png",

Full Screen

Full Screen

Screenshot

Using AI Code Generation

copy

Full Screen

1using OpenQA.Selenium;2using OpenQA.Selenium.Chrome;3using OpenQA.Selenium.Support.Extensions;4using System;5using System.Drawing.Imaging;6using System.IO;7{8 {9 static void Main(string[] args)10 {11 IWebDriver driver = new ChromeDriver();12 Screenshot screenshot = driver.TakeScreenshot();13 screenshot.SaveAsFile(@"D:\screenshot.png", ScreenshotImageFormat.Png);14 driver.Close();15 }16 }17}

Full Screen

Full Screen

Screenshot

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using NUnit.Framework;7using OpenQA.Selenium;8using OpenQA.Selenium.Firefox;9using System.Threading;10using WebDriverAPI;11{12 {13 IWebDriver driver;14 public void StartBrowser()15 {16 driver = new FirefoxDriver();17 }18 public void TakeScreenshot()19 {20 Thread.Sleep(2000);21 ITakesScreenshot takesScreenshot = driver as ITakesScreenshot;22 Screenshot screenshot = takesScreenshot.GetScreenshot();23 screenshot.SaveAsFile("D:\\screenshot.png", ScreenshotImageFormat.Png);24 }25 public void CloseBrowser()26 {27 driver.Close();28 }29 }30}31using System;32using System.Collections.Generic;33using System.Linq;34using System.Text;35using System.Threading.Tasks;36using NUnit.Framework;37using OpenQA.Selenium;38using OpenQA.Selenium.Firefox;39using System.Threading;40using WebDriverAPI;41{42 {43 IWebDriver driver;44 public void StartBrowser()45 {46 driver = new FirefoxDriver();47 }48 public void TakeScreenshot()49 {50 Thread.Sleep(2000);51 ITakesScreenshot takesScreenshot = driver as ITakesScreenshot;52 Screenshot screenshot = takesScreenshot.GetScreenshot();53 screenshot.SaveAsFile("D:\\screenshot.png", ScreenshotImageFormat.Png);54 }55 public void CloseBrowser()56 {57 driver.Close();58 }59 }60}61using System;62using System.Collections.Generic;63using System.Linq;64using System.Text;65using System.Threading.Tasks;66using NUnit.Framework;67using OpenQA.Selenium;68using OpenQA.Selenium.Firefox;69using System.Threading;70using WebDriverAPI;71{72 {73 IWebDriver driver;74 public void StartBrowser()75 {76 driver = new FirefoxDriver();77 }

Full Screen

Full Screen

Screenshot

Using AI Code Generation

copy

Full Screen

1using System;2using System.IO;3using System.Drawing;4using System.Drawing.Imaging;5using System.Windows.Forms;6using OpenQA.Selenium;7using OpenQA.Selenium.Firefox;8using OpenQA.Selenium.Support.UI;9using OpenQA.Selenium.Remote;10using WebDriverAPI;11{12 {13 static void Main(string[] args)14 {15 FirefoxDriver driver = new FirefoxDriver();16 driver.Manage().Window.Maximize();17 Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();18 ss.SaveAsFile("C:\\screenshot.png", ImageFormat.Png);19 driver.Quit();20 }21 }22}23Screenshot ss = new Screenshot();24ss.CaptureScreenToFile("C:\\screenshot.png", ImageFormat.Png);

Full Screen

Full Screen

Screenshot

Using AI Code Generation

copy

Full Screen

1using System;2using System.IO;3using OpenQA.Selenium;4using OpenQA.Selenium.Chrome;5using WebDriverAPI;6using System.Threading;7{8 {9 static void Main(string[] args)10 {11 IWebDriver driver = new ChromeDriver();12 Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();13 ss.SaveAsFile("C:/Users/HP/Desktop/Screenshot.png", ScreenshotImageFormat.Png);14 driver.Quit();15 }16 }17}

Full Screen

Full Screen

Screenshot

Using AI Code Generation

copy

Full Screen

1using OpenQA.Selenium;2using OpenQA.Selenium.Support.UI;3using OpenQA.Selenium.Support.Events;4using OpenQA.Selenium.Support.PageObjects;5using OpenQA.Selenium.Chrome;6using OpenQA.Selenium.Support.Extensions;7using System.Threading;8using System;9using System.Collections.Generic;10using System.Linq;11using System.Text;12using System.Threading.Tasks;13{14 {15 static void Main(string[] args)16 {17 IWebDriver driver = new ChromeDriver();18 Screenshot ss = driver.TakeScreenshot();19 ss.SaveAsFile(@"C:\Users\Public\Documents\test.png", ScreenshotImageFormat.Png);20 driver.Quit();21 }22 }23}24using OpenQA.Selenium;25using OpenQA.Selenium.Support.UI;26using OpenQA.Selenium.Support.Events;27using OpenQA.Selenium.Support.PageObjects;28using OpenQA.Selenium.Chrome;29using OpenQA.Selenium.Support.Extensions;30using System.Threading;31using System;32using System.Collections.Generic;33using System.Linq;34using System.Text;35using System.Threading.Tasks;36{37 {38 static void Main(string[] args)39 {40 IWebDriver driver = new ChromeDriver();41 Screenshot ss = driver.TakeScreenshot();42 ss.SaveAsFile(@"C:\Users\Public\Documents\test.png", ScreenshotImageFormat.Png);43 driver.Quit();44 }45 }46}47using OpenQA.Selenium;48using OpenQA.Selenium.Support.UI;49using OpenQA.Selenium.Support.Events;50using OpenQA.Selenium.Support.PageObjects;51using OpenQA.Selenium.Chrome;52using OpenQA.Selenium.Support.Extensions;53using System.Threading;54using System;55using System.Collections.Generic;56using System.Linq;57using System.Text;58using System.Threading.Tasks;59{60 {61 static void Main(string[] args)62 {

Full Screen

Full Screen

Screenshot

Using AI Code Generation

copy

Full Screen

1Screenshot screenshot = ((ITakesScreenshot)driver).GetScreenshot();2screenshot.SaveAsFile("C:\\Users\\Public\\Desktop\\screenshot.png", ScreenshotImageFormat.Png);3Screenshot screenshot = ((ITakesScreenshot)driver).GetScreenshot();4screenshot.SaveAsFile("C:\\Users\\Public\\Desktop\\screenshot.png", ScreenshotImageFormat.Png);5Screenshot screenshot = ((ITakesScreenshot)driver).GetScreenshot();6screenshot.SaveAsFile("C:\\Users\\Public\\Desktop\\screenshot.png", ScreenshotImageFormat.Png);7Screenshot screenshot = ((ITakesScreenshot)driver).GetScreenshot();8screenshot.SaveAsFile("C:\\Users\\Public\\Desktop\\screenshot.png", ScreenshotImageFormat.Png);9Screenshot screenshot = ((ITakesScreenshot)driver).GetScreenshot();10screenshot.SaveAsFile("C:\\Users\\Public\\Desktop\\screenshot.png", ScreenshotImageFormat.Png);11Screenshot screenshot = ((ITakesScreenshot)driver).GetScreenshot();12screenshot.SaveAsFile("C:\\Users\\Public\\Desktop\\screenshot.png", ScreenshotImageFormat.Png);13Screenshot screenshot = ((ITakesScreenshot)driver).GetScreenshot();14screenshot.SaveAsFile("C:\\Users\\Public\\Desktop\\screenshot.png", ScreenshotImageFormat.Png);15Screenshot screenshot = ((ITakesScreenshot)driver).GetScreenshot();16screenshot.SaveAsFile("C:\\Users\\Public\\Desktop\\screenshot.png", ScreenshotImageFormat.Png);

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run WinAppDriver automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful