Best WinAppDriver code snippet using NotepadCalculatorTest.CalculatorSession
CalculatorSession.cs
Source: CalculatorSession.cs
...19using System;20using System.Threading;21namespace NotepadCalculatorTest22{23 public class CalculatorSession : IDisposable24 {25 // Note: append /wd/hub to the URL if you're directing the test at Appium26 private const string WindowsApplicationDriverUrl = "http://127.0.0.1:4723";27 private const string CalculatorAppId = "Microsoft.WindowsCalculator_8wekyb3d8bbwe!App";28 public WindowsDriver<WindowsElement> Session { get; private set; }29 public WindowsElement CalculatorHeader { get; private set; }30 public WindowsElement CalculatorResult { get; private set; }31 public CalculatorSession()32 {33 // Create a new session to bring up an instance of the Calculator application34 // Note: Multiple calculator windows (instances) share the same process Id35 DesiredCapabilities appCapabilities = new DesiredCapabilities();36 appCapabilities.SetCapability("app", CalculatorAppId);37 appCapabilities.SetCapability("deviceName", "WindowsPC");38 this.Session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appCapabilities);39 Assert.IsNotNull(Session);40 // Set implicit timeout to 1.5 seconds to make element search to retry every 500 ms for at most three times41 Session.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(1.5);42 // Identify calculator mode by locating the calculator header43 try44 {45 CalculatorHeader = Session.FindElementByAccessibilityId("Header");...
SingleSession.cs
Source: SingleSession.cs
...23 [TestClass]24 public class SingleSession25 {26 private static NotepadSession notepadSession;27 private static CalculatorSession calculatorSession;28 29 [TestMethod]30 [DataRow("One", "Plus", "Seven")]31 [DataRow("Nine", "Minus", "One")]32 [DataRow("Eight", "Divide by", "Eight")]33 public void Templatized(string input1, string operation, string input2)34 {35 // we (re) start with our notepad session36 notepadSession.Session.SwitchTo();37 notepadSession.NotepadMainEditBox.SendKeys($"{input1} {operation} {input2} = ");38 // now let's switch to calculator39 calculatorSession.Session.SwitchTo();40 // Run sequence of button presses specified above and validate the results41 calculatorSession.Session.FindElementByName(input1).Click();42 calculatorSession.Session.FindElementByName(operation).Click();43 calculatorSession.Session.FindElementByName(input2).Click();44 calculatorSession.Session.FindElementByName("Equals").Click();45 // and back to notepad for the result 46 notepadSession.Session.SwitchTo();47 notepadSession.NotepadMainEditBox.SendKeys($"{GetCalculatorResultText()}\n");48 }49 [TestInitialize]50 public void TestInitialize()51 {52 notepadSession.Cleanup();53 }54 [ClassInitialize]55 public static void ClassInitialize(TestContext testContext) 56 {57 notepadSession = new NotepadSession();58 calculatorSession = new CalculatorSession();59 }60 [ClassCleanup]61 public static void ClassCleanup()62 {63 calculatorSession.Dispose();64 notepadSession.Dispose();65 }66 private string GetCalculatorResultText()67 {68 return calculatorSession.CalculatorResult.Text.Replace("Display is", string.Empty).Trim();69 }70 }71}...
MultiSession.cs
Source: MultiSession.cs
...23 [TestClass]24 public class MultiSession25 {26 private NotepadSession notepadSession;27 private CalculatorSession calculatorSession;28 29 [TestMethod]30 [DataRow("One", "Plus", "Seven")]31 [DataRow("Nine", "Minus", "One")]32 [DataRow("Eight", "Divide by", "Eight")]33 public void Templatized(string input1, string operation, string input2)34 {35 // we (re) start with our notepad session36 notepadSession.Session.SwitchTo();37 notepadSession.NotepadMainEditBox.SendKeys($"{input1} {operation} {input2} = ");38 // now let's switch to calculator39 calculatorSession.Session.SwitchTo();40 // Run sequence of button presses specified above and validate the results41 calculatorSession.Session.FindElementByName(input1).Click();42 calculatorSession.Session.FindElementByName(operation).Click();43 calculatorSession.Session.FindElementByName(input2).Click();44 calculatorSession.Session.FindElementByName("Equals").Click();45 // and back to notepad for the result 46 notepadSession.Session.SwitchTo();47 notepadSession.NotepadMainEditBox.SendKeys($"{GetCalculatorResultText()}\n");48 }49 [TestInitialize]50 public void TestInitialize() 51 {52 notepadSession = new NotepadSession();53 calculatorSession = new CalculatorSession();54 }55 [TestCleanup]56 public void TestCleanup()57 {58 calculatorSession.Dispose();59 notepadSession.Dispose();60 }61 private string GetCalculatorResultText()62 {63 return calculatorSession.CalculatorResult.Text.Replace("Display is", string.Empty).Trim();64 }65 }66}...
CalculatorSession
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using NotepadCalculatorTest;7{8 {9 static void Main(string[] args)10 {11 CalculatorSession cs = new CalculatorSession();12 Console.WriteLine("Sum of 5 and 6 is: " + cs.Add(5, 6));13 Console.WriteLine("Subtraction of 5 and 6 is: " + cs.Subtract(5, 6));14 Console.WriteLine("Multiplication of 5 and 6 is: " + cs.Multiply(5, 6));15 Console.WriteLine("Division of 5 and 6 is: " + cs.Divide(5, 6));16 }17 }18}
CalculatorSession
Using AI Code Generation
1using NotepadCalculatorTest;2using System;3using System.Collections.Generic;4using System.Linq;5using System.Text;6using System.Threading.Tasks;7{8 {9 public int Add(int a, int b)10 {11 return a + b;12 }13 public int Subtract(int a, int b)14 {15 return a - b;16 }17 public int Multiply(int a, int b)18 {19 return a * b;20 }21 public int Divide(int a, int b)22 {23 return a / b;24 }25 }26}27using NotepadCalculatorTest;28using System;29using System.Collections.Generic;30using System.Linq;31using System.Text;32using System.Threading.Tasks;33{34 {35 public int Add(int a, int b)36 {37 return a + b;38 }39 public int Subtract(int a, int b)40 {41 return a - b;42 }43 public int Multiply(int a, int b)44 {45 return a * b;46 }47 public int Divide(int a, int b)48 {49 return a / b;50 }51 }52}53using NotepadCalculatorTest;54using System;55using System.Collections.Generic;56using System.Linq;57using System.Text;58using System.Threading.Tasks;59{60 {61 public int Add(int a, int b)62 {63 return a + b;64 }65 public int Subtract(int a, int b)66 {67 return a - b;68 }69 public int Multiply(int a, int b)70 {71 return a * b;72 }73 public int Divide(int a, int b)74 {75 return a / b;76 }77 }78}79using NotepadCalculatorTest;80using System;81using System.Collections.Generic;82using System.Linq;83using System.Text;84using System.Threading.Tasks;85{86 {87 public int Add(int a, int b)88 {89 return a + b;90 }
CalculatorSession
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using NotepadCalculatorTest;7using System.Windows.Automation;8using System.Threading;9{10 {11 static void Main(string[] args)12 {13 CalculatorSession calc = new CalculatorSession();14 calc.Start();15 calc.Add(10, 20);16 calc.Subtract(10, 5);17 calc.Multiply(10, 10);18 calc.Divide(100, 10);19 calc.Close();20 }21 }22}23using System;24using System.Collections.Generic;25using System.Linq;26using System.Text;27using System.Threading.Tasks;28using NotepadCalculatorTest;29using System.Windows.Automation;30using System.Threading;31{32 {33 static void Main(string[] args)34 {35 CalculatorSession calc = new CalculatorSession();36 calc.Start();37 calc.Add(10, 20);38 calc.Subtract(10, 5);39 calc.Multiply(10, 10);40 calc.Divide(100, 10);41 calc.Close();42 }43 }44}45using System;46using System.Collections.Generic;47using System.Linq;48using System.Text;49using System.Threading.Tasks;50using NotepadCalculatorTest;51using System.Windows.Automation;52using System.Threading;53{54 {55 static void Main(string[] args)56 {57 CalculatorSession calc = new CalculatorSession();58 calc.Start();59 calc.Add(10, 20);60 calc.Subtract(10, 5);61 calc.Multiply(10, 10);62 calc.Divide(100, 10);63 calc.Close();64 }65 }66}67using System;68using System.Collections.Generic;69using System.Linq;70using System.Text;71using System.Threading.Tasks;72using NotepadCalculatorTest;73using System.Windows.Automation;74using System.Threading;75{76 {77 static void Main(string[] args)78 {79 CalculatorSession calc = new CalculatorSession();80 calc.Start();81 calc.Add(10, 20);
CalculatorSession
Using AI Code Generation
1using NotepadCalculatorTest;2{3 {4 static void Main(string[] args)5 {6 CalculatorSession calc = new CalculatorSession();7 calc.Start();8 calc.Add(2, 3);9 calc.Multiply(5, 6);10 calc.Divide(6, 2);11 calc.Subtract(8, 4);12 calc.Stop();13 }14 }15}16using NotepadCalculatorTest;17{18 {19 static void Main(string[] args)20 {21 CalculatorSession calc = new CalculatorSession();22 calc.Start();23 calc.Add(2, 3);24 calc.Multiply(5, 6);25 calc.Divide(6, 2);26 calc.Subtract(8, 4);27 calc.Stop();28 }29 }30}31using NotepadCalculatorTest;32{33 {34 static void Main(string[] args)35 {36 CalculatorSession calc = new CalculatorSession();37 calc.Start();38 calc.Add(2, 3);39 calc.Multiply(5, 6);40 calc.Divide(6, 2);41 calc.Subtract(8, 4);42 calc.Stop();43 }44 }45}46using NotepadCalculatorTest;47{48 {49 static void Main(string[] args)50 {51 CalculatorSession calc = new CalculatorSession();52 calc.Start();53 calc.Add(2, 3);54 calc.Multiply(5, 6);55 calc.Divide(6, 2);56 calc.Subtract(8, 4);57 calc.Stop();58 }59 }60}61using NotepadCalculatorTest;62{63 {64 static void Main(string[] args)65 {66 CalculatorSession calc = new CalculatorSession();67 calc.Start();68 calc.Add(2, 3);69 calc.Multiply(5, 6);
CalculatorSession
Using AI Code Generation
1using NotepadCalculatorTest;2{3 {4 static void Main(string[] args)5 {6 CalculatorSession calc = new CalculatorSession();7 calc.Start();8 }9 }10}11using System;12using System.Diagnostics;13using System.Runtime.InteropServices;14{15 {16 [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]17 private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);18 [DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]19 private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);20 [DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true)]21 private static extern IntPtr SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);22 private const int WM_SETTEXT = 0x000C;23 private const int WM_GETTEXT = 0x000D;24 private const int WM_CLICK = 0x00F5;25 private const int BN_CLICKED = 0x00F5;26 private const int WM_CLOSE = 0x0010;27 private const int WM_DESTROY = 0x0002;28 public void Start()29 {30 Process.Start("calc.exe");31 IntPtr calcHandle = FindWindow(null, "Calculator");32 IntPtr child = FindWindowEx(calcHandle, IntPtr.Zero, "Button", "8");33 SendMessage(child, WM_CLICK, IntPtr.Zero, IntPtr.Zero);34 }35 }36}37After this, we have used FindWindowEx() method to find the button with text “8” in the Calculator application
CalculatorSession
Using AI Code Generation
1using NotepadCalculatorTest;2{3public static void Main()4{5CalculatorSession calc = new CalculatorSession();6calc.Add(1, 2);7calc.Subtract(2, 1);8}9}10using calc = NotepadCalculatorTest;11{12public static void Main()13{14calc.CalculatorSession calc = new calc.CalculatorSession();15calc.Add(1, 2);16calc.Subtract(2, 1);17}18}19using calc = NotepadCalculatorTest;20{21public static void Main()22{23calc.CalculatorSession calc = new calc.CalculatorSession();24calc.Add(1, 2);25calc.Subtract(2, 1);26}27}28using calc = NotepadCalculatorTest;29{30public static void Main()31{32calc.CalculatorSession calc = new calc.CalculatorSession();33calc.Add(1, 2);34calc.Subtract(2, 1);35}36}
Check out the latest blogs from LambdaTest on this topic:
Mobile devices and mobile applications – both are booming in the world today. The idea of having the power of a computer in your pocket is revolutionary. As per Statista, mobile accounts for more than half of the web traffic worldwide. Mobile devices (excluding tablets) contributed to 54.4 percent of global website traffic in the fourth quarter of 2021, increasing consistently over the past couple of years.
Have you ever visited a website that only has plain text and images? Most probably, no. It’s because such websites do not exist now. But there was a time when websites only had plain text and images with almost no styling. For the longest time, websites did not focus on user experience. For instance, this is how eBay’s homepage looked in 1999.
Hey LambdaTesters! We’ve got something special for you this week. ????
One of the most important tasks of a software developer is not just writing code fast; it is the ability to find what causes errors and bugs whenever you encounter one and the ability to solve them quickly.
I was once asked at a testing summit, “How do you manage a QA team using scrum?” After some consideration, I realized it would make a good article, so here I am. Understand that the idea behind developing software in a scrum environment is for development teams to self-organize.
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!!