## 元素查找插件
從1.9.2版本開始,Appium支持使用插件來輔助查找元素,通過使用 `-custom` 定位策略。 目前此功能為實驗性功能。
### 用法
1. 安裝基于 Appium 元素查找插件標準開發的第三方元素查找插件 (參見下文)。 (必須是Node模塊,通過NPM安裝或者本地安裝)。插件可以安裝在你系統上的任意位置,不過主要有三種方式:
* 獨立于Appium的目錄 (通過在任意文件夾運行 `npm install <plugin>`)
* 在Appium依賴樹下 (通過在Appium根目錄下運行`npm install <plugin>`)
* 在系統上全局安裝 (通過運行 `npm install -g <plugin>`)
(當然,插件本身可能有安裝和部署說明,在插件的文檔中會有詳細說明。)
2. 在你的測試中添加一個新的capability:`customFindModules`。 這個capability必須是一個對象,至少有一個鍵和一個值。鍵名為 "插件快捷鍵",值為"插件引用"。 例如:
```
{
"customFindModules": {
"plug": "my-element-finding-plugin"
}
}
```
"plug"是快捷方式,而"my-element-finding-plugin"是引用。在你的測試代碼中只需要使用快捷方式,可以是滿足JSON鍵名條件的任意字符串。引用必須是插件的Node模塊引用,而且Appium可以通過Node的[模塊獲取方法](https://medium.freecodecamp.org/requiring-modules-in-node-js-everything-you-need-to-know-e7fbd119be8)來[獲取](https://nodejs.org/api/modules.html#modules_require)。
一旦使用上述capability來啟動一個session,可以說插件(或者多個插件---多插件當然是支持的)已經被注冊上了。你可以通過下面的步驟來使用已注冊的插件查找元素:
1. 使用`-custom` 定位策略
2. 在你的選擇器前加上`<快捷方式(shortcut)>:`
因此,對于上面例子中的插件,如果你想使用"foo"選擇器來查找元素,可以這樣寫(在客戶端代碼中):
```js
driver.findElement('-custom', 'plug:foo');
```
換句話說,使用`-custom`定位策略,并發送`foo`作為選擇器,從而確保Appium知道應該使用`plug`插件來處理查找元素請求。
如果只有一個插件注冊了,可以在選擇器中省略插件快捷方式(因為Appium不會混淆你想使用哪個插件):
```js
driver.findElement('-custom', 'foo');
```
目前并非所有的Appium客戶端都很好地支持了`-custom`定位策略;查看客戶端文檔獲取此策略的正確調用方式。
### 開發插件
任何人都可以開發Appium元素查找插件。只需符合下列規則:
* 插件必須是一個Node模塊,且有一個名為`find`的導出(export)
* 當被調用的時候返回元素對象列表(可以為空)
當Appium調用你的`find`方法時,它會傳遞下列參數:
* 一個`driver`對象實例,代表當前session(比如`XCUITestDriver`實例)
* 一個日志對象,可以寫入Appium日志
* 選擇器(字符串),用戶傳入用于查找元素
* 一個布爾值:是否查找多個元素(true)或(false)。注意你必須返回一個數組,不管用戶是否要查找多個元素。傳遞這個標志位是為了針對不需要返回多個元素的查詢進行優化。
這就是目前的全部內容! 具體示例可查看以下已知插件列表。
### 已知插件列表
* [Test.ai Classifier](https://github.com/testdotai/appium-classifier-plugin)
- 關于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 包結構
- 鳴謝