Android automation with appium

Appium

Automation for Apps

Appium is an open source test automation framework for use with native, hybrid and mobile web apps.
It drives iOS, Android, and Windows apps using the WebDriver protocol.

Introducing Appium.

Is native app automation missing from your tool belt? Problem solved.

Watch the Videos

Appium Philosophy

Appium is built on the idea that testing native apps shouldn’t require including an SDK or recompiling your app. And that you should be able to use your preferred test practices, frameworks, and tools. Appium is an open source project and has made design and tool decisions to encourage a vibrant contributing community.

Use your favorite tools

Appium aims to automate any mobile app from any language and any test framework, with full access to back-end APIs and DBs from test code. Write tests with your favorite dev tools using all the above programming languages, and probably more (with the Selenium WebDriver API and language-specific client libraries).

Источник

Appium

Automation for Apps

Appium is an open source test automation framework for use with native, hybrid and mobile web apps.
It drives iOS, Android, and Windows apps using the WebDriver protocol.

Introducing Appium.

Is native app automation missing from your tool belt? Problem solved.

Watch the Videos

Appium Philosophy

Appium is built on the idea that testing native apps shouldn’t require including an SDK or recompiling your app. And that you should be able to use your preferred test practices, frameworks, and tools. Appium is an open source project and has made design and tool decisions to encourage a vibrant contributing community.

Use your favorite tools

Appium aims to automate any mobile app from any language and any test framework, with full access to back-end APIs and DBs from test code. Write tests with your favorite dev tools using all the above programming languages, and probably more (with the Selenium WebDriver API and language-specific client libraries).

Источник

Android automation with appium

First of all, make sure developer mode is turned on in your Safari preferences so that the remote debugger port is open.

Mobile Safari on a Real iOS Device

For XCUITest

We use appium-ios-device to handle Safari since Appium 1.15. You no longer need to install additional dependencies.

For Instruments

For iOS 9.3 and below (pre-XCUITest), we use the SafariLauncher App app to launch Safari and run tests against mobile Safari. This is because Safari is an app that is owned by Apple, and Instruments cannot launch it on real devices. Once Safari has been launched by SafariLauncher , the Remote Debugger automatically connects using the ios-webkit-debug-proxy. When working with ios-webkit-debug-proxy , you have to trust the machine before you can can run tests against your iOS device.

For instruction on how to install and run ios-webkit-debugger-proxy see iOS WebKit debug proxy documentation.

Setup for an iOS real device

Before you can run your tests against Safari on a real device you will need to:

  • XCUITest and Instruments
    • Turn on web inspector on iOS device (settings > safari > advanced)
  • Only for Instruments
    • Have the ios-webkit-debug-proxy installed, running and listening on port 27753 (see the hybrid docs for instructions)
    • Make sure that SafariLauncher will work (see the SafariLauncher docs for instructions)

Running your test

To configure you test to run against safari simply set the «browserName» to be «Safari» .

Android mobile web automation

Appium supports automating the Chrome browser both real and emulated Android devices.

  • Make sure Chrome is installed on your device or emulator.
  • Chromedriver needs to be installed (a default version comes with Appium) and configured for automating the specific version of Chrome available on the device. See here for more information and details about compatibility.

Then, use desired capabilties like these to run your test in Chrome:

Note that on 4.4+ devices, you can also use the ‘Browser’ browserName cap to automate the built-in browser. On all devices you can use the ‘Chromium’ browserName cap to automate a build of Chromium which you have installed.

Troubleshooting Chromedriver

If your test target requires newer Chromedriver version, chromedriver_autodownload feature will help. It has been available since Appium 1.15.0 with the security option. Read the linked documentation to learn how to use it. chromedriverExecutableDir capability also helps when you need a specific Chromedriver version.

As of Chrome version 33, a rooted device is no longer required. If running tests on older versions of Chrome, devices needed to be rooted as Chromedriver required write access to the /data/local directory to set Chrome’s command line arguments.

If testing on Chrome app prior to version 33, ensure adb shell has read/write access to /data/local directory on the device:

There is a desired capability showChromedriverLog which, when set to true , writes the Chromedriver logs inline with the Appium logs. This can be helpful for debugging.

For more Chromedriver specific documentation see ChromeDriver documentation.

Источник

Android automation with appium

Appium’s flagship support for automating Android apps is via the UiAutomator2 driver. (New to Appium? Read our introduction to Appium drivers). This driver leverages Google’s UiAutomator2 technology to facilitate automation on a device or emulator.

Читайте также:  Sony headphones connect для андроид

Development of the UiAutomator2 driver happens at the appium-uiautomator2-driver repo.

Older Android-based drivers include: * The UiAutomator Driver

Requirements and Support

