Sahi Pro - Testing Native and Hybrid Android apps
abstract
This section details how to test native and hybrid apps on an Android device or emulator.
License
SahiProPlus versions now support testing of native and hybrid content on Android devices. In future, they will support testing of iOS devices as well.Native and hybrid Android apps testing is not supported on SahiPro versions. However Android browsers can be tested from SahiPro versions as well as from SahiProPlus versions.
SahiProPlus software requires a different license file than the one SahiPro uses. Add the new license to userdata/config folder.
Features
-
Supports running scripts against a connected Android device or emulator, at or above SDK 17
-
Supports testing against multiple devices, but devices need to be connected to the machine on which the script runs.
-
Supports testing of native as well as hybrid apps. Hybrid apps contain native content as well as web content hosted inside a WebView.
-
AndroidViewer and Controller allow easy interaction with the connected device.
-
Scripts can be recorded on native and hybrid content from the Android controller. Object Spy features are available for native and hybrid content.
-
Native API syntax similar to Sahi Pro APIs for browser testing.
Limitations
-
Does not support testing Android device or emulator below SDK 17.
Pre-requisites
-
Desktop
-
Desktop needs to have Android Platform Tools installed so that adb.exe is available on the desktop. Having a full blown Android installation is even better.
-
Make sure the path to the Android Platform Tools folder is added to the PATH environment variable. To test this, open a command prompt and type adb(or adb.exe).
Adb should start up if the Android Platform Tools folder is in the PATH.
-
Desktop needs to have Android Platform Tools installed so that adb.exe is available on the desktop. Having a full blown Android installation is even better.
-
Device
-
Connect the device. Enable USB debugging from Settings -> Developer options.
-
Now open a command prompt and type adb shell. If you already have the driver for the device installed, you should now be connected through adb. If not, install the USB driver for the device.
info IMPORTANT: adb connection HAS to work for native and hybrid Android apps testing. So please make sure that adb is setup correctly.
-
The device has to be connected through USB cable throughout the duration of the test. Alternatively you can setup ADB over Wi-Fi as well if you do not wish to tether the device.
-
Screen lock should be removed and Display options should be set such that the device does not sleep before the start of the script.
-
When the device is connected for the first time, you may get an alert asking for permission to connect. Set it to always accept the connection.
-
IMPORTANT: To test hybrid content, network proxy settings have to be configured. The device has to be on the same network as the Desktop. Configure the Desktop IP address and port (9999).
To test native content only, the device does not need to be on the network at all. Network is required only for testing hybrid content.
-
Connect the device. Enable USB debugging from Settings -> Developer options.
Installation
-
Desktop
-
Sahi Pro Plus installer works exactly the same as a Sahi Pro installer. Install Sahi Pro Plus to a location without spaces in the path.
-
Sahi Pro Plus installer works exactly the same as a Sahi Pro installer. Install Sahi Pro Plus to a location without spaces in the path.
-
Device: Android testing requires some binaries to be copied to the device.
-
Connect the device through USB and run
userdata/bin/android/setup_android.bat
.
-
This has to be done once for each device/emulator that you plan to test with.
-
Connect the device through USB and run
Android Viewer
Android Viewer allows an end user to view the Android screen on a Desktop browser. The user can launch the Controller and then identify Android elements bypressing the Ctrl key and hovering the mouse over the element. One can identify native as well as web elements inside a WebView.
This section details how to use the Android Viewer.
-
Launch a Chrome or Firefox browser from the Dashboard.
-
Click on the
Android Viewer
link. This brings up a page with a single buttonConnect Session
-
Make sure that the device is connected through a USB cable. Click on
Connect Session
. If the connection is successful, you will see 3 buttonsDisconnect Session
,Controller
andRefresh
.
You will see the device screenshot in a short while.
-
The screen does not get refreshed automatically. When some action is performed from the Controller, the screenshot will be refreshed a few times, to display the latest device state.
However, if actions are performed on the device manually, the screenshot will not get refreshed. Click on theRefresh
button manually to refresh the screenshot.
-
One can interact with the device from the Android Viewer itself without launching the Controller. Please refer to the Recording section for details on how to do this.
-
Launch the Android Controller by clicking on the
Controller
button. This will bring up the Controller. This looks exactly the same as a normal Browser controller.
-
You can now start identifying elements by pressing the Ctrl key and hovering the mouse over elements on the Desktop screenshot. You will notice the Accessor textbox on the Controller, showing the accessor for the element. The Alternatives dropdown displays alternatives for the accessor.
-
One can identify both native elements and web elements in a hybrid app. When web elements are identified, you will notice the Prefix textbox populated with content prefixed with
_webcontext
.
For example:_webcontext("in.co.sahi.android.testwebview").
The_webcontext
prefix indicates that the accessor is for a Web element. For a native element, the prefix is blank.
Native element:
Hybrid element:
info
A few things to note...
-
Currently, Android Viewer is supported only on Chrome and Firefox. This does NOT work on IE.
-
If the app orientation changes from portrait to landscape or vice versa, do a manual refresh by clicking on the Refresh button, to view the screenshot correctly.
-
If you resize the Android Viewer page, do a manual refresh by clicking on the Refresh button, to view the screenshot correctly.
IMPORTANT NOTE: Please ensure that the page does NOT display a vertical scrollbar. The page should always be a single page. If you resize the page such that a vertical scrollbar is seen, element identification will not happen properly.
-
Android Viewer and Controller, support identification of native elements and web elements inside a native app. They do NOT support identification of web elements inside an Android browser.
Hence use this only for a native or hybrid app. Do not use it for an Android browser.
-
Please do not yank the device before disconnecting the session. Incase you do so and on reconnecting you find that
Connect Session
does not seem to connect the device, please have a look at the Sahi console.
If the Sahi console indicates that you should reconnect the device, please do so.
-
Closing the Android Viewer browser does NOT disconnect the session. So please always disconnect the session before closing the browser. If you fail to do so, and open the Android Viewer again, it will show the session to be in a connected state. The elements will still get identified.
-
As of now, Android Viewer supports only one device at a time. Device has to be connected to the desktop that runs Sahi.
Recording a script
This is similar to recording a Sahi script for normal browser testing.-
To record a script, launch the Controller from the Android Viewer page, choose a script to record and press Record.
-
Actions done from the Android Viewer page are recorded automatically.
Press the Ctrl key and hover your mouse over an element on the screen.
-
Native elements
-
Hovering the mouse over an EditText will overlay a textbox where you can type in text. After typing the text, press Enter or click outside the textbox. This will cause the text content to be sent to the device. The _setValue step will get recorded automatically.
-
Hovering the mouse over other controls will display a pointer cursor. The user can then click on the pointed element. This will cause the element on the device to be clicked. The _click step will get recorded automatically.
-
Hovering the mouse over an EditText will overlay a textbox where you can type in text. After typing the text, press Enter or click outside the textbox. This will cause the text content to be sent to the device. The _setValue step will get recorded automatically.
-
Web elements
-
Hovering the mouse over an input text or input password control will overlay a textbox where you can type in text. After typing the text, press Enter or click outside the textbox. This will cause the text content to be sent to the device. The _setValue step will get recorded automatically.
-
Hovering the mouse over other controls will display a pointer cursor. The user can then click on the pointed element. This will cause the element on the device to be clicked. The _click step will get recorded automatically.
-
Hovering the mouse over an input text or input password control will overlay a textbox where you can type in text. After typing the text, press Enter or click outside the textbox. This will cause the text content to be sent to the device. The _setValue step will get recorded automatically.
-
Native elements
-
Steps can also be recorded from the Controller. You can use Click, Set, Assert, choose Other Actions, Test an expression from Evaluate Expression to perform an action on the Device.
But you will need to record these steps explicitly by clicking onAppend to Script
.
-
Whenever a native step is followed by a web step or vice versa, _aStartWebContext or _aStopWebContext get recorded automatically. The user does not need to explicitly write code for this.
-
Since the device screenshot gets refreshed a few times after a Controller action is performed, the user can continue to interact with the changing screen from the Desktop itself. If the device screenshot is not refreshed correctly, click on the Refresh button manually to reflect the current screenshot.
-
For web elements, recording can also be done from the Device itself. Actions performed on the Device during a Recording session will automatically get recorded.
info This is useful in recording actions inside an iframe with a different domain to the main domain. Actions cannot be recorded from the Android Viewer inside an iframe with a different domain.
info For native elements, recording CANNOT be done from the Device. Android Viewer and Controller have to be used for recording native elements.
-
Accessor Repository is not supported for Android elements as of now.
Using the controller
The Controller can be used to interact with the device in a non Record mode as well. You can use Click, Set, Assert, choose Other Actions, Test an expression from Evaluate Expression to perform an action on the Device.Playback from the Controller
Once a script is recorded, it can be played back from the Controller Playback tab itself.-
Choose the script.
-
You can leave the Start url blank. This is not used.
-
Click on Play to playback the script.
-
Click on Logs to view the logs of the played back script.
-
NOTE: When the script is being played back from the Controller, you cannot see the playback happening on the Android Viewer. The AndroidViewer does not get refreshed during playback.
Playback from the Editor
You can playback the script from the Editor as well.info
IMPORTANT:
A script cannot be played back on an Android device if a session is open from the Android Viewer. Disconnect the session from the Android Viewer and then playback the script.
If the session is open and you play back a script from the Editor, the script will not run till you close the open session.
A script cannot be played back on an Android device if a session is open from the Android Viewer. Disconnect the session from the Android Viewer and then playback the script.
If the session is open and you play back a script from the Editor, the script will not run till you close the open session.
-
Choose the script from the Editor and click on the Playback button.
-
Choose any Browser.
-
Enter the default mobile device to connect to. If left blank, the value "default" is used.
-
Choose the Start Mode as
Mobile Device
-
Choose any Start URL. This is not used.
-
Choose the
Run in parallel
option.
-
Press
Run
to playback the script.
Playing back a suite
-
Choose the suite from the Editor and click on the Playback button.
-
Choose any Browser.
-
Enter the default mobile device to connect to. If left blank, the value "default" is used.
-
Choose the Start Mode as
Mobile Device
-
Choose any Start URL. This is not used.
-
Choose the
Run in parallel
option.
info IMPORTANT: Choose Threads as 1. Do NOT leave this as 5.
-
Press
Run
to playback the script.
Notes on Hybrid content
-
As mentioned before, testing of web content requires proxy configuration on the device. The device has to be on the same network as the Desktop. Configure the Desktop IP address and port (9999).
-
IMPORTANT: Content in a webview tends to be cached. If you have ever used the app before, the webview will have cached pages. The cached pages need to be cleared before we can start testing the hybrid content.
To clear the cache, please do the following (UI will be different on different devices, so please adapt these instructions as appropriate).
-
Go to Settings -> Apps on your device.
-
From the All Apps tab, open the Hybrid app.
-
Click on the
CLEAR DATA
button.
info Please note that clicking onCLEAR CACHE
does NOT typically clear the webview cache. So click onCLEAR DATA
.
-
Configure the proxy settings if not already configured.
-
Launch the hybrid app and access the web content. Since the webview cache has been cleared, the pages should get loaded through Sahi's proxy.
-
Go to Settings -> Apps on your device.
Notes on SSL content
Additional configuration is required to test SSL content inside a WebView.Accept Certificate
-
On the Android device, open a browser and access
http://sahi.example.com:9999
. You should see a page like this
If you do not see the page, you need to set the network proxy settings to point to the Sahi Desktop machine at port 9999.
-
Click on the
crt
link. Give a certificate name, saySahi
.
Click OK to install the certificate.
-
Clear the Application cache as explained in section Notes on Hybrid content.
-
Now you should be able to automate the SSL content in your application's WebView.
Effect of proxy on some apps
You may notice that after you configure Sahi's proxy, some apps that require internet connection do not work. This happens because they are using an SSL connection and configuring the proxy causes the connection to fail.To fix the problem, accept the SSL certificate as stated above in the Accept Certificate section.