Appium混合應用測試


Appium測試混合應用

混合應用即是原生應用中間混着html頁面,需要在兩種類型的頁面之間跳轉。

測試Android混合應用

前期設置

  1. 4.4以下版本使用automationName:Selendroid
  2. 4.4及其以上的版本使用automationName:Appium
  3. 需要設置webview的debug模式為true,默認是false

元素定位

Selendroid模式:selendroid inspector
Appium模式:chrome://inspect

代碼示例

設置webview的debug模式,需要插入在android源碼當中:

if (Build.VERSION.SDK_INT >=Build.VERSION_CODES.KITKAT){
    WebView.setWebContentsDebuggingEnabled(true);
}

Selendroid代碼示例,appium模式替換automationName:Appium即可:

//配置 webdriver 並啟動 webview 應用。
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setCapability("device", "Selendroid");
desiredCapabilities.setCapability("app", "/path/to/some.apk");  
URL url = new URL("http://127.0.0.1:4723/wd/hub");
RemoteWebDriver remoteWebDriver = new RemoteWebDriver(url, desiredCapabilities);

// 切換到最新的web視圖
remoteWebDriver.switchTo().window("WEBVIEW");

//在 guinea-pig 頁面用 id 和 元素交互。
WebElement div = remoteWebDriver.findElement(By.id("i_am_an_id"));
Assert.assertEquals("I am a div", div.getText()); //驗證得到的文本是否正確。
remoteWebDriver.findElement(By.id("comments")).sendKeys("My comment"); //填寫評論。

//離開 webview,回到原生應用。
remoteWebDriver.switchTo().window("NATIVE_APP");

//關閉應用。
remoteWebDriver.quit();

測試iOS混合應用

前期設置

  1. 模擬器和本地應用相同
  2. 測試真機時需要ios_webkit_debug_proxy工具
brew install ios_webkit_debug_proxy

ios_webkit_debug_proxy -c [udid]:27753 -d

元素定位

使用Safari或者chrome的開發者工具

代碼示例

//配置 webdriver 並啟動 webview 應用。
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setCapability("device", "iPhone Simulator");
desiredCapabilities.setCapability("app", "http://appium.s3.amazonaws.com/WebViewApp6.0.app.zip");  
URL url = new URL("http://127.0.0.1:4723/wd/hub");
RemoteWebDriver remoteWebDriver = new RemoteWebDriver(url, desiredCapabilities);

// 切換到最新的web視圖
for(String context : driver.getContextHandles()){
	if (context.contains("WEBVIEW")) {
		driver.context(context);
	}
}

//在 guinea-pig 頁面用 id 和 元素交互。
WebElement div = remoteWebDriver.findElement(By.id("i_am_an_id"));
Assert.assertEquals("I am a div", div.getText()); //驗證得到的文本是否正確。
remoteWebDriver.findElement(By.id("comments")).sendKeys("My comment"); //填寫評論。

//離開 webview,回到原生應用。
remoteWebDriver.executeScript("mobile: leaveWebView");

//關閉應用。
remoteWebDriver.quit();

參考官方文檔


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM