Earlier, I have built the environment required by appium. In this article, I use the calculator app that comes with the system as the test object to learn.
1. Connect the tester to the PC
(I am using a real machine) Connect the real machine to the PC through the Usb interface. We have installed the SDK in the appium installation environment, so open the CMD command window and use the adb devices
command to check whether the mobile device can be detected. If The following figure shows the device, which proves that we can test the mobile phone.
Make a note of the device name, which the test code will use.
2. Open the specified Activity of the specified test App
Through the previous operation, I have seen that my device can be operated, so how should appium automatically find the specified APP and specified Activity? If you want to open a specified App, you need to know the package name of the App. Similarly, if you want to open a specified Activity, you also need to know its name. How to get it?
1. Ask the company's developers, it must be reliable.
2. Obtain by command: adb shell dumpsys window w |findstr \/ |findstr name=
(The function of this command is to obtain the name of the current app and current Activity. I have already opened the calculator application on my mobile phone) as shown in the figure:
3. Open uiautomatorviewer to get elements
Next, you need uiautomatorviewer
to grab the elements (button, text Field) of the test app, such as id, class, and text to determine specific elements. uiautomatorviewer is a tool in android-sdk, specific directory \android-sdk-windows\tools\uiautomatorviewer.bat
.
operate:
1. Double-click uiautomatorviewer.bat to open the UI Automator viewer window, as shown in the figure:
2. When the mobile phone is connected to the PC, click "Device screenshot", and the window to connect the mobile phone application will be displayed, as shown in the figure:
3. To test the use case of "1+2=3", in the uiautomatorviewer interface, click "1", select the button, and check the panel on the right to find the value of "text" or "resource-id" or "class", as shown in the figure :
save it: resource-id
save it: resource-id
save it: resource-id
save it: resource-id
Fourth, create a java project
We have obtained the desired elements, the next step is to write test cases with code, open the eclipse software, create a java project, and import the dependencies, as shown in the figure:
Five, test the source code
Code to achieve 1+2=3 automated testing
package com.occall.ui;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import java.net.URL;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
public class loginOccall2 {
private AppiumDriver driver;
@BeforeClass
public void setup() throws Exception {
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(CapabilityType.BROWSER_NAME, "");
cap.setCapability("platformName", "Android"); //指定测试平台
cap.setCapability("deviceName", "71MBBLG26R76"); //指定测试机的ID,通过adb命令`adb devices`获取
cap.setCapability("platformVersion", "5.1");
//将上面获取到的包名和Activity名设置为值
cap.setCapability("appPackage", "com.meizu.flyme.calculator");
cap.setCapability("appActivity", "com.meizu.flyme.calculator.Calculator");
//A new session could not be created的解决方法
cap.setCapability("appWaitActivity","com.meizu.flyme.calculator.Calculator");
//每次启动时覆盖session,否则第二次后运行会报错不能新建session
cap.setCapability("sessionOverride", true);
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);
}
@Test
public void plus(){
//获取1
driver.findElementById("com.meizu.flyme.calculator:id/digit1").click();
//获取+
driver.findElementById("com.meizu.flyme.calculator:id/plus").click();
//获取2
driver.findElementById("com.meizu.flyme.calculator:id/digit2").click();
//获取=
driver.findElementById("com.meizu.flyme.calculator:id/eq").click();
}
@AfterClass
public void tearDown() throws Exception {
driver.quit();
}
}
6. Start Appium
The mobile device is connected to the PC, and the screen of the mobile phone is turned on. Then start Appium, the startup is successful as shown below:
Seven, run the test class
Eight, you're done, pay attention to check the running log and appium console log
It can be seen that the calculator app on the mobile phone will also automatically appear the automatic input test of 1+2 = 3
http://www.cnblogs.com/stonewang313/p/3986465.html