Skip to content

Commit eb96f86

Browse files
authored
Updated first test script for Android and iOS according to QSG Python spec (#9)
* Update first test and Readme as per the QSG spec * Update webdriver remote URL * Providing access credentials from appium capabilities * Correcting Selenium dependency version * Updating readme with dashboard link * Keeping a single requirements.txt file * Removing Selenium dependency * Updated readme * Updated code changes for Local test * Setting browserstack.local capability to True * Updating Github Readme with Local test * Readme styling changes * Readme styling changes * Readme styling changes * Readme text update * Update README.md * Update README.md * Update README.md * Update README.md * Changing file names according to QSG * Updated readme * updating 'desired_caps' to 'desired_cap' to match with QSG * BrowserStack access credentials URL in readme.
1 parent b19994d commit eb96f86

12 files changed

Lines changed: 300 additions & 170 deletions

README.md

Lines changed: 135 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,140 @@
11
# python-appium-app-browserstack
2-
App Automate Python Samples
3-
---------------------
42

5-
This repository contains code for Automated Native App tests. Please feel free to clone the repo and use the example code.
6-
Refer [Getting Started with App Automate](https://www.browserstack.com/app-automate/appium-python) for more details.
3+
This repository demonstrates how to run Appium Python tests on BrowserStack App Automate.
4+
5+
## Setup
6+
7+
### Requirements
8+
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/)
13+
14+
2. Package Manager pip
15+
16+
Note : `pip` comes installed with Python 2.7.9+ and python 3.4+
17+
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:
20+
21+
```sh
22+
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
23+
```
24+
25+
- After dowloading, run the file :
26+
27+
- For Python 3
28+
29+
```sh
30+
python3 get-pip.py
31+
```
32+
33+
- For Python 2
34+
35+
```sh
36+
python2 get-pip.py
37+
```
38+
39+
### Install the dependencies
40+
41+
To install the dependencies, run the following command in project's base directory:
42+
43+
- For Python 3
44+
45+
```sh
46+
pip3 install -r requirements.txt
47+
```
48+
49+
- For Python 2
50+
51+
```sh
52+
pip2 install -r requirements.txt
53+
```
54+
55+
## Getting Started
56+
57+
Getting Started with Appium tests in Python on BrowserStack couldn't be easier!
58+
59+
### Run your first test :
60+
61+
**1. Upload your Android or iOS App**
62+
63+
Upload your Android app (.apk or .aab file) or iOS app (.ipa file) to BrowserStack servers using our REST API. Here is an example cURL request :
64+
65+
```
66+
curl -u "YOUR_USERNAME:YOUR_ACCESS_KEY" \
67+
-X POST "https://api-cloud.browserstack.com/app-automate/upload" \
68+
-F "file=@/path/to/apk/file"
69+
```
70+
71+
Ensure that @ symbol is prepended to the file path in the above request. Please note the `app_url` value returned in the API response. We will use this to set the application under test while configuring the test later on.
72+
73+
**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).
74+
75+
76+
**2. Configure and run your first test**
77+
78+
Open `browserstack_sample.py` file in `Android` or `iOS` folder :
79+
80+
- Replace `YOUR_USERNAME` & `YOUR_ACCESS_KEY` with your BrowserStack access credentials. Get your BrowserStack access credentials from [here](https://www.browserstack.com/accounts/settings)
81+
82+
- Replace `bs://<app-id>` wkth the URL obtained from app upload step
83+
84+
- Set the device and OS version
85+
86+
- If you have uploaded your own app update the test case
87+
88+
- Run `python browserstack_sample.py`
89+
90+
- You can access the test execution results, and debugging information such as video recording, network logs on [App Automate dashboard](https://app-automate.browserstack.com/dashboard)
91+
92+
---
93+
94+
### **Use Local testing for apps that access resources hosted in development or testing environments :**
95+
96+
**1. Upload your Android or iOS App**
97+
98+
Upload your Android app (.apk or .aab file) or iOS app (.ipa file) that access resources hosted on your internal or test environments to BrowserStack servers using our REST API. Here is an example cURL request :
99+
100+
```
101+
curl -u "YOUR_USERNAME:YOUR_ACCESS_KEY" \
102+
-X POST "https://api-cloud.browserstack.com/app-automate/upload" \
103+
-F "file=@/path/to/apk/file"
104+
```
105+
106+
Ensure that @ symbol is prepended to the file path in the above request. Please note the `app_url` value returned in the API response. We will use this to set the application under test while configuring the test later on.
107+
108+
**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).
109+
110+
111+
**2. Configure and run your local test**
112+
113+
Open `browserstack_sample_local` file in `Android` or `iOS` folder :
114+
115+
- Replace `YOUR_USERNAME` & `YOUR_ACCESS_KEY` with your BrowserStack access credentials. Get your BrowserStack access credentials from [here](https://www.browserstack.com/accounts/settings)
116+
117+
- Replace `bs://<app-id>` wkth the URL obtained from app upload step
118+
119+
- Set the device and OS version
120+
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.
122+
123+
- If you have uploaded your own app update the test case
124+
125+
- Run `python browserstack_sample_local.py`
126+
127+
- You can access the test execution results, and debugging information such as video recording, network logs on [App Automate dashboard](https://app-automate.browserstack.com/dashboard)
128+
129+
## Integration with other python frameworks
130+
131+
For other Python frameworks samples, refer to following repositories :
7132
8-
For frameworks integration with BrowserStack, refer to their individual repositories -
9-
- [Lettuce](https://github.com/browserstack/lettuce-appium-app-browserstack)
10133
- [Behave](https://github.com/browserstack/behave-appium-app-browserstack)
134+
- [Lettuce](https://github.com/browserstack/lettuce-appium-app-browserstack)
135+
136+
Note: For other test frameworks supported by App-Automate refer our [Developer documentation](https://www.browserstack.com/docs/)
137+
138+
## Getting Help
11139
12-
For python 2 support refer the branch
13-
- [python-2-master](https://github.com/browserstack/python-appium-app-browserstack/tree/python-2-master)
140+
If you are running into any issues or have any queries, please check [Browserstack Support page](https://www.browserstack.com/support/app-automate) or [get in touch with us](https://www.browserstack.com/contact?ref=help).

android/BrowserStackAndroid.py

Lines changed: 0 additions & 32 deletions
This file was deleted.

android/README.md

Lines changed: 0 additions & 34 deletions
This file was deleted.

android/browserstack_sample.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from appium import webdriver
2+
from appium.webdriver.common.mobileby import MobileBy
3+
from selenium.webdriver.support.ui import WebDriverWait
4+
from selenium.webdriver.support import expected_conditions as EC
5+
import time
6+
7+
desired_cap = {
8+
# Set your access credentials
9+
"browserstack.user" : "YOUR_USERNAME",
10+
"browserstack.key" : "YOUR_ACCESS_KEY",
11+
12+
# Set URL of the application under test
13+
"app" : "bs://<app-id>",
14+
15+
# Specify device and os_version for testing
16+
"device" : "Google Pixel 3",
17+
"os_version" : "9.0",
18+
19+
# Set other BrowserStack capabilities
20+
"project" : "First Python project",
21+
"build" : "Python Android",
22+
"name" : "first_test"
23+
}
24+
25+
# 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+
32+
# Test case for the BrowserStack sample Android app.
33+
# If you have uploaded your app, update the test case here.
34+
search_element = WebDriverWait(driver, 30).until(
35+
EC.element_to_be_clickable((MobileBy.ACCESSIBILITY_ID, "Search Wikipedia"))
36+
)
37+
search_element.click()
38+
search_input = WebDriverWait(driver, 30).until(
39+
EC.element_to_be_clickable((MobileBy.ID, "org.wikipedia.alpha:id/search_src_text"))
40+
)
41+
search_input.send_keys("BrowserStack")
42+
time.sleep(5)
43+
search_results = driver.find_elements_by_class_name("android.widget.TextView")
44+
assert(len(search_results) > 0)
45+
46+
# Invoke driver.quit() after the test is done to indicate that the test is completed.
47+
driver.quit()
Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,28 @@
55
from browserstack.local import Local
66
import time
77

8-
userName = "BROWSERSTACK_USERNAME"
9-
accessKey = "BROWSERSTACK_ACCESS_KEY"
8+
# Set your BrowserStack access credentials here
9+
userName = "YOUR_USERNAME"
10+
accessKey = "YOUR_ACCESS_KEY"
1011

1112
desired_caps = {
12-
"build": "Python 3 Android Local",
13-
"device": "Samsung Galaxy S7",
13+
"browserstack.user" : userName,
14+
"browserstack.key" : accessKey,
15+
16+
# Set URL of the application under test
17+
"app" : "<bs://app-id>",
18+
19+
# 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
1424
"browserstack.local": True,
15-
"app": "bs://<hashed app-id>"
25+
26+
# Set other BrowserStack capabilities
27+
"project" : "First Python Local project",
28+
"build" : "Python Android Local",
29+
"name" : "local_test"
1630
}
1731

1832
bs_local = None
@@ -27,29 +41,38 @@ def stop_local():
2741
global bs_local
2842
bs_local.stop()
2943

44+
# Start BrowserStack local binary
3045
start_local()
31-
driver = webdriver.Remote("http://" + userName + ":" + accessKey + "@hub-cloud.browserstack.com/wd/hub", desired_caps)
46+
47+
# 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+
)
53+
54+
# Test case for the BrowserStack sample Android app.
55+
# If you have uploaded your app, update the test case here.
3256
test_button = WebDriverWait(driver, 30).until(
3357
EC.element_to_be_clickable((MobileBy.ID, "com.example.android.basicnetworking:id/test_action"))
3458
)
3559
test_button.click()
3660
WebDriverWait(driver, 30).until(
3761
EC.element_to_be_clickable((MobileBy.CLASS_NAME, "android.widget.TextView"))
3862
)
39-
4063
test_element = None
4164
search_results = driver.find_elements_by_class_name("android.widget.TextView")
4265
for result in search_results:
4366
if result.text.__contains__("The active connection is"):
4467
test_element = result
45-
4668
if test_element is None:
4769
raise Exception("Cannot find the needed TextView element from app")
48-
4970
matched_string = test_element.text
5071
print (matched_string)
5172
assert(matched_string.__contains__("The active connection is wifi"))
5273
assert(matched_string.__contains__("Up and running"))
5374

75+
# Invoke driver.quit() after the test is done to indicate that the test is completed.
5476
driver.quit()
77+
5578
stop_local()

android/requirements.txt

Lines changed: 0 additions & 3 deletions
This file was deleted.

ios/BrowserStackIOS.py

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)