16
Mobile Automation. Automation for Android.

Mobile Automation. Automation for Android

Embed Size (px)

Citation preview

Mobile Automation. Automation for Android.

Scope:

1. What is automation?

2. What problems could be solved using automation?

3. Automation tool Appium.

- What is appium, how could it be used?

- Environment setup.

- How does it work?!

- How to detect elements on screen?

- How to create selectors for elements?

- How to get application activity and application package?

- Examples of simple test. (Based on Page Object Pattern)

- Example of actions creation.

4. Future tasks.

What is automation?

Test automation is the use of special software (separate from the software being tested) to control the execution of tests and the comparison of actual outcomes with predicted outcomes. Test automation can automate some repetitive but necessary tasks in a formalized testing process already in place, or add additional testing that would be difficult to perform manually.

Test automation is required for: - Verify main features functionality (regression testing)- Minimize QA involvement- Minimize ‘Human factor’ - Automation Does What Manual Testing Cannot

- Automated QA Testing Helps Developers and Testers

- Save time and money.

What is it required?

What problems could be solved using automation?

- Detect issue related to all parts of functionality- Detect rarely reproducible issues or verify that they are not present any more- Simulate multi users stories running through one test- Could be used as KPI for build reporting stability - Could be used as preventive action for customer complain- Could be used as a mandatory requirement for success ‘push’ on remote

repository.

Possible usage in our company:

Automation tool Appium. Automation for Android.

Appium.

Appium is an open-source tool for automating native, mobile web, and hybrid applications on iOS and Android platforms. … http://appium.io/slate/en/master/?java#

Android webview: Native app: Google chrome:

1. Environment setup.

Main points:

Appium could run your tests on different Android device using 2 vendor-provided frameworks:

- Android 4.2+: Google’s UiAutomator (Device API higher or equal 17).

- Android 2.3+: Google’s Instrumentation. (Instrumentation support is provided by bundling a

separate project, Selendroid).

Full information about Appium Environment configuration and software required : http://appium.io/slate/en/master/?java#appium-on-real-android-devices

Device configuration:

http://www.androidcentral.com/all-about-your-phones-developer-options

- Apium could run on real device or on emulator.

- Developers options / USB debugging should be turned on device.

Full information about appium configuration could be easily found on Appium website, links are

given below.

How does it work?!

3. How to detect elements on screen? To detect elements on screen is used UiAutomator Viewer. It is capturing device screen and showing all elements located on it, so id/className/packageName or parent is visible.

Connect your device to PC.Run 'uiautomatorviewer'

in command line. 'Click Device screenshot'

Device screen should be captured.

All screen hierarchy should be shown.

So, with UiAutomatoViewer you can find all necessary information about element ...

Hierarchy information

Elementinformation

If element don't have unique identifier you can find it using uISelector.

4. How to create selectors for elements?

Find by 'ID':

Find by 'className':

@AndroidFindBy(className = "")WebElement element;

@AndroidFindBy(uiAutomator = "new iSelector().className(\"android.widget.ImageButton\")")

WebElement functionButton;

Find by 'UiAuomator':

Best case is if you are having unique className selector or unique ID value for element. But it is rare cases.

@AndroidFindBy(uiAutomator = "new UiSelector().className(\"android.view.View\").index(3)"+".childSelector(new UiSelector().className(\"android.widget.EditText\"))")WebElement passwordCP;

Detailed list of all UiSelector: http://developer.android.com/tools/help/uiautomator/UiSelector.html

Sometimes even UiSelector is not enough. So you need to move up/down in hierarchy tree and

that is possible with some 'advanced' UiSelectors.

@AndroidFindBy(id = APP_PACKAGE + ":id/abs_title_custom")WebElement element;

DesiredCapabilities capabilities = new DesiredCapabilities();capabilities.setCapability("browserName", "");

capabilities.setCapability("deviceName", "192.168.56.101:5555");

capabilities.setCapability("platformName", "Android");capabilities.setCapability("platformVersion", "4.4.4");capabilities.setCapability("app", app.getAbsolutePath());capabilities.setCapability("app_package", apk_package.com);

capabilities.setCapability("appActivity", "apk_start_activity.SplashActivity");

capabilities.setCapability("appWaitActivity","aplication_wait_.activity.LoginActivity");

5. Appium capabilities. To start execution of test on appium, first you need to create appium driver.

