Best WinAppDriver code snippet using AlarmClockTest.AlarmClockSession
Scenario.cs
Source:Scenario.cs
...24 [TestClass]25 public class Scenario26 {27 protected const string WindowsApplicationDriverUrl = "http://127.0.0.1:4723";28 protected static WindowsDriver<WindowsElement> AlarmClockSession;29 protected static WindowsDriver<WindowsElement> DesktopSession;30 private const string NewAlarmName = "Windows Application Driver Test Alarm";31 [ClassInitialize]32 public static void Setup(TestContext context)33 {34 // Launch the AlarmClock app35 DesiredCapabilities appCapabilities = new DesiredCapabilities();36 appCapabilities.SetCapability("app", "Microsoft.WindowsAlarms_8wekyb3d8bbwe!App");37 AlarmClockSession = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appCapabilities);38 Assert.IsNotNull(AlarmClockSession);39 AlarmClockSession.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(2));40 // Create a session for Desktop41 DesiredCapabilities desktopCapabilities = new DesiredCapabilities();42 desktopCapabilities.SetCapability("app", "Root");43 DesktopSession = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), desktopCapabilities);44 Assert.IsNotNull(DesktopSession);45 // Ensure app is started in the default main page46 ReturnToMainPage();47 }48 [ClassCleanup]49 public static void TearDown()50 {51 ReturnToMainPage();52 SwitchToAlarmTab();53 // Delete all created alarms54 while (true)55 {56 try57 {58 var alarmEntry = AlarmClockSession.FindElementByXPath(string.Format("//ListItem[starts-with(@Name, \"{0}\")]", NewAlarmName));59 AlarmClockSession.Mouse.ContextClick(alarmEntry.Coordinates);60 AlarmClockSession.FindElementByName("Delete").Click();61 }62 catch63 {64 break;65 }66 }67 AlarmClockSession.Quit();68 AlarmClockSession = null;69 }70 [TestMethod]71 public void FullScenario()72 {73 // Read the current local time74 SwitchToWorldClockTab();75 string localTimeText = ReadLocalTime();76 Assert.IsTrue(localTimeText.Length > 0);77 // Add an alarm at 1 minute after local time78 SwitchToAlarmTab();79 AddAlarm(localTimeText);80 var alarmEntries = AlarmClockSession.FindElementsByXPath(string.Format("//ListItem[starts-with(@Name, \"{0}\")]", NewAlarmName));81 Assert.IsTrue(alarmEntries.Count > 0);82 // Try to dismiss notification after 1 minute83 System.Threading.Thread.Sleep(60000);84 DismissNotification();85 }86 public static void SwitchToAlarmTab()87 {88 AlarmClockSession.FindElementByAccessibilityId("AlarmPivotItem").Click();89 }90 public void SwitchToWorldClockTab()91 {92 AlarmClockSession.FindElementByAccessibilityId("WorldClockPivotItem").Click();93 }94 public string ReadLocalTime()95 {96 string localTimeText = "";97 AppiumWebElement worldClockPivotItem = AlarmClockSession.FindElementByAccessibilityId("WorldClockPivotItem");98 if (worldClockPivotItem != null)99 {100 try101 {102 localTimeText = worldClockPivotItem.FindElementByClassName("ClockCardItem").Text;103 }104 catch (Exception)105 {106 // On Windows 10 anniversary edition, the ClockCardItem has been changed to ListViewItem107 // If the previous item wasn't found, then look for the new one.108 localTimeText = worldClockPivotItem.FindElementByClassName("ListViewItem").Text;109 }110 var timeStrings = localTimeText.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);111 foreach (string timeString in timeStrings)112 {113 // Get the time. E.g. "11:32 AM" from "Local time, Monday, February 22, 2016, 11:32 AM, "114 if (timeString.Contains(":"))115 {116 localTimeText = timeString;117 break;118 }119 }120 }121 return localTimeText;122 }123 public void AddAlarm(string timeText)124 {125 if (timeText.Length > 0)126 {127 // Remove any zero-width spaces (\u200b, \u200e, \u200f)128 timeText = timeText.Replace("\u200b", String.Empty);129 timeText = timeText.Replace("\u200e", String.Empty);130 timeText = timeText.Replace("\u200f", String.Empty);131 // Create a test alarm 1 minute after the read local time132 DateTimeFormatInfo fi = CultureInfo.CurrentUICulture.DateTimeFormat;133 DateTime alarmTime = DateTime.Parse(timeText, fi);134 alarmTime = alarmTime.AddMinutes(1.0);135 // The alarm app uses the long time format specifier136 var timeFormat = fi.LongTimePattern;137 var formatStrings = timeFormat.Split(new char[] { fi.TimeSeparator[0], ' ' });138 // A single character format string is treated as a standard format. Escape it139 if (formatStrings[0].Length == 1)140 {141 formatStrings[0] = "%" + formatStrings[0];142 }143 string hourString = alarmTime.ToString(formatStrings[0]);144 // If the format for the hour includes a preceding 0, remove it145 if (hourString.StartsWith("0"))146 {147 hourString = hourString.Substring(1);148 }149 // A single character format string is treated as a standard format. Escape it150 if (formatStrings[1].Length == 1)151 {152 formatStrings[1] = "%" + formatStrings[1];153 }154 string minuteString = alarmTime.ToString(formatStrings[1]);155 // Only add the period if the time format has it156 string period = string.Empty;157 if (formatStrings.Length > 3)158 {159 period = alarmTime.ToString(formatStrings[3]);160 }161 AlarmClockSession.FindElementByAccessibilityId("AddAlarmButton").Click();162 AlarmClockSession.FindElementByAccessibilityId("AlarmNameTextBox").Clear();163 AlarmClockSession.FindElementByAccessibilityId("AlarmNameTextBox").SendKeys(NewAlarmName);164 AlarmClockSession.FindElementByAccessibilityId("HourSelector").FindElementByName(hourString).Click();165 AlarmClockSession.FindElementByAccessibilityId("MinuteSelector").FindElementByName(minuteString).Click();166 if (!string.IsNullOrEmpty(period))167 {168 AlarmClockSession.FindElementByAccessibilityId("PeriodSelector").FindElementByName(period).Click();169 }170 AlarmClockSession.FindElementByAccessibilityId("AlarmSaveButton").Click();171 }172 }173 public void DismissNotification()174 {175 try176 {177 AppiumWebElement newNotification = DesktopSession.FindElementByName("New notification");178 Assert.IsTrue(newNotification.FindElementByAccessibilityId("MessageText").Text.Contains(NewAlarmName));179 newNotification.FindElementByName("Dismiss").Click();180 }181 catch { }182 }183 private static void ReturnToMainPage()184 {185 // Try to return to main page in case application is started in nested view186 try187 {188 AppiumWebElement backButton = null;189 do190 {191 backButton = AlarmClockSession.FindElementByAccessibilityId("Back");192 backButton.Click();193 }194 while (backButton != null);195 }196 catch { }197 }198 }199}...
ScenarioAlarm.cs
Source:ScenarioAlarm.cs
...4using System;5namespace AlarmClockTest6{7 [TestClass]8 public class ScenarioAlarm : AlarmClockSession9 {10 private const string NewAlarmName = "Sample Test Alarm";11 [TestMethod]12 public void AlarmAdd()13 {14 15 Thread.Sleep(TimeSpan.FromSeconds(1));16 session.FindElementByAccessibilityId("AddAlarmButton").Click();17 session.FindElementByName("Alarm adı").Clear();18 session.FindElementByName("Alarm adı").SendKeys(NewAlarmName);19 20 session.FindElementByAccessibilityId("HourPicker").Click();21 WindowsElement hourSelector = session.FindElementByAccessibilityId("HourPicker");22 Thread.Sleep(TimeSpan.FromSeconds(1));...
AlarmClockSession.cs
Source:AlarmClockSession.cs
...4using System;5using System.Threading;6namespace AlarmClockTest7{8 public class AlarmClockSession9 {10 private const string WindowsApplicationDriverUrl = "http://127.0.0.1:4723";11 private const string AlarmClockAppId = "Microsoft.WindowsAlarms_8wekyb3d8bbwe!App";12 protected static WindowsDriver<WindowsElement> session;13 protected static RemoteTouchScreen touchScreen;14 public static void Setup(TestContext context)15 {16 if (session == null || touchScreen == null)17 {18 TearDown();19 DesiredCapabilities appCapabilities = new DesiredCapabilities();20 appCapabilities.SetCapability("app", AlarmClockAppId);21 session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appCapabilities);22 Assert.IsNotNull(session);...
AlarmClockSession
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using AlarmClockTest;7{8 {9 private AlarmClock _alarmClock;10 public AlarmClockSession()11 {12 _alarmClock = new AlarmClock();13 }14 public void SetAlarm()15 {16 _alarmClock.SetAlarm();17 }18 public void SetTime()19 {20 _alarmClock.SetTime();21 }22 public void ShowTime()23 {24 _alarmClock.ShowTime();25 }26 public void ShowAlarm()27 {28 _alarmClock.ShowAlarm();29 }30 }31}32using System;33using System.Collections.Generic;34using System.Linq;35using System.Text;36using System.Threading.Tasks;37using AlarmClockTest;38{39 {40 static void Main(string[] args)41 {42 AlarmClockSession alarmClockSession = new AlarmClockSession();43 alarmClockSession.SetAlarm();44 alarmClockSession.SetTime();45 alarmClockSession.ShowTime();46 alarmClockSession.ShowAlarm();47 Console.ReadKey();48 }49 }50}51using System;52using System.Collections.Generic;53using System.Linq;54using System.Text;55using System.Threading.Tasks;56using AlarmClockTest;57{58 {59 private int _hour;60 private int _minute;61 private int _second;62 private int _alarmHour;63 private int _alarmMinute;64 private int _alarmSecond;65 public void SetTime()66 {67 Console.Write("Enter hour: ");68 _hour = Convert.ToInt32(Console.ReadLine());69 Console.Write("Enter minute: ");70 _minute = Convert.ToInt32(Console.ReadLine());71 Console.Write("Enter second: ");72 _second = Convert.ToInt32(Console.ReadLine());73 }74 public void SetAlarm()75 {76 Console.Write("Enter alarm hour: ");77 _alarmHour = Convert.ToInt32(Console.ReadLine());78 Console.Write("Enter alarm minute: ");79 _alarmMinute = Convert.ToInt32(Console.ReadLine());80 Console.Write("Enter alarm second: ");81 _alarmSecond = Convert.ToInt32(Console.ReadLine());82 }83 public void ShowTime()84 {85 Console.WriteLine("
AlarmClockSession
Using AI Code Generation
1using AlarmClockTest;2{3 static void Main()4 {5 AlarmClockSession acs = new AlarmClockSession();6 acs.Start();7 }8}9using AlarmClockTest;10{11 static void Main()12 {13 AlarmClockSession acs = new AlarmClockSession();14 acs.Start();15 }16}17using AlarmClockTest;18{19 static void Main()20 {21 AlarmClockSession acs = new AlarmClockSession();22 acs.Start();23 }24}25using AlarmClockTest;26{27 static void Main()28 {29 AlarmClockSession acs = new AlarmClockSession();30 acs.Start();31 }32}33using AlarmClockTest;34{35 static void Main()36 {37 AlarmClockSession acs = new AlarmClockSession();38 acs.Start();39 }40}41using AlarmClockTest;42{43 static void Main()44 {45 AlarmClockSession acs = new AlarmClockSession();46 acs.Start();47 }48}49using AlarmClockTest;50{51 static void Main()52 {53 AlarmClockSession acs = new AlarmClockSession();54 acs.Start();55 }56}57using AlarmClockTest;58{59 static void Main()60 {61 AlarmClockSession acs = new AlarmClockSession();62 acs.Start();63 }64}65using AlarmClockTest;66{67 static void Main()68 {
AlarmClockSession
Using AI Code Generation
1using AlarmClockTest;2using System;3{4 {5 static void Main(string[] args)6 {7 AlarmClock alarmClock = new AlarmClock();8 alarmClock.Time = 6;9 alarmClock.AlarmTime = 7;10 alarmClock.IsAlarmOn = true;11 alarmClock.DisplayTime();12 alarmClock.DisplayAlarmTime();13 alarmClock.DisplayIsAlarmOn();14 Console.ReadKey();15 }16 }17}
AlarmClockSession
Using AI Code Generation
1using AlarmClockTest;2{3{4public void StartSession()5{6Console.WriteLine("Session started");7}8public void StopSession()9{10Console.WriteLine("Session stopped");11}12}13}14using AlarmClockTest;15{16{17public void StartAlarm()18{19Console.WriteLine("Alarm started");20}21public void StopAlarm()22{23Console.WriteLine("Alarm stopped");24}25}26}27using AlarmClockTest;28{29{30public void StartSession()31{32Console.WriteLine("Session started");33}34public void StopSession()35{36Console.WriteLine("Session stopped");37}38}39}40using AlarmClockTest;41{42{43public void StartAlarm()44{45Console.WriteLine("Alarm started");46}47public void StopAlarm()48{49Console.WriteLine("Alarm stopped");50}51}52}
AlarmClockSession
Using AI Code Generation
1using AlarmClockTest;2using System;3{4 static void Main(string[] args)5 {6 AlarmClockSession alarmClock = new AlarmClockSession();7 alarmClock.StartAlarmClock();8 }9}10Alarm class has two methods, StartAlarm() and StopAlarm(). StartAlarm() method is used to start the Alarm Clock and StopAlarm() method is used to stop the Alarm Clock. The Alarm class has a private field of type Alarm which is an interface. The Alarm class uses the Alarm interface to access the Alarm class. The Alarm class can access the
AlarmClockSession
Using AI Code Generation
1using AlarmClockTest;2{3 static void Main(string[] args)4 {5 AlarmClockSession alarm = new AlarmClockSession();6 alarm.Alarm = 12;7 alarm.Time = 11;8 alarm.SetAlarm();9 }10}
AlarmClockSession
Using AI Code Generation
1using System;2using AlarmClockTest;3{4 static void Main(string[] args)5 {6 AlarmClockSession session = new AlarmClockSession();7 session.SetAlarm(10, 10);8 }9}10using System;11using AlarmClockTest;12{13 static void Main(string[] args)14 {15 AlarmClockSession session = new AlarmClockSession();16 session.SetAlarm(10, 10);17 }18}19using System;20using AlarmClockTest;21{22 static void Main(string[] args)23 {24 AlarmClockSession session = new AlarmClockSession();25 session.SetAlarm(10, 10);26 }27}28using System;29using AlarmClockTest;30{31 static void Main(string[] args)32 {33 AlarmClockSession session = new AlarmClockSession();34 session.SetAlarm(10, 10);35 }36}37using System;38using AlarmClockTest;39{40 static void Main(string[] args)41 {42 AlarmClockSession session = new AlarmClockSession();43 session.SetAlarm(10, 10);44 }45}46using System;47using AlarmClockTest;48{49 static void Main(string[] args)50 {51 AlarmClockSession session = new AlarmClockSession();
AlarmClockSession
Using AI Code Generation
1using AlarmClockTest;2using System;3{4 static void Main()5 {6 AlarmClockSession ac = new AlarmClockSession();7 ac.AwakeTime = "9:00";8 ac.AwakeTime = "8:00";9 Console.WriteLine(ac.AwakeTime);10 Console.ReadLine();11 }12}13ac.AwakeTime = "9:00";14using AlarmClockTest;15using System;16{17 static void Main()18 {19 AlarmClockSession ac = new AlarmClockSession();20 ac.AwakeTime = "9:00";21 ac.AwakeTime = "8:00";22 Console.WriteLine(ac.AwakeTime);23 Console.ReadLine();24 }25}26{27 {28 private string _awakeTime;29 {30 get { return _awakeTime; }31 set { _awakeTime = value; }32 }33 }34}35ac.AwakeTime = "9:00";
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!!