How to use CreateSessionFromExistingWindowHandle_ModernApp method of WebDriverAPI.Session class

Best WinAppDriver code snippet using WebDriverAPI.Session.CreateSessionFromExistingWindowHandle_ModernApp

Session.cs

Source:Session.cs Github

copy

Full Screen

...194 Assert.AreEqual(secondarySession.CurrentWindowHandle, session.CurrentWindowHandle);195 Assert.AreEqual(secondarySession.FindElementByClassName("Edit"), session.FindElementByClassName("Edit"));196 }197 [TestMethod]198 public void CreateSessionFromExistingWindowHandle_ModernApp()199 {200 // Get the top level window handle of a running application201 secondarySession = Utility.CreateNewSession(CommonTestSettings.CalculatorAppId);202 var existingApplicationTopLevelWindow = secondarySession.CurrentWindowHandle;203 // Create a new session by attaching to an existing application top level window204 DesiredCapabilities appCapabilities = new DesiredCapabilities();205 appCapabilities.SetCapability("appTopLevelWindow", existingApplicationTopLevelWindow);206 session = new WindowsDriver<WindowsElement>(new Uri(CommonTestSettings.WindowsApplicationDriverUrl), appCapabilities);207 Assert.IsNotNull(session);208 Assert.IsNotNull(session.SessionId);209 // Verify that newly created session is indeed attached to the correct application top level window210 Assert.AreEqual(secondarySession.CurrentWindowHandle, session.CurrentWindowHandle);211 Assert.AreEqual(secondarySession.FindCalculatorTitleByAccessibilityId(), session.FindCalculatorTitleByAccessibilityId());212 }...

Full Screen

Full Screen

CreateSessionFromExistingWindowHandle_ModernApp

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.Appium;8using OpenQA.Selenium.Appium.Windows;9using OpenQA.Selenium.Remote;10{11 {12 static void Main(string[] args)13 {

Full Screen

Full Screen

CreateSessionFromExistingWindowHandle_ModernApp

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.Windows.Automation;7using OpenQA.Selenium;8using OpenQA.Selenium.Appium;9using OpenQA.Selenium.Appium.Windows;10using OpenQA.Selenium.Remote;11using System.Diagnostics;12using System.Threading;13using System.Windows.Automation;14using System.Runtime.InteropServices;15using System.Drawing;16using System.Windows.Forms;17using System.Drawing.Imaging;18using System.IO;19using System.Drawing.Drawing2D;20using System.Drawing.Text;21using System.Windows;22{

Full Screen

Full Screen

CreateSessionFromExistingWindowHandle_ModernApp

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.Threading;8using OpenQA.Selenium;9using OpenQA.Selenium.Remote;10using OpenQA.Selenium.Appium;11using OpenQA.Selenium.Appium.Windows;12using OpenQA.Selenium.Appium.Enums;13using OpenQA.Selenium.Appium.Service;14using OpenQA.Selenium.Appium.Service.Options;15{16 {17 static void Main(string[] args)18 {19 Process appProcess = Process.Start(@"C:\Windows\System32\calc.exe");20 int processId = appProcess.Id;21 IntPtr appWindowHandle = appProcess.MainWindowHandle;22 AppiumServiceBuilder appiumServiceBuilder = new AppiumServiceBuilder();23 AppiumLocalService appiumLocalService = appiumServiceBuilder.UsingAnyFreePort().Build();24 appiumLocalService.Start();25 AppiumOptions appiumOptions = new AppiumOptions();26 appiumOptions.AddAdditionalCapability(MobileCapabilityType.PlatformName, "Windows");27 appiumOptions.AddAdditionalCapability(MobileCapabilityType.DeviceName, "WindowsPC");28 appiumOptions.AddAdditionalCapability("appTopLevelWindow", appWindowHandle.ToString());29 WindowsDriver<WindowsElement> windowsDriver = new WindowsDriver<WindowsElement>(appiumLocalService, appiumOptions);30 Thread.Sleep(10000);31 appProcess.Kill();32 appiumLocalService.Dispose();33 }34 }35}

Full Screen

Full Screen

CreateSessionFromExistingWindowHandle_ModernApp

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.Appium;7using OpenQA.Selenium.Appium.Windows;8using OpenQA.Selenium.Remote;9{10 {11 static void Main(string[] args)12 {13 DesiredCapabilities appCapabilities = new DesiredCapabilities();14 appCapabilities.SetCapability("app", "C:\\Windows\\System32\\notepad.exe");

Full Screen

Full Screen

CreateSessionFromExistingWindowHandle_ModernApp

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 OpenQA.Selenium;10using OpenQA.Selenium.Remote;11using OpenQA.Selenium.Appium.Windows;12using OpenQA.Selenium.Appium;13using OpenQA.Selenium.Appium.Enums;14using OpenQA.Selenium.Appium.Service;15using OpenQA.Selenium.Appium.Service.Options;16using OpenQA.Selenium.Appium.MultiTouch;17using OpenQA.Selenium.Interactions;18{19 {20 static void Main(string[] args)21 {22 WindowsDriver<WindowsElement> session = CreateSessionFromExistingWindowHandle_ModernApp("C:\\Windows\\System32\\calc.exe");

Full Screen

Full Screen

CreateSessionFromExistingWindowHandle_ModernApp

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.Runtime.InteropServices.ComTypes;9using System.Windows.Automation;10using System.Windows.Forms;11{12 {13 static void Main(string[] args)14 {15 Process[] process = Process.GetProcessesByName("Microsoft.WindowsCalculator_8wekyb3d8bbwe!App");16 if (process.Length > 0)17 {18 IntPtr windowHandle = process[0].MainWindowHandle;19 if (windowHandle != null)20 {21 WebDriverAPI.Session session = WebDriverAPI.Session.CreateSessionFromExistingWindowHandle_ModernApp(windowHandle);22 if (session != null)23 {24 Console.WriteLine("Session created successfully");25 Console.WriteLine("Session Id: " + session.Id);26 Console.WriteLine("Session Window Handle: " + session.WindowHandle);27 Console.WriteLine("Session Window Title: " + session.WindowTitle);28 Console.WriteLine("Session Window Size: " + session.WindowSize);29 session.Quit();30 }31 {32 Console.WriteLine("Session creation failed");33 }34 }35 {36 Console.WriteLine("Main window handle not found");37 }38 }39 {40 Console.WriteLine("Modern App not found");41 }42 Console.WriteLine("Press any key to exit");43 Console.ReadKey();44 }45 }46}47Session Window Size: (384, 640)

Full Screen

Full Screen

CreateSessionFromExistingWindowHandle_ModernApp

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.Appium.Windows;7using OpenQA.Selenium.Remote;8{9 {10 static void Main(string[] args)11 {12 DesiredCapabilities appCapabilities = new DesiredCapabilities();13 appCapabilities.SetCapability("app", "Microsoft.WindowsCalculator_8wekyb3d8bbwe!App");

Full Screen

Full Screen

CreateSessionFromExistingWindowHandle_ModernApp

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.Windows.Automation;8using System.Threading;9using System.Windows;10using System.Windows.Forms;11using System.Windows.Automation.Peers;12using System.Windows.Automation.Provider;13using System.Drawing;14using System.Drawing.Imaging;15using System.IO;16using System.Runtime.InteropServices;17using System.Windows.Interop;18using System.Windows.Media;19using System.Windows.Media.Imaging;20using System.Windows.Threading;21using System.Windows.Controls;22using System.Windows.Shapes;23using System.Windows.Input;24using System.Windows.Media.Animation;25using System.Windows.Navigation;26using System.Windows.Documents;27using System.Windows.Markup;28using System.Windows.Data;29using System.Windows.Media.Effects;30using System.Windows.Media.Media3D;31using System.Windows.Media.Animation;32using System.Windows.Media.Imaging;33using System.Windows.Media.TextFormatting;34using System.Windows.Media.Converters;35using System.Windows.Media.Composition;36using System.Windows.Media.Media3D;37using System.Windows.Media.Effects;38using System.Windows.Media.Animation;39using System.Windows.Media.Converters;40using System.Windows.Media.Imaging;41using System.Windows.Media.TextFormatting;42using System.Windows.Media.Composition;43using System.Windows.Media.Media3D;44using System.Windows.Media.Effects;45using System.Windows.Media.Animation;46using System.Windows.Media.Converters;47using System.Windows.Media.Imaging;48using System.Windows.Media.TextFormatting;49using System.Windows.Media.Composition;50using System.Windows.Media.Media3D;51using System.Windows.Media.Effects;52using System.Windows.Media.Animation;53using System.Windows.Media.Converters;54using System.Windows.Media.Imaging;55using System.Windows.Media.TextFormatting;56using System.Windows.Media.Composition;57using System.Windows.Media.Media3D;58using System.Windows.Media.Effects;59using System.Windows.Media.Animation;60using System.Windows.Media.Converters;61using System.Windows.Media.Imaging;62using System.Windows.Media.TextFormatting;63using System.Windows.Media.Composition;64using System.Windows.Media.Media3D;65using System.Windows.Media.Effects;66using System.Windows.Media.Animation;67using System.Windows.Media.Converters;68using System.Windows.Media.Imaging;69using System.Windows.Media.TextFormatting;70using System.Windows.Media.Composition;71using System.Windows.Media.Media3D;72using System.Windows.Media.Effects;73using System.Windows.Media.Animation;74using System.Windows.Media.Converters;75using System.Windows.Media.Imaging;76using System.Windows.Media.TextFormatting;77using System.Windows.Media.Composition;78using System.Windows.Media.Media3D;

Full Screen

Full Screen

CreateSessionFromExistingWindowHandle_ModernApp

Using AI Code Generation

copy

Full Screen

1using System;2using System.Diagnostics;3using System.Drawing;4using System.Drawing.Imaging;5using System.IO;6using System.Windows.Forms;7using OpenQA.Selenium;8using OpenQA.Selenium.Remote;9using OpenQA.Selenium.Appium;10{11 {12 public Form1()13 {14 InitializeComponent();15 }16 private void Form1_Load(object sender, EventArgs e)17 {18 Process.Start("C:\\Windows\\System32\\calc.exe");19 }20 private void button1_Click(object sender, EventArgs e)21 {22 IntPtr hWnd = FindWindow(null, "Calculator");23 Process.Start("C:\\Windows\\System32\\calc.exe");24 Process.Start("C:\\Windows\\System32\\calc.exe");25 IntPtr hWnd = FindWindow(null, "Calculator");26 Process.Start("C:\\Windows\\System32\\calc.exe");27 IntPtr hWnd = FindWindow(null, "Calculator");28 Process.Start("C:\\Windows\\System32\\calc.exe");29 IntPtr hWnd = FindWindow(null, "Calculator");30 Process.Start("C:\\Windows\\System32\\calc.exe");31 IntPtr hWnd = FindWindow(null, "Calculator");32 Process.Start("C:\\Windows\\System32\\calc.exe");33 IntPtr hWnd = FindWindow(null, "Calculator");34 Process.Start("C:\\Windows\\System32\\calc.exe");35 IntPtr hWnd = FindWindow(null, "Calculator");36 Process.Start("C:\\Windows\\System32\\calc.exe");37 IntPtr hWnd = FindWindow(null, "Calculator");38 Process.Start("C:\\Windows\\System32\\calc.exe");39 IntPtr hWnd = FindWindow(null, "Calculator");

Full Screen

Full Screen

CreateSessionFromExistingWindowHandle_ModernApp

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.Support.UI;10{11 {12 static void Main(string[] args)13 {14 ChromeOptions options = new ChromeOptions();15 options.BinaryLocation = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe";16 ChromeDriverService service = ChromeDriverService.CreateDefaultService(@"C:\Users\Downloads\chromedriver_win32");17 service.HideCommandPromptWindow = true;18 IWebDriver driver = new ChromeDriver(service, options);19 String handle = driver.CurrentWindowHandle;20 Console.WriteLine(handle);21 driver.FindElement(By.Id("button1")).Click();22 IList<String> handles = driver.WindowHandles;23 foreach (String handle1 in handles)24 {25 Console.WriteLine(handle1);26 }27 driver.SwitchTo().Window(handles[1]);28 driver.FindElement(By.Name("q")).SendKeys("ToolsQA");29 driver.Close();30 driver.SwitchTo().Window(handles[0]);31 }32 }33}34Starting ChromeDriver 2.21.371461 (4c4e4e3a3f7e0c0e8a7d3ef0b1f7b1f9d9d7a0f2) on port 9515

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