Best WinAppDriver code snippet using WebDriverAPI.Sessions
Sessions.cs
Source:Sessions.cs
...20using OpenQA.Selenium.Appium.Windows;21namespace WebDriverAPI22{23 [TestClass]24 public class Sessions25 {26 [TestMethod]27 public void GetSessions_CurrentList()28 {29 using (HttpWebResponse response = WebRequest.Create(CommonTestSettings.WindowsApplicationDriverUrl + "/sessions").GetResponse() as HttpWebResponse)30 {31 var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();32 JObject responseObject = JObject.Parse(responseString);33 Assert.AreEqual(0, (int)responseObject["status"]);34 JArray capabilitiesArray = (JArray)responseObject["value"];35 Assert.IsNotNull(capabilitiesArray);36 Assert.IsTrue(capabilitiesArray.Count >= 0);37 }38 }39 [TestMethod]40 public void GetSessions_SingleEntry()41 {42 WindowsDriver<WindowsElement> session = Utility.CreateNewSession(CommonTestSettings.AlarmClockAppId);43 Assert.IsNotNull(session);44 using (HttpWebResponse response = WebRequest.Create(CommonTestSettings.WindowsApplicationDriverUrl + "/sessions").GetResponse() as HttpWebResponse)45 {46 var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();47 JObject responseObject = JObject.Parse(responseString);48 Assert.AreEqual(0, (int)responseObject["status"]);49 JArray capabilitiesArray = (JArray)responseObject["value"];50 Assert.IsTrue(capabilitiesArray.Count >= 1);51 // Verify that the newly created session is on the list52 JToken newSessionEntry = null;53 foreach (var entry in capabilitiesArray.Children())54 {55 if (entry["id"].ToString() == session.SessionId.ToString())56 {57 newSessionEntry = entry;58 break;59 }60 }61 Assert.IsNotNull(newSessionEntry);62 Assert.AreEqual(CommonTestSettings.AlarmClockAppId, newSessionEntry["capabilities"]["app"].ToString());63 }64 session.Quit();65 }66 [TestMethod]67 public void GetSessions_MultipleEntry()68 {69 WindowsDriver<WindowsElement> calculatorSession = Utility.CreateNewSession(CommonTestSettings.CalculatorAppId);70 Assert.IsNotNull(calculatorSession);71 WindowsDriver<WindowsElement> notepadSession = Utility.CreateNewSession(CommonTestSettings.NotepadAppId);72 Assert.IsNotNull(notepadSession);73 int openSessionsCount = 0;74 using (HttpWebResponse response = WebRequest.Create(CommonTestSettings.WindowsApplicationDriverUrl + "/sessions").GetResponse() as HttpWebResponse)75 {76 var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();77 JObject responseObject = JObject.Parse(responseString);78 Assert.AreEqual(0, (int)responseObject["status"]);79 // There needs to be at least 2 open sessions after we create the 2 session above80 JArray capabilitiesArray = (JArray)responseObject["value"];81 openSessionsCount = capabilitiesArray.Count;82 Assert.IsTrue(openSessionsCount >= 2);83 // Verify that both calculator and notepad sessions are created84 JToken calculatorSessionEntry = null;85 JToken notepadSessionEntry = null;86 foreach (var entry in capabilitiesArray.Children())87 {88 if (entry["id"].ToString() == calculatorSession.SessionId.ToString())89 {90 calculatorSessionEntry = entry;91 }92 else if (entry["id"].ToString() == notepadSession.SessionId.ToString())93 {94 notepadSessionEntry = entry;95 }96 if (calculatorSessionEntry != null && notepadSessionEntry != null)97 {98 break;99 }100 }101 Assert.IsNotNull(calculatorSessionEntry);102 Assert.AreEqual(CommonTestSettings.CalculatorAppId, calculatorSessionEntry["capabilities"]["app"].ToString());103 Assert.IsNotNull(notepadSessionEntry);104 Assert.AreEqual(CommonTestSettings.NotepadAppId, notepadSessionEntry["capabilities"]["app"].ToString());105 }106 // Close the newly created sessions107 calculatorSession.Quit();108 notepadSession.Quit();109 using (HttpWebResponse response = WebRequest.Create(CommonTestSettings.WindowsApplicationDriverUrl + "/sessions").GetResponse() as HttpWebResponse)110 {111 var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();112 JObject responseObject = JObject.Parse(responseString);113 Assert.AreEqual(0, (int)responseObject["status"]);114 // There needs to be 2 less sessions after we closed the 2 sessions above115 JArray capabilitiesArray = (JArray)responseObject["value"];116 Assert.AreEqual(openSessionsCount - 2, capabilitiesArray.Count);117 }118 }119 }120}...
Sessions
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;11{12 {13 static void Main(string[] args)14 {15 IWebDriver driver = new ChromeDriver();16 driver.Manage().Window.Maximize();17 Console.WriteLine("Current URL of the page is : " + driver.Url);18 Console.WriteLine("Current Title of the page is : " + driver.Title);19 Console.WriteLine("Current window handle is : " + driver.CurrentWindowHandle);20 Console.WriteLine("The current window handles are : ");21 foreach (string handle in driver.WindowHandles)22 {23 Console.WriteLine(handle);24 }25 Console.WriteLine("Current session ID is : " + ((RemoteWebDriver)driver).SessionId);26 driver.Quit();27 }28 }29}
Sessions
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.Remote;8{9 {10 static void Main(string[] args)11 {12 ICapabilities cap = DesiredCapabilities.Chrome();13 Console.WriteLine("Session ID is: " + ((RemoteWebDriver)driver).SessionId);14 driver.Quit();15 }16 }17}
Sessions
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 OpenQA.Selenium.Interactions;10using System.Threading;11using OpenQA.Selenium.Firefox;12{13 {14 static void Main(string[] args)15 {16 IWebDriver driver = new ChromeDriver();17 driver.Manage().Window.Maximize();18 Thread.Sleep(5000);19 Thread.Sleep(5000);20 driver.Navigate().Back();21 Thread.Sleep(5000);22 driver.Navigate().Forward();23 Thread.Sleep(5000);24 driver.Navigate().Refresh();25 driver.Quit();26 }27 }28}29using System;30using System.Collections.Generic;31using System.Linq;32using System.Text;33using System.Threading.Tasks;34using OpenQA.Selenium;35using OpenQA.Selenium.Chrome;36using OpenQA.Selenium.Support.UI;37using OpenQA.Selenium.Interactions;38using System.Threading;39using OpenQA.Selenium.Firefox;40{41 {42 static void Main(string[] args)43 {44 IWebDriver driver = new ChromeDriver();45 driver.Manage().Window.Maximize();46 Thread.Sleep(5000);47 Thread.Sleep(5000);48 Thread.Sleep(5000);49 driver.SwitchTo().Window(driver.WindowHandles[1]);50 Thread.Sleep(5000);51 driver.SwitchTo().Window(driver.WindowHandles[0]);52 Thread.Sleep(5000);53 driver.SwitchTo().Window(driver.WindowHandles[2]);54 Thread.Sleep(5000);55 driver.SwitchTo().Window(driver.WindowHandles[0]);56 Thread.Sleep(5000);57 driver.Quit();58 }59 }60}61using System;62using System.Collections.Generic;63using System.Linq;64using System.Text;65using System.Threading.Tasks;66using OpenQA.Selenium;67using OpenQA.Selenium.Chrome;
Sessions
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.Remote;9{10 {11 static void Main(string[] args)12 {13 IWebDriver driver = new ChromeDriver();14 driver.Manage().Window.Maximize();15 Console.WriteLine("Title of the page is: " + driver.Title);16 Console.WriteLine("Url of the page is: " + driver.Url);17 Console.WriteLine("Page Source is: " + driver.PageSource);18 driver.Close();19 }20 }21}
Sessions
Using AI Code Generation
1using OpenQA.Selenium;2using OpenQA.Selenium.Chrome;3using OpenQA.Selenium.Support.UI;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.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);16 driver.FindElement(By.Name("q")).SendKeys("Selenium");17 driver.FindElement(By.Name("btnK")).Click();18 driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);19 driver.FindElement(By.LinkText("Selenium - Web Browser Automation")).Click();20 string sessionId = driver.SessionId.ToString();21 Console.WriteLine("Session id is: " + sessionId);22 driver.Close();23 driver.Quit();24 }25 }26}27using OpenQA.Selenium;28using OpenQA.Selenium.Chrome;29using OpenQA.Selenium.Support.UI;30using System;31using System.Collections.Generic;32using System.Linq;33using System.Text;34using System.Threading.Tasks;35{36 {37 static void Main(string[] args)38 {39 IWebDriver driver = new ChromeDriver();40 driver.Manage().Window.Maximize();41 driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);42 driver.FindElement(By.Name("q")).SendKeys("Selenium");43 driver.FindElement(By.Name("btnK")).Click();44 driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
Sessions
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.Remote;9{10 {11 static void Main(string[] args)12 {13 IWebDriver driver = new ChromeDriver();14 String sessionid = ((RemoteWebDriver)driver).SessionId.ToString();15 Console.WriteLine("Session ID is: " + sessionid);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.Chrome;27using OpenQA.Selenium.Remote;28{29 {30 static void Main(string[] args)31 {32 IWebDriver driver = new ChromeDriver();33 String sessionid = ((RemoteWebDriver)driver).SessionId.ToString();34 Console.WriteLine("Session ID is: " + sessionid);35 ICapabilities capabilities = ((RemoteWebDriver)driver).Capabilities;36 Console.WriteLine("Capabilities are: " + capabilities);37 driver.Quit();38 }39 }40}41Capabilities are: {acceptInsecureCerts: False, browserName: chrome, browserVersion: 87.0.4280.88, chrome: {chromedriverVersion: 87.0.4280.20 (edb6b9e6d8d1..., userDataDir: C:\Users\user\AppData\Local\Temp\scoped_dir...}, goog:chromeOptions: {debuggerAddress: localhost:50610}, javascriptEnabled: True,
Sessions
Using AI Code Generation
1using OpenQA.Selenium;2using OpenQA.Selenium.Firefox;3using OpenQA.Selenium.Support.UI;4using System;5{6 {7 static void Main(string[] args)8 {9 IWebDriver driver = new FirefoxDriver();10 IWebElement element = driver.FindElement(By.Name("q"));11 element.SendKeys("Selenium WebDriver");12 element.Submit();13 WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));14 wait.Until(d => d.Title.StartsWith("Selenium WebDriver", StringComparison.OrdinalIgnoreCase));15 Console.WriteLine("Page title is: " + driver.Title);16 driver.Quit();17 }18 }19}
Sessions
Using AI Code Generation
1using OpenQA.Selenium;2using OpenQA.Selenium.Support.UI;3using OpenQA.Selenium.Chrome;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 ChromeDriver driver = new ChromeDriver();14 driver.Sessions.Start("session1");15 driver.Sessions.Start("session2");16 driver.Sessions.Start("session3");17 driver.Sessions.SwitchTo("session1");18 driver.Sessions.SwitchTo("session2");19 driver.Sessions.SwitchTo("session3");20 driver.Sessions.Close();21 }22 }23}
Sessions
Using AI Code Generation
1using OpenQA.Selenium;2using OpenQA.Selenium.Chrome;3using System;4{5 {6 static void Main(string[] args)7 {8 ChromeDriver driver = new ChromeDriver();9 string sessionId = driver.SessionId.ToString();10 Console.WriteLine("Session ID: " + sessionId);11 string title = driver.Title;12 Console.WriteLine("Title of the page: " + title);13 string currentURL = driver.Url;14 Console.WriteLine("Current URL: " + currentURL);15 string pageSource = driver.PageSource;16 Console.WriteLine("Page Source: " + pageSource);17 driver.Close();18 }19 }20}
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!!