Forum Xamarin Test Cloud (Read Only)

Enabling Wifi as test set up

Hi, I need to enable wifi on the device to run my test. How do I do that using Xamarin UITest?

Answers

  • JimBennettJimBennett GBXamarin Team, Insider, University, Developer Group Leader ✭✭✭✭

    iOS or Android? We control WiFi on Android via a backdoor - so a method we invoke on our app to toggle it. Not possible on iOS unfortunately.

  • RaniKavirajanRaniKavirajan USUniversity

    Our App support both iOS and Android. Could you please give more information about how this was achieved in Android and why the solution will not work for iOS? Also, how do we get the Wifi access point information?

  • JimBennettJimBennett GBXamarin Team, Insider, University, Developer Group Leader ✭✭✭✭

    On Android we wrote code in our app to turn wifi on and off - plenty of examples around if you want to do a quick google.

    Something like:

    http://stackoverflow.com/questions/8863509/how-to-programmatically-turn-off-wifi-on-android-device

    Once you have your code in your app you can create a backdoor that you can call in your test:

    https://developer.xamarin.com/guides/testcloud/uitest/working-with/backdoors/

    This works for Android because the OS allows your app to control Wifi, this is a well documented API. On iOS there is no public API for you to control Wifi, so no way to do this from a backdoor.

  • stepheawstepheaw Member ✭✭

    I wrote some helper class called IOSControlCenterManger. Use this in your UITest project. Only iOS 11 is supported. iPhone SE and iPhone X are not supported -- but it would be easy to update this code, you just need the coordinates of the Control Center Buttons

    using System;
    using System.Linq;
    using NUnit.Framework;
    using Pass.Shared.Services;
    using Xamarin.UITest;
    
    namespace Pass.Shared.UITests
    {
      public class IOSControlCenterManager
      {
        // Hook to UITest app
        private IApp app;
    
        // iOS Major Version numbers
        public const int IOS10VersionNumber = 10;
        public const int IOS11VersionNumber = 11;
    
        // iPhone 6, iPhone 7, iPhone 8 - iOS 11
        public const float IphoneFourPointSevenInchIOS11WifiButtonXCoordinate = 65f;
        public const float IphoneFourPointSevenInchIOS11WifiButtonYCoordinate = 345f;
    
        public const float IphoneFourPointSevenInchIOS11BluetoothButtonXCoordinate = 135f;
        public const float IphoneFourPointSevenInchIOS11BluetoothButtonYCoordinate = 345f;
    
        public const float IphoneFourPointSevenInchIOS11CellularButtonXCoordinate = 135f;
        public const float IphoneFourPointSevenInchIOS11CellularButtonYCoordinate = 275f;
    
        public const float IphoneFourPointSevenInchIOS11AirplaneModeButtonXCoordinate = 65f;
        public const float IphoneFourPointSevenInchIOS11AirplaneModeButtonYCoordinate = 275f;
    
        // iPhone 6Plus, iPhone 7Plus,iPhone 8Plus - iOS 11
        public const float IphoneFivePointFiveInchIOS11WifiButtonXCoordinate = 80f;
        public const float IphoneFivePointFiveInchIOS11WifiButtonYCoordinate = 380f;
    
        public const float IphoneFivePointFiveInchIOS11BluetoothButtonXCoordinate = 150f;
        public const float IphoneFivePointFiveInchIOS11BluetoothButtonYCoordinate = 380f;
    
        public const float IphoneFivePointFiveInchIOS11CellularButtonXCoordinate = 150f;
        public const float IphoneFivePointFiveInchIOS11CellularButtonYCoordinate = 300f;
    
        public const float IphoneFivePointFiveInchIOS11AirplaneModeButtonXCoordinate = 80f;
        public const float IphoneFivePointFiveInchIOS11AirplaneModeButtonYCoordinate = 300f;
    
    
        public IOSControlCenterManager(IApp _app)
        {
          // Set class-level variables
          app = _app;
    
          // These are are native methods exposed in the AppDelegate used for UITesting
          OSVersionString = app.Invoke("getDeviceOSVersion:", "the value").ToString();
          DeviceHardwareVersion = app.Invoke("getHardwareVersion:", "the value").ToString();
    
          // Try parse the version string being passed to a Version object so we can extract the Version major/minor
          Version.TryParse(OSVersionString, out Version version);
          OSVersion = version;
    
          // Retrieve and load the Coordinate Plane of the device in use
          GetTouchCoordinates();
        }
    
        // UIView Coordinate Values
        public float? TopLeftXCoordinate { get; set; }
        public float? TopLeftYCoordinate { get; set; }
    
        public float? TopRightXCoordinate { get; set; }
        public float? TopRightYCoordinate { get; set; }
    
        public float? BottomLeftXCoordinate { get; set; }
        public float? BottomLeftYCoordinate { get; set; }
    
        public float? BottomRightXCoordinate { get; set; }
        public float? BottomRightYCoordinate { get; set; }
    
        public float? CenterXCoordinate { get; set; }
        public float? CenterYCoordinate { get; set; }
    
        public string DeviceHardwareVersion { get; set; }
        public string OSVersionString { get; set; }
        public Version OSVersion { get; set; }
    
        // Get a hook to the UI window so we can get the screen width/height and coordinate plane
        private void GetTouchCoordinates()
        {
          // Get hook to the application window
          var windowQuery = app.Query().FirstOrDefault();
    
          // Get some coordinates and load them as class-level variables 
          TopLeftXCoordinate = windowQuery?.Rect?.X;
          TopLeftYCoordinate = windowQuery?.Rect?.Y;
    
          TopRightXCoordinate = windowQuery?.Rect?.X + windowQuery?.Rect?.Width;
          TopRightYCoordinate = windowQuery?.Rect?.Y;
    
          BottomLeftXCoordinate = windowQuery?.Rect?.X;
          BottomLeftYCoordinate = windowQuery?.Rect?.Y + windowQuery?.Rect?.Height;
    
          BottomRightXCoordinate = windowQuery?.Rect?.X + windowQuery?.Rect?.Width;
          BottomRightYCoordinate = windowQuery?.Rect?.Y + windowQuery?.Rect?.Height;
    
          CenterXCoordinate = windowQuery?.Rect?.CenterX;
          CenterYCoordinate = windowQuery?.Rect?.CenterY;
        }
    
        public void OpenControlCenter()
        {
          // Swipe up to open Control Center drawer
          app.DragCoordinates(BottomLeftXCoordinate.Value, BottomLeftYCoordinate.Value, TopLeftXCoordinate.Value, TopLeftYCoordinate.Value);
        }
    
        public void CloseControlCenter()
        {
          // Tap the top left corner of the screen to close Control Center drawer
          app.TapCoordinates(TopLeftXCoordinate.Value, TopLeftYCoordinate.Value);
        }
    
        public void TapWifiButton()
        {
          // iOS 11
          if (OSVersion.Major == IOS11VersionNumber)
          {
            // iPhone Plus Model
            if (DeviceHardwareVersion.Contains("Plus"))
            {
              // Tap Wi-Fi button
              app.TapCoordinates(IphoneFivePointFiveInchIOS11WifiButtonXCoordinate, IphoneFivePointFiveInchIOS11WifiButtonYCoordinate);
            }
            else if(DeviceHardwareVersion.Contains("SE"))
            {
              Assert.Inconclusive();
            }
            else if (DeviceHardwareVersion.Contains("X"))
            {
              Assert.Inconclusive();
            }
            else
            {
              // Tap Wi-Fi button
              app.TapCoordinates(IphoneFourPointSevenInchIOS11WifiButtonXCoordinate, IphoneFourPointSevenInchIOS11WifiButtonYCoordinate);
            }
          }
          // iOS 10
          else if (OSVersion.Major == IOS10VersionNumber)
          {
            // Support iOS 10 here if needed
            Assert.Inconclusive();
          }
          // Version unkown or unsupported 
          else
          {
            Assert.Inconclusive();
          }
        }
    
        public void TapBluetoothButton()
        {
          // iOS 11
          if (OSVersion.Major == IOS11VersionNumber)
          {
            // iPhone Plus Model
            if (DeviceHardwareVersion.Contains("Plus"))
            {
              // Tap Bluetooth button
              app.TapCoordinates(IphoneFivePointFiveInchIOS11BluetoothButtonXCoordinate, IphoneFivePointFiveInchIOS11BluetoothButtonYCoordinate);
            }
            else if (DeviceHardwareVersion.Contains("SE"))
            {
              Assert.Inconclusive();
            }
            else if (DeviceHardwareVersion.Contains("X"))
            {
              Assert.Inconclusive();
            }
            else
            {
              // Tap Bluetooth button
              app.TapCoordinates(IphoneFourPointSevenInchIOS11BluetoothButtonXCoordinate, IphoneFourPointSevenInchIOS11BluetoothButtonYCoordinate);
            }
          }
          // iOS 10
          else if (OSVersion.Major == IOS10VersionNumber)
          {
            // Support iOS 10 here if needed
            Assert.Inconclusive();
          }
          // Version unkown or unsupported 
          else
          {
            Assert.Inconclusive();
          }
        }
    
        public void TapCellularButton()
        {
          // iOS 11
          if (OSVersion.Major == IOS11VersionNumber)
          {
            // iPhone Plus Model
            if (DeviceHardwareVersion.Contains("Plus"))
            {
              // Tap Cellular button
              app.TapCoordinates(IphoneFivePointFiveInchIOS11CellularButtonXCoordinate, IphoneFivePointFiveInchIOS11CellularButtonYCoordinate);
            }
            else if (DeviceHardwareVersion.Contains("SE"))
            {
              Assert.Inconclusive();
            }
            else if (DeviceHardwareVersion.Contains("X"))
            {
              Assert.Inconclusive();
            }
            else
            {
              // Tap Cellular button
              app.TapCoordinates(IphoneFourPointSevenInchIOS11CellularButtonXCoordinate, IphoneFourPointSevenInchIOS11CellularButtonYCoordinate);
            }
          }
          // iOS 10
          else if (OSVersion.Major == IOS10VersionNumber)
          {
            // Support iOS 10 here if needed
            Assert.Inconclusive();
          }
          // Version unkown or unsupported 
          else
          {
            Assert.Inconclusive();
          }
        }
    
        public void TapAirplaneModeButton()
        {
          // iOS 11
          if (OSVersion.Major == IOS11VersionNumber)
          {
            // iPhone Plus Model
            if (DeviceHardwareVersion.Contains("Plus"))
            {
              // Tap Airplane Mode button
              app.TapCoordinates(IphoneFivePointFiveInchIOS11AirplaneModeButtonXCoordinate, IphoneFivePointFiveInchIOS11AirplaneModeButtonYCoordinate);
            }
            else if (DeviceHardwareVersion.Contains("SE"))
            {
              Assert.Inconclusive();
            }
            else if (DeviceHardwareVersion.Contains("X"))
            {
              Assert.Inconclusive();
            }
            else
            {
              // Tap Airplane Mode button
              app.TapCoordinates(IphoneFourPointSevenInchIOS11AirplaneModeButtonXCoordinate, IphoneFourPointSevenInchIOS11AirplaneModeButtonYCoordinate);
            }
          }
          // iOS 10
          else if (OSVersion.Major == IOS10VersionNumber)
          {
            // Support iOS 10 here if needed
            Assert.Inconclusive();
          }
          // Version unkown or unsupported 
          else
          {
            Assert.Inconclusive();
          }
        }
      }
    }
    
  • stepheawstepheaw Member ✭✭

    I wrote some helper class called IOSControlCenterManger. Use this in your UITest project. Only iOS 11 is supported. iPhone SE and iPhone X are not supported -- but it would be easy to update this code, you just need the coordinates of the Control Center Buttons

    using System;
    using System.Linq;
    using NUnit.Framework;
    using Pass.Shared.Services;
    using Xamarin.UITest;
    
    namespace Pass.Shared.UITests
    {
      public class IOSControlCenterManager
      {
        // Hook to UITest app
        private IApp app;
    
        // iOS Major Version numbers
        public const int IOS10VersionNumber = 10;
        public const int IOS11VersionNumber = 11;
    
        // iPhone 6, iPhone 7, iPhone 8 - iOS 11
        public const float IphoneFourPointSevenInchIOS11WifiButtonXCoordinate = 65f;
        public const float IphoneFourPointSevenInchIOS11WifiButtonYCoordinate = 345f;
    
        public const float IphoneFourPointSevenInchIOS11BluetoothButtonXCoordinate = 135f;
        public const float IphoneFourPointSevenInchIOS11BluetoothButtonYCoordinate = 345f;
    
        public const float IphoneFourPointSevenInchIOS11CellularButtonXCoordinate = 135f;
        public const float IphoneFourPointSevenInchIOS11CellularButtonYCoordinate = 275f;
    
        public const float IphoneFourPointSevenInchIOS11AirplaneModeButtonXCoordinate = 65f;
        public const float IphoneFourPointSevenInchIOS11AirplaneModeButtonYCoordinate = 275f;
    
        // iPhone 6Plus, iPhone 7Plus,iPhone 8Plus - iOS 11
        public const float IphoneFivePointFiveInchIOS11WifiButtonXCoordinate = 80f;
        public const float IphoneFivePointFiveInchIOS11WifiButtonYCoordinate = 380f;
    
        public const float IphoneFivePointFiveInchIOS11BluetoothButtonXCoordinate = 150f;
        public const float IphoneFivePointFiveInchIOS11BluetoothButtonYCoordinate = 380f;
    
        public const float IphoneFivePointFiveInchIOS11CellularButtonXCoordinate = 150f;
        public const float IphoneFivePointFiveInchIOS11CellularButtonYCoordinate = 300f;
    
        public const float IphoneFivePointFiveInchIOS11AirplaneModeButtonXCoordinate = 80f;
        public const float IphoneFivePointFiveInchIOS11AirplaneModeButtonYCoordinate = 300f;
    
    
        public IOSControlCenterManager(IApp _app)
        {
          // Set class-level variables
          app = _app;
    
          // These are are native methods exposed in the AppDelegate used for UITesting
          OSVersionString = app.Invoke("getDeviceOSVersion:", "the value").ToString();
          DeviceHardwareVersion = app.Invoke("getHardwareVersion:", "the value").ToString();
    
          // Try parse the version string being passed to a Version object so we can extract the Version major/minor
          Version.TryParse(OSVersionString, out Version version);
          OSVersion = version;
    
          // Retrieve and load the Coordinate Plane of the device in use
          GetTouchCoordinates();
        }
    
        // UIView Coordinate Values
        public float? TopLeftXCoordinate { get; set; }
        public float? TopLeftYCoordinate { get; set; }
    
        public float? TopRightXCoordinate { get; set; }
        public float? TopRightYCoordinate { get; set; }
    
        public float? BottomLeftXCoordinate { get; set; }
        public float? BottomLeftYCoordinate { get; set; }
    
        public float? BottomRightXCoordinate { get; set; }
        public float? BottomRightYCoordinate { get; set; }
    
        public float? CenterXCoordinate { get; set; }
        public float? CenterYCoordinate { get; set; }
    
        public string DeviceHardwareVersion { get; set; }
        public string OSVersionString { get; set; }
        public Version OSVersion { get; set; }
    
        // Get a hook to the UI window so we can get the screen width/height and coordinate plane
        private void GetTouchCoordinates()
        {
          // Get hook to the application window
          var windowQuery = app.Query().FirstOrDefault();
    
          // Get some coordinates and load them as class-level variables 
          TopLeftXCoordinate = windowQuery?.Rect?.X;
          TopLeftYCoordinate = windowQuery?.Rect?.Y;
    
          TopRightXCoordinate = windowQuery?.Rect?.X + windowQuery?.Rect?.Width;
          TopRightYCoordinate = windowQuery?.Rect?.Y;
    
          BottomLeftXCoordinate = windowQuery?.Rect?.X;
          BottomLeftYCoordinate = windowQuery?.Rect?.Y + windowQuery?.Rect?.Height;
    
          BottomRightXCoordinate = windowQuery?.Rect?.X + windowQuery?.Rect?.Width;
          BottomRightYCoordinate = windowQuery?.Rect?.Y + windowQuery?.Rect?.Height;
    
          CenterXCoordinate = windowQuery?.Rect?.CenterX;
          CenterYCoordinate = windowQuery?.Rect?.CenterY;
        }
    
        public void OpenControlCenter()
        {
          // Swipe up to open Control Center drawer
          app.DragCoordinates(BottomLeftXCoordinate.Value, BottomLeftYCoordinate.Value, TopLeftXCoordinate.Value, TopLeftYCoordinate.Value);
        }
    
        public void CloseControlCenter()
        {
          // Tap the top left corner of the screen to close Control Center drawer
          app.TapCoordinates(TopLeftXCoordinate.Value, TopLeftYCoordinate.Value);
        }
    
        public void TapWifiButton()
        {
          // iOS 11
          if (OSVersion.Major == IOS11VersionNumber)
          {
            // iPhone Plus Model
            if (DeviceHardwareVersion.Contains("Plus"))
            {
              // Tap Wi-Fi button
              app.TapCoordinates(IphoneFivePointFiveInchIOS11WifiButtonXCoordinate, IphoneFivePointFiveInchIOS11WifiButtonYCoordinate);
            }
            else if(DeviceHardwareVersion.Contains("SE"))
            {
              Assert.Inconclusive();
            }
            else if (DeviceHardwareVersion.Contains("X"))
            {
              Assert.Inconclusive();
            }
            else
            {
              // Tap Wi-Fi button
              app.TapCoordinates(IphoneFourPointSevenInchIOS11WifiButtonXCoordinate, IphoneFourPointSevenInchIOS11WifiButtonYCoordinate);
            }
          }
          // iOS 10
          else if (OSVersion.Major == IOS10VersionNumber)
          {
            // Support iOS 10 here if needed
            Assert.Inconclusive();
          }
          // Version unkown or unsupported 
          else
          {
            Assert.Inconclusive();
          }
        }
    
        public void TapBluetoothButton()
        {
          // iOS 11
          if (OSVersion.Major == IOS11VersionNumber)
          {
            // iPhone Plus Model
            if (DeviceHardwareVersion.Contains("Plus"))
            {
              // Tap Bluetooth button
              app.TapCoordinates(IphoneFivePointFiveInchIOS11BluetoothButtonXCoordinate, IphoneFivePointFiveInchIOS11BluetoothButtonYCoordinate);
            }
            else if (DeviceHardwareVersion.Contains("SE"))
            {
              Assert.Inconclusive();
            }
            else if (DeviceHardwareVersion.Contains("X"))
            {
              Assert.Inconclusive();
            }
            else
            {
              // Tap Bluetooth button
              app.TapCoordinates(IphoneFourPointSevenInchIOS11BluetoothButtonXCoordinate, IphoneFourPointSevenInchIOS11BluetoothButtonYCoordinate);
            }
          }
          // iOS 10
          else if (OSVersion.Major == IOS10VersionNumber)
          {
            // Support iOS 10 here if needed
            Assert.Inconclusive();
          }
          // Version unkown or unsupported 
          else
          {
            Assert.Inconclusive();
          }
        }
    
        public void TapCellularButton()
        {
          // iOS 11
          if (OSVersion.Major == IOS11VersionNumber)
          {
            // iPhone Plus Model
            if (DeviceHardwareVersion.Contains("Plus"))
            {
              // Tap Cellular button
              app.TapCoordinates(IphoneFivePointFiveInchIOS11CellularButtonXCoordinate, IphoneFivePointFiveInchIOS11CellularButtonYCoordinate);
            }
            else if (DeviceHardwareVersion.Contains("SE"))
            {
              Assert.Inconclusive();
            }
            else if (DeviceHardwareVersion.Contains("X"))
            {
              Assert.Inconclusive();
            }
            else
            {
              // Tap Cellular button
              app.TapCoordinates(IphoneFourPointSevenInchIOS11CellularButtonXCoordinate, IphoneFourPointSevenInchIOS11CellularButtonYCoordinate);
            }
          }
          // iOS 10
          else if (OSVersion.Major == IOS10VersionNumber)
          {
            // Support iOS 10 here if needed
            Assert.Inconclusive();
          }
          // Version unkown or unsupported 
          else
          {
            Assert.Inconclusive();
          }
        }
    
        public void TapAirplaneModeButton()
        {
          // iOS 11
          if (OSVersion.Major == IOS11VersionNumber)
          {
            // iPhone Plus Model
            if (DeviceHardwareVersion.Contains("Plus"))
            {
              // Tap Airplane Mode button
              app.TapCoordinates(IphoneFivePointFiveInchIOS11AirplaneModeButtonXCoordinate, IphoneFivePointFiveInchIOS11AirplaneModeButtonYCoordinate);
            }
            else if (DeviceHardwareVersion.Contains("SE"))
            {
              Assert.Inconclusive();
            }
            else if (DeviceHardwareVersion.Contains("X"))
            {
              Assert.Inconclusive();
            }
            else
            {
              // Tap Airplane Mode button
              app.TapCoordinates(IphoneFourPointSevenInchIOS11AirplaneModeButtonXCoordinate, IphoneFourPointSevenInchIOS11AirplaneModeButtonYCoordinate);
            }
          }
          // iOS 10
          else if (OSVersion.Major == IOS10VersionNumber)
          {
            // Support iOS 10 here if needed
            Assert.Inconclusive();
          }
          // Version unkown or unsupported 
          else
          {
            Assert.Inconclusive();
          }
        }
      }
    }
    
Sign In or Register to comment.