21. uwp UI automated testing (WinAppDriver) and uwpwinappdriver

Source: Internet
Author: User
Tags dotnet appium iqiyi

21. uwp UI automated testing (WinAppDriver) and uwpwinappdriver

 

The advantage of using UI automated testing is to write the case in the Code logic to achieve the "once and for all" function, and the automated testing can simulate the failure of the personnel to meet the requirements, for example, the quick switch page and quick click button are helpful for improving the stability of the software.

 

Installed software:

1. WinAppDriver

Github: https://github.com/microsoft/winappdriver

Some demos above. Application Driver installation address: https://github.com/Microsoft/WinAppDriver/releases

After the installation is complete, the system defaults to: C: \ Program Files (x86) \ Windows Application Driver \ WinAppDriver.exe

The purpose of this tool is to write a test project and communicate with WinAppDriver in json mode through the local localhost (127.0.0.1), so that WinAppDriver can call win32 api to Simulate Screen operations, if you click, drag, or touch screen, it is said that you will support operations such as the xbox handle and hololens.

2. inspect.exe

This tool is used to view the Name, ID, and Text of the UI elements of the running software (uwp, win32, win form, and wpf. Included in the Windows SDK.

After Visual Studio2015 is installed, you can find: C: \ Program Files (x86) \ Windows Kits \ 10 \ bin \ x64 \ inspect.exe under drive C.

Element table:

Client API Locator Strategy Matched Attribute Example
FindElementByAccessibilityId Accessibility id AutomationId AppNameTitle
FindElementByClassName Class name ClassName TextBlock
FindElementById Id RuntimeId (decimal) 42.333896.3.1
FindElementByName Name Name Calculator
FindElementByTagName Tag name LocalizedControlType (upper camel case) Text

 

 

 

 

 

 

 

Here, iQiYi uwp is used as an example to describe how to use it:

1. Start WinAppDriver.exe on the running interface:

Path: C: \ Program Files (x86) \ Windows Application Driver \ WinAppDriver.exe

If you do not run it, vs will throw an exception when the UI automation project is running a unit test:

/* Start C: \ Program Files (x86) \ Windows Application Driver \ WinAppDriver.exe first, otherwise the Unexpected error will be thrown. system. net. webException: unable to connect to the remote server ---> System. net. sockets. socketException: Unable to connect because the target computer is actively rejected. 127.0.0.1: 4723 in System. net. sockets. socket. doConnect (EndPoint endPointSnapshot, SocketAddress socketAddress) in System. net. servicePoint. connectSocketInternal (Boolean connectFailure, Socket s4, Socket s6, Socket & socket, IPAddress & address, ConnectSocketState state, IAsyncResult asyncResult, Exception & exception) --- end of internal Exception stack trace --- at OpenQA. selenium. appium. service. appiumCommandExecutor. execute (Command commandToExecute) in OpenQA. selenium. remote. remoteWebDriver. execute (String driverCommandToExecute, Dictionary '2 parameters )*/

  

2. Use the inspect.exe tool to determine the ID/Name of the event UI element to be triggered when the software is running.

Path: C: \ Program Files (x86) \ Windows Kits \ 10 \ bin \ x64 \ inspect.exe

For example, add two buttons in xaml:

The first additional property is set: AutomationProperties. AutomationId = "button_1"

Second setting: x: Name = "button_2"

 <StackPanel Width="500" Height="500" Background="Blue" Grid.RowSpan="3">     <StackPanel.Resources>         <Style TargetType="Button">             <Setter Property="Margin" Value="10"/>         </Style>     </StackPanel.Resources>     <Button AutomationProperties.AutomationId="button_1">AutomationId="button_1"</Button>     <Button x:Name="button_2">x:Name="button_2"</Button> </StackPanel>

Observed with inspect:

 

 

It can be seen that in inspect, Name is displayed as the Button. Content attribute. If it is a TextBlock control, Name is displayed as the TextBlock. Text attribute.

 

 

3. Create a test project

1) create a common C # class library project:

 

2) Reference related class libraries through nuget:

 

3) obtain the uwp startup id. Right-click the app shortcut and select properties:

 

 

3) add two types of QyClientTestBase and PlayerPageScenarios to the project to enable iQiYi uwp. Click a video on the home page and jump to the playback page. After playing the video for 3 seconds, drag the progress bar to the middle position, then, you can play the selected video randomly and recursively play it on the playback page.

 

Public class QyClientTestBase {// Note: append/wd/hub to the URL if you're directing the test at Appium protected const string WindowsApplicationDriverUrl =" http://127.0.0.1:4723 "; Protected static WindowsElement Hanburg; protected static WindowsElement BackButton; protected static WindowsDriver <WindowsElement> QyClientSession; protected static string OriginalCalculatorMode; public static void BaseSetup (TestContext context) {if (QyClientSession = null) {// start iQiYi app DesiredCapabilities appCapabilities = new DesiredCapabilities (); appCapabilities. setCapability ("app", "0C 72C7CD. Beta_atj5cpqqdhzyp! App "); appCapabilities. setCapability ("deviceName", "WindowsPC"); QyClientSession = new WindowsDriver <WindowsElement> (new Uri (WindowsApplicationDriverUrl), appCapabilities); Assert. isNotNull (QyClientSession); QyClientSession. manage (). timeouts (). implicitlyWait (TimeSpan. fromSeconds (3); Hanburg = QyClientSession. findElementByName ("Menu"); // when the page is being played // BackButton = QyClientSession. findElementByAccessib IlityId ("Back") ;}// on the homepage, the hamburger button is displayed. On the playback page, the Back button displays Assert. isNotNull (Hanburg);} public static void BaseTearDown () {if (QyClientSession! = Null) {Hanburg = null; QyClientSession. Dispose (); QyClientSession = null ;}}}

 

[TestClass] public class PlayerPageScenarios: QyClientTestBase // The subtype must be set to public. Otherwise, {[ClassInitialize] // before executing each unit test method, call the initialization method public static void Setup (TestContext context) {BaseSetup (context);} [ClassCleanup] // after each unit test method is executed, call the cleanup method public static void TearDown () {BaseTearDown ();} bool isPlaying = false; [TestMethod] public void SliderClick () {if (isPlaying = false) {// start with the app Home Page, randomly play a video, jump to the PlayBRandom (); Thread. sleep (TimeSpan. fromSeconds (3); isPlaying = true;} ShowControls (); // The element needs to be displayed on the page. For example, if Visibility = "Collapsed", the WindowsElement slider = QyClientSession cannot be obtained. findElementByAccessibilityId ("slider_position"); // progress bar Assert. isNotNull (slider); // drag the video progress bar to the middle position QyClientSession. mouse. mouseMove (slider. coordinates, slider. size. width/2, 0); // seek progress slider. click (); Thread. Sleep (TimeSpan. fromSeconds (3); // PlayBRandom (); Thread. sleep (TimeSpan. fromSeconds (5); // recursive call, switching the current page to play the SliderClick ();} /// <summary> /// play any B object on the current page // </summary> void PlayBRandom () {var list = QyClientSession. findElementsByName ("QyClient. _ 3. _ 0. model. _ B "); if (list. count> 0) {int min = isPlaying? 0: 30; // on the app homepage, the B object int index = new Random () after 30 is played (). next (min, list. count-1); WindowsElement e = list. elementAt (index); e. click ();} Assert. areNotEqual (list. count, 0); Thread. sleep (3000);} // <summary> // display the broadcast control bar; otherwise, the broadcast control button cannot be obtained /// </summary> void ShowControls () {WindowsElement win = QyClientSession. findElementByAccessibilityId ("mediaPlayer"); win. click (); Thread. sleep (500 );}}

 

4) In "test resource manager", you can run and breakpoint debugging tests:

 

5) run the test, switch the selected sets cyclically, and play the video.

 

 

 

Sample project code

 

 

References:

UI Testing: What's new with WinAppDriver: https://channel9.msdn.com/Events/Build/2017/P4084

Improving App Quality with UI Automation: https://channel9.msdn.com/Events/Connect/2016/202? Ocid = player

Automation Windows And Mac Apps With The WebDriver Protocol-Dan Cuellar, FOODIt: https://www.youtube.com/watch? V = MgBRvQOZhec

StarDriver Enterprise Appium to the Future | Jonathan Lipps: https://www.youtube.com/watch? V = e61OhZzbsEI

Github WinAppDriver: https://github.com/microsoft/winappdriver

Modern Dev Practices: Unit Testing: https://channel9.msdn.com/Shows/Visual-Studio-Toolbox/Modern-Dev-Practices-Unit-Testing

Inspect.exe: https://msdn.microsoft.com/en-us/library/windows/desktop/dd318521 (v = vs.85). aspx

Windows SDK and emulator archive: https://developer.microsoft.com/en-us/windows/downloads/sdk-archive

Windows Automation API: UI Automation: https://msdn.microsoft.com/zh-cn/library/ms726294 (vs.85). aspx

AutomationID: https://docs.microsoft.com/en-us/dotnet/framework/ui-automation/use-the-automationid-property

UI Automation Overview:
Https://docs.microsoft.com/en-us/dotnet/framework/ui-automation/ui-automation-overview
Https://msdn.microsoft.com/zh-cn/library/ms728097 (v = vs.85). aspx

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.