本套框架實現了appium全自動執行,多台設備同時執行,自動啟服務,自動生成testng文件,監聽,重連.......只需寫測試腳本
編寫腳本順序:page、action、test
本次實戰以“聯通手機營業廳app”為例進行腳本編寫,一下的page類和action類可以合並,但分開更為明確,根據自己喜好來寫;斷言可寫在action中也可以寫在test中;
用例:app初次使用出現的引導頁,滑動3次,可點擊進入app應用
1、創建配置文件
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.properties
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
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
2、創建page包,創建page類
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"));
}
}
3、創建action包,Action類
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();
}
}
4、創建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();
}