不一樣類型的應用 | 區別 |
---|---|
native app | 純原聲app,Android用Java些,iOS用object c寫 |
hybrid app | 套用原聲應用的外殼,既有原生的UI頁面,又經過內置web view組件(至關於內置瀏覽器),能夠訪問本地和遠程的html文件,Native Hardware Access能夠訪問本地文件和經過js腳本或html調用本地的函數方法。 |
web app | 只靠瀏覽器訪問使用 |
微信小程序 | 微信內層嵌入的組件 |
模擬器默認支持
真機須要打開app內開關css
//必須從您的應用啓用web view調試。要啓用web view調試,請在web view類上調用靜態方法setWebContentsDebuggingEnabled。 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){ WebView.setWebContentDebuggingEnabled(true) }
@Test public void testWebView1() throws InterruptedException{ System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName()); WebDriverWait wait = new WebDriverWait(driver,10); String jiaoyi = "//*[@text='交易']"; wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(jiaoyi))); for(int i=0;i<3;i++) { System.out.println("首頁: " + driver.getContextHandles());//查看當前頁面有幾個上下文 Thread.sleep(2000); } driver.findElementByXPath(jiaoyi).click(); for(int i=0;i<3;i++){ System.out.println("交易: "+driver.getContextHandles()); Thread.sleep(2000); } }
模擬器
使用真機,不會打印web view
html
@Test public void testWebview3() throws InterruptedException{ System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName()); WebDriverWait wait = new WebDriverWait(driver,10); WebElement el = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//android.widget.TextView[@text=\"App\"]"))); WebElement list = driver.findElement(By.id("android:id/list")); MobileElement webview1 = list.findElement(MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView("+"new UiSelector().text(\"Views\"));")); driver.findElementByXPath("//android.widget.TextView[@text=\"Views\"]").click(); MobileElement webview = list.findElement(MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView("+"new UiSelector().text(\"WebView\"));")); driver.findElementByXPath("//android.widget.TextView[@text=\"WebView\"]").click(); for(int i=0;i<5;i++){ System.out.println(driver.getContextHandles()); Thread.sleep(2000); } }
使用模擬器:
java
使用真機
python
public void testWebview2(){ System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName()); WebDriverWait wait = new WebDriverWait(driver,10); String jiaoyi = "//*[@text='交易']"; wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(jiaoyi))); driver.findElementByXPath(jiaoyi).click(); System.out.println("Native page source:\n"+driver.getPageSource()); driver.context("WEBVIEW_com.xueqiu.android"); System.out.println("webview page source:\n"+driver.getPageSource()); }
native爲xml
webview是html
android
#代碼有錯誤 # 切換到web view webview = driver.contexts.last driver.switch_to.context(webview) #點擊頁面中class爲green_button的控件 driver.find_element(:css,".green_button").click #切回原生頁面 driver.switch_to.context(driver.contexts.first) driver.quit()
關於web view測試的內容等待補充git
io.appium.java_client.NoSuchContextExcetpion: An unknown server-side error occurred while processing the command. Original error: Failed to start Chromedriver session: A new session could not be created. (Original error:session not created exception:Chrome version must be >= 64.0.3282.0
(Driver info:chromedriver=2.37.544337 (8c0344a12e552148c185f7d5117db1f28d6c9e85), platform=Mac OS X 10.13.3 X86-64))
當前Chromedriver版本爲2.37對應Chrome版本64. Chrome版本低於64,session建立失敗。在github中appium下搜索Chrome version,找到chromedriver.md文件,查看Chrome和driver對應關係
3.1 查看Chrome版本
方法1:開發者模式-WebView實現-查看Chrome版本
方法2:使用命令github
adb -s 406e8f3 shell pm list packages|grep webview adb -s 406e8f3 shell dumpsys package com.google.android.webview|grep versionName
webview版本爲62.0,下載對應的2.35版本的driver便可。web
appium -g /tmp/csj527.log --session-override --chromedriver-executable /users/csj/Downloads/chromedriver.2.35/chromedriver