appium安卓自動化的 常用driver方法封裝


appium安卓自動化的 常用driver方法封裝 

 

做安卓自動化的時候,很多方法寫起來會造成代碼冗余,把這部分封裝起來 ,添加到androidUI工具類里,隨時可調用

都放在這個類下面:

@Component
public class AndroidUI{

首先要進行driver實例的連接,連接后才能做相應的一些操作,及得造作完成要關閉driver,避免內存占用

連接driver

/*
* @method: 連接driver
*/
public void setUp(DesiredCapabilities dCapabilities) throws MalformedURLException {
driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), dCapabilities);
logger.info(driver.currentActivity());
}

斷開driver

/*
* @method: 斷開driver
*/
public void tearDown() throws Exception {
if (null != driver) {
driver.quit();
}
}

休眠方法時間定義毫秒或秒都行吧,看個人喜好,我這里為了寫着定義成秒的,系統自帶是毫秒

/*
* @method: 休眠方法
* @param: int seconds 休眠的時間,單位為s(必須大於等於1)
* @other: Appium類內設置的所有操作方法均會在正常結束后休眠1s,無需在用例中手動休眠
*/
public void sleep(int time) {
if (!((time >= 1) && (time % 1 == 0))){
Assert.fail("sleep: Parameter ERROR!");
}
try {
Thread.sleep(time * 1000);
//System.out.println("休息");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

上面休眠方法是死的,而元素加載時間不確定的,可以用下面等待找到元素的靈活等待時間

/*
* @method: 比sleep更有客觀的延遲方法,在一定時間內等待某元素在屏幕中出現。
* @param: String target 等待的目標
* int time 等待時間,單位為ms,必須是1的正整數倍
* @other: Appium類內設置的所有操作方法均會在正常結束后休眠1s,無需在用例中手動休眠
*/
public boolean waitFor(String target,int time) {

try {
if (!((time >= 1) && (time % 1 == 0))){
Assert.fail("waitFor: Parameter ERROR!");
}
String result = null;
String page = driver.getPageSource();
logger.info("Waiting for " + target + " ...");

for (int i = 1; i <= time; i++) {
if (page.contains(target)) {
result = "Succeeded.";
logger.info(result);
break;
} else {
sleep(1);
page = driver.getPageSource();
}
}
} catch (Exception e) {
logger.error("元素:"+target+"不存在");
sleep(1);
e.printStackTrace();
return false;
}
return true;
}

然后是滑動操作,這個操作是比較常用的手勢,按照方向做了封裝,調用時把方向作為參數使用
/*
* @method: 滑動操作
* @param: String direction, right 向右四分之一/left 向左四分之一/up 向上四分之一/down 向下四分之一/top 到頂/end 到底
*/
public void swipeTo(String direction) {

int width=driver.manage().window().getSize().width;
int height=driver.manage().window().getSize().height;


if (direction == "right") {
driver.swipe(width/4, height/2, width*3/4,height/2, 500);
logger.info("右滑");
} else if (direction == "left") {
driver.swipe(width*3/4, height/2, width/4, height/2, 500);
logger.info("左滑");
} else if (direction == "down") {
driver.swipe(width/2, height/4, width/2, height*3/4, 500);
logger.info("下滑");
} else if (direction == "up") {
driver.swipe(width/2, height*3/4, width/2, height/4, 500);
logger.info("上滑");
} else if (direction == "end") {
String page1;
String page2;
do {
page1 = driver.getPageSource();
driver.swipe(width/2, height*3/4, width/2, height/4, 500);
sleep(2);
page2 = driver.getPageSource();
} while (!page1.equals(page2));
logger.info("滑到底");
} else if (direction == "top") {
String page1;
String page2;
do {
page1 = driver.getPageSource();
driver.swipe(width/2, height/4, width/2, height*3/4, 500);
sleep(4);
page2 = driver.getPageSource();
} while (!page1.equals(page2));
logger.info("滑到頂");
}
sleep(1);
}

學過安卓開發的都知道,layout中有各種view、包括textView、imageView、Button、checkBox、radioButton、alertDialog、processDialog啥的
所以對於這些的點擊處理最好是根據id或text作為參數封裝方法來進行定位點擊操作
例如:
/*
* @method: 通過想點擊文字的控件id和順序index點擊文字,當第一屏未找到文字會向下滾動查找,若滾到底扔未發現文字斷言失敗;方法結束后休眠1s
* @param: String id 想點擊文字的id
* int index 順序
*/
public void clickTextById(String id, int index) {

String page1;
String page2;
int result = 0;

do {
page1 = driver.getPageSource();
if (page1.contains(id)) {
if (index == 0) {
AndroidElement imageElement = driver.findElementByXPath("//android.widget.TextView[contains(@resource-id,'" + id + "')]");
logger.info("Text " + id + " found.");
imageElement.click();
logger.info("Text " + id + " clicked.");
result = 1;
} else if (index > 0) {
List<AndroidElement> imageElements = driver.findElementsByXPath("//android.widget.TextView[contains(@resource-id,'" + id + "')]");
AndroidElement imageElement = imageElements.get(index);
logger.info("Text " + id + " found.");
imageElement.click();
logger.info("Text " + id + " clicked.");
result = 1;
}
break;
} else {
this.swipeTo("down");
page2 = driver.getPageSource();
}
} while (!page1.equals(page2));

if (result == 0) {
Assert.fail("Clicking Text " + id + " failed.");
}
sleep(1);
}

當然,我們進行測試,是為了確定頁面中有某元素或是沒有某元素,所以用的最多的方法是search

public void search(String list) {

String pageSource = this.pageSource();
if (!list.contains(",")) {
if (pageSource.contains(list)) {
logger.info(list + " is found.");
} else {
takeScreenShot("FAILURE(search)_" + list);
Assert.fail("Searching " + list + " failed.");
}
} else {
String elements[] = list.split(",");
for (int i = 0; i <= ((elements.length)-1); i++) {
if (elements[i].contains(" ")) {
elements[i] = elements[i].trim();
}
if (pageSource.contains(elements[i])) {
logger.info(elements[i] + " is found.");
} else {
takeScreenShot("FAILURE(search)_" + list);
Assert.fail("Searching " + elements[i] + " failed.");
}
}
}
sleep(1);
}

上面要說明一下。
takeScreenShot("FAILURE(search)_" + list); 這個是單獨寫的截屏函數,
這個方法在出現錯誤時,保存個截圖到某一文件夾下更好,這樣可以方便定位問題
public void takeScreenShot(String methodName) {
File srcFile = AndroidUI.driver.getScreenshotAs(OutputType.FILE);
//利用FileUtils工具類的copyFile()方法保存getScreenshotAs()返回的文件對象。
// FileUtils.copyFile(srcFile, new File("screenshot.png"));
String fileName = methodName + "_" + DateUtil.formatNowTime(12) + ".png";
String filePath = "C:\\Users\\linyuchen\\Pictures\\AndroidUI";
try {
FileUtils.copyFile(srcFile, new File(filePath+"\\"+fileName));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// System.out.println("taking ScreenShots");

}

其實還有很多很多方法 ,大家可以根據自己喜好來定義 都可以隨意些

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 
       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM