WebDriver Commands and Functions for Mobile Device Testing
You can use WebDriver commands and functions to automate both iOS and Android mobile devices. The free iOS Gateway is required to automate iOS devices. For Android testing, you'll need Appium server and a few other components.
In mobile WebDriver testing, connections are made to a specific app rather than to a browser on your system under test (SUT). Testing with WebDriver is based around interacting with various elements in the app you're testing.
本ドキュメント全体を通じて、以下の基準を構文ボックス内のコード要素を定義するのに使用します。
- 太字:正確に入力する必要がある必須の単語
- イタリック:式または変数要素
- 角括弧[ ]:選択肢のオプションを囲むのに使用します。角括弧内ではいずれか1つのリストを利用できますが、必ず1アイテムを使用する必要があります。リスト内のオプションは縦棒|で区切られます。
- 波括弧{ }:オプションの要素を囲むのに使用します。オプション要素には選択肢を含めることができ、リスト内のアイテムは縦棒|で区切られます。
例:
open file ファイル名{ for [reading | writing | readwrite | appending | updating] }
pattern at {the} [end | ending] of {a | the} word
最初の例では、オプションを示す波括弧の中に一連の選択肢に加えて要素が含まれるため、角括弧が使用されています。 2例目では、2番目の選択肢全体がオプションであるため、(2番目の選択肢に対して)角括弧が省略されています。
WebElement Identifiers
To access a WebElement within the app you're testing, such as when using the FindElement() function or the Click command with a WebDriver connection, you must specify the element you want to interact with. The following WebElement Identifiers can be used to locate elements on a mobile device:
- webID: Use this property to locate an element by the value of its ID attribute.
- webName: Use this property to locate an element by the value of its Name attribute.
- webClassName: Use this property to locate an element by the value of its Class attribute.
- webXPath: Use this property to locate an element by using an XPath expression. For information about XPath and XPath expressions, see XPath in Selenium WebDriver: Complete Tutorial.
- webAccessibilityID: Use this property to locate an element by the value of its Accessibility ID attribute.
WebConnect Command
Behavior: This command makes a connection to a specified app on a mobile device.
Parameters: The WebConnect command can take the following parameters:
- host: (Required) For mobile device connections, this is always localhost.
- port: (Required) The port number over which to create a mobile WebDriver connection. The default port for iOS connections is 8100. The default port for Android connections is 4723.
- name: (Optional) A name to use for referring to this connection. If specified, this is the name that appears in the Connection List.
- platformName: (Required) The operating system of the mobile device.
- bundleId: (iOS only. Required.) The identifier of the mobile app you want to open and test. This parameter is case-sensitive.
- device: (Android only. Required.) The ID number of the device. This can be found by entering adb devices in the command line.
- appPackage: (Android only. Required.) The identifier of the mobile app you want to open and test. This parameter is case-sensitive.
- appActivity: (Android only. Required.) The screen in the app that you want the app to open on. This parameter is case-sensitive.
- automationName: (Android only. Required.) The automation engine to use to test the device.
Syntax:
WebConnect host: "hostAddress", port: "port", {name: "connectionName"} {, platformName: "value"} {, bundleId: "value"} {, device: "value"} {, appPackage: "value"} {, appActivity: "value"} {, automationName: "value"}
例:
WebConnect host:"localhost", port:"8100", platformName: "iOS", bundleId: "com.apple.Maps"
例:
WebConnect host: "localhost", port: "4723", platformName: "Android", device: "04157df49c1d0533", deviceName: "Android", appPackage: "com.google.android.apps.maps", appActivity: "com.google.android.maps.MapsActivity", automationName: "UIAutomator2"
WebDisconnect Command
Behavior: This command disconnects WebDriver connections.
Parameters: None, a connection name, or All. If you do not specify any parameters, the active WebDriver disconnects. Use WebDisconnect with a specific connection name to close that connection, or use All to close all active connections.
Syntax:
例:
WebDisconnect // Disconnects the active WebDriver connection
例:
WebDisconnect All // Disconnects all WebDriver connections
例:
webDisconnect "iphone7"// Disconnects the WebDriver connection named "iphone7"
WebDriver() Function
Behavior: By default, the function returns properties for the active WebDriver connection (if there is one). You can include parameters as specified below to switch to a different open connection (if available) or open a new connection.
When opening a new connection or switching the active connection, the WebDriver() function performs the same actions as the WebConnect command.
Parameters: The WebDriver() function can take the following parameters:
- host: (Required) For mobile device connections, this is always localhost.
- port: (Required) The port number over which to create a mobile WebDriver connection. The default port for iOS connections is 8100. The default port for Android connections is 4723.
-
name: (Optional) A name to use for referring to this connection. If specified, this is the name that appears in the Connection List.
- platformName: (Required) The operating system of the mobile device.
- bundleId: (iOS only. Required.) The identifier of the mobile app you want to open and test. This parameter is case-sensitive.
- device: (Android only. Required.) The ID number of the device. This can be found by entering adb devices in the command line.
- appPackage: (Android only. Required.) The identifier of the mobile app you want to open and test. This parameter is case-sensitive.
- appActivity: (Android only. Required.) The screen in the app that you want the app to open on. This parameter is case-sensitive.
- automationName: (Android only. Required.) The automation engine to use to test the device.
Syntax:
WebDriver (host: hostAddress, port: port, {name: "connectionName"} {, name: "value"} {, platformName: "value"} {, bundleId: "value"} {, device: "value"} {, appPackage: "value"} {, appActivity: "value"} {, automationName: "value"} )
Returns: If you call the WebDriver() function without parameters, it returns the WebDriverConnection object of the active WebDriver connection.
If you open a WebDriver connection with the WebDriver() function, it returns the WebDriverConnection object for the newly created WebDriver connection.
例:
put WebDriver() // Returns information about the active WebDriver connection: <WebDriverConnection with Name:"iphone6plus", PlatformName:"iOS", Host:"localhost", BundleID:"com.testplant.YetAnotherSampleApp">
例:
set driver to WebDriver(host:"localhost", port: "8100", platformName: "iOS", bundleId: "com.testplant.YetAnotherSampleApp")
例:
set driver to WebDriver(host: "localhost", port: "4723", platformName: "Android", appPackage:"com.google.android.apps.maps", appActivity:"com.google.android.maps.MapsActivity", automationName:"UIAutomator2")
FindElement() Function
Behavior: This command locates a specific WebElement that you can interact with.
Parameters: The WebElement Identifier of the element that you want to find.
Syntax:
例:
set textField to findElement (webName: "Enter Text Field") // Creates the variable textField containing the element with the WebName "Enter Text Field"
SwipeDown, SwipeLeft, SwipeRight, SwipeUp commands
Behavior: These commands execute a directional swipe on the specified element.
Parameters: The WebElement Identifier of the element to execute the swipe on.
Syntax:
SwipeDown (identifierType: "value")
SwipeDown (identifierType: "value")
SwipeLeft (identifierType: "value")
SwipeRight (identifierType: "value")
例:
SwipeUp (webClassName: "XCUIElementTypeMap")
ClearKeys Command
Behavior: This command clears the text entered in the specified WebElement.
Parameters: The WebElement Identifier of the element to clear.
Syntax:
例:
ClearKeys (webName: "Enter Text Field") // Clears text from the element with the webName "Enter Text Field"
例:
set textField to findElement(webName: "Enter Text Field") // Puts the name of a WebElement into a variable
ClearKeys textField // Clears text from the WebElement
SendKeys Command
Behavior: This command sends keystrokes to an element on the mobile device.
Parameters: A text string enclosed in quotation marks. If you need to include quote characters in your text string, you can enclose the text in double angle brackets (<<>>).
Syntax:
例:
SendKeys "Hello World!" // Sends text to the active element
例:
set textField to findElement (webName: "Enter Text Field") // Puts the name of a WebElement into a variable
SendKeys textField, "A nod's as good as a wink to a blind bat" // Uses the variable to make the WebElement active, then sends the keystrokes to type: "A nod's as good as a wink to a blind bat"
Tap Command
Behavior: The Tap command locates an element within the DOM and simulates a tap event at the found location.
Parameters: The WebElement Identifier of the element to tap.
Syntax:
例:
Tap (webName: "Map") // Locates the "Map" element and executes a tap
DoubleTap Command
Behavior: The DoubleTap command locates an element within the DOM and simulates a double-tap event at the found location.
Parameters: The WebElement Identifier of the element to double-tap.
Syntax:
例:
DoubleTap (webClassName: "XCUIElementTypeMap") // Locates the element with class name "XCUIElementTypeMap" and executes a double-tap
WebScreenshot Command
Behavior: This command takes a screenshot of the mobile device with the active WebDriver connection and saves it according to the specified file name or full file path.
Parameters: A required file name that can optionally include the full file path of the desired save location. If no file path is included in the filename parameter, the screenshot is saved into the user's documents directory.
Syntax:
例:
WebScreenshot "shot1" // Captures a WebDriver screenshot for the active WebDriverConnection and saves it with the name 'shot1'
例:
WebScreenshot "/Users/admin/Desktop/shot5" // Saves a screenshot to the Desktop with name 'shot5'
GetDeviceOrientation() Function
Behavior: This function returns the current rotation of the screen of the device with the active WebDriver connection.
パラメータ:なし。
Syntax:
Returns: The possible values are the same as the parameters for the setDeviceOrientation command: Landscape, LandscapeRight, Portrait, PortraitUpsideDown.
例:
put GetDeviceOrientation() // Returns the orientation of the device screen
例:
put "Device rotated to " & GetDeviceOrientation() // Returns "Device rotated to" the orientation of the device screen
Related:
SetDeviceOrientation Command
Behavior: Changes the orientation of the device with the active WebDriver connection to what you specify with the parameter. The different orientations work only if the active app and operating system support them.
Parameters: The device orientation that you want to set. Options are Portrait, PortraitUpsideDown, Landscape, LandscapeRight. Note that Android devices accept only Portrait and Landscape. If the requested orientation isn't supported by the operating system or app, no error is returned and the screen remains in its previous orientation.
Syntax:
例:
SetDeviceOrientation LandscapeRight
Related: