How to use CreateSessionFromExistingWindowHandle_ClassicApp method of WebDriverAPI.Session class

Best WinAppDriver code snippet using WebDriverAPI.Session.CreateSessionFromExistingWindowHandle_ClassicApp

Session.cs

Source:Session.cs Github

copy

Full Screen

...178 Assert.AreEqual("Bad capabilities. Specify either app or appTopLevelWindow to create a session", exception.Message);179 }180 }181 [TestMethod]182 public void CreateSessionFromExistingWindowHandle_ClassicApp()183 {184 // Get the top level window handle of a running application185 secondarySession = Utility.CreateNewSession(CommonTestSettings.NotepadAppId);186 var existingApplicationTopLevelWindow = secondarySession.CurrentWindowHandle;187 // Create a new session by attaching to an existing application top level window188 DesiredCapabilities appCapabilities = new DesiredCapabilities();189 appCapabilities.SetCapability("appTopLevelWindow", existingApplicationTopLevelWindow);190 session = new WindowsDriver<WindowsElement>(new Uri(CommonTestSettings.WindowsApplicationDriverUrl), appCapabilities);191 Assert.IsNotNull(session);192 Assert.IsNotNull(session.SessionId);193 // Verify that newly created session is indeed attached to the correct application top level window194 Assert.AreEqual(secondarySession.CurrentWindowHandle, session.CurrentWindowHandle);195 Assert.AreEqual(secondarySession.FindElementByClassName("Edit"), session.FindElementByClassName("Edit"));196 }...

Full Screen

Full Screen

CreateSessionFromExistingWindowHandle_ClassicApp

Using AI Code Generation

copy

Full Screen

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;9using OpenQA.Selenium.IE;10using OpenQA.Selenium.Firefox;11using OpenQA.Selenium.Edge;12using OpenQA.Selenium.Safari;13using System.Diagnostics;14using System.Runtime.InteropServices;15using System.Threading;16using System.Windows.Automation;17using System.Windows.Forms;18using System.Drawing;19using System.Windows;20using System.Windows.Interop;21using System.Windows.Media;22using System.Windows.Media.Imaging;23using System.IO;24using System.ComponentModel;25using System.Collections.ObjectModel;26using System.Windows.Automation.Peers;27using System.Windows.Automation.Provider;28using System.Windows.Shapes;29using System.Windows.Controls;30using System.Windows.Data;31using System.Windows.Documents;32using System.Windows.Input;33using System.Windows.Navigation;34using System.Windows.Shapes;35using System.Windows.Threading;36using System.Windows.Media.Animation;37using System.Windows.Markup;38using System.Windows.Controls.Primitives;39using System.Windows.Media.Effects;40using System.Windows.Media.Media3D;41using System.Windows.Media.Animation;42using System.Windows.Media;43using System.Windows.Media.Imaging;44{45 {46 [DllImport("user32.dll", SetLastError = true)]47 public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);48 [DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]49 public static extern bool SetForegroundWindow(IntPtr hWnd);50 [DllImport("user32.dll")]51 public static extern IntPtr GetForegroundWindow();52 [DllImport("user32.dll")]53 public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);54 [DllImport("user32.dll")]55 public static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);56 [DllImport("user32.dll")]57 public static extern bool GetWindowRect(IntPtr hWnd, ref Rectangle lpRect);58 [DllImport("user32.dll")]59 public static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);60 [DllImport("user32.dll", SetLastError = true)]61 public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);62 [DllImport("user32.dll")]63 public static extern bool IsWindowVisible(IntPtr hWnd);64 [DllImport("user32.dll")]65 public static extern bool IsIconic(IntPtr hWnd);

Full Screen

Full Screen

CreateSessionFromExistingWindowHandle_ClassicApp

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using System.Diagnostics;7using System.Runtime.InteropServices;8using System.Threading;9using OpenQA.Selenium;10using OpenQA.Selenium.Remote;11{12 {13 static void Main(string[] args)14 {15 Process p = new Process();16 p.StartInfo.FileName = "C:\\Windows\\System32\\calc.exe";17 p.Start();18 IntPtr hWnd = p.MainWindowHandle;19 DesiredCapabilities capabilities = new DesiredCapabilities();20 capabilities.SetCapability("app", hWnd);21 driver.FindElement(By.Name("Seven")).Click();22 driver.FindElement(By.Name("Plus")).Click();23 driver.FindElement(By.Name("Eight")).Click();24 driver.FindElement(By.Name("Equals")).Click();25 driver.Quit();26 }27 }28}29using System;30using System.Collections.Generic;31using System.Linq;32using System.Text;33using System.Threading.Tasks;34using System.Diagnostics;35using System.Runtime.InteropServices;36using System.Threading;37using OpenQA.Selenium;38using OpenQA.Selenium.Remote;39{40 {41 static void Main(string[] args)42 {43 Process p = new Process();44 p.StartInfo.FileName = "C:\\Windows\\System32\\calc.exe";45 p.Start();46 IntPtr hWnd = p.MainWindowHandle;47 DesiredCapabilities capabilities = new DesiredCapabilities();48 capabilities.SetCapability("appTopLevelWindow", hWnd);49 driver.FindElement(By.Name("Seven")).Click();50 driver.FindElement(By.Name("Plus")).Click();51 driver.FindElement(By.Name("Eight")).Click();52 driver.FindElement(By.Name("Equals")).Click();53 driver.Quit();54 }55 }56}

Full Screen

Full Screen

CreateSessionFromExistingWindowHandle_ClassicApp

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using System.Diagnostics;7using System.Runtime.InteropServices;8using System.Windows.Automation;9using System.Threading;10using System.Windows.Forms;11using System.Drawing;12using OpenQA.Selenium;13using OpenQA.Selenium.IE;14using OpenQA.Selenium.Remote;15using OpenQA.Selenium.Support.UI;16using OpenQA.Selenium.Interactions;17{18 {19 static void Main(string[] args)20 {21 InternetExplorerDriver driver = new InternetExplorerDriver();22 string windowHandle = driver.CurrentWindowHandle;23 SessionId session = driver.CreateSessionFromExistingWindowHandle_ClassicApp(windowHandle);24 InternetExplorerDriver driver1 = new InternetExplorerDriver(session);25 driver.Close();26 driver.Quit();27 }28 }29}

Full Screen

Full Screen

CreateSessionFromExistingWindowHandle_ClassicApp

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using System.Diagnostics;7using System.Runtime.InteropServices;8using System.Windows.Automation;9using System.Windows.Forms;10{11 {12 static void Main(string[] args)13 {14 string processName = "notepad";15 int processId = GetProcessId(processName);16 IntPtr windowHandle = GetWindowHandle(processId);17 int sessionId = GetSessionId(processId);18 CreateSessionFromExistingWindowHandle_ClassicApp(windowHandle, sessionId);19 }20 public static int GetProcessId(string processName)21 {22 Process[] process = Process.GetProcessesByName(processName);23 return process[0].Id;24 }25 public static IntPtr GetWindowHandle(int processId)26 {27 Process process = Process.GetProcessById(processId);28 return process.MainWindowHandle;29 }30 public static int GetSessionId(int processId)31 {32 Process process = Process.GetProcessById(processId);33 return process.SessionId;34 }35 public static void CreateSessionFromExistingWindowHandle_ClassicApp(IntPtr windowHandle, int sessionId)36 {37 WebDriverAPI.Session session = new WebDriverAPI.Session();38 string sessionId1 = session.CreateSessionFromExistingWindowHandle_ClassicApp(windowHandle, sessionId);39 Console.WriteLine("Session Id: " + sessionId1);40 }41 }42}43using System;44using System.Collections.Generic;45using System.Linq;46using System.Text;47using System.Threading.Tasks;48using System.Diagnostics;49using System.Runtime.InteropServices;50using System.Windows.Automation;51using System.Windows.Forms;52{53 {

Full Screen

Full Screen

CreateSessionFromExistingWindowHandle_ClassicApp

Using AI Code Generation

copy

Full Screen

1using System;2using System.Diagnostics;3using System.Windows.Forms;4using System.Threading;5using System.Runtime.InteropServices;6using System.Drawing;7using System.IO;8using System.Reflection;9using System.Text;10using System.Text.RegularExpressions;11using System.Collections;12using System.Collections.Generic;13using System.Globalization;14using System.Linq;15using OpenQA.Selenium;16using OpenQA.Selenium.Firefox;17using OpenQA.Selenium.IE;18using OpenQA.Selenium.Chrome;19using OpenQA.Selenium.Support.UI;20using OpenQA.Selenium.Interactions;21using OpenQA.Selenium.Remote;22{23 {24 [DllImport("user32.dll", SetLastError = true)]25 static extern IntPtr FindWindow(string lpClassName, string lpWindowName);26 [DllImport("user32.dll", SetLastError = true)]27 static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);28 [DllImport("user32.dll", SetLastError = true)]29 static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);30 [DllImport("user32.dll", SetLastError = true)]31 static extern bool SetForegroundWindow(IntPtr hWnd);32 [DllImport("user32.dll", SetLastError = true)]33 static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);34 [DllImport("user32.dll", SetLastError = true)]35 static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);36 [DllImport("user32.dll", SetLastError = true)]37 static extern bool IsWindowVisible(IntPtr hWnd);38 [DllImport("user32.dll", SetLastError = true)]39 static extern bool IsIconic(IntPtr hWnd);40 [DllImport("user32.dll", SetLastError = true)]41 static extern bool IsZoomed(IntPtr hWnd);42 [DllImport("user32.dll", SetLastError = true)]43 static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);44 [DllImport("user32.dll", SetLastError = true)]45 static extern bool EnumChildWindows(IntPtr hWndParent, EnumChildProc lpEnumFunc, IntPtr lParam);46 [DllImport("user32.dll", SetLastError = true)]47 static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);48 [DllImport("user32.dll", SetLastError = true)]49 static extern int GetWindowTextLength(IntPtr hWnd);50 [DllImport("user32.dll", SetLastError =

Full Screen

Full Screen

CreateSessionFromExistingWindowHandle_ClassicApp

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using System.Runtime.InteropServices;7using System.Diagnostics;8{9 {10 [DllImport("user32.dll", SetLastError = true)]11 static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);12 [DllImport("user32.dll", SetLastError = true)]13 static extern IntPtr GetDesktopWindow();14 [DllImport("user32.dll", SetLastError = true)]15 static extern IntPtr GetShellWindow();16 [DllImport("user32.dll", SetLastError = true)]17 static extern IntPtr GetForegroundWindow();18 [DllImport("user32.dll", SetLastError = true)]19 static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);20 [DllImport("user32.dll", SetLastError = true)]21 static extern bool IsWindowVisible(IntPtr hWnd);22 const uint GW_CHILD = 5;23 const uint GW_HWNDNEXT = 2;24 static void Main(string[] args)25 {26 IntPtr desktopWindow = GetDesktopWindow();27 IntPtr shellWindow = GetShellWindow();28 IntPtr foregroundWindow = GetForegroundWindow();29 uint processId;30 GetWindowThreadProcessId(foregroundWindow, out processId);31 Process process = Process.GetProcessById((int)processId);32 IntPtr mainWindowHandle = process.MainWindowHandle;33 IntPtr childWindowHandle = GetWindow(mainWindowHandle, GW_CHILD);34 IntPtr childWindowHandle1 = GetWindow(mainWindowHandle, GW_HWNDNEXT);35 IntPtr childWindowHandle2 = GetWindow(childWindowHandle1, GW_HWNDNEXT);36 IntPtr childWindowHandle3 = GetWindow(childWindowHandle2, GW_HWNDNEXT);37 IntPtr childWindowHandle4 = GetWindow(childWindowHandle3, GW_HWND

Full Screen

Full Screen

CreateSessionFromExistingWindowHandle_ClassicApp

Using AI Code Generation

copy

Full Screen

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;9using WebDriverAPI;10{11 {12 static void Main(string[] args)13 {14 IWebDriver driver = new ChromeDriver();15 string windowHandle = driver.CurrentWindowHandle;16 Session session = new Session();17 session.CreateSessionFromExistingWindowHandle_ClassicApp("Chrome", windowHandle);18 driver.Quit();19 }20 }21}22using System;23using System.Collections.Generic;24using System.Linq;25using System.Text;26using System.Threading.Tasks;27using OpenQA.Selenium;28using OpenQA.Selenium.Chrome;29using OpenQA.Selenium.Remote;30using WebDriverAPI;31{32 {33 static void Main(string[] args)34 {35 IWebDriver driver = new ChromeDriver();36 string windowHandle = driver.CurrentWindowHandle;37 Session session = new Session();38 session.CreateSessionFromExistingWindowHandle_WPFApp("Chrome", windowHandle);39 driver.Quit();40 }41 }42}43using System;

Full Screen

Full Screen

CreateSessionFromExistingWindowHandle_ClassicApp

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using OpenQA.Selenium;7using OpenQA.Selenium.Remote;8using OpenQA.Selenium.Firefox;9using OpenQA.Selenium.Support.UI;10using WebDriverAPI;11using System.Threading;12{13 {14 static void Main(string[] args)15 {16 FirefoxDriver driver = new FirefoxDriver();17 string handle = driver.CurrentWindowHandle;18 Session session = new Session(handle);19 string handle1 = session.CurrentWindowHandle;20 string title = session.Title;21 session.Quit();22 }23 }24}

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful