Skip to content

Commit 04c0964

Browse files
committed
Fix Sauce Labs test in Python
1 parent c61f21c commit 04c0964

6 files changed

Lines changed: 168 additions & 161 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,7 @@ target/
5454

5555
# npm
5656
node_modules
57+
58+
# Python
59+
__pycache__
60+
*.pyc
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Python samples
2+
==============
3+
4+
These are simple samples of how to use Python to run Appium tests. It is suggested that you use a test runner such as pytest or nose.
5+
6+
Sauce Labs examples require at least version 0.12 of the Appium Python Client, which includes the `appium.SauceTestCase` base class.
7+
8+
Usage:
9+
10+
```shell
11+
py.test -n2 --boxed ios_simple.py
12+
```
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
from appium import webdriver
2+
from appium import SauceTestCase, on_platforms
3+
from appium.webdriver.common.touch_action import TouchAction
4+
from appium.webdriver.common.multi_action import MultiAction
5+
6+
from time import sleep
7+
8+
app = "http://appium.github.io/appium/assets/ApiDemos-debug.apk"
9+
platforms = [{
10+
"platformName": "Android",
11+
"platformVersion": "4.4",
12+
"deviceName": "Android Emulator",
13+
"appActivity": ".graphics.TouchPaint",
14+
"app": app,
15+
"appiumVersion": "1.3.4"
16+
}]
17+
18+
19+
@on_platforms(platforms)
20+
class AndroidGesturesSauceTests(SauceTestCase):
21+
22+
def test_drag_and_drop(self):
23+
# first get to the right activity
24+
self.driver.start_activity("io.appium.android.apis", ".view.DragAndDropDemo")
25+
26+
start = self.driver.find_element_by_id("io.appium.android.apis:id/drag_dot_3")
27+
end = self.driver.find_element_by_id("io.appium.android.apis:id/drag_dot_2")
28+
29+
action = TouchAction(self.driver);
30+
action.long_press(start).move_to(end).release().perform()
31+
32+
sleep(.5)
33+
34+
text = self.driver.find_element_by_id("io.appium.android.apis:id/drag_result_text").text
35+
self.assertEqual(text, "Dropped!")
36+
37+
38+
def test_smiley_face(self):
39+
# just for the fun of it.
40+
# this doesn't really assert anything.
41+
# paint
42+
eye1 = TouchAction()
43+
eye1.press(x=150, y=100).release()
44+
45+
eye2 = TouchAction()
46+
eye2.press(x=250, y=100).release()
47+
48+
smile = TouchAction()
49+
smile.press(x=110, y=200) \
50+
.move_to(x=1, y=1) \
51+
.move_to(x=1, y=1) \
52+
.move_to(x=1, y=1) \
53+
.move_to(x=1, y=1) \
54+
.move_to(x=1, y=1) \
55+
.move_to(x=2, y=1) \
56+
.move_to(x=2, y=1) \
57+
.move_to(x=2, y=1) \
58+
.move_to(x=2, y=1) \
59+
.move_to(x=2, y=1) \
60+
.move_to(x=3, y=1) \
61+
.move_to(x=3, y=1) \
62+
.move_to(x=3, y=1) \
63+
.move_to(x=3, y=1) \
64+
.move_to(x=3, y=1) \
65+
.move_to(x=4, y=1) \
66+
.move_to(x=4, y=1) \
67+
.move_to(x=4, y=1) \
68+
.move_to(x=4, y=1) \
69+
.move_to(x=4, y=1) \
70+
.move_to(x=5, y=1) \
71+
.move_to(x=5, y=1) \
72+
.move_to(x=5, y=1) \
73+
.move_to(x=5, y=1) \
74+
.move_to(x=5, y=1) \
75+
.move_to(x=5, y=0) \
76+
.move_to(x=5, y=0) \
77+
.move_to(x=5, y=0) \
78+
.move_to(x=5, y=0) \
79+
.move_to(x=5, y=0) \
80+
.move_to(x=5, y=0) \
81+
.move_to(x=5, y=0) \
82+
.move_to(x=5, y=0) \
83+
.move_to(x=5, y=-1) \
84+
.move_to(x=5, y=-1) \
85+
.move_to(x=5, y=-1) \
86+
.move_to(x=5, y=-1) \
87+
.move_to(x=5, y=-1) \
88+
.move_to(x=4, y=-1) \
89+
.move_to(x=4, y=-1) \
90+
.move_to(x=4, y=-1) \
91+
.move_to(x=4, y=-1) \
92+
.move_to(x=4, y=-1) \
93+
.move_to(x=3, y=-1) \
94+
.move_to(x=3, y=-1) \
95+
.move_to(x=3, y=-1) \
96+
.move_to(x=3, y=-1) \
97+
.move_to(x=3, y=-1) \
98+
.move_to(x=2, y=-1) \
99+
.move_to(x=2, y=-1) \
100+
.move_to(x=2, y=-1) \
101+
.move_to(x=2, y=-1) \
102+
.move_to(x=2, y=-1) \
103+
.move_to(x=1, y=-1) \
104+
.move_to(x=1, y=-1) \
105+
.move_to(x=1, y=-1) \
106+
.move_to(x=1, y=-1) \
107+
.move_to(x=1, y=-1)
108+
smile.release()
109+
110+
ma = MultiAction(self.driver)
111+
ma.add(eye1, eye2, smile)
112+
ma.perform()
113+
114+
# so you can see it
115+
sleep(10)
Lines changed: 14 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,20 @@
1-
import os
2-
import httplib
3-
import base64
4-
try:
5-
import json
6-
except ImportError:
7-
import simplejson as json
8-
9-
import unittest
10-
111
from appium import webdriver
2+
from appium import SauceTestCase, on_platforms
123

