APP自動化之Hybrid自動化腳本實現示例(八)

需求示例:打開58同城=>本地服務=>搬家java

1.Hybrid app 例如58同城node

  >  Hybrid自動化開發思路
  >  識別-webview視圖
  >  獲取全部的contexts:driver.getContextHandles();
  >  切換-切換到webview視圖:driver.context(webview視圖)
  >  定位-定位webview中的元素,並執行操做:web網頁元素定位和操做
  >  切換-切換回默認的視圖:driver.context(native視圖)android

2.元素準備:ios

(1) 首先咱們使用uiautomatorviewer.bat獲取native頁面元素:resources-id: com.wuba:id/iv_icon_big_groupweb

點擊進入本地服務後,進入webview頁面,使用uiautomatorviewer.bat沒法獲取元素的時候咱們採用UC開發者工具定位元素chrome

(2)使用UC開發者工具獲取webview頁面元素npm

 

搬家xpath : //*[@id="branchList"]/div/div[1]/div[2]/div/ul/li[2]/div/spanwindows

 3.上代碼瀏覽器

package app_auto;

import java.net.URL;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;

import io.appium.java_client.android.AndroidDriver;

public class HybridDemo {
    public static AndroidDriver<WebElement> androidDriver = null;
    public static void main(String[] args) throws Exception{
        //1.建立配置對象
        DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
        //2.添加配置
        //deviceName:找到咱們要測試的設備
        desiredCapabilities.setCapability("deviceName", "127.0.0.1:62001");
        //platformName 肯定咱們要測試的系統平臺 Android or ios
        desiredCapabilities.setCapability("platformName", "Android");
        //appPackage 找到咱們要測試的應用 
        //aapt dump badging D:\appium\apk\58同城.apk | findstr package
        desiredCapabilities.setCapability("appPackage", "com.wuba");
        //appActivity 經過入口咱們啓動測試的app
        //aapt dump badging D:\appium\apk\58同城.apk | findstr activity
        desiredCapabilities.setCapability("appActivity", "com.wuba.activity.launch.LaunchActivity");
        // noReset:啓動App測試不會清除掉應用的數據 true:不清楚 false:清除掉
        desiredCapabilities.setCapability("noReset", true);
        //實例化驅動,啓動測試APP
        //第一個參數,appium通信地址
        //第二個參數,配置
        androidDriver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"),desiredCapabilities);
        //添加隱試等待的方法 30秒
        androidDriver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
        Thread.sleep(5000);
        //調用實現方法
        native_webView();
        Thread.sleep(5000);
        androidDriver.quit();
        System.out.println("執行結束!");
    }
    public static void native_webView() throws InterruptedException {
        //1.點擊主頁上的本地服務
        androidDriver.findElementsByAndroidUIAutomator("resourceId(\"com.wuba:id/iv_icon_big_group\")").get(6).click();
        //2.休眠等待,而後獲取頁面全部的contexts(至關於web自動化中的句柄)
        Thread.sleep(5000);
        //3.獲取當前頁面的全部contexts,
        //原生頁面的context:NATIVE_APP, web頁面的context:WEBVIEW_com.wuba
        Set<String> contexts = androidDriver.getContextHandles();
        System.out.println(contexts);
        //4.切換到web頁面對應的context中
        for (String contextName : contexts) {
            //若是contextName有包含WEBVIEW關鍵字的話就是咱們要找的web頁面context
            if(contextName.contains("WEBVIEW_com.wuba")) {
                androidDriver.context(contextName);
            }
        }
        Thread.sleep(5000);
        //此時咱們就切換到了web頁面的定位方式
        androidDriver.findElementByXPath("//*[@id=\"branchList\"]/div/div[1]/div[2]/div/ul/li[2]/div/span").click();;
        //web頁面操做完成以後要記得回到native頁面的context中,不然沒法定位到原生app的元素
        androidDriver.context("NATIVE_APP");
    }    
}

4.運行報錯微信

查看appium日誌分析緣由:是chromeDriver與瀏覽器內置System webView控件不兼容致使

[info] [Chromedriver] Set chromedriver binary as: C:\Users\Administrator\AppData\Local\appium-desktop\app-1.5.0\resources\app\node_modules\appium\node_modules\appium-chromedriver\chromedriver\win\chromedriver.exe
[info] [Chromedriver] Spawning chromedriver with: C:\Users\Administrator\AppData\Local\appium-desktop\app-1.5.0\resources\app\node_modules\appium\node_modules\appium-chromedriver\chromedriver\win\chromedriver.exe --url-base=wd/hub --port=8000 --adb-port=5037 --verbose[debug] [Chromedriver] Chromedriver version: '2.20.353145'
[error] [Chromedriver] Error: Failed to start Chromedriver session: A new session could not be created. (Original error: session not created exception
from unknown error: Runtime.executionContextCreated has invalid 'context': {"auxData":{"frameId":"6AB007871163EAF0A479AC630302F975","isDefault":true,"type":"default"},"id":1,"name":"","origin":"https://app.58.com"}
  (Session info: chrome=74.0.3729.136)
  (Driver info: chromedriver=2.20.353145 (343b531d31eeb933ec778dbcf7081628a1396067),platform=Windows NT 6.1 SP1 x86_64))
    at Object.wrappedLogger.errorAndThrow (C:\Users\Administrator\AppData\Local\appium-desktop\app-1.5.0\resources\app\node_modules\appium\node_modules\appium-support\lib\logging.js:69:13)
    at Chromedriver.callee$2$0$ (C:\Users\Administrator\AppData\Local\appium-desktop\app-1.5.0\resources\app\node_modules\appium\node_modules\appium-chromedriver\lib\chromedriver.js:217:13)
    at tryCatch (C:\Users\Administrator\AppData\Local\appium-desktop\app-1.5.0\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:67:40)
    at GeneratorFunctionPrototype.invoke [as _invoke] (C:\Users\Administrator\AppData\Local\appium-desktop\app-1.5.0\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:315:22)
    at GeneratorFunctionPrototype.prototype.(anonymous function) [as throw] (C:\Users\Administrator\AppData\Local\appium-desktop\app-1.5.0\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:100:21)
    at GeneratorFunctionPrototype.invoke (C:\Users\Administrator\AppData\Local\appium-desktop\app-1.5.0\resources\app\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:136:37)
    at <anonymous>

解決方法:
(1) 根據瀏覽器版本更換驅動
(2) 根據驅動版本,更新升級手機中的WebView組件
這裏咱們查看本機控件版本,在UC工具中有顯示:

 

咱們下載對應的驅動:https://npm.taobao.org/mirrors/chromedriver/

 

下載解壓後覆蓋本地appium驅動:
C:\Users\Administrator\AppData\Local\appium-desktop\app-1.5.0\resources\app\node_modules\appium\node_modules\appium-chromedriver\chromedriver\win

 

再次運行腳本執行報錯:

Exception in thread "main" io.appium.java_client.NoSuchContextException: An unknown server-side error occurred while processing the command. Original error: Failed to start Chromedriver session: An unknown server-side error occurred while processing the command. (Original error: unknown error: Failed to forward ports to device 127.0.0.1:62001. No port chosen: . Perhaps your adb version is out of date. ChromeDriver 2.39 and newer require adb version 1.0.38 or newer. Run 'adb version' in your terminal of the host device to find your version of adb.

分析緣由:

adb版本已通過時了。ChromeDriver 2.39及更新版本須要adb 1.0.38或更新版本。在主機設備的終端上運行「adb版本」

C:\Users\Administrator\Desktop>adb version
Android Debug Bridge version 1.0.31

更換adb版本

C:\Users\Administrator\Desktop>adb version
Android Debug Bridge version 1.0.40
Version 4986621
Installed as D:\appium\android-sdk-windows-appium\platform-tools\adb.exe

再次運行腳本執行成功:

5. 擴展:

打開手機中的應用管理.>>找到所有中的WebView

App應用程序是使用系統內置的WebView組件來渲染app內的web頁面的;可是有些應用,如微信和QQ等使用的是騰訊二次開發定製的WebView也就是X5內核,來渲染內嵌web網頁,如公衆號等,能夠根據當前WebView的組件版本下載對應的ChromeDriver.exe驅動 

未完待續................

相關文章
相關標籤/搜索