Skip to content

Commit 6933175

Browse files
authored
Merge pull request #1 from browserstack/app_automate_tests
Add App Automate tests
2 parents 9147d25 + 0d988a0 commit 6933175

9 files changed

Lines changed: 259 additions & 0 deletions

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
11
# python-appium-app-browserstack
2+
App Automate Python Samples
3+
---------------------
4+
5+
This repository contains code for Automated Native App tests. Please feel free to clone the repo and use the example code.
6+
7+
For frameworks integration with BrowserStack, refer to their individual repositories -
8+
9+
- [Lettuce](https://github.com/browserstack/lettuce-appium-app-browserstack)
10+
- [Behave](https://github.com/browserstack/behave-appium-app-browserstack)

android/BrowserStackAndroid.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
userName = "BROWSERSTACK_USERNAME"
8+
accessKey = "BROWSERSTACK_ACCESS_KEY"
9+
10+
desired_caps = {
11+
"build": "Python Android",
12+
"realMobile": True,
13+
"device": "Samsung Galaxy S7",
14+
"app": "bs://<hashed app-id>"
15+
}
16+
17+
driver = webdriver.Remote("http://" + userName + ":" + accessKey + "@hub.browserstack.com/wd/hub", desired_caps)
18+
19+
search_element = WebDriverWait(driver, 30).until(
20+
EC.element_to_be_clickable((MobileBy.ACCESSIBILITY_ID, "Search Wikipedia"))
21+
)
22+
search_element.click()
23+
24+
search_input = WebDriverWait(driver, 30).until(
25+
EC.element_to_be_clickable((MobileBy.ID, "org.wikipedia.alpha:id/search_src_text"))
26+
)
27+
search_input.send_keys("BrowserStack")
28+
time.sleep(5)
29+
30+
search_results = driver.find_elements_by_class_name("android.widget.TextView")
31+
assert(len(search_results) > 0)
32+
33+
driver.quit()

android/LocalSampleAndroid.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
from browserstack.local import Local
6+
import time
7+
8+
userName = "BROWSERSTACK_USERNAME"
9+
accessKey = "BROWSERSTACK_ACCESS_KEY"
10+
11+
desired_caps = {
12+
"build": "Python Android Local",
13+
"device": "Samsung Galaxy S7",
14+
"realMobile": True,
15+
"browserstack.local": True,
16+
"app": "bs://<hashed app-id>"
17+
}
18+
19+
bs_local = None
20+
21+
def start_local():
22+
global bs_local
23+
bs_local = Local()
24+
bs_local_args = { "key": accessKey, "forcelocal": "true" }
25+
bs_local.start(**bs_local_args)
26+
27+
def stop_local():
28+
global bs_local
29+
bs_local.stop()
30+
31+
start_local()
32+
driver = webdriver.Remote("http://" + userName + ":" + accessKey + "@hub.browserstack.com/wd/hub", desired_caps)
33+
test_button = WebDriverWait(driver, 30).until(
34+
EC.element_to_be_clickable((MobileBy.ID, "com.example.android.basicnetworking:id/test_action"))
35+
)
36+
test_button.click()
37+
WebDriverWait(driver, 30).until(
38+
EC.element_to_be_clickable((MobileBy.CLASS_NAME, "android.widget.TextView"))
39+
)
40+
41+
test_element = None
42+
search_results = driver.find_elements_by_class_name("android.widget.TextView")
43+
for result in search_results:
44+
if result.text.__contains__("The active connection is"):
45+
test_element = result
46+
47+
if test_element is None:
48+
raise Exception("Cannot find the needed TextView element from app")
49+
50+
matched_string = test_element.text
51+
print matched_string
52+
assert(matched_string.__contains__("The active connection is wifi"))
53+
assert(matched_string.__contains__("Up and running"))
54+
55+
driver.quit()
56+
stop_local()

android/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
## Dependencies
2+
3+
For installing the required packages, use the following command
4+
5+
```
6+
$ pip install -r requirements.txt
7+
```
8+
9+
## Running your tests
10+
11+
- Do remember to switch the BROWSERSTACK_USERNAME and BROWSERSTACK_ACCESS_KEY with your own browserstack credentials.
12+
- Upload your Native App (.apk file) to BrowserStack servers using upload API and update the app capability:
13+
14+
```
15+
curl -u "username:accesskey" -X POST "https://api.browserstack.com/app-automate/upload" -F "file=@/path/to/app/file/Application-debug.apk"
16+
```
17+
18+
- If you do not have an .apk file and looking to simply try App Automate, you can download our [sample app](https://www.browserstack.com/app-automate/sample-apps/android/WikipediaSample.apk) and upload to the BrowserStack servers using the above API.
19+
- For LocalSample tests, you can use our [local sample app](https://www.browserstack.com/app-automate/sample-apps/android/LocalSample.apk).
20+
- Update the desired capability "app" with the App URL returned from the above API call
21+
22+
## Notes
23+
* You can view your test results on the [BrowserStack App Automate dashboard](https://www.browserstack.com/app-automate)
24+
* Refer [Get Started](https://www.browserstack.com/app-automate/appium-python) document to configure the capabilities
25+
26+
For frameworks integration with BrowserStack, refer to their individual repositories -
27+
28+
- [Lettuce](https://github.com/browserstack/lettuce-appium-app-browserstack)
29+
- [Behave](https://github.com/browserstack/behave-appium-app-browserstack)

android/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Appium-Python-Client
2+
browserstack-local
3+
selenium

ios/BrowserStackIOS.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
userName = "BROWSERSTACK_USERNAME"
8+
accessKey = "BROWSERSTACK_ACCESS_KEY"
9+
10+
desired_caps = {
11+
"build": "Python iOS",
12+
"realMobile": True,
13+
"device": "iPhone 7",
14+
"automationName": "XCUITest",
15+
"app": "bs://<hashed app-id>"
16+
}
17+
18+
driver = webdriver.Remote("http://" + userName + ":" + accessKey + "@hub.browserstack.com/wd/hub", desired_caps)
19+
20+
login_button = WebDriverWait(driver, 30).until(
21+
EC.element_to_be_clickable((MobileBy.ACCESSIBILITY_ID, "Log In"))
22+
)
23+
login_button.click()
24+
25+
email_input = WebDriverWait(driver, 30).until(
26+
EC.element_to_be_clickable((MobileBy.ACCESSIBILITY_ID, "Email address"))
27+
)
28+
email_input.send_keys("hello@browserstack.com")
29+
30+
driver.find_element_by_accessibility_id("Next").click()
31+
time.sleep(5)
32+
33+
text_elements = driver.find_elements_by_xpath("//XCUIElementTypeStaticText")
34+
assert(len(text_elements) > 0)
35+
elements = filter(
36+
lambda x: x.__contains__("not registered on WordPress.com"),
37+
[x.text for x in text_elements]
38+
)
39+
assert(len(elements) > 0)
40+
driver.quit()

ios/LocalSampleIOS.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
from browserstack.local import Local
6+
import os
7+
8+
userName = "BROWSERSTACK_USERNAME"
9+
accessKey = "BROWSERSTACK_ACCESS_KEY"
10+
11+
desired_caps = {
12+
"build": "Python iOS Local",
13+
"realMobile": True,
14+
"device": "iPhone 7",
15+
"automationName": "XCUITest",
16+
"browserstack.local": True,
17+
"app": "bs://<hashed app-id>"
18+
}
19+
20+
bs_local = None
21+
22+
def start_local():
23+
global bs_local
24+
bs_local = Local()
25+
bs_local_args = { "key": accessKey, "forcelocal": "true" }
26+
bs_local.start(**bs_local_args)
27+
28+
def stop_local():
29+
global bs_local
30+
bs_local.stop()
31+
32+
def existence_lambda(s):
33+
result = s.find_element_by_accessibility_id("ResultBrowserStackLocal").get_attribute("value")
34+
return result and len(result) > 0
35+
36+
start_local()
37+
driver = webdriver.Remote("http://" + userName + ":" + accessKey + "@hub.browserstack.com/wd/hub", desired_caps)
38+
39+
test_button = WebDriverWait(driver, 30).until(
40+
EC.element_to_be_clickable((MobileBy.ACCESSIBILITY_ID, "TestBrowserStackLocal"))
41+
)
42+
test_button.click()
43+
44+
WebDriverWait(driver, 30).until(existence_lambda)
45+
result_element = driver.find_element_by_accessibility_id("ResultBrowserStackLocal")
46+
result_string = result_element.text.lower()
47+
48+
if result_string.__contains__("not working"):
49+
screenshot_file = "%s/screenshot.png" % os.getcwd()
50+
driver.save_screenshot(screenshot_file)
51+
print "Screenshot stored at %s" % screenshot_file
52+
print "Unexpected BrowserStackLocal test result"
53+
else:
54+
assert(result_string.__contains__("up and running"))
55+
56+
driver.quit()
57+
stop_local()

ios/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
## Dependencies
2+
3+
For installing the required packages, use the following command
4+
5+
```
6+
$ pip install -r requirements.txt
7+
```
8+
9+
## Running your tests
10+
11+
- Do remember to switch the BROWSERSTACK_USERNAME and BROWSERSTACK_ACCESS_KEY with your own browserstack credentials.
12+
- Upload your Native App (.ipa file) to BrowserStack servers using upload API and update the app capability:
13+
14+
```
15+
curl -u "username:accesskey" -X POST "https://api.browserstack.com/app-automate/upload" -F "file=@/path/to/app/file/Application-debug.ipa"
16+
```
17+
18+
- If you do not have an .ipa file and looking to simply try App Automate, you can download our [sample app](https://www.browserstack.com/app-automate/sample-apps/ios/WordPressSample.ipa) and upload to the BrowserStack servers using the above API.
19+
- For LocalSample tests, you can use our [local sample app](https://www.browserstack.com/app-automate/sample-apps/ios/LocalSample.ipa).
20+
- Update the desired capability "app" with the App URL returned from the above API call
21+
22+
## Notes
23+
* You can view your test results on the [BrowserStack App Automate dashboard](https://www.browserstack.com/app-automate)
24+
* Refer [Get Started](https://www.browserstack.com/app-automate/appium-python) document to configure the capabilities
25+
26+
For frameworks integration with BrowserStack, refer to their individual repositories -
27+
28+
- [Lettuce](https://github.com/browserstack/lettuce-appium-app-browserstack)
29+
- [Behave](https://github.com/browserstack/behave-appium-app-browserstack)

ios/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Appium-Python-Client
2+
browserstack-local
3+
selenium

0 commit comments

Comments
 (0)