13-
SAUCE_USERNAME = os.environ.get('SAUCE_USERNAME')
14-
SAUCE_ACCESS_KEY = os.environ.get('SAUCE_ACCESS_KEY')
15-
base64string = base64.encodestring('%s:%s' % (SAUCE_USERNAME, SAUCE_ACCESS_KEY))[:-1]
16-
17-
class SimpleAndroidSauceTests(unittest.TestCase):
184

19-
def setUp(self):
20-
# set up appium
21-
app = "http://appium.s3.amazonaws.com/NotesList.apk"
22-
self.driver = webdriver.Remote(
23-
command_executor='http://%s:%[email protected]:80/wd/hub' % (SAUCE_USERNAME, SAUCE_ACCESS_KEY),
24-
desired_capabilities={
25-
'appiumVersion': '1.2.2',
26-
'platformName': 'Android',
27-
'deviceName': 'Android Emulator',
28-
'platformVersion': '4.4',
29-
'app': app,
30-
'name': 'Appium Python Android Test',
31-
'appPackage': 'com.example.android.notepad',
32-
'appActivity': '.NotesList'
33-
})
5+
app = "http://appium.s3.amazonaws.com/NotesList.apk"
6+
platforms = [{
7+
"platformName": "Android",
8+
"platformVersion": "4.4",
9+
"deviceName": "Android Emulator",
10+
"appPackage": "com.example.android.notepad",
11+
"appActivity": ".NotesList",
12+
"app": app,
13+
"appiumVersion": "1.3.4"
14+
}]
3415

35-
def tearDown(self):
36-
print("Link to your job: https://saucelabs.com/jobs/%s" % self.driver.session_id)
37-
self.driver.quit()
38-
39-
def _set_test_status(self, jobid, passed=True):
40-
# Report the status of your test to Sauce
41-
body_content = json.dumps({"passed": passed})
42-
connection = httplib.HTTPConnection("saucelabs.com")
43-
connection.request('PUT', '/rest/v1/%s/jobs/%s' % (SAUCE_USERNAME, jobid),
44-
body_content,
45-
headers={"Authorization": "Basic %s" % base64string})
46-
result = connection.getresponse()
47-
return result.status == 200
16+
@on_platforms(platforms)
17+
class SimpleAndroidSauceTests(SauceTestCase):
4818

4919
def test_create_note(self):
5020
el = self.driver.find_element_by_accessibility_id("New note")
@@ -57,17 +27,6 @@ def test_create_note(self):
5727
el.click()
5828

5929
els = self.driver.find_elements_by_class_name("android.widget.TextView")
60-
try:
61-
self.assertEqual(els[2].text, "This is a new note!")
62-
self._set_test_status(self.driver.session_id, True)
63-
except:
64-
self._set_test_status(self.driver.session_id, False)
30+
self.assertEqual(els[2].text, "This is a new note!")
6531

6632
els[2].click()
67-
68-
69-
if __name__ == '__main__':
70-
if not (SAUCE_USERNAME and SAUCE_ACCESS_KEY):
71-
print "Make sure you have SAUCE_USERNAME and SAUCE_ACCESS_KEY set as environment variables."
72-
else:
73-
unittest.main()

sample-code/examples/python/ios_sauce.py

Lines changed: 11 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,25 @@
33
This test assumes SAUCE_USERNAME and SAUCE_ACCESS_KEY are environment variables
44
set to your Sauce Labs username and access key."""
55

6-
import unittest
7-
import os
8-
import httplib
9-
import base64
106
from random import randint
117
from appium import webdriver
12-
try:
13-
import json
14-
except ImportError:
15-
import simplejson as json
8+
from appium import SauceTestCase, on_platforms
169

17-
SAUCE_USERNAME=os.environ.get('SAUCE_USERNAME')
18-
SAUCE_ACCESS_KEY=os.environ.get('SAUCE_ACCESS_KEY')
19-
base64string = base64.encodestring('%s:%s' % (SAUCE_USERNAME, SAUCE_ACCESS_KEY))[:-1]
2010

21-
class SimpleIOSSauceTests(unittest.TestCase):
22-
23-
def setUp(self):
24-
# set up appium
25-
app = 'http://appium.s3.amazonaws.com/TestApp6.0.app.zip'
26-
27-
self.driver = webdriver.Remote(
28-
command_executor = 'http://%s:%[email protected]:80/wd/hub' % (SAUCE_USERNAME, SAUCE_ACCESS_KEY),
29-
desired_capabilities = {
30-
'appiumVersion': '1.2.2',
31-
'name': 'Appium Python iOS Test',
11+
platforms = [{
3212
'platformName': 'iOS',
33-
'deviceName': 'iPhone Simulator',
3413
'platformVersion': '7.1',
35-
'app': app
36-
})
37-
38-
def tearDown(self):
39-
print("Link to your job: https://saucelabs.com/jobs/%s" % self.driver.session_id)
40-
self.driver.quit()
14+
'deviceName': 'iPhone Simulator',
15+
'app': 'http://appium.s3.amazonaws.com/TestApp6.0.app.zip',
16+
'appiumVersion': '1.3.4'
17+
}]
4118

42-
def _set_test_status(self, jobid, passed=True):
43-
# Report the status of your test to Sauce
44-
body_content = json.dumps({"passed": passed})
45-
connection = httplib.HTTPConnection("saucelabs.com")
46-
connection.request('PUT', '/rest/v1/%s/jobs/%s' % (SAUCE_USERNAME, jobid),
47-
body_content,
48-
headers={"Authorization": "Basic %s" % base64string})
49-
result = connection.getresponse()
50-
return result.status == 200
19+
@on_platforms(platforms)
20+
class SimpleIOSSauceTests(SauceTestCase):
5121

5222
def _populate(self):
5323
# populate text fields with two random numbers
54-
els = self.driver.find_elements_by_ios_uiautomation('elements()')
24+
els = self.driver.find_elements_by_class_name('UIATextField')
5525

5626
self._sum = 0
5727
for i in range(2):
@@ -67,18 +37,5 @@ def test_ui_computation(self):
6737
self.driver.find_element_by_accessibility_id('ComputeSumButton').click()
6838

6939
# is sum equal ?
70-
# sauce does not handle class name, so get fourth element
71-
# sum = self.driver.find_elements_by_class_name("UIAStaticText")[0].text
72-
sum = self.driver.find_element_by_ios_uiautomation('elements()[3]').text
73-
try:
74-
self.assertEqual(int(sum), self._sum)
75-
self._set_test_status(self.driver.session_id, True)
76-
except:
77-
self._set_test_status(self.driver.session_id, False)
78-
79-
80-
if __name__ == '__main__':
81-
if not (SAUCE_USERNAME and SAUCE_ACCESS_KEY):
82-
print "Make sure you have SAUCE_USERNAME and SAUCE_ACCESS_KEY set as environment variables."
83-
else:
84-
unittest.main()
40+
sum = self.driver.find_elements_by_class_name("UIAStaticText")[0].text
41+
self.assertEqual(int(sum), self._sum)

sample-code/examples/python/ios_sauce_webview.py

Lines changed: 12 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,24 @@
33
This test assumes SAUCE_USERNAME and SAUCE_ACCESS_KEY are environment variables
44
set to your Sauce Labs username and access key."""
55

6-
import unittest
7-
import os
8-
import httplib
9-
import base64
106
from random import randint
117
from appium import webdriver
12-
try:
13-
import json
14-
except ImportError:
15-
import simplejson as json
8+
from appium import SauceTestCase, on_platforms
169
from time import sleep
1710

1811
from selenium.webdriver.common.keys import Keys
1912

20-
SAUCE_USERNAME=os.environ.get('SAUCE_USERNAME')
21-
SAUCE_ACCESS_KEY=os.environ.get('SAUCE_ACCESS_KEY')
22-
base64string = base64.encodestring('%s:%s' % (SAUCE_USERNAME, SAUCE_ACCESS_KEY))[:-1]
13+
app = 'http://appium.s3.amazonaws.com/WebViewApp6.0.app.zip'
14+
platforms = [{
15+
'platformName': 'iOS',
16+
'platformVersion': '7.1',
17+
'deviceName': 'iPhone Simulator',
18+
'appiumVersion': '1.3.4',
19+
'app': app
20+
}]
2321

24-
class WebViewIOSSauceTests(unittest.TestCase):
25-
26-
def setUp(self):
27-
# set up appium
28-
app = 'http://appium.s3.amazonaws.com/WebViewApp6.0.app.zip'
29-
30-
self.driver = webdriver.Remote(
31-
command_executor = 'http://%s:%[email protected]:80/wd/hub' % (SAUCE_USERNAME, SAUCE_ACCESS_KEY),
32-
desired_capabilities = {
33-
'appiumVersion': '1.2.2',
34-
'name': 'Appium iOS WebView Test',
35-
'platformName': 'iOS',
36-
'deviceName': 'iPhone Simulator',
37-
'platformVersion': '7.1',
38-
'app': app
39-
})
40-
41-
def tearDown(self):
42-
print("Link to your job: https://saucelabs.com/jobs/%s" % self.driver.session_id)
43-
self.driver.quit()
44-
45-
def _set_test_status(self, jobid, passed=True):
46-
# Report the status of your test to Sauce
47-
body_content = json.dumps({"passed": passed})
48-
connection = httplib.HTTPConnection("saucelabs.com")
49-
connection.request('PUT', '/rest/v1/%s/jobs/%s' % (SAUCE_USERNAME, jobid),
50-
body_content,
51-
headers={"Authorization": "Basic %s" % base64string})
52-
result = connection.getresponse()
53-
return result.status == 200
22+
@on_platforms(platforms)
23+
class WebViewIOSSauceTests(SauceTestCase):
5424

5525
def test_get_url(self):
5626
url_el = self.driver.find_element_by_xpath('//UIAApplication[1]/UIAWindow[1]/UIATextField[1]')
@@ -68,15 +38,5 @@ def test_get_url(self):
6838
# allow the page to load
6939
sleep(1)
7040

71-
try:
72-
self.assertEquals('sauce labs - Google Search', self.driver.title)
73-
self._set_test_status(self.driver.session_id, True)
74-
except:
75-
self._set_test_status(self.driver.session_id, False)
76-
41+
self.assertEquals('sauce labs', self.driver.title[:10])
7742

78-
if __name__ == '__main__':
79-
if not (SAUCE_USERNAME and SAUCE_ACCESS_KEY):
80-
print "Make sure you have SAUCE_USERNAME and SAUCE_ACCESS_KEY set as environment variables."
81-
else:
82-
unittest.main()

0 commit comments

Comments
 (0)