Uwp restart uitests on crash (#5420)
authorShane Neuville <shane94@hotmail.com>
Tue, 5 Mar 2019 18:07:57 +0000 (11:07 -0700)
committerSamantha Houts <samhouts@users.noreply.github.com>
Tue, 5 Mar 2019 18:07:57 +0000 (10:07 -0800)
* [UWP] if a UI tests crashes the UWP window this will restart the window so all the rest of the tests can run

* simplify

Xamarin.Forms.Core.Windows.UITests/WinDriverApp.cs
Xamarin.Forms.Core.Windows.UITests/WindowsTestBase.cs
Xamarin.Forms.Core.Windows.UITests/WindowsTestServer.cs

index 0af4060..617e959 100644 (file)
@@ -27,7 +27,7 @@ namespace Xamarin.Forms.Core.UITests
                        { "button", "ControlType.Button" }
                };
 
-               readonly WindowsDriver<WindowsElement> _session;
+               WindowsDriver<WindowsElement> _session;
 
                readonly Dictionary<string, string> _translatePropertyAccessor = new Dictionary<string, string>
                {
@@ -43,8 +43,18 @@ namespace Xamarin.Forms.Core.UITests
 
                public WinDriverApp(WindowsDriver<WindowsElement> session)
                {
+                       Init(session);
+               }
+
+               void Init(WindowsDriver<WindowsElement> session)
+               {
                        _session = session;
-                       TestServer = new WindowsTestServer(_session);
+                       TestServer = new WindowsTestServer(_session, this);
+               }
+
+               public void RestartFromCrash()
+               {
+                       Init(WindowsTestBase.CreateWindowsDriver());
                }
 
                public void Back()
@@ -511,7 +521,7 @@ namespace Xamarin.Forms.Core.UITests
                        MouseClickAt(x, y);
                }
 
-               public ITestServer TestServer { get; }
+               public ITestServer TestServer { get; private set; }
 
                public void TouchAndHold(Func<AppQuery, AppQuery> query)
                {
index 95bb815..6af46ad 100644 (file)
@@ -1,20 +1,11 @@
 using System;
 using System.Diagnostics;
-using System.Resources;
-using System.Text;
 using NUnit.Framework;
 using OpenQA.Selenium;
-using OpenQA.Selenium.Appium.MultiTouch;
-using OpenQA.Selenium.Appium.Windows;
-using OpenQA.Selenium.Interactions.Internal;
-using OpenQA.Selenium.Remote;
-using Xamarin.Forms.Xaml;
-using Xamarin.UITest;
 using OpenQA.Selenium.Appium;
-using System.Collections.Generic;
+using OpenQA.Selenium.Appium.Windows;
 using OpenQA.Selenium.Interactions;
-using OpenQA.Selenium.Appium.Windows.Enums;
-using System.Windows.Input;
+using Xamarin.UITest;
 
 namespace Xamarin.Forms.Core.UITests
 {
@@ -26,19 +17,24 @@ namespace Xamarin.Forms.Core.UITests
                public static IApp ConfigureApp()
                {
                        if (Session == null)
-                       {
-                               AppiumOptions options = new AppiumOptions();
-                               options.AddAdditionalCapability("app", "0d4424f6-1e29-4476-ac00-ba22c3789cb6_ph1m9x8skttmg!App");
-                               options.AddAdditionalCapability("appArguments", "RunningAsUITests");
-                               Session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), options);
-                               Assert.IsNotNull(Session);
-                               Session.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(1);
-                               Reset();
-                       }
+                               Session = CreateWindowsDriver();
 
                        return new WinDriverApp(Session);
                }
 
+               public static WindowsDriver<WindowsElement> CreateWindowsDriver()
+               {
+                       AppiumOptions options = new AppiumOptions();
+                       options.AddAdditionalCapability("app", "0d4424f6-1e29-4476-ac00-ba22c3789cb6_ph1m9x8skttmg!App");
+                       options.AddAdditionalCapability("appArguments", "RunningAsUITests");
+                       Session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), options);
+                       Assert.IsNotNull(Session);
+                       Session.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(1);
+                       Reset();
+
+                       return Session;
+               }
+
                internal static void HandleAppClosed(Exception ex)
                {
                        if (ex is InvalidOperationException && ex.Message == "Currently selected window has been closed")
index e60fb53..b25f951 100644 (file)
@@ -7,10 +7,12 @@ namespace Xamarin.Forms.Core.UITests
        internal class WindowsTestServer : ITestServer
        {
                readonly WindowsDriver<WindowsElement> _session;
+               readonly WinDriverApp _winDriverApp;
 
-               public WindowsTestServer(WindowsDriver<WindowsElement> session)
+               public WindowsTestServer(WindowsDriver<WindowsElement> session, WinDriverApp winDriverApp)
                {
                        _session = session;
+                       _winDriverApp = winDriverApp;
                }
 
                public string Post(string endpoint, object arguments = null)
@@ -31,6 +33,11 @@ namespace Xamarin.Forms.Core.UITests
                                {
                                        return _session.CurrentWindowHandle;
                                }
+                               catch (OpenQA.Selenium.WebDriverException we)
+                               when (we.Message.Contains("Currently selected window has been closed"))
+                               {
+                                       _winDriverApp.RestartFromCrash();
+                               }
                                catch (Exception exception)
                                {
                                        WindowsTestBase.HandleAppClosed(exception);