使用appium同時支援iOS和android的case結構及jenkins job配置

來源:互聯網
上載者:User

標籤:packages   driver   \n   depend   xxxx   ranch   部分   end   throws   

之前我們大多數的case branch,僅能支援iOS,或者android。若想兩端都做自動化,只能寫兩份代碼。瞭解了appium及testng後,我們在自動化中開始嘗試使用同一套代碼,即支援iOS,又支援Android。希望此種方式能提高我們的自動化開發效率。

1.case代碼支援1.1 業務case部分

如果iOS和android對同一功能的操作互動類似,我們就可以使用同一套業務case代碼。appium對iOS和Android系統的操作介面基本是一致的。若不一致,可使用driver instanceof IOSDriver 或 driver instanceof AndroidDriver 來進行分支判斷。

舉例:

public void login() throws Exception{//此處iOS與Android app上的互動不同,所以做了不同處理。        if(driver instanceof IOSDriver) {            loginPage.getOtherWayToLogin().click();            loginPage.getBtnUsePassword().click();        }        Assert.assertTrue(loginPage.setPhone(TestData.account),"無法輸入正確電話");        if(driver instanceof AndroidDriver){            loginPage.getBtnNext().click();            loginPage.getBtnUsePassword().click();        }        getAutomationUtil().setValue(loginPage.getEdtxtPassword(), TestData.password);        loginPage.getBtnLogin().click();        Assert.assertTrue(loginPage.getMenuButton() != null,"疑似登入未成功");    }

  

1.2 page類

業務case裡使用的元素,都依賴page類來識別。使用appium提供的注釋方式,寫起代碼來較為簡潔。舉例:

public class LoginPage extends BasePage{    @AndroidFindBy(uiAutomator = "new UiSelector().resourceId(\"xxx:id/xxxx\")")    @iOSXCUITFindBy(accessibility = "同意")    private MobileElement btnPrivacyRuleAgree;    @iOSXCUITFindBy(accessibility = "使用其它方式登入")    private MobileElement otherWayToLogin;        public MobileElement getOtherWayToLogin() {        return otherWayToLogin;    }    public MobileElement getBtnPrivacyRuleAgree() {        return btnPrivacyRuleAgree;    }        ...}public class BasePage {    public BasePage(AppiumDriver<WebElement> driver) {        //appium通過此代碼支援注釋方式定位元素        PageFactory.initElements(new                AppiumFieldDecorator(driver, new TimeOutDuration(30, TimeUnit.SECONDS)), this);    }    ...}

  

2. testng支援2.1 testng case相關配置
  • testng可將case分成不同的組,每次根據suit的配置來運行指定的case group。

  • 可通過dependsOnGroups表示依賴關係,如必須先運行ios_login分組的case後,才能運行ios_case組的case。

  • 可通過priority來決定同一分組裡的case的優先順序。數值越大,越先運行;不指定,則預設值為0;值相同的case的運行先後順序,由testng決定。

@Test(groups = {"ios_login","adr_login"})public void login() throws Exception{...}@Test(groups = "adr_login",priority = 1)public void getUuidTest() {...}@Test(enabled = true,groups = "ios_case",dependsOnGroups = "ios_login")public void testXXXX(){...}

  

2.2 testng suit配置

testng可在suit設定檔中指定要啟動並執行分組。例子中,指定運行三個分組。

<?xml version="1.0" encoding="UTF-8"?><suite name="Android-All">    <test name="android">        <groups>            <run>                <include name="android_login"/>                <include name="android_cases"/>                <include name="android_logout"/>            </run>        </groups>        <packages>            <package name="com.HHHH.autotest.cases"/>        </packages>    </test></suite>

  

3. jenkins job支援

單個branch若只支援ios或android,使用的app.properties和testng suit配置可使用case裡相應的檔案內容。但通過jenkins 在公司的自動化平台上運行自動化,app.properties檔案已被公司的自動化平台改寫僅需傳入部分配置。為支援同一branch可測兩端,testng suit配置也需要我們在運行自動化改寫。我們可以在jenkins上重寫這兩個檔案。

3.1 重寫app.properties 和 testng.xml

在command裡, YYYY 為job的名字。iOS只用傳platformName和automationName;android只用傳platformName和appActivity。

create_app_properties(){    if [ $platform == ‘iOS‘ ];then        `echo -e "platformName = iOS\nautomationName = XCUITest" > app.properties`        `mv app.properties YYYY`        `echo -e "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<suite name=\"iOS-All\">\n<test name=\"ios\">\n<groups>\n<run>\n<include name=\"ios_login\"/>\n<include name=\"ios_cases\"/>\n<include name=\"ios_logout\"/>" > testng.xml`        `echo -e "</run>\n</groups>\n<packages>\n<package name=\"com.HHHH.autotest.cases\"/>\n</packages>\n</test>\n</suite>" >> testng.xml`        `mv testng.xml YYYY`           else        `echo -e "platformName = Android\nappActivity=com.HHHH.main.activity.MainSplashActivity" > app.properties`        `mv app.properties YYYY`        `echo -e "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<suite name=\"Android-All\">\n<test name=\"android\">\n<groups>\n<run>\n<include name=\"android_login\"/>\n<include name=\"android_cases\"/>\n<include name=\"android_logout\"/>" > testng.xml`        `echo -e "</run>\n</groups>\n<packages>\n<package name=\"com.HHHH.autotest.cases\"/>\n</packages>\n</test>\n</suite>" >> testng.xml`        `mv testng.xml YYYY`}

  

使用appium同時支援iOS和android的case結構及jenkins job配置

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.