Best WinAppDriver code snippet using WebDriverAPI.SendKeys
SendKeys.cs
Source:SendKeys.cs
...20using System;21namespace WebDriverAPI22{23 [TestClass]24 public class SendKeys25 {26 protected static WindowsDriver<WindowsElement> session;27 protected static WindowsElement editBox;28 [ClassInitialize]29 public static void ClassInitialize(TestContext context)30 {31 session = Utility.CreateNewSession(CommonTestSettings.NotepadAppId);32 Assert.IsNotNull(session);33 editBox = session.FindElementByClassName("Edit");34 Assert.IsNotNull(editBox);35 }36 [ClassCleanup]37 public static void ClassCleanup()38 {39 if (session != null)40 {41 ClearEditBox();42 session.Quit();43 session = null;44 }45 }46 [TestInitialize]47 public void TestInitialize()48 {49 ClearEditBox();50 }51 public static void ClearEditBox()52 {53 // Select all text and delete using keyboard shortcut Ctrl + A and Delete54 editBox.Click();55 session.Keyboard.SendKeys(Keys.Control + "a" + Keys.Control);56 session.Keyboard.SendKeys(Keys.Delete);57 Assert.AreEqual(string.Empty, editBox.Text);58 }59 [TestMethod]60 public void SendKeys_Alphabet()61 {62 session.Keyboard.SendKeys("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");63 Assert.AreEqual("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", editBox.Text);64 }65 [TestMethod]66 public void SendKeys_EmptySequence()67 {68 session.Keyboard.SendKeys(string.Empty);69 Assert.AreEqual(string.Empty, editBox.Text);70 }71 [TestMethod]72 public void SendKeys_ModifierAlt()73 {74 session.Keyboard.SendKeys(Keys.Alt + "ED" + Keys.Alt); // Insert Time/Date75 Assert.IsTrue(editBox.Text.Length > 0);76 }77 [TestMethod]78 public void SendKeys_ModifierControl()79 {80 session.Keyboard.SendKeys("789");81 session.Keyboard.SendKeys(Keys.Control + "a" + Keys.Control); // Select all82 session.Keyboard.SendKeys(Keys.Control + "c" + Keys.Control); // Copy83 session.Keyboard.SendKeys(Keys.Control + "vvv" + Keys.Control); // Paste 3 times84 Assert.AreEqual("789789789", editBox.Text);85 }86 [TestMethod]87 public void SendKeys_ModifierExplicitRelease()88 {89 // Keys persist all modifier between API call and requires ecplicit modifier release90 session.Keyboard.SendKeys(Keys.Shift + "abcwxyz1237890"); // Shift modifier is still pressed91 session.Keyboard.SendKeys("abcwxyz1237890" + Keys.Shift);92 session.Keyboard.SendKeys("abcwxyz1237890");93 Assert.AreEqual("ABCWXYZ!@#&*()ABCWXYZ!@#&*()abcwxyz1237890", editBox.Text);94 }95 [TestMethod]96 public void SendKeys_ModifierShift()97 {98 session.Keyboard.SendKeys(Keys.Shift + "abcdefghijklmnopqrstuvwxyz\n1234567890\t`-=[]\\;',./" + Keys.Shift);99 Assert.AreEqual("ABCDEFGHIJKLMNOPQRSTUVWXYZ\r\n!@#$%^&*()\t~_+{}|:\"<>?", editBox.Text); // Assumes 101 keys US Keyboard layout100 }101 [TestMethod]102 public void SendKeys_ModifierWindowsKey()103 {104 WindowsDriver<WindowsElement> desktopSession = Utility.CreateNewSession(CommonTestSettings.DesktopAppId);105 Assert.IsNotNull(desktopSession);106 // Launch action center using Window Keys + A107 session.Keyboard.SendKeys(Keys.Command + "a" + Keys.Command);108 Thread.Sleep(TimeSpan.FromSeconds(2));109 // Verify that Action Center pane is displayed110 WindowsElement actionCenterElement = desktopSession.FindElementByName("Action center");111 Assert.IsNotNull(actionCenterElement);112 Assert.IsNotNull(actionCenterElement.FindElementByName("Tablet mode"));113 // Dismiss action center and cleanup the temporary session114 actionCenterElement.SendKeys(Keys.Escape);115 editBox.Click();116 desktopSession.Quit();117 }118 [TestMethod]119 public void SendKeys_NonPrintableKeys()120 {121 session.Keyboard.SendKeys("9");122 session.Keyboard.SendKeys(Keys.Home + "8");123 session.Keyboard.SendKeys(Keys.Home + "7");124 session.Keyboard.SendKeys(Keys.Home + Keys.Tab);125 session.Keyboard.SendKeys(Keys.Home + Keys.Enter);126 session.Keyboard.SendKeys(Keys.Up + Keys.Tab + "78");127 session.Keyboard.SendKeys(Keys.Home + Keys.Enter);128 session.Keyboard.SendKeys(Keys.Up + Keys.Tab + "7");129 Assert.AreEqual("\t7\r\n\t78\r\n\t789", editBox.Text);130 }131 [TestMethod]132 public void SendKeys_Number()133 {134 session.Keyboard.SendKeys("0123456789");135 Assert.AreEqual("0123456789", editBox.Text);136 }137 [TestMethod]138 public void SendKeys_SymbolsEscapedCharacter()139 {140 // Line endings such as \r or \n are replaced with \r\n141 // form feeds (\f) or vertical tab feeds (\v) are removed142 session.Keyboard.SendKeys("\a\b\f\n\r\t\v\'\"\\\r\n");143 Assert.AreEqual("\r\n\r\n\t\'\"\\\r\n\r\n", editBox.Text);144 }145 [TestMethod]146 public void SendKeys_SymbolsKeys()147 {148 session.Keyboard.SendKeys("`-=[]\\;',./~!@#$%^&*()_+{}|:\"<>?");149 Assert.AreEqual("`-=[]\\;',./~!@#$%^&*()_+{}|:\"<>?", editBox.Text);150 }151 [TestMethod]152 public void SendKeysError_NoSuchWindow()153 {154 try155 {156 Utility.GetOrphanedSession().Keyboard.SendKeys("keys");157 Assert.Fail("Exception should have been thrown");158 }159 catch (InvalidOperationException exception)160 {161 Assert.AreEqual(ErrorStrings.NoSuchWindow, exception.Message);162 }163 }164 }165}...
SendKeys
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.Chrome;8{9 {10 static void Main(string[] args)11 {12 IWebDriver driver = new ChromeDriver();13 driver.Manage().Window.Maximize();14 driver.FindElement(By.Name("q")).SendKeys("Hello World");15 driver.Close();16 }17 }18}19using System;20using System.Collections.Generic;21using System.Linq;22using System.Text;23using System.Threading.Tasks;24using OpenQA.Selenium;25using OpenQA.Selenium.Chrome;26{27 {28 static void Main(string[] args)29 {30 IWebDriver driver = new ChromeDriver();31 driver.Manage().Window.Maximize();32 driver.FindElement(By.Name("q")).SendKeys("Hello World");33 driver.FindElement(By.Name("q")).SendKeys(Keys.Enter);34 driver.Close();35 }36 }37}38using System;39using System.Collections.Generic;40using System.Linq;41using System.Text;42using System.Threading.Tasks;43using OpenQA.Selenium;44using OpenQA.Selenium.Chrome;45{46 {47 static void Main(string[] args)48 {49 IWebDriver driver = new ChromeDriver();50 driver.Manage().Window.Maximize();51 driver.FindElement(By.Name("q")).SendKeys("Hello World");52 driver.FindElement(By.Name("q")).SendKeys(Keys.Enter);53 driver.FindElement(By.Name("btnK")).Click();54 driver.Close();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.Chrome;65{66 {67 static void Main(string[] args)68 {69 IWebDriver driver = new ChromeDriver();70 driver.Manage().Window.Maximize();71 driver.FindElement(By.Name("q
SendKeys
Using AI Code Generation
1using OpenQA.Selenium;2using OpenQA.Selenium.Chrome;3using System;4using System.Collections.Generic;5using System.Linq;6using System.Text;7using System.Threading.Tasks;8{9 {10 static void Main(string[] args)11 {12 IWebDriver driver = new ChromeDriver();13 driver.Manage().Window.Maximize();14 IWebElement search = driver.FindElement(By.Name("q"));15 search.SendKeys("Selenium");16 search.SendKeys(Keys.Enter);17 Console.ReadLine();18 }19 }20}21using OpenQA.Selenium;22using OpenQA.Selenium.Chrome;23using System;24using System.Collections.Generic;25using System.Linq;26using System.Text;27using System.Threading.Tasks;28{29 {30 static void Main(string[] args)31 {32 IWebDriver driver = new ChromeDriver();33 driver.Manage().Window.Maximize();34 IWebElement search = driver.FindElement(By.Name("q"));35 Actions action = new Actions(driver);36 action.Click(search).SendKeys("Selenium").SendKeys(Keys.Enter).Build().Perform();37 Console.ReadLine();38 }39 }40}41using OpenQA.Selenium;42using OpenQA.Selenium.Chrome;43using System;44using System.Collections.Generic;45using System.Linq;46using System.Text;47using System.Threading.Tasks;48{49 {50 static void Main(string[] args)51 {52 IWebDriver driver = new ChromeDriver();53 driver.Manage().Window.Maximize();54 IWebElement search = driver.FindElement(By.Name("q"));55 Actions action = new Actions(driver);56 action.Click(search).SendKeys("Selenium").SendKeys(Keys.Enter).Build().Perform();57 JavascriptExecutor js = (JavascriptExecutor)driver;58 js.ExecuteScript("arguments[0].scrollIntoView();", selenium);59 Console.ReadLine();60 }61 }62}63using OpenQA.Selenium;64using OpenQA.Selenium.Chrome;65using System;
SendKeys
Using AI Code Generation
1using OpenQA.Selenium;2using OpenQA.Selenium.Chrome;3using OpenQA.Selenium.Interactions;4using System;5using System.Collections.Generic;6using System.Linq;7using System.Text;8using System.Threading.Tasks;9{10 {11 static void Main(string[] args)12 {13 IWebDriver driver = new ChromeDriver();14 driver.Manage().Window.Maximize();15 driver.FindElement(By.Name("q")).SendKeys("Selenium");16 driver.FindElement(By.Name("btnK")).SendKeys(Keys.Enter);17 driver.Close();18 }19 }20}21using OpenQA.Selenium;22using OpenQA.Selenium.Chrome;23using OpenQA.Selenium.Interactions;24using System;25using System.Collections.Generic;26using System.Linq;27using System.Text;28using System.Threading.Tasks;29{30 {31 static void Main(string[] args)32 {33 IWebDriver driver = new ChromeDriver();34 driver.Manage().Window.Maximize();35 driver.FindElement(By.Name("q")).SendKeys("Selenium");36 Actions a = new Actions(driver);37 a.SendKeys(Keys.Enter).Build().Perform();38 driver.Close();39 }40 }41}42using OpenQA.Selenium;43using OpenQA.Selenium.Chrome;44using OpenQA.Selenium.Interactions;45using System;46using System.Collections.Generic;47using System.Linq;48using System.Text;49using System.Threading.Tasks;50{51 {52 static void Main(string[] args)53 {54 IWebDriver driver = new ChromeDriver();55 driver.Manage().Window.Maximize();56 driver.FindElement(By.Name("q")).SendKeys("Selenium");57 Actions a = new Actions(driver);58 a.SendKeys(Keys.Enter).Build().Perform();59 driver.Close();60 }61 }62}63using OpenQA.Selenium;64using OpenQA.Selenium.Chrome;65using OpenQA.Selenium.Interactions;66using OpenQA.Selenium.Support.UI;67using System;68using System.Collections.Generic;69using System.Linq;70using System.Text;71using System.Threading.Tasks;72{
SendKeys
Using AI Code Generation
1using OpenQA.Selenium;2using OpenQA.Selenium.Chrome;3using System;4using System.Threading;5{6{7static void Main(string[] args)8{9IWebDriver driver = new ChromeDriver(@"C:\Users\shailendra\Downloads\chromedriver_win32");10driver.Manage().Window.Maximize();11Thread.Sleep(5000);12IWebElement element = driver.FindElement(By.Name("q"));13element.SendKeys("Selenium");14Thread.Sleep(5000);15driver.Close();16}17}18}
SendKeys
Using AI Code Generation
1using OpenQA.Selenium;2using OpenQA.Selenium.Chrome;3using System;4using System.Threading;5{6 {7 static void Main(string[] args)8 {9 IWebDriver driver = new ChromeDriver();10 driver.SwitchTo().ActiveElement().SendKeys("Selenium" + Keys.Enter);11 Thread.Sleep(5000);12 driver.Close();13 }14 }15}16using OpenQA.Selenium;17using OpenQA.Selenium.Chrome;18using System;19using System.Threading;20{21 {22 static void Main(string[] args)23 {24 IWebDriver driver = new ChromeDriver();25 driver.SwitchTo().ActiveElement().SendKeys("Selenium" + Keys.Enter);26 Thread.Sleep(5000);27 driver.Quit();28 }29 }30}
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!!