## 自動化混合應用
Appium 的核心理念之一是,你不應該為了測試而改變被測的應用程序。在這種理念中,可以使用像 Selenium 測試 Web 應用的方式去測試混合應用。Appium 需要知道你是想自動化應用的原生部分還是 Web 視圖,這在技術上有一點復雜。但值得慶幸的是,我們可以繼續使用 Selenium WebDriver 做所有的事。
一旦測試處于 Web 視圖上下文之中,所有 [Selenium](http://www.seleniumhq.org/) [WebDriver API](http://www.seleniumhq.org/docs/03_webdriver.jsp) 指令集都是可用的。
### 進入 Web 視圖上下文(context)
在你的 Appium 測試中,以下是與 Web 視圖通信所必須的幾個步驟:
1. 導航到你的應用程序中的 Web 視圖的部分
1. [取得當前可用的上下文](../commands/context/get-contexts.md)
* 這將返回一個包含我們可以訪問的上下文的列表,例如 `'NATIVE_APP'` 或 `'WEBVIEW_1'`
1. 使用想要訪問的上下文的 id [設置上下文](../commands/context/set-context.md)
* 這會使你的 Appium 會話進入一個模式,
處于該模式時所有命令被解釋為用來自動化 Web 視圖,而不是應用程序的原生部分。
例如,如果你運行 `getElementByTagName`,它將操作 Web 視圖中的 DOM,而不是返回原生元素。
當然,某些 WebDriver 方法只在一個或另一個上下文中有效,所以在錯誤的上下文中執行時你會收到一個報錯信息。
1. 想要停止對 Web 視圖的上下文中自動化,并返回到應用程序的原生部分,簡單的 [設置上下文](../commands/context/set-context.md) 即可。將上下文賦值為原生上下文的 id(通常是 `'NATIVE_APP'`)便可離開 Web 上下文,重新使用原生指令。
### 在會話(session)開啟時自動進入 Web 視圖上下文(context)
如果你的應用程序在 web 視圖中開始,并且你不想在自動化應用程序的原生部分后,再進入 Web 視圖,你可以通過設置「[desired capability](../writing-running-appium/caps.md)」 中的 `autoWebview` 為 `true` 使得 Appium 在會話初始化時自動進入 Web 視圖上下文。
### 示例
```javascript
// javascript
// 假定我們已經為這個應用程序初始化了 `driver` 對象
driver
.contexts().then(function (contexts) { // get list of available views. Returns array: ["NATIVE_APP","WEBVIEW_1"]
return driver.context(contexts[1]); // choose the webview context
})
// 執行一些 web 測試
.elementsByCss('.green_button').click()
.context('NATIVE_APP') // leave webview context
// 如果你想,可以在這做一些原生操作
.quit() // stop webdrivage
```
```java
// java
// 假定我們設置了 capabilities
driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
Set<String> contextNames = driver.getContextHandles();
for (String contextName : contextNames) {
System.out.println(contextName); //prints out something like NATIVE_APP \n WEBVIEW_1
}
driver.context(contextNames.toArray()[1]); // set context to WEBVIEW_1
// 執行一些 web 測試
String myText = driver.findElement(By.cssSelector(".green_button")).click();
driver.context("NATIVE_APP");
// 如果你想,可以在這做一些原生操作
driver.quit();
```
```ruby
# ruby_lib_core
# 假定我們設置了 capabilities
@driver = Appium::Core.for(url: SERVER_URL, desired_capabilities: capabilities).start_driver
# ruby_lib
# opts = { caps: capabilities, appium_lib: { custom_url: SERVER_URL }}
# @driver = Appium::Driver.new(opts, true).start_driver
# 我切換到了最后一個上下文,因為在我們的示例中它總是 webview,在其他情況下,您可能需要明確指定一個上下文
# 在運行 @driver.contexts 期間,查看 appium 日志確定想要的上下文,并找到關聯的 ID
# 使用 @driver.switch_to.context("WEBVIEW_6") 切換到想要的上下文
Given(/^I switch to webview$/) do
webview = @driver.available_contexts.last
@driver.switch_to.context(webview)
end
Given(/^I switch out of webview$/) do
@driver.switch_to.context(@driver.contexts.first)
end
# 現在使用 CSS 選擇器在你的 webview 中選擇一個元素
And(/^I click a webview button $/) do
@driver.find_element(:css, ".green_button").click
end
```
```python
# python
# 假定我們已經為這個應用程序初始化了 `driver` 對象
# 切換到 webview
webview = driver.contexts.last
driver.switch_to.context(webview)
# 執行一些 web 測試
driver.find_element(:css, ".green_button").click
# 切換回原生視圖
driver.switch_to.context(driver.contexts.first)
# 如果你想,可以在這做一些原生操作
driver.quit()
```
```php
// php
// 假定我們已經在這個AppiumTestCase中初始化了 `driver` 對象
public function testThings()
{
$expected_contexts = array(
0 => 'NATIVE_APP',
1 => 'WEBVIEW_1'
);
$contexts = $this->contexts();
$this->assertEquals($expected_contexts, $contexts);
$this->context($contexts[1]);
$context = $this->context();
$this->assertEquals('WEBVIEW_1', $context);
// 做一些關于 web 的事
$this->context('NATIVE_APP');
// 做一些關于原生的事
}
```
### 自動化 Android 的混合應用
Appium [通過 Chromedriver 內建的混合應用支持](../writing-running-appium/web/chromedriver.md) ,使得任何 Chrome 支持的 Android Web 視圖都可以自動化。
不幸的是,你必須在構建應用程序時做額外的一步。正如 Android [遠程調試文檔](https://developers.google.com/web/tools/chrome-devtools/remote-debugging/webviews) 中所述,必須設置 [android.webkit.WebView](http://developer.android.com/reference/android/webkit/WebView.html) 元素的 [setWebContentsDebuggingEnabled](http://developer.android.com/reference/android/webkit/WebView.html#setWebContentsDebuggingEnabled(boolean)) 屬性設置為 `true`。
一旦你設置了 [desired capabilities](../writing-running-appium/caps.md) 并開啟了 Appium 會話,請遵循上面的通用說明。
### 自動化 iOS 的混合應用
Appium 使用自定義的遠程調試器建立連接去與 web 視圖交互。當 Appium 和模擬器和在同一臺機器且對模擬器執行時,此鏈接直接建立到模擬器。Appium 可以自動化 [WkWebView](https://developer.apple.com/documentation/webkit/wkwebview) 和 [UIWebView](https://developer.apple.com/documentation/uikit/uiwebview) 元素。不幸的是,目前還不能處理 [SafariViewController](https://developer.apple.com/documentation/safariservices/sfsafariviewcontroller) 元素。
一旦你設置了 [desired capabilities](../writing-running-appium/caps.md) 并開啟了 Appium 會話,請遵循上面的通用說明。
#### 對真實的 iOS 設備執行
當對真實的 iOS 設備執行時,Appium 不能直接訪問 web 試圖。所以必須通過 USB 線纜連接設備。從版本 1.15 開始,Appium 可以通過 [appium-ios-device](https://github.com/appium/appium-ios-device) 通過本機建立連接。 只有版本低于 1.15 的Appium才需要使用 [ios-webkit-debugger-proxy](https://github.com/google/ios-webkit-debug-proxy) 建立連接。
關于如何安裝并運行 `ios-webkit-debugger-proxy` 的教學,請查看 [iOS webkit 調試代理](../writing-running-appium/web/ios-webkit-debug-proxy.md) 文檔
現在你可以開啟 Appium 測試會話,并遵循上面的通用說明。
本文由 [CrazyForPoor](https://github.com/CrazyForPoor) 翻譯。
- 關于TesterHome和MTSC
- 關于Appium
- 簡介
- Appium 客戶端
- 入門指南
- 已支持的平臺
- API 文檔
- Appium驅動
- XCUITest (iOS)
- XCUITest Real Devices (iOS)
- UIAutomation (iOS)
- UIAutomation Safari Launcher (iOS)
- UIAutomator (Android)
- UIAutomator2 (Android)
- Espresso (Android)
- Windows
- Mac
- Appium命令
- Status
- Execute Mobile Command
- Session
- Create
- End
- Get Session Capabilities
- Go Back
- Screenshot
- Source
- Timeouts
- Timeouts
- Implicit Wait
- Async Script
- Orientation
- Get Orientation
- Set Orientation
- Geolocation
- Get Geolocation
- Set Geolocation
- Logs
- Get Log Types
- Get Logs
- Events
- Log event
- Get events
- Settings
- Update Settings
- Get Device Settings
- Settings
- Update Settings
- Get Device Settings
- Execute Driver Script
- Device
- Activity
- Start Activity
- Current Activity
- Current Package
- App
- Install App
- Is App Installed
- Launch App
- Background App
- Close App
- Reset App
- Remove App
- Activate App
- Terminate App
- Get App State
- Get App Strings
- End Test Coverage
- Clipboard
- Get Clipboard
- Set Clipboard
- Emulator
- Power AC
- Power Capacity
- Files
- Push File
- Pull File
- Pull Folder
- Interactions
- Shake
- Lock
- Unlock
- Is Locked
- Rotate
- Keys
- Press keycode
- Long press keycode
- Hide Keyboard
- Is Keyboard Shown
- Network
- Toggle Airplane Mode
- Toggle Data
- Toggle WiFi
- Toggle Location Services
- Send SMS
- GSM Call
- GSM Signal
- GSM Voice
- Network Speed
- Performance Data
- Get Performance Data
- Performance Data Types
- Screen Recording
- Start Screen Recording
- Stop Screen Recording
- Simulator
- Perform Touch ID
- Toggle Touch ID Enrollment
- System
- Open Notifications
- System Bars
- System Time
- Display density
- Authentication
- Finger Print
- Element
- Find Element
- Find Elements
- Actions
- Click
- Send Keys
- Clear
- Attributes
- Text
- Name
- Attribute
- Selected
- Enabled
- Displayed
- Location
- Size
- Rect
- CSS Property
- Location in View
- Other
- Submit
- Active Element
- Equals Element
- Context
- Get Context
- Get All Contexts
- Set Context
- Interactions
- Mouse
- Move To
- Click
- Double Click
- Button Down
- Button Up
- Touch
- Single Tap
- Double Tap
- Move
- Touch Down
- Touch Up
- Long Press
- Scroll
- Flick
- Multi Touch Perform
- Touch Perform
- W3C Actions
- Web
- Window
- Set Window
- Close Window
- Get Handle
- Get Handles
- Get Title
- Get Window Size
- Set Window Size
- Get Window Position
- Set Window Position
- Maximize Window
- Navigation
- Go to URL
- Get URL
- Back
- Forward
- Refresh
- Storage
- Get All Cookies
- Set Cookie
- Delete Cookie
- Delete All Cookies
- Frame
- Switch to Frame
- Switch to Parent Frame
- Execute Async
- Execute
- 編寫 & 運行Appium腳本
- Running Tests
- Desired Capabilities
- The --default-capabilities flag
- Finding Elements
- Touch Actions
- CLI Arguments
- Server Security
- Web/Web Views
- Mobile Web Testing
- Automating Hybrid Apps
- Using ios-webkit-debug-proxy
- Using Chromedriver
- Image Comparison
- iOS
- Low-Level Insights on iOS Input Events
- XCUITest Mobile Gestures
- XCUITest Mobile App Management
- iOS Pasteboard Guide
- iOS Predicate Guide
- iOS Touch ID Guide
- iOS Install Certificate
- tvOS support
- Pushing/Pulling files
- Audio Capture
- Android
- Low-Level Insights on Android Input Events
- UiSelector Guide
- Espresso Datamatcher Guide
- Android Code Coverage Guide
- Activities Startup Troubleshooting Guide
- How To Execute Shell Commands On The Remote Device
- Android Device Screen Streaming
- How To Emulate IME Actions Generation
- How To Test Android App Bundle
- Other
- Reset Strategies
- Network Connection Guide
- Using Unicode with Appium
- Troubleshooting
- Tutorial
- Swipe Tutorial
- Screen
- Element
- Partial screen
- Simple
- Multiple scroll views
- Add scroll layout
- Tricks and Tips
- Screen
- Element
- Element search
- Fast
- Slow
- Guide
- 進階概念
- 定位圖像中的元素
- 使用定位元素的插件
- 遷移到 XCUITest
- 在 Appium 中使用 Selenium Grid
- Appium Logs Filtering
- 跨域 iframes
- 使用自定義 WDA 服務器
- 使用不同版本的 Xcode 運行
- The Event Timings API
- 并行測試的設置
- The Settings API
- Memory Collection
- 向Appium項目做貢獻
- 從源代碼運行 Appium
- 開發者概述
- 標準開發命令
- Appium 風格指南
- 如何編寫文檔
- Appium 包結構
- 鳴謝