Best WinAppDriver code snippet using WebDriverAPI.ElementSendKeys.TestInit
ElementSendKeys.cs
Source:ElementSendKeys.cs
...33 public static void ClassCleanup()34 {35 TearDown();36 }37 [TestInitialize]38 public override void TestInit()39 {40 // Open new alarm page if app is currently in different view41 try42 {43 alarmNameTextBox = session.FindElementByAccessibilityId("AlarmNameTextBox");44 }45 catch46 {47 base.TestInit();48 alarmTabElement.FindElementByAccessibilityId("AddAlarmButton").Click();49 Thread.Sleep(TimeSpan.FromSeconds(1.5));50 alarmNameTextBox = session.FindElementByAccessibilityId("AlarmNameTextBox");51 }52 // Select all text and delete using keyboard shortcut Ctrl + A and Delete53 alarmNameTextBox.SendKeys(Keys.Control + "a");54 alarmNameTextBox.SendKeys(Keys.Delete);55 Assert.AreEqual(string.Empty, alarmNameTextBox.Text);56 }57 [TestMethod]58 public void SendKeysToElement_Alphabet()59 {60 alarmNameTextBox.SendKeys("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");61 Assert.AreEqual("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", alarmNameTextBox.Text);62 }63 [TestMethod]64 public void SendKeysToElement_EmptySequence()65 {66 alarmNameTextBox.SendKeys(string.Empty);67 Assert.AreEqual(string.Empty, alarmNameTextBox.Text);68 }69 [TestMethod]70 public void SendKeysToElement_ModifierAlt()71 {72 alarmNameTextBox.SendKeys(Keys.Space);73 Assert.AreEqual("True", alarmNameTextBox.GetAttribute("HasKeyboardFocus"));74 alarmNameTextBox.SendKeys(Keys.Alt + Keys.Enter + Keys.Alt); // Alt + Enter moves the focus to the next element75 Assert.AreEqual("False", alarmNameTextBox.GetAttribute("HasKeyboardFocus"));76 }77 [TestMethod]78 public void SendKeysToElement_ModifierControl()79 {80 alarmNameTextBox.SendKeys("789");81 alarmNameTextBox.SendKeys(Keys.Control + "a" + Keys.Control); // Select all82 alarmNameTextBox.SendKeys(Keys.Control + "c" + Keys.Control); // Copy83 alarmNameTextBox.SendKeys(Keys.Control + "vvv" + Keys.Control); // Paste 3 times84 Assert.AreEqual("789789789", alarmNameTextBox.Text);85 }86 [TestMethod]87 public void SendKeysToElement_ModifierImplicitRelease()88 {89 // SendKeys implicitly depress all modifier at the end of the sequence (every API call)90 alarmNameTextBox.SendKeys(Keys.Shift + "abcwxyz1237890"); // Implicit shift release at the end of the sequence91 alarmNameTextBox.SendKeys("abcwxyz1237890" + Keys.Shift);92 alarmNameTextBox.SendKeys("abcwxyz1237890");93 Assert.AreEqual("ABCWXYZ!@#&*()abcwxyz1237890abcwxyz1237890", alarmNameTextBox.Text);94 }95 [TestMethod]96 public void SendKeysToElement_ModifierShift()97 {98 alarmNameTextBox.SendKeys(Keys.Shift + "abcwxyz1237890`-=[]\\;',./" + Keys.Shift);99 Assert.AreEqual("ABCWXYZ!@#&*()~_+{}|:\"<>?", alarmNameTextBox.Text); // Assumes 101 keys US Keyboard layout100 }101 [TestMethod]102 public void SendKeysToElement_NonPrintableKeys()103 {104 Thread.Sleep(TimeSpan.FromSeconds(1));105 alarmNameTextBox.SendKeys("9");106 alarmNameTextBox.SendKeys(Keys.Home + "8");107 alarmNameTextBox.SendKeys(Keys.Left + "7");108 Assert.AreEqual("789", alarmNameTextBox.Text);109 }110 [TestMethod]111 public void SendKeysToElement_Number()112 {113 alarmNameTextBox.SendKeys("0123456789");114 Assert.AreEqual("0123456789", alarmNameTextBox.Text);115 }116 [TestMethod]117 public void SendKeysToElement_SymbolsKeys()118 {119 alarmNameTextBox.SendKeys("`-=[]\\;',./~!@#$%^&*()_+{}|:\"<>?");120 Assert.AreEqual("`-=[]\\;',./~!@#$%^&*()_+{}|:\"<>?", alarmNameTextBox.Text);121 }122 [TestMethod]123 public void SendKeysToElementError_ElementNotVisible()124 {125 // Navigate to Stopwatch tab and attempt to click on addAlarmButton that is no longer displayed126 base.TestInit();127 WindowsElement addAlarmButton = session.FindElementByAccessibilityId("AddAlarmButton");128 session.FindElementByAccessibilityId("StopwatchPivotItem").Click();129 Thread.Sleep(TimeSpan.FromSeconds(1));130 Assert.IsFalse(addAlarmButton.Displayed);131 try132 {133 addAlarmButton.SendKeys("keys");134 Assert.Fail("Exception should have been thrown");135 }136 catch (InvalidOperationException exception)137 {138 Assert.AreEqual(ErrorStrings.ElementNotVisible, exception.Message);139 }140 }...
TestInit
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;8using OpenQA.Selenium.Firefox;9using OpenQA.Selenium.IE;10using OpenQA.Selenium.Remote;11using OpenQA.Selenium.Support.UI;12using WebDriverAPI;13{14 {15 public static void TestInit(IWebDriver driver)16 {17 IWebElement element = driver.FindElement(By.Name("q"));18 element.SendKeys("Cheese" + Keys.RETURN);19 driver.Quit();20 }21 }22}23using System;24using System.Collections.Generic;25using System.Linq;26using System.Text;27using System.Threading.Tasks;28using OpenQA.Selenium;29using OpenQA.Selenium.Chrome;30using OpenQA.Selenium.Firefox;31using OpenQA.Selenium.IE;32using OpenQA.Selenium.Remote;33using OpenQA.Selenium.Support.UI;34using WebDriverAPI;35{36 {37 public static void TestInit(IWebDriver driver)38 {39 IWebElement element = driver.FindElement(By.Name("q"));40 element.SendKeys("Cheese" + Keys.RETURN);41 driver.Quit();42 }43 }44}45using System;46using System.Collections.Generic;47using System.Linq;48using System.Text;49using System.Threading.Tasks;50using OpenQA.Selenium;51using OpenQA.Selenium.Chrome;52using OpenQA.Selenium.Firefox;53using OpenQA.Selenium.IE;54using OpenQA.Selenium.Remote;55using OpenQA.Selenium.Support.UI;56using WebDriverAPI;57{58 {59 public static void TestInit(IWebDriver driver)60 {61 IWebElement element = driver.FindElement(By.Name("q"));62 element.SendKeys("Cheese" + Keys.RETURN);63 driver.Quit();64 }65 }66}67using System;68using System.Collections.Generic;69using System.Linq;70using System.Text;71using System.Threading.Tasks;
TestInit
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.Safari;12using OpenQA.Selenium.Remote;13using OpenQA.Selenium.Support.UI;14using WebDriverAPI;15{16 {17 static void Main(string[] args)18 {19 SafariDriver driver = new SafariDriver();20 driver.Manage().Window.Maximize();21 ElementSendKeys TestInit = new ElementSendKeys(driver);22 TestInit.TestInit();23 driver.Quit();24 }25 }26}27using System;28using System.Collections.Generic;29using System.Linq;30using System.Text;31using System.Threading.Tasks;32using OpenQA.Selenium;33using OpenQA.Selenium.Firefox;34using OpenQA.Selenium.Chrome;35using OpenQA.Selenium.IE;36using OpenQA.Selenium.Opera;37using OpenQA.Selenium.Safari;38using OpenQA.Selenium.Remote;39using OpenQA.Selenium.Support.UI;40using WebDriverAPI;41{42 {43 static void Main(string[] args)44 {45 SafariDriver driver = new SafariDriver();46 driver.Manage().Window.Maximize();47 ElementSendKeys TestInit = new ElementSendKeys(driver);48 TestInit.TestInit();49 driver.Quit();50 }51 }52}53using System;54using System.Collections.Generic;55using System.Linq;56using System.Text;57using System.Threading.Tasks;58using OpenQA.Selenium;59using OpenQA.Selenium.Firefox;60using OpenQA.Selenium.Chrome;61using OpenQA.Selenium.IE;62using OpenQA.Selenium.Opera;63using OpenQA.Selenium.Safari;
TestInit
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;8using OpenQA.Selenium.Support.UI;9using WebDriverAPI;10{11 {12 public static void TestInit()13 {14 IWebDriver driver = new ChromeDriver();15 IWebElement element = driver.FindElement(By.Name("q"));16 element.SendKeys("Selenium");17 driver.Quit();18 }19 }20}21using System;22using System.Collections.Generic;23using System.Linq;24using System.Text;25using System.Threading.Tasks;26using OpenQA.Selenium;27using OpenQA.Selenium.Chrome;28using OpenQA.Selenium.Support.UI;29using WebDriverAPI;30{31 {32 public static void TestInit()33 {34 IWebDriver driver = new ChromeDriver();35 IWebElement element = driver.FindElement(By.Name("q"));36 element.SendKeys("Selenium");37 element.SendKeys(Keys.Enter);38 driver.Quit();39 }40 }41}42using System;43using System.Collections.Generic;44using System.Linq;45using System.Text;46using System.Threading.Tasks;47using OpenQA.Selenium;48using OpenQA.Selenium.Chrome;49using OpenQA.Selenium.Support.UI;50using WebDriverAPI;51{52 {53 public static void TestInit()54 {55 IWebDriver driver = new ChromeDriver();56 IWebElement element = driver.FindElement(By.Name("q"));57 element.SendKeys("Selenium");58 element.SendKeys(Keys.Enter);59 element.Clear();60 driver.Quit();61 }62 }63}64using System;65using System.Collections.Generic;66using System.Linq;67using System.Text;68using System.Threading.Tasks;69using OpenQA.Selenium;70using OpenQA.Selenium.Chrome;71using OpenQA.Selenium.Support.UI;72using WebDriverAPI;73{74 {75 public static void TestInit()76 {77 IWebDriver driver = new ChromeDriver();
TestInit
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using System.Threading;7using OpenQA.Selenium;8using OpenQA.Selenium.Chrome;9using OpenQA.Selenium.Support.UI;10using OpenQA.Selenium.Interactions;11using System.IO;12using NUnit.Framework;13using WebDriverAPI;14{15 {16 IWebDriver driver = null;17 public void TestInit()18 {19 driver = new ChromeDriver();20 driver.Manage().Window.Maximize();21 IWebElement element = driver.FindElement(By.Name("q"));22 element.SendKeys("selenium");23 Thread.Sleep(2000);24 element.Clear();25 element.SendKeys("webdriver");26 Thread.Sleep(2000);27 driver.Close();28 }29 }30}31Test run for C:\Users\HP\Desktop\WebDriverAPI\bin\Debug\netcoreapp2.0\WebDriverAPI.dll(.NETCoreApp,Version=v2.0)32Microsoft (R) Test Execution Command Line Tool Version 15.8.0
TestInit
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.Support.UI;9using WebDriverAPI;10{11 {12 static void Main(string[] args)13 {14 IWebDriver driver = new FirefoxDriver();15 driver.FindElement(By.Id("lst-ib")).SendKeys("Selenium");16 driver.FindElement(By.Id("lst-ib")).SendKeys(Keys.Enter);17 driver.Close();18 }19 }20}21using System;22using System.Collections.Generic;23using System.Linq;24using System.Text;25using System.Threading.Tasks;26using OpenQA.Selenium;27using OpenQA.Selenium.Firefox;28using OpenQA.Selenium.Support.UI;29using WebDriverAPI;30{31 {32 static void Main(string[] args)33 {34 IWebDriver driver = new FirefoxDriver();35 driver.FindElement(By.Id("lst-ib")).SendKeys("Selenium");36 driver.FindElement(By.Name("btnG")).Submit();37 driver.Close();38 }39 }40}41using System;42using System.Collections.Generic;43using System.Linq;44using System.Text;45using System.Threading.Tasks;46using OpenQA.Selenium;47using OpenQA.Selenium.Firefox;48using OpenQA.Selenium.Support.UI;49using WebDriverAPI;50{51 {52 static void Main(string[] args)53 {54 IWebDriver driver = new FirefoxDriver();55 driver.FindElement(By.Id("lst-ib")).SendKeys("Selenium");56 driver.FindElement(By.Name("btnG")).Submit();57 driver.FindElement(By.Id("lst-ib")).Clear();58 driver.Close();59 }60 }61}62using System;63using System.Collections.Generic;
TestInit
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using WebDriverAPI;7using OpenQA.Selenium;8using OpenQA.Selenium.Firefox;9using OpenQA.Selenium.Chrome;10using OpenQA.Selenium.IE;11using OpenQA.Selenium.Edge;12using OpenQA.Selenium.PhantomJS;13using OpenQA.Selenium.Opera;14using OpenQA.Selenium.Safari;15using OpenQA.Selenium.Remote;16using OpenQA.Selenium.Support.UI;17using System.Threading;18using System.IO;19{20 {21 public static void Main(string[] args)22 {23 TestInit();24 }25 public static void TestInit()26 {27 IWebDriver driver = new FirefoxDriver();28 driver = new FirefoxDriver();29 IWebElement searchBox = driver.FindElement(By.Name("q"));30 searchBox.SendKeys("Selenium WebDriver");31 searchBox.Submit();32 driver.Quit();33 }34 }35}36using System;37using System.Collections.Generic;38using System.Linq;39using System.Text;40using System.Threading.Tasks;41using WebDriverAPI;42using OpenQA.Selenium;43using OpenQA.Selenium.Firefox;44using OpenQA.Selenium.Chrome;45using OpenQA.Selenium.IE;46using OpenQA.Selenium.Edge;47using OpenQA.Selenium.PhantomJS;48using OpenQA.Selenium.Opera;49using OpenQA.Selenium.Safari;50using OpenQA.Selenium.Remote;51using OpenQA.Selenium.Support.UI;52using System.Threading;53using System.IO;54{55 {56 public static void Main(string[] args)57 {58 TestInit();59 }60 public static void TestInit()61 {62 IWebDriver driver = new FirefoxDriver();63 driver = new FirefoxDriver();
TestInit
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using WebDriverAPI;7using OpenQA.Selenium;8using OpenQA.Selenium.Firefox;9using OpenQA.Selenium.Support.UI;10using OpenQA.Selenium.Interactions;11using OpenQA.Selenium.Support.PageObjects;12using System.Threading;13using System.IO;14using System.Reflection;15using System.Diagnostics;16{17 {18 static void Main(string[] args)19 {20 IWebDriver driver = new FirefoxDriver();21 driver.Manage().Window.Maximize();22 driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5));23 ElementSendKeys.TestInit(driver);24 driver.Close();25 }26 }27}28using System;29using System.Collections.Generic;30using System.Linq;31using System.Text;32using System.Threading.Tasks;33using WebDriverAPI;34using OpenQA.Selenium;35using OpenQA.Selenium.Firefox;36using OpenQA.Selenium.Support.UI;37using OpenQA.Selenium.Interactions;38using OpenQA.Selenium.Support.PageObjects;39using System.Threading;40using System.IO;41using System.Reflection;42using System.Diagnostics;43{44 {45 static void Main(string[] args)46 {47 IWebDriver driver = new FirefoxDriver();48 driver.Manage().Window.Maximize();49 driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5));50 ElementClick.TestInit(driver);51 driver.Close();52 }53 }54}55using System;56using System.Collections.Generic;57using System.Linq;58using System.Text;59using System.Threading.Tasks;60using WebDriverAPI;61using OpenQA.Selenium;
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!!