原文地址http://www.cnblogs.com/tobecrazy/p/4592405.html
原文地址http://www.cnblogs.com/tobecrazy/
該博主有很多干貨,可以多去研究研究
adb基本命令總結(Android Debug Bridge)
adb 是PC和設備連接的橋梁,可以通過adb對devices進行相關操作
- adb devices 列出你的devices
- adb kill-server 殺掉adb服務(如果設備連接出問題,可嘗試)
- adb start-server 重啟adb服務
- adb shell 進入默認device的Linux shell,可以直接執行Linux命令
- adb shell screenrecord /sdcard/runCase.mp4 錄制視頻保存,默認3min,也可以加--time-limit 60限制時間
- adb install jd.apk 向設備安裝app
- adb pull /sdcard/runCase.mp4 c:// 把手機中文件copy到電腦
- adb push C://runCase.mp4 /sdcard/ 把電腦中文件放到手機
以上就是一些基本的adb 命令
利用adb命令先切換為自己的輸入法,按了搜索再切換為appium的輸入法
查看當前手機的輸入法
cmd執行下面的的代碼
adb shell ime list -s
可以看到類似下面的結果,
C:\Users\LITP>adb shell ime list -s com.baidu.input_mi/.ImeService com.sohu.inputmethod.sogou.xiaomi/.SogouIME io.appium.android.ime/.UnicodeIME C:\Users\LITP>
執行adb命令
先寫好一個執行cmd的方法
/** * 執行adb命令 * @param s 要執行的命令 */ private void excuteAdbShell(String s) { Runtime runtime=Runtime.getRuntime(); try{ runtime.exec(s); }catch(Exception e){ print("執行命令:"+s+"出錯"); } }
在需要搜索的時候執行下面的代碼,切換的輸入法用自己查看列表的輸入法內容,我這里是搜狗輸入法
//使用adb shell 切換輸入法-更改為搜狗拼音,這個看你本來用的什么輸入法 excuteAdbShell("adb shell ime set com.sohu.inputmethod.sogou.xiaomi/.SogouIME"); //再次點擊輸入框,調取鍵盤,軟鍵盤被成功調出 clickView(page.getSearch()); //點擊右下角的搜索,即ENTER鍵 pressKeyCode(AndroidKeyCode.ENTER); //再次切回 輸入法鍵盤為Appium unicodeKeyboard excuteAdbShell("adb shell ime set io.appium.android.ime/.UnicodeIME");
appium實現截圖
由於我有webdriver 的基礎,理解起來比較easy,appium截圖實際是繼承webdriver的
selenium 中使用的是TakesScreenShot接口getScreenShotAs方法,具體實現如下
1 /** 2 * This Method create for take screenshot 3 * 4 * @author Young 5 * @param drivername 6 * @param filename 7 */ 8 public static void snapshot(TakesScreenshot drivername, String filename) { 9 // this method will take screen shot ,require two parameters ,one is 10 // driver name, another is file name 11 12 String currentPath = System.getProperty("user.dir"); // get current work 13 // folder 14 File scrFile = drivername.getScreenshotAs(OutputType.FILE); 15 // Now you can do whatever you need to do with it, for example copy 16 // somewhere 17 try { 18 System.out.println("save snapshot path is:" + currentPath + "/" 19 + filename); 20 FileUtils 21 .copyFile(scrFile, new File(currentPath + "\\" + filename)); 22 } catch (IOException e) { 23 System.out.println("Can't save screenshot"); 24 e.printStackTrace(); 25 } finally { 26 System.out.println("screen shot finished, it's in " + currentPath 27 + " folder"); 28 } 29 }
調用: snapshot((TakesScreenshot) driver, "zhihu_showClose.png");
你可以在捕獲異常的時候調用,可以實現錯誤截圖,做測試結果分析很有效
appium清空EditText(一個坑)
在使用appium過程中,發現sendkeys和clear方法並不太好使,只好自己封裝模擬手工一個一個刪除
這里用到keyEvent,具體內容請參考api http://appium.github.io/java-client/
要刪除一段文字,該怎么做:
1. 獲取文本長度
2. 移動到文本最后
3. 按下刪除按鈕,直到和文本一樣長度
移動到文本最后: 123刪除67
public static final int |
BACKSPACE |
67 |
public static final int |
DEL |
67 |
public static final int |
KEYCODE_MOVE_END |
123 |
實現代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
|
/**
* This method for delete text in textView
*
* @author Young
* @param text
*/
public
void
clearText(String text) {
driver.sendKeyEvent(
123
);
for
(
int
i =
0
; i < text.length(); i++) {
driver.sendKeyEvent(
67
);
}
}
|
整個case的代碼貼一下
初始化driver,執行cmd.exe /C adb shell screenrecord /sdcard/runCase.mp4 開始錄制測試視頻
執行login
執行修改知乎的個人介紹
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
package
com.dbyl.core;
import
org.apache.commons.io.FileUtils;
import
org.openqa.selenium.By;
import
org.openqa.selenium.OutputType;
import
org.openqa.selenium.TakesScreenshot;
import
org.openqa.selenium.WebElement;
import
org.openqa.selenium.remote.CapabilityType;
import
org.openqa.selenium.remote.DesiredCapabilities;
import
org.testng.Assert;
import
org.testng.annotations.AfterClass;
import
org.testng.annotations.BeforeClass;
import
org.testng.annotations.Test;
import
io.appium.java_client.android.AndroidDriver;
import
java.io.File;
import
java.io.IOException;
import
java.net.URL;
import
java.util.List;
import
java.util.concurrent.TimeUnit;
public
class
zhiHu {
private
AndroidDriver driver;
/**
* @author Young
* @throws IOException
*/
public
void
startRecord()
throws
IOException {
Runtime rt = Runtime.getRuntime();
// this code for record the screen of your device
rt.exec(
"cmd.exe /C adb shell screenrecord /sdcard/runCase.mp4"
);
}
@BeforeClass
(alwaysRun =
true
)
public
void
setUp()
throws
Exception {
// set up appium
File classpathRoot =
new
File(System.getProperty(
"user.dir"
));
File appDir =
new
File(classpathRoot,
"apps"
);
File app =
new
File(appDir,
"zhihu.apk"
);
DesiredCapabilities capabilities =
new
DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME,
""
);
capabilities.setCapability(
"platformName"
,
"Android"
);
capabilities.setCapability(
"deviceName"
,
"Android Emulator"
);
capabilities.setCapability(
"platformVersion"
,
"4.4"
);
// if no need install don't add this
capabilities.setCapability(
"app"
, app.getAbsolutePath());
capabilities.setCapability(
"appPackage"
,
"com.zhihu.android"
);
// support Chinese
capabilities.setCapability(
"unicodeKeyboard"
,
"True"
);
capabilities.setCapability(
"resetKeyboard"
,
"True"
);
// no need sign
capabilities.setCapability(
"noSign"
,
"True"
);
capabilities.setCapability(
"appActivity"
,
".ui.activity.GuideActivity"
);
driver =
new
AndroidDriver(
new
URL(
"http://127.0.0.1:4723/wd/hub"
),
capabilities);
startRecord();
}
@Test
(groups = {
"login"
})
public
void
login() {
// find login button
WebElement loginButton = driver.findElement(By
.id(
"com.zhihu.android:id/login"
));
loginButton.click();
// wait for 20s
driver.manage().timeouts().implicitlyWait(
20
, TimeUnit.SECONDS);
// find login userName and password editText
List<WebElement> textFieldsList = driver
.findElementsByClassName(
"android.widget.EditText"
);
textFieldsList.get(
0
).sendKeys(
"seleniumcookies@126.com"
);
textFieldsList.get(
1
).sendKeys(
"cookies123"
);
driver.manage().timeouts().implicitlyWait(
20
, TimeUnit.SECONDS);
// find ok button byName
driver.findElementById(
"android:id/button1"
).click();
driver.manage().timeouts().implicitlyWait(
90
, TimeUnit.SECONDS);
// find keyword 首頁 and verify it is display
Assert.assertTrue(driver.findElement(By.name(
"首頁"
)).isDisplayed());
}
@Test
(groups = {
"profileSetting"
}, dependsOnMethods =
"login"
)
public
void
profileSetting() {
driver.manage().timeouts().implicitlyWait(
30
, TimeUnit.SECONDS);
// find keyword 首頁 and verify it is display
Assert.assertTrue(driver.findElement(By.name(
"首頁"
)).isDisplayed());
WebElement myButton = driver.findElement(By
.className(
"android.widget.ImageButton"
));
myButton.click();
driver.manage().timeouts().implicitlyWait(
30
, TimeUnit.SECONDS);
driver.swipe(
700
,
500
,
100
,
500
,
10
);
driver.manage().timeouts().implicitlyWait(
30
, TimeUnit.SECONDS);
List<WebElement> textViews = driver
.findElementsByClassName(
"android.widget.TextView"
);
textViews.get(
0
).click();
driver.manage().timeouts().implicitlyWait(
30
, TimeUnit.SECONDS);
driver.findElementById(
"com.zhihu.android:id/name"
).click();
driver.manage().timeouts().implicitlyWait(
30
, TimeUnit.SECONDS);
List<WebElement> showClose = driver
.findElementsById(
"com.zhihu.android:id/showcase_close"
);
if
(!showClose.isEmpty()) {
snapshot((TakesScreenshot) driver,
"zhihu_showClose.png"
);
showClose.get(
0
).click();
}
Assert.assertTrue(driver
.findElementsByClassName(
"android.widget.TextView"
).get(
0
)
.getText().contains(
"selenium"
));
driver.findElementById(
"com.zhihu.android:id/menu_people_edit"
).click();
driver.manage().timeouts().implicitlyWait(
30
, TimeUnit.SECONDS);
WebElement intro = driver
.findElementById(
"com.zhihu.android:id/introduction"
);
intro.click();
driver.manage().timeouts().implicitlyWait(
30
, TimeUnit.SECONDS);
WebElement content = driver
.findElementById(
"com.zhihu.android:id/content"
);
String text = content.getAttribute(
"text"
);
content.click();
clearText(text);
content.sendKeys(
"Appium Test. Create By Young"
);
driver.findElementById(
"com.zhihu.android:id/menu_question_done"
)
.click();
WebElement explanation = driver
.findElementById(
"com.zhihu.android:id/explanation"
);
explanation.click();
driver.manage().timeouts().implicitlyWait(
30
, TimeUnit.SECONDS);
content = driver.findElementById(
"com.zhihu.android:id/content"
);
text = content.getAttribute(
"text"
);
content.click();
clearText(text);
content.sendKeys(
"Appium Test. Create By Young. This is an appium type hahahahah"
);
driver.findElementById(
"com.zhihu.android:id/menu_question_done"
)
.click();
snapshot((TakesScreenshot) driver,
"zhihu.png"
);
}
/**
* This method for delete text in textView
*
* @author Young
* @param text
*/
public
void
clearText(String text) {
driver.sendKeyEvent(
123
);
for
(
int
i =
0
; i < text.length(); i++) {
driver.sendKeyEvent(
67
);
}
}
@AfterClass
(alwaysRun =
true
)
public
void
tearDown()
throws
Exception {
driver.quit();
}
/**
* This Method create for take screenshot
*
* @author Young
* @param drivername
* @param filename
*/
public
static
void
snapshot(TakesScreenshot drivername, String filename) {
// this method will take screen shot ,require two parameters ,one is
// driver name, another is file name
String currentPath = System.getProperty(
"user.dir"
);
// get current work
// folder
File scrFile = drivername.getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy
// somewhere
try
{
System.out.println(
"save snapshot path is:"
+ currentPath +
"/"
+ filename);
FileUtils
.copyFile(scrFile,
new
File(currentPath +
"\\"
+ filename));
}
catch
(IOException e) {
System.out.println(
"Can't save screenshot"
);
e.printStackTrace();
}
finally
{
System.out.println(
"screen shot finished, it's in "
+ currentPath
+
" folder"
);
}
}
}
|
這是運行中的兩處截圖: