Best WinAppDriver code snippet using WebDriverAPI.WindowTransform.TestCleanup
Window.cs
Source:Window.cs
...25 [TestClass]26 public class Window27 {28 private WindowsDriver<WindowsElement> session = null;29 [TestCleanup]30 public void TestCleanup()31 {32 if (session != null)33 {34 session.Quit();35 session = null;36 }37 }38 [TestMethod]39 public void CloseWindow()40 {41 session = Utility.CreateNewSession(CommonTestSettings.AlarmClockAppId);42 Assert.IsNotNull(session.SessionId);43 // Close the application window without deleting the session44 session.Close();45 Assert.IsNotNull(session);46 Assert.IsNotNull(session.SessionId);47 // Delete the session48 session.Quit();49 session = null;50 }51 [TestMethod]52 public void CloseWindowError_NoSuchWindow()53 {54 // Attempt to close the previously closed application window55 try56 {57 Utility.GetOrphanedSession().Close();58 Assert.Fail("Exception should have been thrown");59 }60 catch (InvalidOperationException exception)61 {62 Assert.AreEqual(ErrorStrings.NoSuchWindow, exception.Message);63 }64 }65 [TestMethod]66 public void GetWindowHandle()67 {68 session = Utility.CreateNewSession(CommonTestSettings.CalculatorAppId);69 Assert.IsNotNull(session.SessionId);70 string windowHandle = session.CurrentWindowHandle;71 Assert.IsNotNull(windowHandle);72 }73 [TestMethod]74 public void GetWindowHandleError_NoSuchWindow()75 {76 try77 {78 string windowHandle = Utility.GetOrphanedSession().CurrentWindowHandle;79 Assert.Fail("Exception should have been thrown");80 }81 catch (InvalidOperationException exception)82 {83 Assert.AreEqual(ErrorStrings.NoSuchWindow, exception.Message);84 }85 }86 [TestMethod]87 public void GetWindowHandles_ClassicApp()88 {89 session = Utility.CreateNewSession(CommonTestSettings.NotepadAppId);90 Assert.IsNotNull(session);91 var handles = session.WindowHandles;92 Assert.IsNotNull(handles);93 Assert.IsTrue(handles.Count > 0);94 }95 [TestMethod]96 public void GetWindowHandles_Desktop()97 {98 session = Utility.CreateNewSession(CommonTestSettings.DesktopAppId);99 Assert.IsNotNull(session);100 var handles = session.WindowHandles;101 Assert.IsNotNull(handles);102 Assert.AreEqual(0, handles.Count);103 }104 [TestMethod]105 public void GetWindowHandles_ModernApp()106 {107 session = Utility.CreateNewSession(CommonTestSettings.EdgeAppId, "-private");108 Assert.IsNotNull(session);109 var windowHandlesBefore = session.WindowHandles;110 Assert.IsNotNull(windowHandlesBefore);111 Assert.IsTrue(windowHandlesBefore.Count > 0);112 // Preserve previously opened Edge window(s) and only manipulate newly opened windows113 List<string> previouslyOpenedEdgeWindows = new List<string>(windowHandlesBefore);114 previouslyOpenedEdgeWindows.Remove(session.CurrentWindowHandle);115 // Set focus on itself116 session.SwitchTo().Window(session.CurrentWindowHandle);117 // Open a new window in private mode118 session.Keyboard.SendKeys(Keys.Control + Keys.Shift + "p" + Keys.Shift + Keys.Control);119 Thread.Sleep(TimeSpan.FromSeconds(3));120 var windowHandlesAfter = session.WindowHandles;121 Assert.IsNotNull(windowHandlesAfter);122 Assert.AreEqual(windowHandlesBefore.Count + 1, windowHandlesAfter.Count);123 // Preserve previously opened Edge Windows by only closing newly opened windows124 List<string> newlyOpenedEdgeWindows = new List<string>(windowHandlesAfter);125 foreach (var previouslyOpenedEdgeWindow in previouslyOpenedEdgeWindows)126 {127 newlyOpenedEdgeWindows.Remove(previouslyOpenedEdgeWindow);128 }129 foreach (var windowHandle in newlyOpenedEdgeWindows)130 {131 session.SwitchTo().Window(windowHandle);132 EdgeBase.CloseEdge(session);133 }134 }135 [TestMethod]136 public void SwitchWindows()137 {138 session = Utility.CreateNewSession(CommonTestSettings.EdgeAppId, CommonTestSettings.EdgeAboutBlankURL);139 Assert.IsNotNull(session);140 // Preserve previously opened Edge window(s) and only manipulate newly opened windows141 List<string> previouslyOpenedEdgeWindows = new List<string>(session.WindowHandles);142 previouslyOpenedEdgeWindows.Remove(session.CurrentWindowHandle);143 // Set focus on itself144 session.SwitchTo().Window(session.CurrentWindowHandle);145 // Open a new window in private mode146 session.Keyboard.SendKeys(Keys.Control + Keys.Shift + "p" + Keys.Shift + Keys.Control);147 Thread.Sleep(TimeSpan.FromSeconds(3));148 var multipleWindowHandles = session.WindowHandles;149 Assert.IsTrue(multipleWindowHandles.Count > 1);150 // Preserve previously opened Edge Windows by only operating on newly opened windows151 List<string> newlyOpenedEdgeWindows = new List<string>(multipleWindowHandles);152 foreach (var previouslyOpenedEdgeWindow in previouslyOpenedEdgeWindows)153 {154 newlyOpenedEdgeWindows.Remove(previouslyOpenedEdgeWindow);155 }156 string previousWindowHandle = string.Empty;157 foreach (var windowHandle in newlyOpenedEdgeWindows)158 {159 session.SwitchTo().Window(windowHandle);160 Assert.AreEqual(session.CurrentWindowHandle, windowHandle);161 Assert.AreNotEqual(windowHandle, previousWindowHandle);162 previousWindowHandle = windowHandle;163 EdgeBase.CloseEdge(session);164 }165 }166 [TestMethod]167 public void SwitchWindowsError_EmptyValue()168 {169 session = Utility.CreateNewSession(CommonTestSettings.CalculatorAppId);170 try171 {172 session.SwitchTo().Window(string.Empty);173 Assert.Fail("Exception should have been thrown");174 }175 catch (InvalidOperationException exception)176 {177 Assert.AreEqual("Missing Command Parameter: name", exception.Message);178 }179 }180 [TestMethod]181 public void SwitchWindowsError_ForeignWindowHandle()182 {183 WindowsDriver<WindowsElement> mainSession = null;184 WindowsDriver<WindowsElement> foreignSession = null;185 try186 {187 mainSession = Utility.CreateNewSession(CommonTestSettings.CalculatorAppId);188 foreignSession = Utility.CreateNewSession(CommonTestSettings.AlarmClockAppId);189 Assert.IsNotNull(mainSession.SessionId);190 Assert.IsNotNull(foreignSession.SessionId);191 // Get a foreign window handle from a different application/process under foreignSession192 var foreignTopLevelWindow = foreignSession.CurrentWindowHandle;193 Assert.IsFalse(string.IsNullOrEmpty(foreignTopLevelWindow));194 mainSession.SwitchTo().Window(foreignTopLevelWindow);195 Assert.Fail("Exception should have been thrown");196 }197 catch (InvalidOperationException exception)198 {199 Assert.AreEqual("Window handle does not belong to the same process/application", exception.Message);200 }201 finally202 {203 if (foreignSession != null)204 {205 foreignSession.Quit();206 }207 if (foreignSession != null)208 {209 mainSession.Quit();210 }211 }212 }213 [TestMethod]214 public void SwitchWindowsError_InvalidValue()215 {216 session = Utility.CreateNewSession(CommonTestSettings.CalculatorAppId);217 try218 {219 session.SwitchTo().Window("-1");220 Assert.Fail("Exception should have been thrown");221 }222 catch (InvalidOperationException exception)223 {224 Assert.AreEqual("String cannot contain a minus sign if the base is not 10.", exception.Message);225 }226 }227 [TestMethod]228 public void SwitchWindowsError_NonTopLevelWindowHandle()229 {230 session = Utility.CreateNewSession(CommonTestSettings.CalculatorAppId);231 var nonTopLevelWindowHandle = session.FindElementByClassName("Windows.UI.Core.CoreWindow").GetAttribute("NativeWindowHandle");232 var nonTopLevelWindowHandleHex = Convert.ToInt32(nonTopLevelWindowHandle).ToString("x");233 try234 {235 session.SwitchTo().Window(nonTopLevelWindowHandleHex); // This needs to be in Hex e.g. 0x00880088236 Assert.Fail("Exception should have been thrown");237 }238 catch (InvalidOperationException exception)239 {240 Assert.IsTrue(exception.Message.EndsWith("is not a top level window handle"));241 }242 }243 [TestMethod]244 public void SwitchWindowsError_NoSuchWindow()245 {246 session = Utility.CreateNewSession(CommonTestSettings.CalculatorAppId);247 // Get an orphaned window handle from a closed application248 var orphanedTopLevelWindow = Utility.GetOrphanedWindowHandle();249 Thread.Sleep(TimeSpan.FromSeconds(3));250 try251 {252 session.SwitchTo().Window(orphanedTopLevelWindow);253 Assert.Fail("Exception should have been thrown");254 }255 catch (InvalidOperationException exception)256 {257 Assert.AreEqual("A request to switch to a window could not be satisfied because the window could not be found.", exception.Message);258 }259 }260 }261 [TestClass]262 public class WindowTransform263 {264 protected static WindowsDriver<WindowsElement> WindowTransformSession;265 protected static Size OriginalSize;266 protected static Point OriginalPosition;267 [ClassInitialize]268 public static void Setup(TestContext context)269 {270 // Launch the Calculator app271 WindowTransformSession = Utility.CreateNewSession(CommonTestSettings.CalculatorAppId);272 Assert.IsNotNull(WindowTransformSession);273 }274 [ClassCleanup]275 public static void TearDown()276 {277 // Close the application and delete the session278 WindowTransformSession.Quit();279 WindowTransformSession = null;280 }281 [TestInitialize]282 public void TestInitialize()283 {284 // Save application window original size and position285 OriginalSize = WindowTransformSession.Manage().Window.Size;286 Assert.IsNotNull(OriginalSize);287 OriginalPosition = WindowTransformSession.Manage().Window.Position;288 Assert.IsNotNull(OriginalPosition);289 }290 [TestCleanup]291 public void TestCleanup()292 {293 // Restore application window original size and position294 WindowTransformSession.Manage().Window.Size = OriginalSize;295 Assert.AreEqual(OriginalSize, WindowTransformSession.Manage().Window.Size);296 WindowTransformSession.Manage().Window.Position = OriginalPosition;297 Assert.AreEqual(OriginalPosition, WindowTransformSession.Manage().Window.Position);298 }299 [TestMethod]300 public void GetWindowPosition()301 {302 var windowPosition = WindowTransformSession.Manage().Window.Position;303 Assert.IsNotNull(windowPosition);304 Assert.AreEqual(OriginalPosition.X, windowPosition.X);305 Assert.AreEqual(OriginalPosition.Y, windowPosition.Y);...
TestCleanup
Using AI Code Generation
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.Chrome;9using OpenQA.Selenium.IE;10using OpenQA.Selenium.Opera;11using OpenQA.Selenium.Edge;12using OpenQA.Selenium.Safari;13using WebDriverAPI;14using Microsoft.VisualStudio.TestTools.UnitTesting;15{16 {17 IWebDriver driver;18 public void TestWindowTransform()19 {20 driver = new ChromeDriver();21 driver.Manage().Window.Maximize();22 System.Threading.Thread.Sleep(5000);23 driver.Manage().Window.Minimize();24 System.Threading.Thread.Sleep(5000);25 driver.Manage().Window.Maximize();26 System.Threading.Thread.Sleep(5000);27 driver.Manage().Window.FullScreen();28 System.Threading.Thread.Sleep(5000);29 driver.Manage().Window.FullScreen();30 System.Threading.Thread.Sleep(5000);31 driver.Manage().Window.Size = new System.Drawing.Size(500, 500);32 System.Threading.Thread.Sleep(5000);33 driver.Manage().Window.Position = new System.Drawing.Point(200, 200);34 System.Threading.Thread.Sleep(5000);35 driver.Manage().Window.Position =
TestCleanup
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using NUnit.Framework;7using WebDriverAPI;8{9 {10 public void TestMaximize()11 {12 WindowTransform window = new WindowTransform();13 window.TestCleanup();14 }15 }16}17using System;18using System.Collections.Generic;19using System.Linq;20using System.Text;21using System.Threading.Tasks;22using NUnit.Framework;23using WebDriverAPI;24{25 {26 public void TestMaximize()27 {28 WindowTransform window = new WindowTransform();29 window.TestCleanup();30 }31 }32}33using System;34using System.Collections.Generic;35using System.Linq;36using System.Text;37using System.Threading.Tasks;38using NUnit.Framework;39using WebDriverAPI;40{41 {42 public void TestMaximize()43 {44 WindowTransform window = new WindowTransform();45 window.TestCleanup();46 }47 }48}49using System;50using System.Collections.Generic;51using System.Linq;52using System.Text;53using System.Threading.Tasks;54using NUnit.Framework;55using WebDriverAPI;56{57 {58 public void TestMaximize()59 {60 WindowTransform window = new WindowTransform();61 window.TestCleanup();62 }63 }64}65using System;66using System.Collections.Generic;67using System.Linq;68using System.Text;69using System.Threading.Tasks;
TestCleanup
Using AI Code Generation
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;9{10 {11 static void Main(string[] args)12 {13 IWebDriver driver = new FirefoxDriver();14 driver.Manage().Window.Maximize();15 System.Threading.Thread.Sleep(3000);16 driver.Manage().Window.Minimize();17 System.Threading.Thread.Sleep(3000);18 driver.Manage().Window.FullScreen();19 System.Threading.Thread.Sleep(3000);20 driver.Manage().Window.Size = new System.Drawing.Size(400, 400);21 System.Threading.Thread.Sleep(3000);22 driver.Manage().Window.Position = new System.Drawing.Point(200, 200);23 System.Threading.Thread.Sleep(3000);24 driver.Manage().Window.Maximize();25 System.Threading.Thread.Sleep(3000);26 driver.Close();27 }28 }29}30using System;31using System.Collections.Generic;32using System.Linq;33using System.Text;34using System.Threading.Tasks;35using OpenQA.Selenium;36using OpenQA.Selenium.Firefox;37using WebDriverAPI;38{39 {40 static void Main(string[] args)41 {42 IWebDriver driver = new FirefoxDriver();43 driver.Manage().Window.Maximize();44 System.Threading.Thread.Sleep(3000);45 driver.Manage().Window.Minimize();46 System.Threading.Thread.Sleep(3000);47 driver.Manage().Window.FullScreen();48 System.Threading.Thread.Sleep(3000);49 driver.Manage().Window.Size = new System.Drawing.Size(400, 400);50 System.Threading.Thread.Sleep(3000);51 driver.Manage().Window.Position = new System.Drawing.Point(200, 200);52 System.Threading.Thread.Sleep(3000);53 driver.Manage().Window.Maximize();54 System.Threading.Thread.Sleep(3000);55 driver.Close();56 }57 }58}
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!