Best WinAppDriver code snippet using NotepadTest.NotepadSession
AdvancedScenario.cs
Source:AdvancedScenario.cs  
...12        protected const string AppDriverUrl = "http://127.0.0.1:4723";13        protected const string NotepadAppId = @"C:\Windows\System32\notepad.exe";14        protected const string ExplorerAppId = @"C:\Windows\System32\explorer.exe";15        protected const string TargetSaveLocation = @"%TEMP%\";16        protected static WindowsDriver<WindowsElement> NotepadSession;17        [ClassInitialize]18        public static void Setup(TestContext context)19        {20            // Launch Notepad Classic Windows Application21            DesiredCapabilities appCapabilities = new DesiredCapabilities();22            appCapabilities.SetCapability("app", NotepadAppId);23            NotepadSession = new WindowsDriver<WindowsElement>(new Uri(AppDriverUrl), appCapabilities);24            Assert.IsNotNull(NotepadSession);25            // Verify that Notepad is started with untitled new file26            Assert.AreEqual("Untitled - Notepad", NotepadSession.Title);27            //Change the font to be more interesting28            NotepadSession.FindElementByName("Format").Click();29            NotepadSession.FindElementByName("Font...").Click();30            System.Threading.Thread.Sleep(1000);31            NotepadSession.FindElementByName("Cooper").Click();32            NotepadSession.FindElementByName("Black").Click();33            //Change the font size to be really big34            NotepadSession.FindElementByName("72").Click();35            //close the font dialog36            NotepadSession.FindElementByName("OK").Click();37            Thread.Sleep(1000);// Wait for 1 second until the main windows is displayed38            //Enable word wrap39            NotepadSession.FindElementByName("Format").Click();40            NotepadSession.FindElementByName("Word Wrap").Click();41        }42        [DataTestMethod]43        [DataRow("This is the first advanced automated test on Classic Windows Application! ",  "NotepadAdvancedTestOutputFile1.txt")]44        [DataRow("This is the second advanced automated test on Classic Windows Application! ", "NotepadAdvancedTestOutputFile2.txt")]45        [DataRow("This is the third advanced automated test on Classic Windows Application! ",  "NotepadAdvancedTestOutputFile3.txt")]46        [DataRow("This is the fourth advanced automated test on Classic Windows Application! ", "NotepadAdvancedTestOutputFile4.txt")]47        [DataRow("This is the fifth advanced automated test on Classic Windows Application! ",  "NotepadAdvancedTestOutputFile5.txt")]48        public void AdvancedScenarioTest(string AdvancedText,string TestFileName)49        {50            EnterText(AdvancedText);51            SaveFile(TestFileName);52            // Verify that Notepad has saved the edited text file with the given name53            Thread.Sleep(1000); // Wait for 1 second until the window title is updated54            Assert.AreEqual(TestFileName + " - Notepad", NotepadSession.Title);55            ClearText();56        }57        public void EnterText(string inputtext)58        {59            // Enter text into the edit field60            var editBox = NotepadSession.FindElementByClassName("Edit");61            // Clear the edit field of any values62            editBox.SendKeys(inputtext);63            Assert.AreEqual(inputtext, editBox.Text);64            // Enter TimeStamp65            NotepadSession.FindElementByName("Edit").Click();66            NotepadSession.FindElementByName("Time/Date").Click();67            Assert.AreNotEqual(inputtext, editBox.Text);68        }69        public void SaveFile(string testfilename)70        {71            NotepadSession.FindElementByName("File").Click();72            NotepadSession.FindElementByName("Save As...").Click();73            System.Threading.Thread.Sleep(1000); // Wait for 1 second until the save dialog appears74            var fileNameBox = NotepadSession.FindElementByAccessibilityId("FileNameControlHost");75            fileNameBox.SendKeys(TargetSaveLocation + testfilename);76            NotepadSession.FindElementByName("Save").Click();77            // Confirm save as dialog in case there's leftover test file from previous test run78            try79            {80                Thread.Sleep(1000); // Wait for 1 second until the dialog comes up81                var confirmSaveAsDialog = NotepadSession.FindElementByName("Confirm Save As");82                confirmSaveAsDialog.FindElementByName("Yes").Click();83            }84            catch { }85            86        }87        public void ClearText()88        {89            NotepadSession.FindElementByName("Edit").Click();90            NotepadSession.FindElementByName("Select All").Click();91            NotepadSession.FindElementByName("Edit").Click();92            NotepadSession.FindElementByName("Delete").Click();93        }94        [ClassCleanup]95        public static void TearDown()96        {97            List<string> filelist = new List<string>();98            filelist.Add("NotepadAdvancedTestOutputFile1.txt");99            filelist.Add("NotepadAdvancedTestOutputFile2.txt");100            filelist.Add("NotepadAdvancedTestOutputFile3.txt");101            filelist.Add("NotepadAdvancedTestOutputFile4.txt");102            filelist.Add("NotepadAdvancedTestOutputFile5.txt");103            //Change the font back to the default104            NotepadSession.FindElementByName("Format").Click();105            System.Threading.Thread.Sleep(1000);// Wait for 1 second until the dialog comes up106            NotepadSession.FindElementByName("Font...").Click();107            //Change the font size back to the default Lucida Console108            NotepadSession.FindElementByName("Lucida Console").Click();109            //Change the font format back to the default regular110            NotepadSession.FindElementByName("Regular").Click();111            //Change the font size back to the default size112            NotepadSession.FindElementByName("10").Click();113            //close the font dialog114            NotepadSession.FindElementByName("OK").Click();115            //Disable word wrap116            NotepadSession.FindElementByName("Format").Click();117            NotepadSession.FindElementByName("Word Wrap").Click();118            119            // Close Notepad120            NotepadSession.FindElementByName("File").Click();121            NotepadSession.FindElementByName("Exit").Click();122           System.Threading.Thread.Sleep(1000);// Wait for 1 second until the dialog comes up123            NotepadSession.FindElementByName("Don't Save").Click();124            NotepadSession = null;125            // Launch Windows Explorer to delete the saved text file above126            DesiredCapabilities appCapabilities = new DesiredCapabilities();127            appCapabilities.SetCapability("app", ExplorerAppId);128            WindowsDriver<WindowsElement> WindowsExplorerSession = new WindowsDriver<WindowsElement>(new Uri(AppDriverUrl), appCapabilities);129            Assert.IsNotNull(WindowsExplorerSession);130            // Create Desktop session to control context menu and access dialogs131            DesiredCapabilities desktopCapabilities = new DesiredCapabilities();132            desktopCapabilities.SetCapability("app", "Root");133            WindowsDriver<WindowsElement> DesktopSession = new WindowsDriver<WindowsElement>(new Uri(AppDriverUrl), desktopCapabilities);134            Assert.IsNotNull(DesktopSession);135            // Navigate Windows Explorer to the target save location folder136            Thread.Sleep(1000); // Wait for 1 second137            var addressBandRoot = WindowsExplorerSession.FindElementByClassName("Address Band Root");138            var addressToolbar = addressBandRoot.FindElementByAccessibilityId("1001"); // Address Band Toolbar...Scenario.cs
Source:Scenario.cs  
...13        protected const string NotepadAppId = @"C:\Windows\System32\notepad.exe";14        protected const string ExplorerAppId = @"C:\Windows\System32\explorer.exe";15        protected const string TestFileName = "NotepadTestOutputFile.txt";16        protected const string TargetSaveLocation = @"%TEMP%\";17        protected static WindowsDriver<WindowsElement> NotepadSession;18        [ClassInitialize]19        public static void Setup(TestContext context)20        {21            // Launch Notepad Classic Windows Application22            DesiredCapabilities appCapabilities = new DesiredCapabilities();23            appCapabilities.SetCapability("app", NotepadAppId);24            appCapabilities.SetCapability("deviceName", "WindowsPC");25            NotepadSession = new WindowsDriver<WindowsElement>(new Uri(AppDriverUrl), appCapabilities);26            Assert.IsNotNull(NotepadSession);27        }28        [TestMethod]29        public void BasicScenario()30        {31            // Verify that Notepad is started with untitled new file32            Assert.AreEqual("Untitled - Notepad", NotepadSession.Title);33            EnterText();34            SaveFile();35            // Verify that Notepad has saved the edited text file with the given name36            Thread.Sleep(1000); // Wait for 1 second until the window title is updated37            Assert.AreEqual(TestFileName + " - Notepad", NotepadSession.Title);38        }39        public void EnterText()40        {41            // Enter text into the edit field42            var editBox = NotepadSession.FindElementByClassName("Edit");43            editBox.SendKeys(TextValue);44            Assert.AreEqual(TextValue, editBox.Text);45            // Enter TimeStamp46            NotepadSession.FindElementByName("Edit").Click();47            NotepadSession.FindElementByName("Time/Date").Click();48            Assert.AreNotEqual(TextValue, editBox.Text);49        }50        public void SaveFile()51        {52            NotepadSession.FindElementByName("File").Click();53            NotepadSession.FindElementByName("Save As...").Click();54            System.Threading.Thread.Sleep(1000); // Wait for 1 second until the save dialog appears55            var fileNameBox = NotepadSession.FindElementByAccessibilityId("FileNameControlHost");56            fileNameBox.SendKeys(TargetSaveLocation + TestFileName);57            NotepadSession.FindElementByName("Save").Click();58            // Confirm save as dialog in case there's leftover test file from previous test run59            try60            {61                Thread.Sleep(1000); // Wait for 1 second until the dialog comes up62                var confirmSaveAsDialog = NotepadSession.FindElementByName("Confirm Save As");63                confirmSaveAsDialog.FindElementByName("Yes").Click();64            }65            catch { }66        }67        [ClassCleanup]68        public static void TearDown()69        {70            // Close Notepad71            NotepadSession.Quit();72            NotepadSession = null;73            // Launch Windows Explorer to delete the saved text file above74            DesiredCapabilities appCapabilities = new DesiredCapabilities();75            appCapabilities.SetCapability("app", ExplorerAppId);76            appCapabilities.SetCapability("deviceName", "WindowsPC");77            WindowsDriver<WindowsElement> WindowsExplorerSession = new WindowsDriver<WindowsElement>(new Uri(AppDriverUrl), appCapabilities);78            Assert.IsNotNull(WindowsExplorerSession);79            // Create Desktop session to control context menu and access dialogs80            DesiredCapabilities desktopCapabilities = new DesiredCapabilities();81            desktopCapabilities.SetCapability("app", "Root");82            desktopCapabilities.SetCapability("deviceName", "WindowsPC");83            WindowsDriver<WindowsElement> DesktopSession = new WindowsDriver<WindowsElement>(new Uri(AppDriverUrl), desktopCapabilities);84            Assert.IsNotNull(DesktopSession);85            // Navigate Windows Explorer to the target save location folder86            Thread.Sleep(1000); // Wait for 1 second...ScenarioEditor.cs
Source:ScenarioEditor.cs  
...24namespace NotepadTest25{26    27    [TestClass]28    public class ScenarioEditor : NotepadSession29    {30        //private static Logger _logger = LogManager.GetCurrentClassLogger();31        Logger _logger = LogManager.GetCurrentClassLogger();3233        [TestMethod]34        public void EditorEnterText()35        {36            var screenRecorder = ScreenRecorder.CreateRecorder(@"D:\Improvement\PrepareForTestReportTeacher\NotepadTest\TestResults\screenRecorder.mp4");37            _logger.Info("start");38            screenRecorder.StartRecording(screenRecorder, 2);39            // Type mixed text and apply shift modifier to 7890_ to generate corresponding symbols40            Thread.Sleep(TimeSpan.FromSeconds(2));41            editBox.SendKeys("abcdeABCDE 12345" + Keys.Shift + "7890-" + Keys.Shift + @"!@#$%");42            WinDriverUtil.ShotScreen(WinDriverUtil.DesktopSession, @"D:\Improvement\PrepareForTestReportTeacher\NotepadTest\TestResults\screen.png");
...NotepadSession
Using AI Code Generation
1using NotepadTest;2{3    public static void Main()4    {5        NotepadSession nSession = new NotepadSession();6        nSession.Open();7        nSession.Write("Hello World!");8        nSession.Close();9    }10}11        (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer12        (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, _13        (ByVal hwnd As Integer, ByVal wCmd As Integer) As IntegerNotepadSession
Using AI Code Generation
1using NotepadTest;2{3    static void Main(string[] args)4    {5        NotepadSession ns = new NotepadSession();6        ns.Start();7        ns.Edit("Hello World");8        ns.Close();9    }10}11using NotepadTest;12{13    static void Main(string[] args)14    {15        NotepadSession ns = new NotepadSession();16        ns.Start();17        ns.Edit("Hello World");18        ns.Close();19    }20}21using NotepadTest;22{23    static void Main(string[] args)24    {25        NotepadSession ns = new NotepadSession();26        ns.Start();27        ns.Edit("Hello World");28        ns.Close();29    }30}31using NotepadTest;32{33    static void Main(string[] args)34    {35        NotepadSession ns = new NotepadSession();36        ns.Start();37        ns.Edit("Hello World");38        ns.Close();39    }40}41using NotepadTest;42{43    static void Main(string[] args)44    {45        NotepadSession ns = new NotepadSession();46        ns.Start();47        ns.Edit("Hello World");48        ns.Close();49    }50}51using NotepadTest;52{53    static void Main(string[] args)54    {55        NotepadSession ns = new NotepadSession();56        ns.Start();57        ns.Edit("Hello World");58        ns.Close();59    }60}61using NotepadTest;62{63    static void Main(string[] args)64    {65        NotepadSession ns = new NotepadSession();66        ns.Start();67        ns.Edit("Hello World");68        ns.Close();69    }70}71using NotepadTest;72{73    static void Main(string[]NotepadSession
Using AI Code Generation
1using NotepadTest;2{3    static void Main()4    {5        NotepadSession ns = new NotepadSession();6        ns.StartNotepad();7        ns.WriteText("Hello World");8        ns.CloseNotepad();9    }10}NotepadSession
Using AI Code Generation
1using System;2using NotepadTest;3{4    static void Main()5    {6        NotepadSession n = new NotepadSession();7        n.Start();8    }9}10using System;11using NotepadTest;12{13    static void Main()14    {15        NotepadSession n = new NotepadSession();16        n.Start();17    }18}19using System;20using NotepadTest;21{22    static void Main()23    {24        NotepadSession n = new NotepadSession();25        n.Start();26    }27}28using System;29using NotepadTest;30{31    static void Main()32    {33        NotepadSession n = new NotepadSession();34        n.Start();35    }36}37using System;38using NotepadTest;39{40    static void Main()41    {42        NotepadSession n = new NotepadSession();43        n.Start();44    }45}46using System;47using NotepadTest;48{49    static void Main()50    {51        NotepadSession n = new NotepadSession();52        n.Start();53    }54}NotepadSession
Using AI Code Generation
1using NotepadTest;2{3    static void Main()4    {5        NotepadSession obj = new NotepadSession();6        obj.Open();7        obj.Write("Hello World");8        obj.Close();9    }10}NotepadSession
Using AI Code Generation
1using NotepadTest;2NotepadSession session = new NotepadSession();3session.Open();4session.Type("Hello World");5session.SaveAs("C:\\MyFile.txt");6session.Close();7using NotepadTest;8NotepadSession session = new NotepadSession();9session.Open();10session.Type("Hello World");11session.SaveAs("C:\\MyFile.txt");12session.Close();13using NotepadTest;14NotepadSession session = new NotepadSession();15session.Open();16session.Type("Hello World");17session.SaveAs("C:\\MyFile.txt");18session.Close();19using NotepadTest;20NotepadSession session = new NotepadSession();21session.Open();22session.Type("Hello World");23session.SaveAs("C:\\MyFile.txt");24session.Close();25using NotepadTest;26NotepadSession session = new NotepadSession();27session.Open();28session.Type("Hello World");29session.SaveAs("C:\\MyFile.txt");30session.Close();31using NotepadTest;32NotepadSession session = new NotepadSession();33session.Open();34session.Type("Hello World");35session.SaveAs("C:\\MyFile.txt");36session.Close();37using NotepadTest;38NotepadSession session = new NotepadSession();39session.Open();40session.Type("Hello World");41session.SaveAs("C:\\MyFile.txt");42session.Close();43using NotepadTest;44NotepadSession session = new NotepadSession();45session.Open();46session.Type("Hello World");47session.SaveAs("C:\\MyFile.txt");48session.Close();49using NotepadTest;50NotepadSession session = new NotepadSession();51session.Open();52session.Type("Hello World");53session.SaveAs("C:\\MyFile.txt");NotepadSession
Using AI Code Generation
1using NotepadTest;2NotepadSession nps = new NotepadSession();3nps.NotepadApp();4nps.NotepadFile();5using NotepadTest;6NotepadSession nps = new NotepadSession();7nps.NotepadApp();8nps.NotepadFile();9using NotepadTest;10NotepadSession nps = new NotepadSession();11nps.NotepadApp();12nps.NotepadFile();13using NotepadTest;14NotepadSession nps = new NotepadSession();15nps.NotepadApp();16nps.NotepadFile();17using NotepadTest;18NotepadSession nps = new NotepadSession();19nps.NotepadApp();20nps.NotepadFile();21using NotepadTest;22NotepadSession nps = new NotepadSession();23nps.NotepadApp();24nps.NotepadFile();25using NotepadTest;26NotepadSession nps = new NotepadSession();27nps.NotepadApp();28nps.NotepadFile();29using NotepadTest;30NotepadSession nps = new NotepadSession();31nps.NotepadApp();32nps.NotepadFile();33using NotepadTest;34NotepadSession nps = new NotepadSession();35nps.NotepadApp();36nps.NotepadFile();37using NotepadTest;38NotepadSession nps = new NotepadSession();39nps.NotepadApp();40nps.NotepadFile();NotepadSession
Using AI Code Generation
1using NotepadTest;2{3{4public static void Main()5{6}7}8}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!!
