├── .gitignore ├── README.md ├── basic.py ├── drag-and-drop.py ├── login-form.py ├── parallel ├── README.md ├── multithreaded │ └── parallel.py └── nose │ ├── test_nose.py │ └── test_nose2.py └── todo-app.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | 5 | # C extensions 6 | *.so 7 | 8 | # Distribution / packaging 9 | .Python 10 | env/ 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | *.egg-info/ 23 | .installed.cfg 24 | *.egg 25 | 26 | # PyInstaller 27 | # Usually these files are written by a python script from a template 28 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 29 | *.manifest 30 | *.spec 31 | 32 | # Installer logs 33 | pip-log.txt 34 | pip-delete-this-directory.txt 35 | 36 | # Unit test / coverage reports 37 | htmlcov/ 38 | .tox/ 39 | .coverage 40 | .coverage.* 41 | .cache 42 | nosetests.xml 43 | coverage.xml 44 | *,cover 45 | 46 | # Translations 47 | *.mo 48 | *.pot 49 | 50 | # Django stuff: 51 | *.log 52 | 53 | # Sphinx documentation 54 | docs/_build/ 55 | 56 | # PyBuilder 57 | target/ 58 | 59 | # Mac Files 60 | .DS_Store 61 | 62 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## selenium-python 2 | 3 | Selenium script examples in Python for CrossBrowserTesting.com 4 | 5 | 6 | -------------------------------------------------------------------------------- /basic.py: -------------------------------------------------------------------------------- 1 | # Please visit http://selenium-python.readthedocs.org/en/latest/index.html for detailed installation and instructions 2 | # Getting started: http://docs.seleniumhq.org/docs/03_webdriver.jsp 3 | # API details: https://github.com/SeleniumHQ/selenium#selenium 4 | 5 | # Requests is the easiest way to make RESTful API calls in Python. You can install it by following the instructions here: 6 | # http://docs.python-requests.org/en/master/user/install/ 7 | 8 | import unittest 9 | from selenium import webdriver 10 | import requests 11 | 12 | class BasicTest(unittest.TestCase): 13 | def setUp(self): 14 | 15 | # Put your username and authey below 16 | # You can find your authkey at crossbrowsertesting.com/account 17 | self.username = "user@email.com" 18 | self.authkey = "12345" 19 | 20 | self.api_session = requests.Session() 21 | self.api_session.auth = (self.username,self.authkey) 22 | 23 | self.test_result = None 24 | 25 | caps = {} 26 | 27 | caps['name'] = 'Basic Example' 28 | caps['build'] = '1.0' 29 | caps['browserName'] = 'Safari' 30 | caps['version'] = '8' 31 | caps['platform'] = 'Mac OSX 10.10' 32 | caps['screenResolution'] = '1366x768' 33 | caps['record_video'] = 'true' 34 | caps['record_network'] = 'false' 35 | 36 | # start the remote browser on our server 37 | self.driver = webdriver.Remote( 38 | desired_capabilities=caps, 39 | command_executor="http://%s:%s@hub.crossbrowsertesting.com:80/wd/hub"%(self.username,self.authkey) 40 | ) 41 | 42 | self.driver.implicitly_wait(20) 43 | 44 | def test_CBT(self): 45 | # We wrap this all in a try/except so we can set pass/fail at the end 46 | try: 47 | # load the page url 48 | print('Loading Url') 49 | self.driver.get('http://crossbrowsertesting.github.io/selenium_example_page.html') 50 | 51 | # maximize the window - DESKTOPS ONLY 52 | #print('Maximizing window') 53 | #self.driver.maximize_window() 54 | 55 | #check the title 56 | print('Checking title') 57 | self.assertEqual("Selenium Test Example Page", self.driver.title) 58 | 59 | # if we are still in the try block after all of our assertions that 60 | # means our test has had no failures, so we set the status to "pass" 61 | self.test_result = 'pass' 62 | 63 | except AssertionError as e: 64 | 65 | # if any assertions are false, we take a snapshot of the screen, log 66 | # the error message, and set the score to "during tearDown()". 67 | 68 | snapshot_hash = self.api_session.post('https://crossbrowsertesting.com/api/v3/selenium/' + self.driver.session_id + '/snapshots').json()['hash'] 69 | self.api_session.put('https://crossbrowsertesting.com/api/v3/selenium/' + self.driver.session_id + '/snapshots/' + snapshot_hash, 70 | data={'description':"AssertionError: " + str(e)}) 71 | self.test_result = 'fail' 72 | raise 73 | 74 | def tearDown(self): 75 | print("Done with session %s" % self.driver.session_id) 76 | self.driver.quit() 77 | # Here we make the api call to set the test's score. 78 | # Pass it it passes, fail if an assertion fails, unset if the test didn't finish 79 | if self.test_result is not None: 80 | self.api_session.put('https://crossbrowsertesting.com/api/v3/selenium/' + self.driver.session_id, 81 | data={'action':'set_score', 'score':self.test_result}) 82 | 83 | 84 | if __name__ == '__main__': 85 | unittest.main() 86 | -------------------------------------------------------------------------------- /drag-and-drop.py: -------------------------------------------------------------------------------- 1 | # Please visit http://selenium-python.readthedocs.org/en/latest/index.html for detailed installation and instructions 2 | # Getting started: http://docs.seleniumhq.org/ 3 | # API details: https://github.com/SeleniumHQ/selenium#selenium 4 | 5 | # Requests is the easiest way to make RESTful API calls in Python. You can install it by following the instructions here: 6 | # http://docs.python-requests.org/en/master/user/install/ 7 | 8 | import unittest 9 | from selenium import webdriver 10 | import requests 11 | from selenium.webdriver.support import expected_conditions as EC 12 | from selenium.webdriver.common.by import By 13 | from selenium.webdriver.support.ui import WebDriverWait 14 | from selenium.webdriver import ActionChains 15 | 16 | class DragAndDrop(unittest.TestCase): 17 | def setUp(self): 18 | 19 | # Put your username and authey below 20 | # You can find your authkey at crossbrowsertesting.com/account 21 | self.username = "user@email.com" 22 | self.authkey = "12345" 23 | 24 | self.api_session = requests.Session() 25 | self.api_session.auth = (self.username,self.authkey) 26 | 27 | self.test_result = None 28 | 29 | caps = {} 30 | 31 | caps['name'] = 'Drag-and-Drop Example' 32 | caps['build'] = '1.0' 33 | caps['browserName'] = 'Chrome' 34 | caps['version'] = '53' 35 | caps['platform'] = 'Windows 10' 36 | caps['screenResolution'] = '1366x768' 37 | caps['record_video'] = 'true' 38 | caps['record_network'] = 'false' 39 | 40 | # start the remote browser on our server 41 | self.driver = webdriver.Remote( 42 | desired_capabilities=caps, 43 | command_executor="http://%s:%s@hub.crossbrowsertesting.com:80/wd/hub"%(self.username,self.authkey) 44 | ) 45 | 46 | self.driver.implicitly_wait(20) 47 | 48 | def test_CBT(self): 49 | # We wrap this all in a try/except so we can set pass/fail at the end 50 | try: 51 | # load the page url 52 | print('Loading Url') 53 | self.driver.get('http://crossbrowsertesting.github.io/drag-and-drop.html') 54 | 55 | # maximize the window - DESKTOPS ONLY 56 | # print('Maximizing window') 57 | # self.driver.maximize_window() 58 | 59 | # grab the first element 60 | print('Grabbing draggable element') 61 | draggable = self.driver.find_element_by_id("draggable") 62 | 63 | # then the second element 64 | print('Grabbing the droppable element') 65 | droppable = self.driver.find_element_by_id("droppable") 66 | 67 | # we use ActionChains to move the element 68 | print('Dragging the element') 69 | actionChains = ActionChains(self.driver) 70 | actionChains.drag_and_drop(draggable, droppable).perform() 71 | 72 | # let's assert that the droppable element is in the state we want it to be 73 | droppableText = self.driver.find_element_by_xpath('//*[@id="droppable"]/p').text 74 | self.assertEqual('Dropped!', droppableText) 75 | 76 | print("Taking snapshot") 77 | snapshot_hash = self.api_session.post('https://crossbrowsertesting.com/api/v3/selenium/' + self.driver.session_id + '/snapshots').json()['hash'] 78 | 79 | # if we are still in the try block after all of our assertions that 80 | # means our test has had no failures, so we set the status to "pass" 81 | self.test_result = 'pass' 82 | 83 | except AssertionError as e: 84 | # log the error message, and set the score to "during tearDown()". 85 | self.api_session.put('https://crossbrowsertesting.com/api/v3/selenium/' + self.driver.session_id + '/snapshots/' + snapshot_hash, 86 | data={'description':"AssertionError: " + str(e)}) 87 | self.test_result = 'fail' 88 | raise 89 | 90 | def tearDown(self): 91 | print("Done with session %s" % self.driver.session_id) 92 | self.driver.quit() 93 | # Here we make the api call to set the test's score. 94 | # Pass it it passes, fail if an assertion fails, unset if the test didn't finish 95 | if self.test_result is not None: 96 | self.api_session.put('https://crossbrowsertesting.com/api/v3/selenium/' + self.driver.session_id, 97 | data={'action':'set_score', 'score':self.test_result}) 98 | 99 | 100 | if __name__ == '__main__': 101 | unittest.main() 102 | -------------------------------------------------------------------------------- /login-form.py: -------------------------------------------------------------------------------- 1 | # Please visit http://selenium-python.readthedocs.org/ for detailed installation and instructions 2 | # Getting started: http://docs.seleniumhq.org/docs/03_webdriver.jsp 3 | # API details: https://github.com/SeleniumHQ/selenium#selenium 4 | 5 | # Requests is the easiest way to make RESTful API calls in Python. You can install it by following the instructions here: 6 | # http://docs.python-requests.org/en/master/user/install/ 7 | 8 | import unittest 9 | from selenium import webdriver 10 | import requests 11 | from selenium.webdriver.support import expected_conditions as EC 12 | from selenium.webdriver.common.by import By 13 | from selenium.webdriver.support.ui import WebDriverWait 14 | 15 | class LoginForm(unittest.TestCase): 16 | def setUp(self): 17 | 18 | # Put your username and authey below 19 | # You can find your authkey at crossbrowsertesting.com/account 20 | self.username = "user@email.com" 21 | self.authkey = "12345" 22 | 23 | self.api_session = requests.Session() 24 | self.api_session.auth = (self.username,self.authkey) 25 | 26 | self.test_result = None 27 | 28 | caps = {} 29 | 30 | caps['name'] = 'Login Form Example' 31 | caps['build'] = '1.0' 32 | caps['browserName'] = 'Chrome' 33 | caps['version'] = '53' 34 | caps['platform'] = 'Windows 10' 35 | caps['screenResolution'] = '1366x768' 36 | caps['record_video'] = 'true' 37 | caps['record_network'] = 'false' 38 | 39 | # start the remote browser on our server 40 | self.driver = webdriver.Remote( 41 | desired_capabilities=caps, 42 | command_executor="http://%s:%s@hub.crossbrowsertesting.com:80/wd/hub"%(self.username,self.authkey) 43 | ) 44 | 45 | self.driver.implicitly_wait(20) 46 | 47 | def test_CBT(self): 48 | # We wrap this all in a try/except so we can set pass/fail at the end 49 | try: 50 | # load the page url 51 | print('Loading Url') 52 | self.driver.get('http://crossbrowsertesting.github.io/login-form.html') 53 | 54 | # maximize the window - DESKTOPS ONLY 55 | #print('Maximizing window') 56 | self.driver.maximize_window() 57 | 58 | # we'll start the login process by entering our username 59 | print('Entering username') 60 | self.driver.find_element_by_name('username').send_keys('tester@crossbrowsertesting.com') 61 | 62 | # then by entering our password 63 | print('Entering password') 64 | self.driver.find_element_by_name('password').send_keys('test123') 65 | 66 | # now we'll click the login button 67 | print('Logging in') 68 | self.driver.find_element_by_css_selector('body > div > div > div > div > form > div.form-actions > button').click() 69 | 70 | # if we've passed the login, we should see the welcome text 71 | 72 | elem = WebDriverWait(self.driver, 10).until( 73 | EC.presence_of_element_located((By.XPATH, '//*[@id=\"logged-in-message\"]/h2')) 74 | ) 75 | 76 | welcomeText = elem.text 77 | self.assertEqual("Welcome tester@crossbrowsertesting.com", welcomeText) 78 | 79 | print("Taking snapshot") 80 | snapshot_hash = self.api_session.post('https://crossbrowsertesting.com/api/v3/selenium/' + self.driver.session_id + '/snapshots').json()['hash'] 81 | 82 | # if we are still in the try block after all of our assertions that 83 | # means our test has had no failures, so we set the status to "pass" 84 | self.test_result = 'pass' 85 | 86 | except AssertionError as e: 87 | # log the error message, and set the score to "during tearDown()". 88 | self.api_session.put('https://crossbrowsertesting.com/api/v3/selenium/' + self.driver.session_id + '/snapshots/' + snapshot_hash, 89 | data={'description':"AssertionError: " + str(e)}) 90 | self.test_result = 'fail' 91 | raise 92 | 93 | def tearDown(self): 94 | print("Done with session %s" % self.driver.session_id) 95 | self.driver.quit() 96 | # Here we make the api call to set the test's score. 97 | # Pass it it passes, fail if an assertion fails, unset if the test didn't finish 98 | if self.test_result is not None: 99 | self.api_session.put('https://crossbrowsertesting.com/api/v3/selenium/' + self.driver.session_id, 100 | data={'action':'set_score', 'score':self.test_result}) 101 | 102 | 103 | if __name__ == '__main__': 104 | unittest.main() 105 | -------------------------------------------------------------------------------- /parallel/README.md: -------------------------------------------------------------------------------- 1 | ## selenium-parallel-python 2 | ### Runs selenium scripts in parallel on Crossbrowsertesting.com 3 | #### Written in Python 4 | 5 | Its done 2 different ways: 6 | 7 | 1. Multithreaded 8 |
9 | python multithreaded/parallel.py
10 |
11 | 2. Nose
12 |
13 | nosetests --processes=\ --where=nose
14 |
15 |
--------------------------------------------------------------------------------
/parallel/multithreaded/parallel.py:
--------------------------------------------------------------------------------
1 | from threading import Thread
2 | from selenium import webdriver
3 | import time
4 |
5 | USERNAME = "USERNAME"
6 | API_KEY = "API_KEY"
7 |
8 |
9 | def get_browser(caps):
10 | return webdriver.Remote(
11 | desired_capabilities=caps,
12 | command_executor="http://%s:%s@hub.crossbrowsertesting.com:80/wd/hub" % (USERNAME, API_KEY)
13 | )
14 |
15 | browsers = [
16 | {"platform": "Windows 7 64-bit", "browserName": "Internet Explorer","version": "10", "name": "Python Parallel"},
17 | {"platform": "Windows 8.1", "browserName": "Chrome", "version": "50", "name": "Python Parallel"},
18 | ]
19 | browsers_waiting = []
20 |
21 |
22 | def get_browser_and_wait(browser_data):
23 | print ("starting %s\n" % browser_data["browserName"])
24 | browser = get_browser(browser_data)
25 | browser.get("http://crossbrowsertesting.com")
26 | browsers_waiting.append({"data": browser_data, "driver": browser})
27 | print ("%s ready" % browser_data["browserName"])
28 | while len(browsers_waiting) < len(browsers):
29 | print ("working on %s.... please wait" % browser_data["browserName"])
30 | browser.get("http://crossbrowsertesting.com")
31 | time.sleep(3)
32 |
33 | threads = []
34 | for i, browser in enumerate(browsers):
35 | thread = Thread(target=get_browser_and_wait, args=[browser])
36 | threads.append(thread)
37 | thread.start()
38 |
39 |
40 | for thread in threads:
41 | thread.join()
42 |
43 | print ("all browsers ready")
44 | for i, b in enumerate(browsers_waiting):
45 | print ("browser %s's title: %s" % (b["data"]["name"], b["driver"].title))
46 | b["driver"].quit()
47 |
--------------------------------------------------------------------------------
/parallel/nose/test_nose.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 |
3 | import unittest
4 | from selenium import webdriver
5 |
6 | USERNAME = "mikeh"
7 | API_KEY = ""
8 |
9 |
10 | class SeleniumCBT(unittest.TestCase):
11 | def setUp(self):
12 | caps = {}
13 |
14 | caps['name'] = 'Python Nose Parallel'
15 | caps['build'] = '1.0'
16 | caps['browser_api_name'] = 'Chrome43x64'
17 | caps['os_api_name'] = 'Win8.1'
18 | caps['screen_resolution'] = '1024x768'
19 |
20 | # start the remote browser on our server
21 | self.driver = webdriver.Remote(
22 | desired_capabilities=caps,
23 | command_executor="http://%s:%s@hub.crossbrowsertesting.com:80/wd/hub" % (USERNAME, API_KEY)
24 | )
25 |
26 | self.driver.implicitly_wait(20)
27 |
28 | def test_CBT(self):
29 |
30 | # load the page url
31 | print('Loading Url')
32 | self.driver.get('http://crossbrowsertesting.github.io/selenium_example_page.html')
33 |
34 | # check the title
35 | print('Checking title')
36 | self.assertTrue("Selenium Test Example Page" in self.driver.title)
37 |
38 | def tearDown(self):
39 | print("Done with session %s" % self.driver.session_id)
40 | self.driver.quit()
41 |
42 | if __name__ == '__main__':
43 | unittest.main()
44 |
--------------------------------------------------------------------------------
/parallel/nose/test_nose2.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 |
3 | import unittest
4 | from selenium import webdriver
5 |
6 | USERNAME = "mikeh"
7 | API_KEY = ""
8 |
9 |
10 | class SeleniumCBT(unittest.TestCase):
11 | def setUp(self):
12 | caps = {}
13 |
14 | caps['name'] = 'Python Nose Parallel'
15 | caps['build'] = '1.0'
16 | caps['browser_api_name'] = 'IE10'
17 | caps['os_api_name'] = 'Win7x64-C2'
18 | caps['screen_resolution'] = '1024x768'
19 |
20 | # start the remote browser on our server
21 | self.driver = webdriver.Remote(
22 | desired_capabilities=caps,
23 | command_executor="http://%s:%s@hub.crossbrowsertesting.com:80/wd/hub" % (USERNAME, API_KEY)
24 | )
25 |
26 | self.driver.implicitly_wait(20)
27 |
28 | def test_CBT(self):
29 |
30 | # load the page url
31 | print('Loading Url')
32 | self.driver.get('http://crossbrowsertesting.github.io/selenium_example_page.html')
33 |
34 | # check the title
35 | print('Checking title')
36 | self.assertTrue("Selenium Test Example Page" in self.driver.title)
37 |
38 | def tearDown(self):
39 | print("Done with session %s" % self.driver.session_id)
40 | self.driver.quit()
41 |
42 | if __name__ == '__main__':
43 | unittest.main()
44 |
--------------------------------------------------------------------------------
/todo-app.py:
--------------------------------------------------------------------------------
1 | # Please visit http://selenium-python.readthedocs.org/en/latest/index.html for detailed installation and instructions
2 | # Getting started: http://docs.seleniumhq.org/docs/03_webdriver.jsp
3 | # API details: https://github.com/SeleniumHQ/selenium#selenium
4 |
5 | # Requests is the easiest way to make RESTful API calls in Python. You can install it by following the instructions here:
6 | # http://docs.python-requests.org/en/master/user/install/
7 |
8 | import unittest
9 | from selenium import webdriver
10 | import requests
11 |
12 | class TodoAppTest(unittest.TestCase):
13 | def setUp(self):
14 |
15 | # Put your username and authey below
16 | # You can find your authkey at crossbrowsertesting.com/account
17 | self.username = "user@email.com"
18 | self.authkey = "12345"
19 |
20 | self.api_session = requests.Session()
21 | self.api_session.auth = (self.username,self.authkey)
22 |
23 | self.test_result = None
24 |
25 | caps = {}
26 |
27 | caps['name'] = 'Todo App Example'
28 | caps['build'] = '1.0'
29 | caps['browserName'] = 'Chrome'
30 | caps['version'] = '53'
31 | caps['platform'] = 'Windows 10'
32 | caps['screenResolution'] = '1366x768'
33 | caps['record_video'] = 'true'
34 | caps['record_network'] = 'false'
35 |
36 | # start the remote browser on our server
37 | self.driver = webdriver.Remote(
38 | desired_capabilities=caps,
39 | command_executor="http://%s:%s@hub.crossbrowsertesting.com:80/wd/hub"%(self.username,self.authkey)
40 | )
41 |
42 | self.driver.implicitly_wait(20)
43 |
44 | def test_CBT(self):
45 | # We wrap this all in a try/except so we can set pass/fail at the end
46 | try:
47 | # load the page url
48 | print('Loading Url')
49 | self.driver.get('http://crossbrowsertesting.github.io/todo-app.html')
50 |
51 | # maximize the window - DESKTOPS ONLY
52 | #print('Maximizing window')
53 | self.driver.maximize_window()
54 |
55 | # if the checkboxes work, I should be able to click on them
56 | print('Clicking Checkbox')
57 | self.driver.find_element_by_name('todo-4').click()
58 | print('Clicking Checkbox')
59 | self.driver.find_element_by_name('todo-5').click()
60 |
61 | # if I clicked on them, their class name should now be 'done-true'
62 | elems = self.driver.find_elements_by_class_name('done-true')
63 | self.assertEqual(2, len(elems))
64 | # if my form element works, I should be able to enter text and add it to my list.
65 | print('Entering Text')
66 | self.driver.find_element_by_id('todotext').send_keys('Run your first Selenium Test')
67 | self.driver.find_element_by_id('addbutton').click()
68 |
69 | # if I entered the text, the following element should contain that text
70 | span_text = self.driver.find_element_by_xpath('/html/body/div/div/div/ul/li[6]/span').text
71 | self.assertEqual('Run your first Selenium Test', span_text)
72 |
73 | # if my archive link works, my checked elements should be archived
74 | print('Archiving old todos')
75 | self.driver.find_element_by_link_text('archive').click()
76 |
77 | elems = self.driver.find_elements_by_class_name('done-false')
78 | self.assertEqual(4, len(elems))
79 |
80 | snapshot_hash = self.api_session.post('https://crossbrowsertesting.com/api/v3/selenium/' + self.driver.session_id + '/snapshots').json()['hash']
81 |
82 | # if we are still in the try block after all of our assertions that
83 | # means our test has had no failures, so we set the status to "pass"
84 | self.test_result = 'pass'
85 |
86 | except AssertionError as e:
87 | # log the error message, and set the score to "during tearDown()".
88 | self.api_session.put('https://crossbrowsertesting.com/api/v3/selenium/' + self.driver.session_id + '/snapshots/' + snapshot_hash,
89 | data={'description':"AssertionError: " + str(e)})
90 | self.test_result = 'fail'
91 | raise
92 |
93 | def tearDown(self):
94 | print("Done with session %s" % self.driver.session_id)
95 | self.driver.quit()
96 | # Here we make the api call to set the test's score.
97 | # Pass it it passes, fail if an assertion fails, unset if the test didn't finish
98 | if self.test_result is not None:
99 | self.api_session.put('https://crossbrowsertesting.com/api/v3/selenium/' + self.driver.session_id,
100 | data={'action':'set_score', 'score':self.test_result})
101 |
102 |
103 | if __name__ == '__main__':
104 | unittest.main()
105 |
--------------------------------------------------------------------------------