appium 自動化測試之知乎Android客戶端

appium是一個開源框架,相對來講還不算很穩定。轉載請註明出處!!!!html

 

前些日子,配置好了appium測試環境,至於環境怎麼搭建,參考:http://www.cnblogs.com/tobecrazy/p/4562199.htmljava

接下來咱們研究一下測試知乎Android客戶端。自行下載android

配置目錄以下:把知乎客戶端命名爲zhihu.apkweb

編寫基於java testng的測試用例:cookie

因爲本人是熟悉selenium webdriver,因此在Android對象識別時,採用的策略和selenium webdriver 相似app

怎麼去識別APP的元素的,這裏用到Android SDK  tools 目錄下的工具uiautomatorviewer ,執行uiautomatorviewer.bat就可框架

  • 首先解析zhihu app 拿到其package 和 activity,這裏使用appium gui界面,具體操做如圖:

  • 接下來識別APP的元素,好比登錄按鈕,看到resourceId,能夠是雨天webdriver的find Element By.id

至於登錄用戶名和密碼,在Android中都是EditText控件,能夠使用By.classide

 

 


完成對象識別,最終代碼以下:工具

 

 

 

 

package com.dbyl.core;
 
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
 


import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import io.appium.java_client.android.AndroidDriver;

import java.io.File;
import java.net.URL;
import java.util.List;
import java.util.concurrent.TimeUnit;
 
public class zhiHu {
    private  AndroidDriver  driver;
 
    @BeforeMethod(alwaysRun=true)
    public void setUp() throws Exception {
        // set up appium
        File classpathRoot = new File(System.getProperty("user.dir"));
        File appDir = new File(classpathRoot, "apps");
        File app = new File(appDir, "zhihu.apk");
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("deviceName","Android Emulator");
        capabilities.setCapability("platformVersion", "4.4");
        //if no need install don't add this
        capabilities.setCapability("app", app.getAbsolutePath());
        capabilities.setCapability("appPackage", "com.zhihu.android");
        //support Chinese 
        capabilities.setCapability("unicodeKeyboard" ,"True");
        capabilities.setCapability("resetKeyboard", "True");
        //no need sign
        capabilities.setCapability("noSign", "True");
        capabilities.setCapability("appActivity", ".ui.activity.GuideActivity");
        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
    }
 
    @AfterMethod(alwaysRun=true)
    public void tearDown() throws Exception {
        driver.quit();
    }
 
    @Test(groups={"ZHTest"})
    public void Login(){
        //find login button
        WebElement loginButton = driver.findElement(By.id("com.zhihu.android:id/login"));
        loginButton.click();
        
        //wait for 20s
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        
        //find login userName and password editText 
        List<WebElement> textFieldsList = driver.findElementsByClassName("android.widget.EditText");
        textFieldsList.get(0).sendKeys("seleniumcookies@126.com");
        textFieldsList.get(1).sendKeys("cookies123");
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        
        //find ok button byName
        driver.findElementByName("OK").click();
        driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
        
        //find keyword 首頁 and verify it is display
        Assert.assertTrue( driver.findElement(By.name("首頁")).isDisplayed());
        
        
    }
}

 

接下來就能夠debug,首先啓動Android模擬器 ,啓動完畢後再明明很測試,若結果以下:測試

說明模擬器能夠使用

接下來在命令行運行apppium

等待1-2分鐘

run as testng,就能看到自動安裝,登錄知乎

PASSED: Login

===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

[TestNG] Time taken by org.testng.reporters.EmailableReporter2@4474c7fe: 1938 ms[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@1fdf50a3: 121 ms[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 0 ms[TestNG] Time taken by org.testng.reporters.XMLReporter@697748ae: 423 ms[TestNG] Time taken by org.testng.reporters.jq.Main@68450212: 1669 ms[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@7f92deaf: 240 ms

相關文章
相關標籤/搜索