In addition to Appium’s general requirements:

  • Java 8 installed and configured correctly for your platform
  • Mac, Windows, or Linux OS with the ability to run the Android SDK
  • The minimum required Android SDK Build Tools version is 24

Further, the UiAutomator2 driver does not support Android versions below 5.0 (Lollipop, API level 21). If you are automating such versions, consider using the UiAutomator driver;

Usage

The way to start a session using the UiAutomator2 driver is to include the automationName capability in your new session request, with the value UiAutomator2 . Of course, you must also include appropriate platformName (= Android ), platformVersion , deviceName , and app capabilities, at a minimum.

It is highly recommended to also set the appPackage and appActivity capabilities in order to let Appium know exactly which package and activity should be launched for your application. Otherwise, Appium will try to determine these automatically from your app manifest.

Capabilities

The UiAutomator2 driver supports a number of standard Appium capabilities, but has an additional set of capabilities that modulate the behavior of the driver. These can be found currently at the Android section of the aforementioned doc.

For web tests, to automate Chrome instead of your own application, leave the app capability empty and instead set the browserName capability to Chrome . Note that you are responsible for ensuring that Chrome is on the emulator/device, and that it is of a version compatible with Chromedriver.

Commands

To see the various commands Appium supports, and specifically for information on how the commands map to behaviors for the UiAutomator2 driver, see the API Reference.

Basic Setup

Ensure that you have Appium’s general dependencies (e.g., Node & NPM) installed and configured.

Ensure that Java (the JDK, not just the JRE) is installed and Java binaries are added to your path. The instructions for this step differ for Mac/Linux and for Windows. Please consult platform-specific documentation, as this is a common task. An example of how to change the PATH on Windows is here.

Ensure that the JAVA_HOME environment variable is also set to the JDK path. For Mac/Linux, for example (the specifics of this path will vary greatly by system), put this in your login script:

On Windows, this will be done by setting the environment variable in the control panel, using the same strategy as for setting PATH above. Android Studio also has JDK in the path like /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home (Mac). You can specify the path, too.

Install the Android SDK. The supported way of doing this nowadays is to use Android Studio. Use the provided GUI to install the Android SDK to a path of your choosing.

