|
1 | 1 | #!/usr/bin/env python |
2 | 2 | # -*- coding: utf-8 -*- |
3 | 3 |
|
4 | | -# To install the Python client library: |
5 | | -# pip install -U selenium |
| 4 | +# Dependencies |
| 5 | +# pip install -U selenium==3.3.1 |
| 6 | +# Usage |
| 7 | +# curl -sSL https://raw.github.com/dosel/t/i/s | python |
6 | 8 | import os |
7 | 9 | import time |
8 | 10 | import datetime |
|
12 | 14 | from selenium import webdriver |
13 | 15 | from selenium.webdriver.common.keys import Keys |
14 | 16 | from selenium.webdriver.common.desired_capabilities import DesiredCapabilities |
| 17 | +from selenium.webdriver.common.by import By |
| 18 | +from selenium.common.exceptions import NoSuchElementException |
15 | 19 |
|
16 | 20 | import argparse |
17 | 21 | parser = argparse.ArgumentParser(description='Perform some basic selenium tests.') |
|
22 | 26 | # http://selenium-python.readthedocs.org/en/latest/api.html |
23 | 27 | if args.browser == 'chrome': |
24 | 28 | caps = DesiredCapabilities.CHROME |
| 29 | + browserName = args.browser |
25 | 30 | elif args.browser == 'mobile_emulation': |
26 | 31 | mobile_emulation = {"deviceName": "iPad"} |
27 | 32 | opts = webdriver.ChromeOptions() |
28 | 33 | opts.add_experimental_option("mobileEmulation", mobile_emulation) |
29 | 34 | caps = opts.to_capabilities() |
| 35 | + browserName = 'chrome' |
30 | 36 | elif args.browser == 'firefox': |
31 | 37 | caps = DesiredCapabilities.FIREFOX |
| 38 | + browserName = args.browser |
32 | 39 | else: |
33 | 40 | raise ValueError("Invalid browser '%s'" % args.browser) |
34 | 41 |
|
35 | | -msleep = float( os.environ.get('TEST_SLEEPS', '0.1') ) |
| 42 | +msleep = float( os.environ.get('TEST_SLEEPS', '0.01') ) |
| 43 | + |
| 44 | +# http://selenium-python.readthedocs.io/api.html#desired-capabilities |
| 45 | +# Create a desired capabilities object as a starting point. |
| 46 | +browserVersion = os.environ.get('CAPS_BROWSER_VERSION', '') |
36 | 47 |
|
37 | 48 | # http://selenium-python.readthedocs.org/en/latest/api.html |
38 | 49 | sel_proto = os.environ.get('SELENIUM_HUB_PROTO','http') |
|
41 | 52 | myselenium_base_url = "%s://%s:%s" % (sel_proto, sel_host, sel_port) |
42 | 53 | myselenium_grid_console_url = "%s/grid/console" % (myselenium_base_url) |
43 | 54 | myselenium_hub_url = "%s/wd/hub" % (myselenium_base_url) |
44 | | -print ("Will use browser=%s" % args.browser) |
45 | | -print ("Will sleep '%s' secs between test steps" % msleep) |
| 55 | +myselenium_hub_url = os.environ.get('SELENIUM_URL', myselenium_hub_url) |
46 | 56 |
|
47 | | -@retry(stop_max_attempt_number=12, stop_max_delay=30100, wait_fixed=300) |
48 | | -def webdriver_connect(): |
49 | | - print ("Will connect to selenium at %s" % myselenium_hub_url) |
50 | | - # http://selenium-python.readthedocs.org/en/latest/getting-started.html#using-selenium-with-remote-webdriver |
51 | | - return webdriver.Remote(command_executor=myselenium_hub_url, desired_capabilities=caps) |
| 57 | +# Group tests by `build` |
| 58 | +buildId = "%s%s" % (os.environ.get('JOB_NAME', ''), os.environ.get('BUILD_NUMBER', '')) |
| 59 | +if buildId == '': |
| 60 | + buildId = 'dosel' |
52 | 61 |
|
53 | | -driver = webdriver_connect() |
54 | | -driver.implicitly_wait(4) |
55 | | -time.sleep(msleep) |
| 62 | +# Within `build` identify one test by `name` |
| 63 | +nameId = os.environ.get('TEST_ID', 'test-adwords') |
| 64 | + |
| 65 | +# Have a long Id for the log outpus |
| 66 | +longId = "%s - %s - %s%s" % (buildId, nameId, browserName, browserVersion) |
56 | 67 |
|
57 | 68 | # Set location top left and size to max allowed on the container |
58 | 69 | width = os.environ.get('SCREEN_WIDTH','800') |
59 | 70 | height = os.environ.get('SCREEN_HEIGHT','600') |
| 71 | + |
| 72 | +# Build the capabilities |
| 73 | +caps = {'browserName': browserName} |
| 74 | +caps['platform'] = os.environ.get('CAPS_OS_PLATFORM', 'ANY') |
| 75 | +caps['version'] = browserVersion |
| 76 | +# caps['tunnelIdentifier'] = os.environ.get('TUNNEL_ID', 'zalenium') |
| 77 | +caps['tunnel-identifier'] = os.environ.get('TUNNEL_ID', 'zalenium') |
| 78 | +# caps['screenResolution'] = "%sx%sx24" % (width, height) |
| 79 | +caps['screenResolution'] = "%sx%s" % (width, height) |
| 80 | +caps['name'] = nameId |
| 81 | +caps['build'] = buildId |
| 82 | +caps['recordVideo'] = 'false' |
| 83 | + |
| 84 | +# http://selenium-python.readthedocs.org/en/latest/getting-started.html#using-selenium-with-remote-webdriver |
| 85 | +print ("%s %s - (01/14) Will connect to selenium at %s" % (datetime.datetime.utcnow(), longId, myselenium_hub_url)) |
| 86 | +driver = webdriver.Remote(command_executor=myselenium_hub_url, desired_capabilities=caps) |
| 87 | +time.sleep(msleep) |
| 88 | + |
| 89 | +def is_element_present(how, what): |
| 90 | + try: driver.find_element(by=how, value=what) |
| 91 | + except NoSuchElementException: return False |
| 92 | + return True |
| 93 | + |
| 94 | +driver.implicitly_wait(4) |
| 95 | + |
60 | 96 | driver.set_window_position(0, 0) |
61 | 97 | driver.set_window_size(width, height) |
62 | 98 |
|
63 | | -@retry(stop_max_attempt_number=5, stop_max_delay=20100, wait_fixed=100) |
| 99 | +@retry(stop_max_attempt_number=8, stop_max_delay=20100, wait_fixed=200) |
64 | 100 | def open_hub_page(): |
65 | | - print ("Opening local selenium grid console page %s" % |
66 | | - myselenium_grid_console_url) |
| 101 | + print ("%s %s - Opening local selenium grid console page %s" % |
| 102 | + (datetime.datetime.utcnow(), longId, myselenium_grid_console_url)) |
67 | 103 | driver.get(myselenium_grid_console_url) |
68 | 104 |
|
69 | | -@retry(stop_max_attempt_number=5, stop_max_delay=20100, wait_fixed=100) |
| 105 | +@retry(stop_max_attempt_number=8, stop_max_delay=20100, wait_fixed=200) |
70 | 106 | def check_hub_title(): |
71 | | - print ("Current title: %s" % driver.title) |
72 | | - print ("Asserting 'Grid Console' in driver.title") |
| 107 | + print ("%s %s - Current title: %s" % (datetime.datetime.utcnow(), longId, driver.title)) |
| 108 | + print ("%s %s - Asserting 'Grid Console' in driver.title" % (datetime.datetime.utcnow(), longId)) |
73 | 109 | assert "Grid Console" in driver.title |
74 | 110 |
|
75 | 111 | if args.browser == 'chrome': |
@@ -100,75 +136,83 @@ def check_hub_title(): |
100 | 136 | page_host = os.environ.get('MOCK_SERVER_HOST','localhost') |
101 | 137 | pageurl = ("http://%s:%s/adwords" % (page_host, page_port)) |
102 | 138 |
|
103 | | -@retry(stop_max_attempt_number=7, stop_max_delay=20100, wait_fixed=300) |
| 139 | +@retry(stop_max_attempt_number=20, stop_max_delay=40100, wait_fixed=300) |
104 | 140 | def open_web_page(): |
105 | | - print ("Opening page %s" % pageurl) |
| 141 | + print ("%s %s - (02/14) Opening page %s" % (datetime.datetime.utcnow(), longId, pageurl)) |
106 | 142 | driver.get(pageurl) |
107 | 143 | time.sleep(msleep) |
108 | | - print ("Current title: %s" % driver.title) |
109 | | - print ("Asserting 'Google Adwords' in driver.title") |
| 144 | + print ("%s %s - (03/14) Current title: %s" % (datetime.datetime.utcnow(), longId, driver.title)) |
| 145 | + print ("%s %s - (04/14) Asserting 'Google Adwords' in driver.title" % (datetime.datetime.utcnow(), longId)) |
110 | 146 | assert "Google AdWords | Pay-per-Click-Onlinewerbung auf Google (PPC)" in driver.title |
111 | 147 |
|
112 | 148 | open_web_page() |
113 | 149 |
|
114 | | -if args.browser == 'mobile_emulation': |
| 150 | +@retry(stop_max_attempt_number=12, stop_max_delay=5000, wait_fixed=300) |
| 151 | +def mobile_emulation_get_costs(): |
115 | 152 | pageurl = ("http://%s:%s/adwords/costs" % (page_host, page_port)) |
116 | | - print ("mobile_emulation test: Opening page %s" % pageurl) |
| 153 | + print ("%s %s - (04/14) mobile_emulation test: Opening page %s" % (datetime.datetime.utcnow(), longId, pageurl)) |
117 | 154 | driver.get(pageurl) |
118 | | - time.sleep(msleep) |
119 | | -else: |
120 | | - print ("Click link 'Kosten'") |
| 155 | + |
| 156 | +@retry(stop_max_attempt_number=40, stop_max_delay=40100, wait_fixed=300) |
| 157 | +def click_kosten(): |
| 158 | + print ("%s %s - (04/14) Click link 'Kosten'" % (datetime.datetime.utcnow(), longId)) |
| 159 | + assert is_element_present(By.LINK_TEXT, "Kosten") |
121 | 160 | link = driver.find_element_by_link_text('Kosten') |
| 161 | + assert link.is_displayed() |
122 | 162 | link.click() |
123 | | - driver.maximize_window() |
124 | | - time.sleep(msleep) |
125 | 163 |
|
126 | | -@retry(stop_max_attempt_number=7, stop_max_delay=20100, wait_fixed=300) |
127 | | -def open_costs_page(): |
128 | | - print ("Current title: %s" % driver.title) |
129 | | - print ("Asserting 'Kosten' in driver.title") |
| 164 | +if args.browser == 'mobile_emulation': |
| 165 | + mobile_emulation_get_costs() |
| 166 | +else: |
| 167 | + try: |
| 168 | + click_kosten() |
| 169 | + except: |
| 170 | + time.sleep(1) |
| 171 | + open_web_page() |
| 172 | + try: |
| 173 | + click_kosten() |
| 174 | + except: |
| 175 | + time.sleep(2) |
| 176 | + open_web_page() |
| 177 | + click_kosten() |
| 178 | + |
| 179 | +time.sleep(msleep) |
| 180 | + |
| 181 | +@retry(stop_max_attempt_number=20, stop_max_delay=40100, wait_fixed=300) |
| 182 | +def assert_at_costs_page(): |
| 183 | + print ("%s %s - (05/14) Current title: %s" % (datetime.datetime.utcnow(), longId, driver.title)) |
| 184 | + print ("%s %s - (06/14) Asserting 'Kosten' in driver.title" % (datetime.datetime.utcnow(), longId)) |
130 | 185 | assert "Kosten von Google AdWords | Google AdWords" in driver.title |
131 | 186 |
|
132 | | -open_costs_page() |
| 187 | +assert_at_costs_page() |
133 | 188 |
|
134 | | -if args.browser != 'mobile_emulation': |
135 | | - @retry(stop_max_attempt_number=4, stop_max_delay=10100, wait_fixed=300) |
136 | | - def assert_overview_page(): |
137 | | - print ("Go back to home page") |
138 | | - screen_shot_path = '/test/overview1.png' |
139 | | - print ("Taking screen shot and saving to %s" % screen_shot_path) |
140 | | - driver.get_screenshot_as_file(screen_shot_path) |
141 | | - try: |
142 | | - link = driver.find_element_by_link_text('Übersicht') |
143 | | - except: |
144 | | - time.sleep(1) |
145 | | - try: |
146 | | - link = driver.find_element_by_link_text('Übersicht') |
147 | | - except: |
148 | | - time.sleep(3) |
149 | | - link = driver.find_element_by_link_text('Übersicht') |
150 | | - |
151 | | - screen_shot_path = '/test/overview2.png' |
152 | | - print ("Taking screen shot and saving to %s" % screen_shot_path) |
153 | | - driver.get_screenshot_as_file(screen_shot_path) |
154 | | - link.click() |
155 | | - time.sleep(msleep) |
156 | | - print ("Current title: %s" % driver.title) |
157 | | - print ("Asserting 'Google (PPC)' in driver.title") |
158 | | - assert "Google AdWords | Pay-per-Click-Onlinewerbung auf Google (PPC)" in driver.title |
159 | | - time.sleep(msleep) |
| 189 | +@retry(stop_max_attempt_number=40, stop_max_delay=40100, wait_fixed=300) |
| 190 | +def assert_overview_page(): |
| 191 | + print ("%s %s - (07/14) Go back to home page" % (datetime.datetime.utcnow(), longId)) |
| 192 | + assert is_element_present(By.LINK_TEXT, 'Übersicht') |
| 193 | + link = driver.find_element_by_link_text('Übersicht') |
| 194 | + assert link.is_displayed() |
| 195 | + link.click() |
| 196 | + time.sleep(msleep) |
| 197 | + print ("%s %s - (10/14) Current title: %s" % (datetime.datetime.utcnow(), longId, driver.title)) |
| 198 | + print ("%s %s - (11/14) Asserting 'Google (PPC)' in driver.title" % (datetime.datetime.utcnow(), longId)) |
| 199 | + assert "Google AdWords | Pay-per-Click-Onlinewerbung auf Google (PPC)" in driver.title |
| 200 | + time.sleep(msleep) |
160 | 201 |
|
| 202 | +if args.browser != 'mobile_emulation': |
161 | 203 | assert_overview_page() |
162 | 204 |
|
163 | | -print ("Close driver and clean up") |
| 205 | +print ("%s %s - (12/14) Test done - will driver.close()" % (datetime.datetime.utcnow(), longId)) |
164 | 206 | try: |
165 | 207 | driver.close() |
166 | 208 | except: |
167 | 209 | pass |
168 | 210 | time.sleep(msleep) |
169 | 211 |
|
170 | | -print ("All done. SUCCESS!") |
| 212 | +print ("%s %s - (13/14) Test done - will driver.quit()" % (datetime.datetime.utcnow(), longId)) |
171 | 213 | try: |
172 | 214 | driver.quit() |
173 | 215 | except: |
174 | 216 | pass |
| 217 | + |
| 218 | +print ("%s %s - (14/14) All done. SUCCESS! - DONE driver.quit()" % (datetime.datetime.utcnow(), longId)) |
0 commit comments