Java code:

driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);

driver.manage().timeouts().implicitlyWait(70, TimeUnit.SECONDS);

public AndroidDriver driver;

....

...

Appium driver can start one of the installed browsers and start execution in it. Required for website testing.

Appium driver can start one of the installed browsers and start execution in it. Required for website testing.

File classPathRood = new File("C:\\Users\\User\\Desktop\\Folder with apk");File app = new File(classPathRood, "some test.apk");

...

Activity that appium will wait for to start the test

http://127.0.0.1:4723/wd/hub - Port on which appium server is started. IP could be easily found in appium configurations.

Value 700 means, that Appium will wait for element to be appeared for 70 seconds. Before failing test.

Aplikation package

Activity that appium will start on device

There are different types of capabilities that could be found by the link

http://appium.io/slate/en/master/?java#appium-server-capabilities

1. Open hierarchy_viewer window and click 'refresh'.

2. Start necessary activity on device. After starting hierarchy viewer will show starting (splash) activity

once application will start it will show 'wait' activity.

appActivity and apcWaitActivity could be same.

6. How to get application activity and application

package?

As was shown on previous slide there are several capabilities that need to be defined before starting test.

capabilities.setCapability("app_package", apk_package.com); capabilities.setCapability("appActivity", "apk_start_activity.SplashActivity");capabilities.setCapability("appWaitActivity","aplication_wait_.activity.LoginActivity");

To get this data, could be used depricated tool 'hierarchyviewer' could be used. It is provided with Android SDK.

apk. package appActivity

apcWaitActivity

Examples of simple test.

https://saucelabs.com/resources/mobile-test-automation-in-java-with-appium

There are a lot of simple test examples in web. Main of them:

http://appium.io/slate/en/tutorial/android.html?java#introduction29

http://appium.io/slate/en/tutorial/android.html?java#page-object-pattern

@Testpublic void test1SwitchingAmongProviders() {

screenShotRule.setTestName(name.getMethodName());HomeScreen homeScreen = new HomeScreen(driver);homeScreen.swtichAmongProvidersCheck();

}

public void swtichAmongProvidersCheck() {assertEquals(providerTitle.getText(), "My Cloud providers");openFirstProvider();backButton.click();openSecondProvider();backButton.click();

}

@AndroidFindBy(id = APP_PACKAGE + ":id/action_bar_ic_toolbar_logo")public static WebElement backButton;

@AndroidFindBy(id = APP_PACKAGE + ":id/abs_title_custom")

public static WebElement providerTitle;

public void openSecondProvider() {provider2.click();waitForElement(SIMPLEWAIT, providerTitle);assertEquals(providerTitle.getText(), "Automation Test");

}

Example of action creation:

public static void swipeElementToRight(WebElement element) {int leftX = element.getLocation().getX();int rightX = leftX + element.getSize().getWidth();int lowerY = element.getLocation().getY();int upperY = lowerY + element.getSize().getHeight();int middleY = lowerY + (upperY - lowerY) / 2;driver.swipe(leftX + 5, middleY, rightX - 5, middleY, 500);

}

public static void swipeScreenToLeft() {int leftX = driver.findElementByAndroidUIAutomator("new

UiSelector().className(\"android.widget.FrameLayout\")").getLocation().getX();int rightX = leftX + driver.findElementByAndroidUIAutomator("new

UiSelector().className(\"android.widget.FrameLayout\")").getSize().getWidth();int lowerY = driver.findElementByAndroidUIAutomator("new

UiSelector().className(\"android.widget.FrameLayout\")").getLocation().getY();int upperY = lowerY + driver.findElementByAndroidUIAutomator("new

UiSelector().className(\"android.widget.FrameLayout\")").getSize().getHeight();int middleY = lowerY + (upperY - lowerY) / 2;driver.swipe(rightX - 5, middleY, leftX + 5, middleY, 500);

}

public static void longClickOnElement(WebElement element) {int leftX = element.getLocation().getX();int rightX = leftX + element.getSize().getWidth();int lowerY = element.getLocation().getY();int upperY = element.getSize().getHeight() + lowerY;int middleX = leftX + (rightX - leftX) / 2;int middleY = lowerY + (upperY - lowerY) / 2;driver.tap(1, middleX, middleY, 1000);

}

Future Tasks....

Thanks.