Skip to content

Commit ff0b2d8

Browse files
authored
Merge pull request #13 from browserstack/w3c
Add w3c support
2 parents 9623ca2 + 0ae84cb commit ff0b2d8

6 files changed

Lines changed: 131 additions & 127 deletions

File tree

README.md

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,53 @@
22

33
This repository demonstrates how to run Appium Python tests on BrowserStack App Automate.
44

5+
## Based on
6+
7+
These code samples are currently based on:
8+
9+
- **Appium-Python-Client:** `2.6.1`
10+
- **Protocol:** `W3C`
11+
512
## Setup
613

714
### Requirements
815

9-
1. Python 3.6+ or Python 2.7+
10-
11-
- For Windows, download latest python version from [here](https://www.python.org/downloads/windows/) and run the installer executable
12-
- For Mac and Linux, run `python --version` to see what python version is pre-installed. If you want a different version download from [here](https://www.python.org/downloads/)
16+
1. Python 3.7+
1317

14-
2. Package Manager pip
18+
> **_NOTE:_** Since v1.0.0, only Python 3.7+ is supported.
19+
20+
- For Windows, download latest python version from [here](https://www.python.org/downloads/windows/) and run the installer executable
21+
- For Mac and Linux, run `python --version` to see what python version is pre-installed. If you want a different version download from [here](https://www.python.org/downloads/)
1522

16-
Note : `pip` comes installed with Python 2.7.9+ and python 3.4+
23+
2. Package Manager pip
1724

18-
- If `pip` is not installed, follow these instructions:
19-
- Securely download get-pip.py by following this link: [get-pip.py](https://bootstrap.pypa.io/get-pip.py) or use following cURL command to download it:
25+
Note : `pip` comes installed with python 3.4+
2026

21-
```sh
22-
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
23-
```
27+
- If `pip` is not installed, follow these instructions:
2428

25-
- After dowloading, run the file :
29+
- Securely download get-pip.py by following this link: [get-pip.py](https://bootstrap.pypa.io/get-pip.py) or use following cURL command to download it:
2630

27-
- For Python 3
31+
```sh
32+
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
33+
```
2834

29-
```sh
30-
python3 get-pip.py
31-
```
35+
- After dowloading, run the file :
3236

33-
- For Python 2
37+
- For Python 3
3438

35-
```sh
36-
python2 get-pip.py
37-
```
39+
```sh
40+
python3 get-pip.py
41+
```
3842

3943
### Install the dependencies
4044

4145
To install the dependencies, run the following command in project's base directory:
4246
4347
- For Python 3
4448
45-
```sh
46-
pip3 install -r requirements.txt
47-
```
48-
49-
- For Python 2
50-
51-
```sh
52-
pip2 install -r requirements.txt
53-
```
49+
```sh
50+
pip3 install -r requirements.txt
51+
```
5452
5553
## Getting Started
5654
@@ -72,7 +70,6 @@ Ensure that @ symbol is prepended to the file path in the above request. Please
7270
7371
**Note**: If you do not have an .apk or .ipa file and are looking to simply try App Automate, you can download and test using our [sample Android app](https://www.browserstack.com/app-automate/sample-apps/android/WikipediaSample.apk) or [sample iOS app](https://www.browserstack.com/app-automate/sample-apps/ios/BStackSampleApp.ipa).
7472
75-
7673
**2. Configure and run your first test**
7774
7875
Open `browserstack_sample.py` file in `Android` or `iOS` folder :
@@ -107,7 +104,6 @@ Ensure that @ symbol is prepended to the file path in the above request. Please
107104
108105
**Note**: If you do not have an .apk or .ipa file and are looking to simply try App Automate, you can download and test using our [sample Android Local app](https://www.browserstack.com/app-automate/sample-apps/android/LocalSample.apk) or [sample iOS Local app](https://www.browserstack.com/app-automate/sample-apps/ios/LocalSample.ipa).
109106
110-
111107
**2. Configure and run your local test**
112108
113109
Open `browserstack_sample_local` file in `Android` or `iOS` folder :
@@ -118,7 +114,7 @@ Open `browserstack_sample_local` file in `Android` or `iOS` folder :
118114
119115
- Set the device and OS version
120116
121-
- Ensure that `browserstack.local` capability is set to `true`. Within the test script, there is code snippet that automatically establishes Local Testing connection to BrowserStack servers using Python binding for BrowserStack Local.
117+
- Ensure that `local` capability is set to `true`. Within the test script, there is code snippet that automatically establishes Local Testing connection to BrowserStack servers using Python binding for BrowserStack Local.
122118
123119
- If you have uploaded your own app update the test case
124120

android/browserstack_sample.py

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,52 @@
11
from appium import webdriver
2-
from appium.webdriver.common.mobileby import MobileBy
2+
from appium.options.android import UiAutomator2Options
3+
from appium.webdriver.common.appiumby import AppiumBy
34
from selenium.webdriver.support.ui import WebDriverWait
45
from selenium.webdriver.support import expected_conditions as EC
56
import time
67

7-
desired_cap = {
8-
# Set your access credentials
9-
"browserstack.user" : "YOUR_USERNAME",
10-
"browserstack.key" : "YOUR_ACCESS_KEY",
8+
# Options are only available since client version 2.3.0
9+
# If you use an older client then switch to desired_capabilities
10+
# instead: https://github.com/appium/python-client/pull/720
11+
options = UiAutomator2Options().load_capabilities({
12+
# Specify device and os_version for testing
13+
"platformName" : "android",
14+
"platformVersion" : "9.0",
15+
"deviceName" : "Google Pixel 3",
1116

1217
# Set URL of the application under test
1318
"app" : "bs://<app-id>",
1419

15-
# Specify device and os_version for testing
16-
"device" : "Google Pixel 3",
17-
"os_version" : "9.0",
18-
1920
# Set other BrowserStack capabilities
20-
"project" : "First Python project",
21-
"build" : "browserstack-build-1",
22-
"name" : "first_test"
23-
}
21+
'bstack:options' : {
22+
"projectName" : "First Python project",
23+
"buildName" : "browserstack-build-1",
24+
"sessionName" : "BStack first_test",
25+
26+
# Set your access credentials
27+
"userName" : "YOUR_USERNAME",
28+
"accessKey" : "YOUR_ACCESS_KEY"
29+
}
30+
})
2431

2532
# Initialize the remote Webdriver using BrowserStack remote URL
26-
# and desired capabilities defined above
27-
driver = webdriver.Remote(
28-
command_executor="http://hub-cloud.browserstack.com/wd/hub",
29-
desired_capabilities=desired_cap
30-
)
33+
# and options defined above
34+
driver = webdriver.Remote("http://hub.browserstack.com/wd/hub", options=options)
3135

32-
# Test case for the BrowserStack sample Android app.
33-
# If you have uploaded your app, update the test case here.
36+
# Test case for the BrowserStack sample Android app.
37+
# If you have uploaded your app, update the test case here.
3438
search_element = WebDriverWait(driver, 30).until(
35-
EC.element_to_be_clickable((MobileBy.ACCESSIBILITY_ID, "Search Wikipedia"))
39+
EC.element_to_be_clickable((AppiumBy.ACCESSIBILITY_ID, "Search Wikipedia"))
3640
)
3741
search_element.click()
3842
search_input = WebDriverWait(driver, 30).until(
39-
EC.element_to_be_clickable((MobileBy.ID, "org.wikipedia.alpha:id/search_src_text"))
43+
EC.element_to_be_clickable(
44+
(AppiumBy.ID, "org.wikipedia.alpha:id/search_src_text"))
4045
)
4146
search_input.send_keys("BrowserStack")
4247
time.sleep(5)
43-
search_results = driver.find_elements_by_class_name("android.widget.TextView")
44-
assert(len(search_results) > 0)
48+
search_results = driver.find_elements(AppiumBy.CLASS_NAME, "android.widget.TextView")
49+
assert (len(search_results) > 0)
4550

4651
# Invoke driver.quit() after the test is done to indicate that the test is completed.
4752
driver.quit()

android/browserstack_sample_local.py

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from appium import webdriver
2-
from appium.webdriver.common.mobileby import MobileBy
2+
from appium.options.android import UiAutomator2Options
3+
from appium.webdriver.common.appiumby import AppiumBy
34
from selenium.webdriver.support.ui import WebDriverWait
45
from selenium.webdriver.support import expected_conditions as EC
56
from browserstack.local import Local
@@ -9,25 +10,28 @@
910
userName = "YOUR_USERNAME"
1011
accessKey = "YOUR_ACCESS_KEY"
1112

12-
desired_caps = {
13-
"browserstack.user" : userName,
14-
"browserstack.key" : accessKey,
15-
13+
# Options are only available since client version 2.3.0
14+
# If you use an older client then switch to desired_capabilities
15+
# instead: https://github.com/appium/python-client/pull/720
16+
options = UiAutomator2Options().load_capabilities({
1617
# Set URL of the application under test
17-
"app" : "<bs://app-id>",
18+
"app" : "bs://<app-id>",
1819

1920
# Specify device and os_version for testing
20-
"device" : "Google Pixel 3",
21-
"os_version" : "9.0",
22-
23-
#Set BrowserStack Local capability as True
24-
"browserstack.local": True,
21+
"deviceName": "Google Pixel 3",
22+
"platformName": "android",
23+
"platformVersion": "9.0",
2524

2625
# Set other BrowserStack capabilities
27-
"project" : "First Python Local project",
28-
"build" : "browserstack-build-1",
29-
"name" : "local_test"
30-
}
26+
"bstack:options": {
27+
"userName" : userName,
28+
"accessKey" : accessKey,
29+
"projectName" : "First Python Local project",
30+
"buildName" : "browserstack-build-1",
31+
"sessionName" : "BStack local_test",
32+
"local" : "true"
33+
}
34+
})
3135

3236
bs_local = None
3337

@@ -45,23 +49,20 @@ def stop_local():
4549
start_local()
4650

4751
# Initialize the remote Webdriver using BrowserStack remote URL
48-
# and desired capabilities defined above
49-
driver = webdriver.Remote(
50-
command_executor="http://hub-cloud.browserstack.com/wd/hub",
51-
desired_capabilities=desired_caps
52-
)
52+
# and options defined above
53+
driver = webdriver.Remote("http://hub.browserstack.com/wd/hub", options=options)
5354

5455
# Test case for the BrowserStack sample Android app.
5556
# If you have uploaded your app, update the test case here.
5657
test_button = WebDriverWait(driver, 30).until(
57-
EC.element_to_be_clickable((MobileBy.ID, "com.example.android.basicnetworking:id/test_action"))
58+
EC.element_to_be_clickable((AppiumBy.ID, "com.example.android.basicnetworking:id/test_action"))
5859
)
5960
test_button.click()
6061
WebDriverWait(driver, 30).until(
61-
EC.element_to_be_clickable((MobileBy.CLASS_NAME, "android.widget.TextView"))
62+
EC.element_to_be_clickable((AppiumBy.CLASS_NAME, "android.widget.TextView"))
6263
)
6364
test_element = None
64-
search_results = driver.find_elements_by_class_name("android.widget.TextView")
65+
search_results = driver.find_elements(AppiumBy.CLASS_NAME,"android.widget.TextView")
6566
for result in search_results:
6667
if result.text.__contains__("The active connection is"):
6768
test_element = result

ios/browserstack_sample.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,49 @@
11
from appium import webdriver
2-
from appium.webdriver.common.mobileby import MobileBy
2+
from appium.options.ios import XCUITestOptions
3+
from appium.webdriver.common.appiumby import AppiumBy
34
from selenium.webdriver.support.ui import WebDriverWait
45
from selenium.webdriver.support import expected_conditions as EC
56
import time
67

7-
desired_cap = {
8-
# Set your access credentials
9-
"browserstack.user" : "YOUR_USERNAME",
10-
"browserstack.key" : "YOUR_ACCESS_KEY",
11-
8+
# Options are only available since client version 2.3.0
9+
# If you use an older client then switch to desired_capabilities
10+
# instead: https://github.com/appium/python-client/pull/720
11+
options = XCUITestOptions().load_capabilities({
1212
# Set URL of the application under test
1313
"app" : "bs://<app-id>",
1414

1515
# Specify device and os_version for testing
16-
"device" : "iPhone 11 Pro",
17-
"os_version" : "13",
18-
16+
"deviceName": "iPhone 11 Pro",
17+
"platformName": "ios",
18+
"platformVersion": "13",
19+
1920
# Set other BrowserStack capabilities
20-
"project" : "First Python project",
21-
"build" : "browserstack-build-1",
22-
"name" : "first_test"
23-
}
21+
"bstack:options": {
22+
"userName" : "YOUR_USERNAME",
23+
"accessKey" : "YOUR_ACCESS_KEY",
24+
"projectName" : "First Python project",
25+
"buildName" : "browserstack-build-1",
26+
"sessionName" : "BStack first_test"
27+
}
28+
})
2429

2530
# Initialize the remote Webdriver using BrowserStack remote URL
26-
# and desired capabilities defined above
27-
driver = webdriver.Remote(
28-
command_executor="http://hub-cloud.browserstack.com/wd/hub",
29-
desired_capabilities=desired_cap
30-
)
31+
# and options defined above
32+
driver = webdriver.Remote("http://hub.browserstack.com/wd/hub", options=options)
3133

32-
# Test case for the BrowserStack sample Android app.
34+
# Test case for the BrowserStack sample iOS app.
3335
# If you have uploaded your app, update the test case here.
3436
text_button = WebDriverWait(driver, 30).until(
35-
EC.element_to_be_clickable((MobileBy.ACCESSIBILITY_ID, "Text Button"))
37+
EC.element_to_be_clickable((AppiumBy.ACCESSIBILITY_ID, "Text Button"))
3638
)
3739
text_button.click()
3840
text_input = WebDriverWait(driver, 30).until(
39-
EC.element_to_be_clickable((MobileBy.ACCESSIBILITY_ID, "Text Input"))
41+
EC.element_to_be_clickable((AppiumBy.ACCESSIBILITY_ID, "Text Input"))
4042
)
4143
text_input.send_keys("hello@browserstack.com"+"\n")
4244
time.sleep(5)
4345
text_output = WebDriverWait(driver, 30).until(
44-
EC.element_to_be_clickable((MobileBy.ACCESSIBILITY_ID, "Text Output"))
46+
EC.element_to_be_clickable((AppiumBy.ACCESSIBILITY_ID, "Text Output"))
4547
)
4648
if text_output!=None and text_output.text=="hello@browserstack.com":
4749
assert True

0 commit comments

Comments
 (0)