How to use GetScreenshot method of WebDriverAPI.Screenshot class

Best WinAppDriver code snippet using WebDriverAPI.Screenshot.GetScreenshot

Screenshot.cs

Source:Screenshot.cs Github

copy

Full Screen

...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

GetScreenshot

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 WebDriverAPI.Screenshot;9using System.IO;10{11 {12 static void Main(string[] args)13 {14 IWebDriver driver = new FirefoxDriver();15 Screenshot.GetScreenshot(driver, "google");16 driver.Close();17 }18 }19}

Full Screen

Full Screen

GetScreenshot

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 System.Threading;10using WebDriverAPI;11{12 {13 static void Main(string[] args)14 {15 IWebDriver driver = new FirefoxDriver();16 Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();17 ss.SaveAsFile("C:\\Users\\Public\\Pictures\\SampleScreenshot.png", System.Drawing.Imaging.ImageFormat.Png);18 driver.Quit();19 }20 }21}22Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();23ss.SaveAsFile("C:\\Users\\Public\\Pictures\\SampleScreenshot.png", System.Drawing.Imaging.ImageFormat.Png);24using System;25using System.Collections.Generic;26using System.Linq;27using System.Text;28using System.Threading.Tasks;29using OpenQA.Selenium;30using OpenQA.Selenium.Firefox;31using OpenQA.Selenium.Support.UI;32using System.Threading;33using WebDriverAPI;34{35 {36 static void Main(string[] args)37 {38 IWebDriver driver = new FirefoxDriver();39 IWebElement element = driver.FindElement(By.Name("q"));40 Screenshot ss = ((ITakes

Full Screen

Full Screen

GetScreenshot

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 System.IO;9{10 {11 static void Main(string[] args)12 {13 IWebDriver driver = new FirefoxDriver();14 Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();15 ss.SaveAsFile(@"C:\Users\Public\Pictures\SampleScreenshot.png", ScreenshotImageFormat.Png);16 driver.Quit();17 }18 }19}20using System;21using System.Collections.Generic;22using System.Linq;23using System.Text;24using System.Threading.Tasks;25using OpenQA.Selenium;26using OpenQA.Selenium.Firefox;27using System.IO;28{29 {30 static void Main(string[] args)31 {32 IWebDriver driver = new FirefoxDriver();33 Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();34 ss.SaveAsFile(@"C:\Users\Public\Pictures\SampleScreenshot.png", ScreenshotImageFormat.Png);35 driver.Quit();36 }37 }38}39using System;40using System.Collections.Generic;41using System.Linq;42using System.Text;43using System.Threading.Tasks;44using OpenQA.Selenium;45using OpenQA.Selenium.Firefox;46using System.IO;47{48 {49 static void Main(string[] args)50 {51 IWebDriver driver = new FirefoxDriver();52 Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();53 ss.SaveAsFile(@"C:\Users\Public\Pictures\SampleScreenshot.png", ScreenshotImageFormat.Png);54 driver.Quit();55 }56 }57}58using System;59using System.Collections.Generic;60using System.Linq;61using System.Text;62using System.Threading.Tasks;63using OpenQA.Selenium;64using OpenQA.Selenium.Firefox;65using System.IO;66{67 {68 static void Main(string[] args)69 {

Full Screen

Full Screen

GetScreenshot

Using AI Code Generation

copy

Full Screen

1using System;2using System.Drawing;3using System.Drawing.Imaging;4using System.IO;5using System.Drawing.Drawing2D;6using OpenQA.Selenium;7using OpenQA.Selenium.Firefox;8using OpenQA.Selenium.Support.UI;9using OpenQA.Selenium.Interactions;10using OpenQA.Selenium.Remote;11using System.Collections.Generic;12using System.Linq;13using System.Text;14using System.Threading.Tasks;15{16 {17 static void Main(string[] args)18 {19 IWebDriver driver = new FirefoxDriver();20 Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();21 ss.SaveAsFile("c:\\temp\\1.jpg", ImageFormat.Jpeg);22 driver.Quit();23 }24 }25}26using System;27using System.Drawing;28using System.Drawing.Imaging;29using System.IO;30using System.Drawing.Drawing2D;31using OpenQA.Selenium;32using OpenQA.Selenium.Firefox;33using OpenQA.Selenium.Support.UI;34using OpenQA.Selenium.Interactions;35using OpenQA.Selenium.Remote;36using System.Collections.Generic;37using System.Linq;38using System.Text;39using System.Threading.Tasks;40{41 {42 static void Main(string[] args)43 {44 IWebDriver driver = new FirefoxDriver();45 Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();46 ss.SaveAsFile("c:\\temp\\1.jpg", ImageFormat.Jpeg);47 driver.Quit();48 }49 }50}51using System;52using System.Drawing;53using System.Drawing.Imaging;54using System.IO;55using System.Drawing.Drawing2D;56using OpenQA.Selenium;57using OpenQA.Selenium.Firefox;58using OpenQA.Selenium.Support.UI;59using OpenQA.Selenium.Interactions;60using OpenQA.Selenium.Remote;61using System.Collections.Generic;62using System.Linq;63using System.Text;64using System.Threading.Tasks;65{66 {67 static void Main(string[] args)68 {69 IWebDriver driver = new FirefoxDriver();70 Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();71 ss.SaveAsFile("

Full Screen

Full Screen

GetScreenshot

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.Chrome;8using OpenQA.Selenium.Support.UI;9using WebDriverAPI;10using System.IO;11{12 {13 static void Main(string[] args)14 {15 IWebDriver driver = new ChromeDriver();16 Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();17 ss.SaveAsFile("D:\\screenshot.png", ScreenshotImageFormat.Png);18 driver.Close();19 }20 }21}

Full Screen

Full Screen

GetScreenshot

Using AI Code Generation

copy

Full Screen

1using System;2using System.Drawing;3using System.Drawing.Imaging;4using System.IO;5using System.Windows.Forms;6using OpenQA.Selenium;7using OpenQA.Selenium.Firefox;8{9 {10 public Form1()11 {12 InitializeComponent();13 }14 private void button1_Click(object sender, EventArgs e)15 {16 IWebDriver driver = new FirefoxDriver();17 Screenshot screenshot = ((ITakesScreenshot)driver).GetScreenshot();18 screenshot.SaveAsFile(@"C:\Users\Public\Documents\Google.png", ImageFormat.Png);19 driver.Quit();20 }21 }22}23using System;24using System.Drawing;25using System.Drawing.Imaging;26using System.IO;27using System.Windows.Forms;28using OpenQA.Selenium;29using OpenQA.Selenium.Firefox;30{31 {32 public Form1()33 {34 InitializeComponent();35 }36 private void button1_Click(object sender, EventArgs e)37 {38 IWebDriver driver = new FirefoxDriver();39 Screenshot screenshot = ((ITakesScreenshot)driver).GetScreenshot();40 MemoryStream stream = new MemoryStream(screenshot.AsByteArray);41 pictureBox1.Image = Image.FromStream(stream);42 driver.Quit();43 }44 }45}46using System;47using System.Drawing;48using System.Drawing.Imaging;49using System.IO;50using System.Windows.Forms;51using OpenQA.Selenium;52using OpenQA.Selenium.Firefox;53{54 {55 public Form1()56 {57 InitializeComponent();58 }59 private void button1_Click(object sender, EventArgs

Full Screen

Full Screen

GetScreenshot

Using AI Code Generation

copy

Full Screen

1using System;2using System.Drawing;3using System.IO;4using System.Windows.Forms;5using OpenQA.Selenium;6using OpenQA.Selenium.Firefox;7using WebDriverAPI.Screenshot;8{9 static void Main()10 {11 IWebDriver driver = new FirefoxDriver();12 GetScreenshot(driver);13 driver.Quit();14 }15 public static void GetScreenshot(IWebDriver driver)16 {17 Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();18 Bitmap bmpScreen = new Bitmap(new MemoryStream(ss.AsByteArray));19 bmpScreen.Save(@"C:\Users\Public\Pictures\SampleScreenshot.png");20 }21}

Full Screen

Full Screen

GetScreenshot

Using AI Code Generation

copy

Full Screen

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

Full Screen

Full Screen

GetScreenshot

Using AI Code Generation

copy

Full Screen

1using System;2using System.Drawing;3using System.Drawing.Imaging;4using System.IO;5using System.Reflection;6using System.Runtime.InteropServices;7using OpenQA.Selenium;8using OpenQA.Selenium.Firefox;9using WebDriverAPI.Screenshot;10{11 {12 static void Main(string[] args)13 {14 IWebDriver driver = new FirefoxDriver();15 IWebElement searchBox = driver.FindElement(By.Name("q"));16 searchBox.SendKeys("Selenium WebDriver");17 searchBox.Submit();18 Screenshot screenshot = ((ITakesScreenshot)driver).GetScreenshot();19 screenshot.SaveAsFile("screenshot.png", ImageFormat.Png);20 driver.Quit();21 }22 }23}24 }25 private void button1_Click(object sender, EventArgs

Full Screen

Full Screen

GetScreenshot

Using AI Code Generation

copy

Full Screen

1using System;2using System.Drawing;3using System.IO;4using System.Windows.Forms;5using OpenQA.Selenium;6using OpenQA.Selenium.Firefox;7using WebDriverAPI.Screenshot;8{9 static void Main()10 {11 IWebDriver driver = new FirefoxDriver();12 GetScreenshot(driver);13 driver.Quit();14 }15 public static void GetScreenshot(IWebDriver driver)16 {17 Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();18 Bitmap bmpScreen = new Bitmap(new MemoryStream(ss.AsByteArray));19 bmpScreen.Save(@"C:\Users\Public\Pictures\SampleScreenshot.png");20 }21}

Full Screen

Full Screen

GetScreenshot

Using AI Code Generation

copy

Full Screen

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

Full Screen

Full Screen

GetScreenshot

Using AI Code Generation

copy

Full Screen

1using System;2using System.Drawing;3using System.Drawing.Imaging;4using System.IO;5using System.Reflection;6using System.Runtime.InteropServices;7using OpenQA.Selenium;8using OpenQA.Selenium.Firefox;9using WebDriverAPI.Screenshot;10{11 {12 static void Main(string[] args)13 {14 IWebDriver driver = new FirefoxDriver();15 IWebElement searchBox = driver.FindElement(By.Name("q"));16 searchBox.SendKeys("Selenium WebDriver");17 searchBox.Submit();18 Screenshot screenshot = ((ITakesScreenshot)driver).GetScreenshot();19 screenshot.SaveAsFile("screenshot.png", ImageFormat.Png);20 driver.Quit();21 }22 }23}24 driver.Close();25 }26 }27}

Full Screen

Full Screen

GetScreenshot

Using AI Code Generation

copy

Full Screen

1using System;2using System.Drawing;3using System.IO;4using System.Windows.Forms;5using OpenQA.Selenium;6using OpenQA.Selenium.Firefox;7using WebDriverAPI.Screenshot;8{9 static void Main()10 {11 IWebDriver driver = new FirefoxDriver();12 GetScreenshot(driver);13 driver.Quit();14 }15 public static void GetScreenshot(IWebDriver driver)16 {17 Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();18 Bitmap bmpScreen = new Bitmap(new MemoryStream(ss.AsByteArray));19 bmpScreen.Save(@"C:\Users\Public\Pictures\SampleScreenshot.png");20 }21}

Full Screen

Full Screen

GetScreenshot

Using AI Code Generation

copy

Full Screen

1using System;2using System.Drawing;3using System.Drawing.Imaging;4using System.IO;5using System.Reflection;6using System.Runtime.InteropServices;7using OpenQA.Selenium;8using OpenQA.Selenium.Firefox;9using WebDriverAPI.Screenshot;10{11 {12 static void Main(string[] args)13 {14 IWebDriver driver = new FirefoxDriver();15 IWebElement searchBox = driver.FindElement(By.Name("q"));16 searchBox.SendKeys("Selenium WebDriver");17 searchBox.Submit();18 Screenshot screenshot = ((ITakesScreenshot)driver).GetScreenshot();19 screenshot.SaveAsFile("screenshot.png", ImageFormat.Png);20 driver.Quit();21 }22 }23}

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