标签:
转自:https://university.utest.com/how-to-set-up-your-first-android-automation-project-with-appium/
Appium is an open-source tool for automating native, mobile web, and hybrid applications on iOS and Android platforms. Native apps are those written using the iOS or Android SDKs. Mobile web apps are web apps accessed using a mobile browser (Appium supports Safari on iOS and Chrome or the built-in ‘Browser’ app on Android). Hybrid apps have a wrapper around a “webview” — a native control that enables interaction with web content. Projects likePhonegap, make it easy to build apps using web technologies that are then bundled into a native wrapper, creating a hybrid app.
Importantly, Appium is cross-platform. It allows you to write tests against multiple platforms (iOS and Android), using the same API. This enables code reuse between iOS and Android test suites.
For this example, I will use a contact manager app provided by Appium as a sample app and a Samsung Galaxy S4 with Android 4.4.2. Get Contact Manager at GitHub.
1. The first step in writing a new test class is to import the needed libraries:
//used to verify if URL is malformed
import java.net.MalformedURLException;
//library used to create URL for the Appium server
import java.net.URL;
//library used to create the path to APK
import java.io.File;
//library used to find elements (by id, class, xpath etc)
import org.openqa.selenium.By;
//library for web element
import org.openqa.selenium.WebElement;
//libraries for configuring Desired Capabilities
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
//library for test methods
import org.junit.*;
//library for Appium drivers
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
Since the apk is stored in the computer and is not already installed on the device we need to create a file object which represents the actual apk file on the disk. I placed the folder ContactManager which contains the apk file inside the Eclipse project.
File classpathRoot = new File(System.getProperty(“user.dir”)); // path to Eclipse project
File appDir = new File(classpathRoot, “/ContactManager”); // path to <project folder>/Contact Manager
File app = new File(appDir, “ContactManager.apk”); path to <project folder>/Contact Manager/ContactManager.apk
To be able to test the app on an actual device Desired Capabilities need to be set. Desired Capabilities are a set of keys and values sent to the Appium server to tell the server what kind of automation session we’re interested in starting up. There are also capabilities used to modify the behaviour of the server during automation.
DesiredCapabilities capabilities = new DesiredCapabilities();
//Name of mobile web browser to automate. Should be an empty string if automating an app instead.
capabilities.setCapability(CapabilityType.BROWSER_NAME, “”);
//which mobile OS to use: Android, iOS or FirefoxOS
capabilities.setCapability(“platformName”, “Android”);
//Mobile OS version – in this case 4.4 since my device is running Android 4.4.2
capabilities.setCapability(CapabilityType.VERSION, “4.4”);
//device name – since this is an actual device name is found using ADB
capabilities.setCapability(“deviceName”, “111bd508″);
//the absolute local path to the APK
capabilities.setCapability(“app”, app.getAbsolutePath());
//Java package of the tested Android app
capabilities.setCapability(“appPackage”, “com.example.android.contactmanager”);
// activity name for the Android activity you want to run from your package. This need to be preceded by a . (example: .MainActivity)
capabilities.setCapability(“appActivity”, “.ContactManager”);
// constructor to initialize driver object
driver = new AndroidDriver(new URL(“http://127.0.0.1:4723/wd/hub”), capabilities);
Since we are trying to run the test on an actual device we need to use ADB to find the device name. This is required when creating desired capabilities to specify on what device you want your tests to be run. You can use this course to see how to use “adb devices” to find your device id. Once you run adb devices command the only thing left is to copy the device id in the code.
Since Appium server is running and all the settings for testing on the actual phone are set we are ready to write test methods:
@Test
public void addContact() throws exception{
// locate Add Contact button and click it
WebElement addContactButton = driver.findElement(By.name(“Add Contact”));
addContactButton.click();
//locate input fields and type name and email for a new contact and save it
List<WebElement> textFieldsList = driver.findElementsByClassName(“android.widget.EditText”);
textFieldsList.get(0).sendKeys(“Some Name”);
textFieldsList.get(2).sendKeys(“Some@example.com”);
driver.findElementByName(“Save”).click();
//insert assertions here
}
Since this is the first basic test to see how and if the automation is working there are no assertions in the code. You can modify the code and add assertions to see if contact was added successfully. Learn more about assertions in this uTest University course.
The entire code:
package tests;
import java.net.MalformedURLException;
import java.net.URL;
import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.junit.*;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
public class TestOne {
private AndroidDriver driver;
@Before
public void setUp() throws MalformedURLException{
File classpathRoot = new File(System.getProperty(“user.dir”));
File appDir = new File(classpathRoot, “/ContactManager”);
File app = new File(appDir, “ContactManager.apk”);
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, “”); //Name of mobile web browser to automate. Should be an empty string if automating an app instead.
capabilities.setCapability(“platformName”, “Android”);
capabilities.setCapability(CapabilityType.VERSION, “4.4”);
capabilities.setCapability(“deviceName”, “111bd508″);
capabilities.setCapability(“app”, app.getAbsolutePath());
capabilities.setCapability(“appPackage”, “com.example.android.contactmanager”);
capabilities.setCapability(“appActivity”, “.ContactManager”);
driver = new AndroidDriver(new URL(“http://127.0.0.1:4723/wd/hub”), capabilities);
}
@Test
public void addContact() throws Exception {
WebElement addContactButton = driver.findElement(By.name(“Add Contact”));
addContactButton.click();
List<WebElement> textFieldsList = driver.findElementsByClassName(“android.widget.EditText”);
textFieldsList.get(0).sendKeys(“Some Name”);
textFieldsList.get(2).sendKeys(“Some@example.com”);
driver.findElementByName(“Save”).click();
//insert assertions here
}
标签:
原文地址:http://www.cnblogs.com/melody-emma/p/4682628.html