1、啓動測試機或者Android模擬器(Genymotion俗稱世界上最快的模擬器,可自行百度安裝)java
2、啓動Appium(Appium環境安裝可自行百度)
android
3、安裝應用到Genymotion上,以下圖我安裝一個計算機的小應用,包名爲CalcTest.apkgit
安裝步驟:(基於Android SDK已經配置好了環境變量,可自行百度)
一、Win + R
二、CMD
三、adb devices --檢查操做,列出存在的設置名稱
四、adb install F:\Appium\CalcTest.apk --正式安裝Appchrome
測試apk下載地址:https://files.cnblogs.com/files/yyym/CalcTest.apkapp
以下圖:192.168.229.101:5555就是我剛開啓的Genymotion虛擬機
ide
4、安裝成功以後回到Genymotiong能夠看到已經安裝成功了
打開該應用,能夠看到實際是個簡單的計算器工具
5、打開Eclipse建立Maven項目並使用uiautomatorviewer工具(Android SDK工具包自帶的)進行基本元素定位操做,元素定位方式前面咱們已經詳細講解過了。
一、打開Android SDK可找到路徑:android-sdks\tools以下(獲取App包名可反編譯:aapt dump badging apk路徑)
二、打開uiautomatorviewr.bat測試
三、編寫基本代碼以下僅供參考:ui
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
package
appium_demo;
import
java.net.MalformedURLException;
import
java.net.URL;
import
java.util.concurrent.TimeUnit;
import
org.openqa.selenium.By;
import
org.openqa.selenium.remote.DesiredCapabilities;
import
io.appium.java_client.android.AndroidDriver;
/** * @author 李小衛 E-mail:yyymlxw@163.com @date 建立時間2018年2月11日上午10:10:02 */
public
class
calc_demo {
public
static
void
main(String[] args)
throws
MalformedURLException {
AndroidDriver driver;
DesiredCapabilities des =
new
DesiredCapabilities();
// des.setCapability("automationName", "Appium");//Selendroid //自動化的模式選擇
// des.setCapability("app", "C:\\software\\CalcTest.apk");//配置待測試的apk的路徑
// des.setCapability("browserName", "chrome"); //h5
des.setCapability(
"platformName"
,
"Android"
);
//平臺名稱
des.setCapability(
"platformVersion"
,
"4.4"
);
//手機操做系統版本
des.setCapability(
"udid"
,
"192.168.229.101:5555"
);
//鏈接的物理設備的惟一設備標識
des.setCapability(
"deviceName"
,
"S4"
);
//使用的手機類型或模擬器類型 UDID
des.setCapability(
"appPackage"
,
"com.sky.jisuanji"
);
//App安裝後的包名,注意與原來的CalcTest.apk不同
des.setCapability(
"appActivity"
,
".JisuanjizixieActivity"
);
//app測試人員經常要獲取activity,進行相關測試,後續會講到
des.setCapability(
"unicodeKeyboard"
,
"True"
);
//支持中文輸入
des.setCapability(
"resetKeyboard"
,
"True"
);
//支持中文輸入
des.setCapability(
"newCommandTimeout"
,
"10"
);
//沒有新命令時的超時時間設置
des.setCapability(
"nosign"
,
"True"
);
//跳過檢查和對應用進行 debug 簽名的步驟
driver =
new
AndroidDriver(
new
URL(
"http://127.0.0.1:4723/wd/hub"
), des);//虛擬機默認地址
driver.manage().timeouts().implicitlyWait(
10
, TimeUnit.SECONDS);
//設置超時等待時間,默認250ms
driver.findElement(By.id(
"com.android.calculator2:id/digit1"
)).click();
//定位'1'
driver.findElement(By.id(
"com.android.calculator2:id/plus"
)).click();
//定位'+'
driver.findElement(By.id(
"com.android.calculator2:id/digit6"
)).click();
//定位'6'
driver.findElement(By.id(
"com.android.calculator2:id/equal"
)).click();
//定位'='
}
}
|
6、使用TestNG編寫正式測試用例並開始執行測試了url
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
package
appium_operate;
import
java.net.MalformedURLException;
import
java.net.URL;
import
java.util.concurrent.TimeUnit;
import
org.openqa.selenium.By;
import
org.openqa.selenium.remote.DesiredCapabilities;
import
org.testng.Assert;
import
org.testng.annotations.BeforeTest;
import
org.testng.annotations.DataProvider;
import
org.testng.annotations.Test;
import
io.appium.java_client.android.AndroidDriver;
/** * @author 李小衛 E-mail:yyymlxw@163.com @date 建立時間2018年2月11日上午10:30:02 */
public
class
CalcTest {
AndroidDriver driver;
@BeforeTest
public
void
setUp()
throws
MalformedURLException{
DesiredCapabilities des =
new
DesiredCapabilities();
// des.setCapability("app", "c:\\");
des.setCapability(
"platformName"
,
"Android"
);
des.setCapability(
"platformVersion"
,
"4.4"
);
des.setCapability(
"udid"
,
"192.168.43.101:5555"
);
des.setCapability(
"deviceName"
,
"s4"
);
des.setCapability(
"appPackage"
,
"com.android.calculator2"
);
//com.android.contacts
des.setCapability(
"appActivity"
,
".Calculator"
);
//.activities.PeopleActivity
des.setCapability(
"unicodeKeyboard"
,
"True"
);
des.setCapability(
"resetKeyboard"
,
"True"
);
des.setCapability(
"newCommandTimeout"
,
"15"
);
des.setCapability(
"nosign"
,
"True"
);
driver =
new
AndroidDriver(
new
URL(
"http://127.0.0.1:4723/wd/hub"
),des);
driver.manage().timeouts().implicitlyWait(
10
, TimeUnit.SECONDS);
}
@Test
(enabled =
false
)
public
void
add() {
driver.findElement(By.xpath(
"//android.widget.Button[@text='5']"
)).click();
driver.findElement(By.xpath(
"//android.widget.Button[@text='+']"
)).click();
driver.findElement(By.xpath(
"//android.widget.Button[@text='8']"
)).click();
driver.findElement(By.xpath(
"//android.widget.Button[@text='=']"
)).click();
String value = driver.findElement(By.xpath(
"//android.widget.EditText[@class='android.widget.EditText']"
)).getAttribute(
"text"
);
Assert.assertEquals(value,
"13"
);
}
@Test
(enabled =
false
)
public
void
sub() {
driver.findElement(By.xpath(
"//android.widget.Button[@text='1']"
)).click();
driver.findElement(By.xpath(
"//android.widget.Button[@text='0']"
)).click();
driver.findElement(By.xpath(
"//android.widget.Button[@text='-']"
)).click();
driver.findElement(By.xpath(
"//android.widget.Button[@text='8']"
)).click();
driver.findElement(By.xpath(
"//android.widget.Button[@text='=']"
)).click();
String value = driver.findElement(By.xpath(
"//android.widget.EditText[@class='android.widget.EditText']"
)).getAttribute(
"text"
);
Assert.assertEquals(value,
"2"
);
}
@Test
(enabled =
false
)
public
void
mul() {
driver.findElement(By.xpath(
"//android.widget.Button[@text='5']"
)).click();
driver.findElement(By.xpath(
"//android.widget.Button[@text='×']"
)).click();
driver.findElement(By.xpath(
"//android.widget.Button[@text='8']"
)).click();
driver.findElement(By.xpath(
"//android.widget.Button[@text='=']"
)).click();
String value = driver.findElement(By.xpath(
"//android.widget.EditText[@class='android.widget.EditText']"
)).getAttribute(
"text"
);
Assert.assertEquals(value,
"40"
);
}
@DataProvider
(name=
"testdata"
)
public
Object[][] getData(){
return
new
Object[][]{{
"20"
,
"80"
,
"100"
,
"+"
},{
"90"
,
"3"
,
"270"
,
"×"
},{
"6"
,
"2"
,
"3"
,
"÷"
}};
}
@Test
(dataProvider =
"testdata"
)
public
void
calcTestcase(String num1,String num2,String result,String calcType){
for
(
char
num:num1.toCharArray()){
driver.findElement(By.xpath(
"//android.widget.Button[@text='"
+String.valueOf(num)+
"']"
)).click();
}
driver.findElement(By.xpath(
"//android.widget.Button[@text='"
+calcType+
"']"
)).click();
for
(
char
num:num2.toCharArray()){
driver.findElement(By.xpath(
"//android.widget.Button[@text='"
+String.valueOf(num)+
"']"
)).click();
}
driver.findElement(By.xpath(
"//android.widget.Button[@text='=']"
)).click();
String value = driver.findElement(By.xpath(
"//android.widget.EditText[@class='android.widget.EditText']"
)).getAttribute(
"text"
);
Assert.assertEquals(value, result);
}
}
|