本套框架實現了appium全自動執行,多臺設備同時執行,自動啓服務,自動生成testng文件,監聽,重連.......只需寫測試腳本java
編寫腳本順序:page、action、testandroid
本次實戰以「聯通手機營業廳app」爲例進行腳本編寫,一下的page類和action類能夠合併,但分開更爲明確,根據本身喜愛來寫;斷言可寫在action中也能夠寫在test中;ios
用例:app初次使用出現的引導頁,滑動3次,可點擊進入app應用app
一、建立配置文件框架
1)yindaopage1001.properties測試
ImageView=classname>android.widget.ImageView
ok_button=id>com.sinovatech.unicom.ui:id/custom_dialog_ok_button
tiaoguo_textview=id>com.sinovatech.unicom.ui:id/welcome_tiaoguo_textview
login=id>com.sinovatech.unicom.ui:id/home_login_button
2)android_caps.propertiesui
app=C:/Users/jff/Desktop/mobileyy.apk
deviceName=devicename
automationName=Appium
newCommandTimeout=600
#appPackage=com.zhihu.android
#appActivity=com.zhihu.android.app.ui.activity.MainActivity
#appWaitActivity=com.hotyq.app.android.activity.MainActivity
appPackage=com.sinovatech.unicom.ui
appActivity=com.sinovatech.unicom.ui.WelcomeClient
noSign=true
unicodeKeyboard=true
resetKeyboard=true
deviceReadyTimeout=30
#noReset=true
#autoGrantPermissions=true
3)global.properties
this
androidserver=http://127.0.0.1
iosserver=http://127.0.0.1
senduser=vop_jiangfeifei@163.com
password=郵箱密碼
tomail=1096101476@qq.com
implicitlyWait=10
input=
#0:android and ios;1:android;2:ios
deviceType=1
#appType 0:原生;1:混合;2:H5
appType=1
#0:均分測試用例每一個設備;1:每一個設備都執行全部用例
testType=1
#測試用例,「,」分割
testcases=com.crazy.appium.testcases.Test10010
二、建立page包,建立page類code
package com.crazy.appium.page;
import com.crazy.appium.driver.CrazyMobileDriver;
import com.crazy.appium.utils.GetLocatorUtils;
import io.appium.java_client.MobileElement;
public class YinDaoPage10010{
public MobileElement start;
public MobileElement tiaoguo;
public CrazyMobileDriver driver;
public MobileElement ok_button;
public GetLocatorUtils locatorUtils;
public YinDaoPage10010(CrazyMobileDriver driver){
this.driver=driver;
this.locatorUtils=new GetLocatorUtils("configs/yindaopage10010.properties");
}
public MobileElement getStart() {
return driver.findElement(locatorUtils.getByLocalor("ImageView"));
}
public MobileElement getOk_button() {
return driver.findElement(locatorUtils.getByLocalor("ok_button"));
}
public MobileElement getTiaoguo() {
return driver.findElement(locatorUtils.getByLocalor("tiaoguo_textview"));
}
}
三、建立action包,Action類server
package com.crazy.appium.action;
import static org.testng.Assert.assertEquals;
import org.testng.Assert;
import com.crazy.appium.driver.CrazyMobileDriver;
import com.crazy.appium.page.YinDaoPage10010;
public class YinDaoAction10010 extends YinDaoPage10010{
public YinDaoAction10010(CrazyMobileDriver driver) {
super(driver);
// TODO Auto-generated constructor stub
}
public void enterHomePage(){
//滑動
for(int i=0;i<3;i++){
driver.swipe("left", 500);
driver.sleep(1000);
}
//最後一頁點擊進入應用
getStart().click();
}
public void kaiqi(){
getOk_button().click();
driver.sleep(500);
//斷言能夠寫在action中,方便維護,test中直接調用便可
Assert.assertEquals(driver.isElementExist(locatorUtils.getByLocalor("login")), true);
}
//直接跳過
public void skip(){
getTiaoguo().click();
}
}
四、建立test包,test類
1)TestBase
package com.crazy.appium.testcases;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import com.crazy.appium.driver.CrazyMobileDriver;
/**定義的driver對象,爲driver、udid賦值
* 所寫的Test都須要繼承這個類,若須要一次執行多個測試類
* 1.配置如安卓,配好後;
* 2.配置global,testcase哪裏用逗號分隔便可
* 3.找到main方法,執行
*/
public class TestBase {
CrazyMobileDriver driver;
//AppiumDriverLocalService service;
String udid;
public CrazyMobileDriver getDriver(){
return driver;
}
public String getUdid(){
return udid;
}
//類初始化
@Parameters({"udid","port"})
@BeforeClass
public void beforeClass(String udid,String port) throws Exception{
this.udid=udid;
driver=new CrazyMobileDriver("android",udid,port);
driver.implicitlyWaitDefault();
driver.sleep(10000);
}
@AfterClass
public void afterClass(){
driver.quit();
//service.stop();
}
}
2)測試用例,能夠把斷言寫在action中,也能夠寫在test中
package com.crazy.appium.testcases;import org.testng.Assert;import org.testng.annotations.Test;import com.crazy.appium.action.YinDaoAction10010;public class Test10010 extends TestBase{ @Test public void tes001_yindao(){ YinDaoAction10010 yd=new YinDaoAction10010(driver); yd.enterHomePage(); driver.sleep(1000); //斷言也能夠寫在test中 Assert.assertEquals(driver.getPageSource().contains("舒適提示"), true); } @Test(dependsOnMethods="tes001_yindao") public void tes001_kaiqi(){ YinDaoAction10010 yd=new YinDaoAction10010(driver); yd.kaiqi(); }