JAVA_Selenium Appium安卓自動化測試 -第一個case

      上文中我們已將環境準備好了,現在我們就利用一個case來認識這套框架。我們先利用Intellij IDEA(下問中我都以IDEA稱)新建一個maven項目。由於我使用的電腦無法連接外網,所以項目中我將依賴重新添加到lib 目錄中添加依賴的過程大家可以連接到中央庫可以忽略。

       1.打開IDEA我們新建一個項目

 

   2.選中Maven,勾選Create from archetype 選這quickstart

 

       3.輸入:GroupId (com.公司域名),ArtifactId(項目名稱),Version(版本號)  之後就一直Next/Finish就可以了(其中Project location可以自己選擇項目目錄)

 

  4.啓動後我我發現我這裏maven由於網絡問題無法連接到中央庫。我只能手動添加依賴了!

 

 5.新建兩個目錄:lib;com.zl.testAppium 由於我這個網絡問題導致我這個目錄結構和實際的有區別,一般com.zl.testAppium會在src\test\java目錄下有的。爲了好統一手動完成添加。如下圖:

 

 

之後就是添加依賴瞭如果是可以連接中央庫,直接可以通過maven進行配置,添加配置如下:(點擊Import Changes 就可完成依賴管理)

 

 

圖中代碼如下:

 

<dependencies>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.53.1</version>
            <!--<version>3.3.1</version>-->
        </dependency>
        <dependency>
            <groupId>net.sourceforge.jexcelapi</groupId>
            <artifactId>jxl</artifactId>
            <version>2.6.12</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.9.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
            <scope>test</scope>
        </dependency>
        <!-- Includes the Sauce JUnit helper libraries -->
        <dependency>
            <groupId>com.saucelabs</groupId>
            <artifactId>sauce_junit</artifactId>
            <version>2.1.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.2.4</version>
        </dependency>
        <dependency>
            <groupId>com.google.inject</groupId>
            <artifactId>guice</artifactId>
            <version>3.0</version>
        </dependency>
        <dependency>
            <groupId>com.netease.qa</groupId>
            <artifactId>arrow</artifactId>
            <version>0.0.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.uncommons</groupId>
            <artifactId>reportng</artifactId>
            <version>1.1.4</version>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.testng</groupId>
                    <artifactId>testng</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
</dependencies>

但如果有人和我一樣無法連接到中央庫就需要自己導入jar包了

    下面是我使用的幾個jar包 下載地址

鏈接:https://pan.baidu.com/s/1cNP_yLRp2RfCIMh31yQpAA 密碼:levn

鏈接:https://pan.baidu.com/s/1znfQ_IJouuSaMsfMx7zbyQ 密碼:ph35

鏈接:https://pan.baidu.com/s/1PppSf9xV91ikRfzykmFiTg 密碼:uxpd

鏈接:https://pan.baidu.com/s/1UGM8S2D7FkZ9rusYBf3rkQ 密碼:1mcg

 

      相關的依賴全部做完了,現在我們開始敲,敲,敲

1.在src\test\java\com.zl.testAppium 目錄下新建一個java 文件 Case1 在新建項中沒有java文件是由於文件類型沒有正確:可以在File\Project Structure\Modules\Sources 中將com.zl.testAppium 設置爲tests 如下結果圖:

 

之後我們就已手機的計算器爲例子,新建3個方法:setUp();test1();tearDown()  分別用來做三個動作:啓動,計算1+2=? 判斷是不是等於3,關閉。

   啓動中我們需要使用selenium中的DesiredCapabilities對Appium進行一下配置如下:

 

這裏裏面有幾個參數的值我提出來說明一下:

deviceName:   設備序列號(使用ADB命令:adb devices 可以獲得)

appPackage:   待測APP 包名(這個在使用安卓工具uiautomatorviewer 中獲得package)

appActivity:   待啓動APP的Activity(啓動的Activity)

appiumUrl:    Appium服務Url(一般爲“http://127.0.0.1:4723/wd/hub”)

     這裏單獨說一下這個appActivity,如果你有APP源碼就可以直接知道了,如果沒有可以使用命令:adb shell 進入shell中輸入命令:monkey -p com.android.bbkcalculator -v 1  (其中com.android.bbkcalculator是計算器的包名)  如圖途中的.Calculator 就是需要獲得的appActivity。

 

我們通過uiautomatorviewer 可以獲得我們需要的幾個控件的id 

 

我們將需要用到的參數都單獨定義出來如下:

 

之後就是找元素進行點擊了,這裏我們需要用到findElementById();和.click();.getText();如下圖

 

最後我們要做的事情就是測試完成後關掉APP 使用:close()

最後這裏用到了testng  這裏就不單獨才說了 最後我們就開始準備運行了,運行前我們需要打開調試模式,運行appiun如下圖

 

到這基礎的用例就完成啦!

運行結果如下:

最後附上代碼如下:

private AndroidDriver<AndroidElement> driver;

    public static String deviceName= "4L4DFA4SLFL7G69D";
    public static String packgeName ="com.android.bbkcalculator";
    public static String mainActivityName =".Calculator";
    public static String appiumUrl ="http://127.0.0.1:4723/wd/hub";

    public static String BUT_1= "com.android.bbkcalculator:id/digit1"; //按鍵1
    public static String BUT_2 ="com.android.bbkcalculator:id/digit2";//按鍵2
    public static String BUT_PLUS ="com.android.bbkcalculator:id/plus";//按鍵+
    public static String BUT_EQUAL ="com.android.bbkcalculator:id/equal";//按鍵=
    public static String BUT_RESULT ="com.android.bbkcalculator:id/edit_result_text";//結果顯示框

@BeforeClass
    public void setUp() throws MalformedURLException {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("automationName","Appium");
        capabilities.setCapability("browserName", "");//待測手機的瀏覽器名稱,自動化測試填空
        capabilities.setCapability("appiumVersion", "1.4.16.1");
        capabilities.setCapability("platformName", "Android");//設備類型
        capabilities.setCapability("deviceName", deviceName);//設備序列號
        capabilities.setCapability("platformVersion", "5.1");//待測安卓系統版本
        capabilities.setCapability("appPackage", packgeName);//待測APP 包名
        capabilities.setCapability("appActivity", mainActivityName);//待啓動APP的Activity
        capabilities.setCapability("resetKeyboard", true);//使用unicodeKeyboard  後在測試完成後恢復輸入法到默認設置
        capabilities.setCapability("unicodeKeyboard", true);//使用Unicode輸入法
        capabilities.setCapability("noSign", true);//設置避免重新簽名
        driver = new AndroidDriver(new URL(appiumUrl), capabilities);
        driver.getContext();
    }
@Test
    public void test1(){
        driver.findElementById(BUT_1).click();//點擊1
        driver.findElementById(BUT_PLUS).click();//點擊+
        driver.findElementById(BUT_2).click();//點擊2
        driver.findElementById(BUT_EQUAL).click();//點擊等於
        int a= Integer.parseInt(driver.findElementById(BUT_RESULT).getText());//獲取結果輸入框的結果
        if(a==3){//判斷結果是不是等於3   輸出文字到控制檯
            System.out.println("厲害了,這都算對了!");
        }else {
            System.out.println("厲害了,這都能算錯!");
        }
    }
@AfterClass
    public void tearDown(){
    driver.quit();
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章