Set the ANDROID_HOME environment variable to match this path. For example, if you installed the SDK to /usr/local/adt , then there will typically be a sdk folder inside of that which contains the SDK files. In that case, on Mac and Linux, add the following line to your login script (e.g.,

On Windows, follow the same steps as before to set the environment variable in the control panel.

Using the SDK manager, ensure you have installed the SDK for Android API levels you wish to automate (e.g., 24).

On Windows, ensure that you always run Appium in Administrator mode.

At this point, your general system setup is done. Follow the steps below based on whether you want to automate an emulator or a real device. In addition you will need your app’s APK (preferably built in Debug mode), whose path or URL you will use as the value of the app capability when running your tests.

Emulator Setup

To run tests on emulators, use the AVD Manager included with Android Studio or the SDK. With this tool, create the emulator that matches your needs. With the emulator launched, Appium will automatically find and use it for its tests. Otherwise, if you specify the avd capability with the value matching the name of your emulator, then Appium will attempt to launch the emulator for you.

Additional tips for emulators:

  • There exists a hardware accelerated emulator for Android, though it has its own limitations. It can be installed from Intel’s website, or through the Android SDK Manager. For more information, go here.
  • Make sure that hw.battery=yes in your AVD’s config.ini , if you want to run any of the Appium tests, or use any of the power commands. (As of Android 5.0, this is the default.)

Real Device Setup

For Android automation, no additional setup is required for testing on real devices, other than these simple requirements:

  • Ensure that Developer mode is turned on for the device.
  • Ensure that the device is connected via USB to the Appium host, and can be seen by ADB (run adb devices to make sure).
  • Ensure that «Verify Apps» in settings is disabled, to allow Appium’s helper apps to function without manual intervention.

(For some specific commands, the device might need to be rooted, though this is not the norm.)

Источник

Tutorial: Automated Testing on Android (with Appium, TestNG and Java on Mac)

This is a 2-part tutorial on automated testing that will guide you from absolute scratch to getting actual tests up and running on your local Mac with a real device.

If you are looking to do automation testing on iOS, read more here.

What is Automated Testing

For the uninitiated, automated testing is the use of an automation tool (such as a set of scripts) to perform tests on software that would usually be done by a manual tester.

Automated test scripts can be written to cover all functionality of your application for regression tests or just specific functionality of the app. Once the scripts are written, the test suite can be re-run over and over with a few clicks, on a schedule or integrated into your build process.

Читайте также:  Exposed андроид что это такое

As you can imagine, automated tests are particularly useful if you have a development process that requires testing your application from end to end repeatedly.

Part 1: Getting your environment setup with Appium and TestNG

Surprisingly, the hardest part of mobile automated testing is figuring out which tools to use, which versions to use and how to install everything so that it works seamlessly together. Sadly, it is no where near as easy as performing a few clicks to install everything you need.

This tutorial describes our recommended tools that we use for automated testing of Android applications at 2359 Media — but there are plenty of other variations that might work just as well. Here is a comprehensive list of everything you need on a fresh installation of OSX.

  • Homebrew
  • Carthage
  • Node & NPM
  • JDK
  • Android Studio and Android SDKs
  • Appium
  • Appium Doctor
  • Gradle
  • Maven
  • Eclipse
  • TestNG

Install Homebrew

Homebrew is a package management software that will make it much simpler for us to install a few other software.

To install, follow the instructions on this page: https://brew.sh/
This step will also install the Xcode Command Line Tools as part of the process.

Install Carthage

Carthage is a dependency manager. In our case, it is required by WebDriverAgent.

In terminal, enter the following:

Install Node & NPM

Node is a javascript run-time environment and npm is the node package manager. We need these because Appium is a node application.

In terminal, enter the following: (this command will install npm as well)

Install JDK and set JAVA_HOME

As we’ll be writing our tests in Java, we need the Java Development Kit (JDK)

Then double click the JDK and follow through all the installation steps.

Next, you’ll need to set JAVA_HOME in your .bash_profile. To do so, first find the location of where the JDK was installed by entering the following into terminal:

The terminal output should be along the lines of

Next edit your bash_profile by editing it in vim editor. Type the following into terminal:

Press the ‘i’ key to enter insert mode then move the cursor a new line and add the following 2 lines

Press the ‘esc’ key and then type ‘:wq’ and press enter to save and quit vim. Restart Terminal to pick up the new .bash_profile settings.

Install Android Studio & Android SDKs

We’ll need the Android SDKs next to write and run tests on Android apps and devices. The quickest way to get these installed is via Android Studio.

Download and install Android Studio from https://developer.android.com/studio/

Launch Android Studio and complete the setup process until you reach the welcome page.

From the bottom, select “Configure” > “SDK Manager” > “Android SDK”.

For the tab “SDK Platforms”, select the Android SDK Platform versions that you intent to run tests against.

For the tab “SDK Tools” and ensure the following are selected

  • Android SDK Build-tools (highest version)
  • Android SDK Platform-Tools
  • Android SDK Tools
  • Intel x86 Emulator Accelerator (HAXM installer)
  • Android Support Respository
  • Google Repository

Click “OK” to install any of the additions that you selected.

Now copy the value of the Android SDK Location at the top of the SDK Manager screen. This is the value that we’ll need to set for ANDROID_HOME in our .bash_profile.

Once again, Type the following into terminal:

Press the ‘i’ key to enter insert mode then move the cursor a new line and add the following 2 lines

Press the ‘esc’ key and then type ‘:wq’ and press enter to save and quit vim.

Restart terminal to pickup the new .bash_profile then run the following command to check that it does not throw an error:

Install Appium

Appium is an open source test automation framework for use with native, hybrid and mobile web apps. It drives iOS, Android and Windows mobile apps using the WebDriver protocol.

In terminal, enter the following:

Install Appium Doctor

Appium doctor is a mini software that checks all (well, almost all) of the preconditions for appium to run successfully.

In terminal, enter the following:

At this point you can run the appium doctor to get a sense of what else we’ll need to get straight before we really get started. To run it, enter the following into terminal:

appium-doctor’s output should look something like this:

Install Gradle

Gradle is an open-source build automation system used to create apk and jar files.

In terminal, enter the following:

Check the gradle version by entering this into the terminal:

Next edit your .bash_profile by adding this line to it (follow the steps using vim above)

(Be sure to set the version number as per the version you installed)

Install Maven

Maven is a tool used for building and managing Java-based projects.

In terminal, enter the following:

Check the maven version by entering this into the terminal:

Next edit your .bash_profile by adding this line to it (follow the steps using vim above)

(Be sure to set the version number as per the version you installed)

Install Eclipse

Eclipse is the Integrated Development Environment (IDE) that we are going to use to write, compile and run our test scripts.

Download and install Eclipse IDE for Java EE Developers from https://www.eclipse.org/downloads/packages/

Install TestNG

TestNG is a testing framework that we’ll be using to manage our automated test suite.

Launch Eclipse, select “Help” > “Install New Software” from the menu bar.

Paste “http://beust.com/eclipse” into the “Work with:” field and press Enter. Eclipse will take a while to fetch the relevant package to display, select the checkbox next to TestNG and click “Next” to complete the dialogs. Eclipse will take a while to install the TestNG package and then prompt you to restart Eclipse.

Alright! Let’s….get started?

Part 2: Writing your test cases

If you’ve completed all the steps in Part 1 of this tutorial, your environment should be ready to create and run some automated test scripts. This section will guide you through the following steps:

  • Setting up your project
  • Writing a basic setup script
  • Running your basic tests on a real device
  • Writing actual tests!
Читайте также:  Таймер для андроид приставок

Setup Your Project

Launch Eclipse, select “File” > “New” > “Maven Project” and follow through the dialogs with the default settings.

You will be required to enter a Group Id and an Artifact Id. The Group ID will uniquely identity your project across all projects. The Artifact ID is the name of the jar (without version). We recommend reverse-domain-packages for the Group ID (e.g. com.2359media.automation) and the project name for the Artifact ID (e.g. sampleproject). The basic maven project should look like this.

Next, let’s add the required JARs to the project. Double click on the pom.xml file and edit the … block to look like this:

This defines the dependancies for maven to download into this project. Right click the project, select “Maven > Update Project…” to trigger downloading the dependencies. Once this is done, you should see a lot more .jar files in the Maven Dependencies dropdown of the Eclipse Project Explorer.

Write A Basic Setup Script

In the Eclipse file explorer, expand “src/test/java” and expand the package. Double click the file “AppTest.java” and delete all its contents except the first line. Paste the following code into the file under the first line.

Here’s a brief rundown on what this all means

  • //1 — Functions that have the @BeforeSuite annotation will be run before every test suite run. Such a function can be used to setup the application into the right state/condition before the individual test cases are run.
  • //2 — We set the URL to the localhost where we will be running appium
  • //3 — Setup an object that will hold the Appium Capabilities
  • //4 — Initialise the driver with the URL and Capabilities object. This driver will be the main object used to interact with the device
  • //5 — Functions that have the @AfterSuite annotation will be run after every test suite run. These functions should be used to clean up any changes done on the device before the next test suite run.
  • //6 — Functions that start with @Test will be identified as TestNG tests. you can enable or disable a test in the test suite by toggling the value in (enabled=true). This test at the moment will just reset the application and no nothing else.

If you’ve set everything up correctly, you should be able to right click on the myFirstTest method, and see “Run As > 1 TestNG Test”. If you select that now you will see a bunch of errors in the console. That’s because we have yet to start Appium and connect our device.

Setup To Run A Test On A Real Device

Connect an Android device to your computer. On the Android phone, you’ll need to enable USB Debugging. If the phone prompts you to Allow USB debugging for the computer that it’s connected to, allow it.

To save yourself a bit of headache you should set the device settings to sleep after 5 minutes of inactivity. This is to prevent the phone from locking before the script runs or during its run.

Check that the device is correctly connected by launching terminal and entering the following command:

You should see one device connected like this:

Next, in terminal start the appium server with the following command

You should get some output that indicates the appium server is listening:

Return to your project in Eclipse and right click on the myFirstTest method, and select “Run As > 1 TestNG Test”. It’ll take a few minutes and you’ll see the device screen flicker a few times and hopefully… Voila!

Assuming you see the successful test case run, what test script did was install the application, restart it a couple times and then uninstall the application. Not very useful, but its working! Now let’s get to doing something productive with this script.

Write An Actual Test

The general flow of an appium automated test is to find an element and then either interact with it or assert something about it.

Let’s install the Material Dialogs APK to the device first to take a look at what we can test. Download it to your local system from here and install it into your device using adb

Here is one flow from the application that we might want to test, written as a Gherkin story.

To identify elements, the Android SDK provides a tool called uiautomatorviewer. Launch Terminal and navigate to the location of the Android SDK and launch uiautomatorviewer with the following command:

(Be sure to change “2359media” to your own username)

Once the uiautomatorviewer is launched, connect your android device to the computer and launch the app on the Android device. Now click the device button on the top left of uiautomatorviewer to grab the view hierarchy, elements and screenshot.

Using this tool, we can identify the elements that we need to write the following test case. Add this function to the AppTest class:

Right click on the test code and run it to see it in action (don’t forget to start the appium server again if you previously stopped it).

Running a set of test cases as a test suite

Test cases should be as modular and granular as possible. Ideally each test case will test only test a specific individual function. So that when you run the test suite, you can view the report and identify which functions exactly have broken.

In the last part of this tutorial, we’re going to show you how to create a test suite to run all your test cases together.

Let’s add a second test function to

Next, in the Project Explorer, right click the project and click “New > File”. Create a file called testsuite.xml. Edit testsuite.xml and paste in the following contents.

Be sure to replace your package name accordingly

Save the file and right click anywhere in the file contents, select “Run As > TestNG Suite”. In the Eclipse test report you should see 2 successful test runs.

Источник

Оцените статью