locked
UI Testing - Airplane Mode? RRS feed

  • Question

  • User298186 posted

    Is there a method in the Xamarin test framework to toggle Airplane Mode/Wifi on and off for Android and/or iOS? If so, does it matter if the device is simulated or physical?

    Thanks!

    Tuesday, March 14, 2017 8:00 PM

Answers

  • User298186 posted

    Follow Up: I got around this with a hacky solution. I swipe from the bottom of the app to open the iOS drawer and click at x,y of where the wifi icon is (found via trial and error).

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, May 17, 2017 5:47 PM

All replies

  • User298186 posted

    Follow Up: I got around this with a hacky solution. I swipe from the bottom of the app to open the iOS drawer and click at x,y of where the wifi icon is (found via trial and error).

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, May 17, 2017 5:47 PM
  • User365328 posted

    Have any sample code?

    Wednesday, February 7, 2018 3:27 PM
  • User365328 posted

    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();
          }
        }
      }
    }
    
    Friday, February 9, 2018 5:36 PM
  • User298186 posted

    I did some PoC code and we abandoned this approach in favor of just having manual testers perform this action. It is pretty dumb in that it requires X, Y coordinates (which you could change by device/config).

         /// <summary>
        /// Toggles the state of the System Setting - Airplane mode
        /// </summary>
        public static void ToggleAirplaneMode()
        {
            // Get the View
            AppResult result = app.Query().FirstOrDefault();
            // Swipe up to open the menu
            app.DragCoordinates(result.Rect.CenterX, result.Rect.Height, result.Rect.CenterX, result.Rect.CenterY);
            // Tap the ?
            app.TapCoordinates(40, 300);
            // Wait a moment for the change to be applied
            System.Threading.Thread.Sleep(1500);
            // Swipe down to close the menu
            app.DragCoordinates(result.Rect.CenterX, result.Rect.CenterY, result.Rect.CenterX, result.Rect.Height);
        }
    
    Friday, February 9, 2018 5:46 PM
  • User365328 posted

    Thanks, @Kirbycope. If you look at my above comment I also went this approach. It's absolutely not maintainable though, I agree. When apple comes out with a new OS version, the coordinates will most likely change and will cause UI tests to fail

    Monday, February 19, 2018 2:35 PM
  • User86177 posted

    Hello, Is possible in andoid? I did the drag from the top to the center of the screen, but nothing happen.

    Tuesday, March 6, 2018 1:08 PM
  • User298186 posted

    You need to think about why it doesn't work or you'll have a long road ahead. In iOS the swipe works as-is because thats how the drawer is opened in iOS (swiping from bottom up). So for Android, just figure out where you need to swipe from and to, then change the code above (hint, it is swiping from the top). Alternatively you can send ADB commands, but I haven't had too much success in that (beyond my local machine).

    Tuesday, March 6, 2018 3:56 PM