forked from appium-boneyard/sample-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsauce_connect.py
More file actions
144 lines (118 loc) · 5.56 KB
/
sauce_connect.py
File metadata and controls
144 lines (118 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
"""An example of Appium running on Sauce using Sauce Connect to access a local webserver.
This test assumes SAUCE_USERNAME and SAUCE_ACCESS_KEY are environment variables
set to your Sauce Labs username and access key.
You'll also need Sauce-Connect.jar in this test directory so we can start it to enable
a tunnel between Sauce Labs and your machine
This is an All-In-One bundle test that does a lot more than usual test would. It does following
things that you would normally do in a different way:
- starts Sauce Connect - which you would normally start from console with
"java -jar Sauce-Connect.jar SAUCE_USERNAME SAUCE_ACCESS_KEY"
- starts a local webserver on port 9999 that serves a sample string - normally you would
like to connect to your own webserver
"""
import unittest
from selenium import webdriver
import os
import subprocess
import sys
import select
from SimpleHTTPServer import SimpleHTTPRequestHandler
from StringIO import StringIO
from threading import Thread
from BaseHTTPServer import HTTPServer
from SocketServer import ThreadingMixIn
SAUCE_USERNAME = os.environ.get('SAUCE_USERNAME')
SAUCE_ACCESS_KEY = os.environ.get('SAUCE_ACCESS_KEY')
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
"""Handle requests in a separate thread."""
class MyRequestHandler(SimpleHTTPRequestHandler):
"""Serve a sample HTML page so we can check if test works properly"""
def do_GET(self):
f = StringIO()
f.write("<html><body>Welcome to the flipside!</body></html>")
f.seek(0)
#send code 200 response
self.send_response(200)
#send header first
self.send_header('Content-type', 'text-html')
self.end_headers()
#send file content to client
self.wfile.write(f.read())
f.close()
return
class Selenium2OnSauce(unittest.TestCase):
def setUpWebServer(self):
# Setting up a local websever in separate thread on port 9999
httpd = ThreadedHTTPServer(("", 9999), MyRequestHandler)
sa = httpd.socket.getsockname()
print "[HTTP Server] Serving HTTP on", sa[0], "port", sa[1], "..."
thread = Thread(target=httpd.serve_forever)
thread.daemon = True # so server gets killed when we exit
thread.start()
def setUpTunnel(self):
# Setting up Sauce Connect 4.x tunnel
# May need to change ./sc/bin/sc depending on OS and directory structure
self.process = subprocess.Popen(['./sc/bin/sc -u $SAUCE_USERNAME -k $SAUCE_ACCESS_KEY'],
shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p = self.process
print "[Sauce Connect]: Waiting for tunnel setup, this make take up to 30s"
print "For detailed documentation on Sauce Connect please refer to " \
"http://https://docs.saucelabs.com/reference/sauce-connect/"
is_ready = False
while True:
reads = [p.stdout.fileno(), p.stderr.fileno()]
ret = select.select(reads, [], [])
for fd in ret[0]:
if fd == p.stdout.fileno():
read = p.stdout.readline()
sys.stdout.write("[Sauce Connect]: %s" % read)
if "Sauce Connect is up, you may start your tests." in read:
print "[Sauce Connect]: Tunnel ready, running the test"
is_ready = True
break
if fd == p.stderr.fileno():
read = p.stderr.readline()
sys.stderr.write("[Sauce Connect]: %s" % read)
if "Finished! Deleting tunnel." in read:
self.process.terminate()
raise Exception("Sauce Connect could not start!")
if is_ready:
break
def setUp(self):
self.setUpWebServer()
self.setUpTunnel()
desired_capabilities = {
'platformName': 'iOS',
'platformVersion': '7.1',
'deviceName': 'iPhone Simulator',
'browserName': 'safari',
'appiumVersion': '1.2.2',
'name': 'Appium Python iOS Test (Sauce Connect)'
}
'''
When using Sauce Connect there are two options for sending commands to Sauce Labs. The first is
sending commands to ondemand.saucelabs.com. The second it sending commands to host running Sauce Connect
which will relay them to Sauce's infrastructure.
Starting Sauce Connect the -P flag will determine which port Sauce Connect starts on, then it's just a matter
of pointing the Remote WebDriver instance to
"http://%s:%s@%s:%S/wd/hub" % (SAUCE_USERNAME, SAUCE_ACCESS_KEY, SAUCE_CONNECT_HOST, SAUCE_CONNECT_PORT)
'''
self.driver = webdriver.Remote(
desired_capabilities=desired_capabilities,
command_executor="http://%s:%s@ondemand.saucelabs.com:80/wd/hub" % (SAUCE_USERNAME, SAUCE_ACCESS_KEY)
)
self.driver.implicitly_wait(30)
def test_basic(self):
driver = self.driver
driver.get("http://localhost:9999/")
body = self.driver.find_element_by_tag_name("body")
self.assertTrue("Welcome to the flipside!" in body.text)
def tearDown(self):
print("Link to your job: https://saucelabs.com/jobs/%s" % self.driver.session_id)
self.driver.quit()
self.process.terminate()
if __name__ == '__main__':
if not (SAUCE_USERNAME and SAUCE_ACCESS_KEY):
print "Make sure you have SAUCE_USERNAME and SAUCE_ACCESS_KEY set as environment variables."
else:
unittest.main()