Skip to content
This repository was archived by the owner on Jun 30, 2021. It is now read-only.

Commit 4394b85

Browse files
committed
Improve tests
1 parent 768a4eb commit 4394b85

File tree

3 files changed

+118
-67
lines changed

3 files changed

+118
-67
lines changed

test/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Sample output
2525
Will connect to selenium at http://172.17.0.6:24444/wd/hub
2626
Opening page http://www.google.com/adwords
2727
Current title: Google AdWords | Pay-per-Click-Onlinewerbung auf Google (PPC)
28-
Asserting 'Google Adwords' in driver.title
28+
Asserting 'Google AdWords' in driver.title
2929
Opening page http://www.python.org
3030
Asserting 'Python' in driver.title
3131
Finding element by name: q

test/python_test.py

Lines changed: 107 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

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
68
import os
79
import time
810
import datetime
@@ -12,6 +14,8 @@
1214
from selenium import webdriver
1315
from selenium.webdriver.common.keys import Keys
1416
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
17+
from selenium.webdriver.common.by import By
18+
from selenium.common.exceptions import NoSuchElementException
1519

1620
import argparse
1721
parser = argparse.ArgumentParser(description='Perform some basic selenium tests.')
@@ -22,17 +26,24 @@
2226
# http://selenium-python.readthedocs.org/en/latest/api.html
2327
if args.browser == 'chrome':
2428
caps = DesiredCapabilities.CHROME
29+
browserName = args.browser
2530
elif args.browser == 'mobile_emulation':
2631
mobile_emulation = {"deviceName": "iPad"}
2732
opts = webdriver.ChromeOptions()
2833
opts.add_experimental_option("mobileEmulation", mobile_emulation)
2934
caps = opts.to_capabilities()
35+
browserName = 'chrome'
3036
elif args.browser == 'firefox':
3137
caps = DesiredCapabilities.FIREFOX
38+
browserName = args.browser
3239
else:
3340
raise ValueError("Invalid browser '%s'" % args.browser)
3441

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', '')
3647

3748
# http://selenium-python.readthedocs.org/en/latest/api.html
3849
sel_proto = os.environ.get('SELENIUM_HUB_PROTO','http')
@@ -41,35 +52,60 @@
4152
myselenium_base_url = "%s://%s:%s" % (sel_proto, sel_host, sel_port)
4253
myselenium_grid_console_url = "%s/grid/console" % (myselenium_base_url)
4354
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)
4656

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'
5261

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)
5667

5768
# Set location top left and size to max allowed on the container
5869
width = os.environ.get('SCREEN_WIDTH','800')
5970
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+
6096
driver.set_window_position(0, 0)
6197
driver.set_window_size(width, height)
6298

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)
64100
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))
67103
driver.get(myselenium_grid_console_url)
68104

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)
70106
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))
73109
assert "Grid Console" in driver.title
74110

75111
if args.browser == 'chrome':
@@ -100,75 +136,83 @@ def check_hub_title():
100136
page_host = os.environ.get('MOCK_SERVER_HOST','localhost')
101137
pageurl = ("http://%s:%s/adwords" % (page_host, page_port))
102138

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)
104140
def open_web_page():
105-
print ("Opening page %s" % pageurl)
141+
print ("%s %s - (02/14) Opening page %s" % (datetime.datetime.utcnow(), longId, pageurl))
106142
driver.get(pageurl)
107143
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))
110146
assert "Google AdWords | Pay-per-Click-Onlinewerbung auf Google (PPC)" in driver.title
111147

112148
open_web_page()
113149

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():
115152
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))
117154
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")
121160
link = driver.find_element_by_link_text('Kosten')
161+
assert link.is_displayed()
122162
link.click()
123-
driver.maximize_window()
124-
time.sleep(msleep)
125163

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))
130185
assert "Kosten von Google AdWords | Google AdWords" in driver.title
131186

132-
open_costs_page()
187+
assert_at_costs_page()
133188

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)
160201

202+
if args.browser != 'mobile_emulation':
161203
assert_overview_page()
162204

163-
print ("Close driver and clean up")
205+
print ("%s %s - (12/14) Test done - will driver.close()" % (datetime.datetime.utcnow(), longId))
164206
try:
165207
driver.close()
166208
except:
167209
pass
168210
time.sleep(msleep)
169211

170-
print ("All done. SUCCESS!")
212+
print ("%s %s - (13/14) Test done - will driver.quit()" % (datetime.datetime.utcnow(), longId))
171213
try:
172214
driver.quit()
173215
except:
174216
pass
217+
218+
print ("%s %s - (14/14) All done. SUCCESS! - DONE driver.quit()" % (datetime.datetime.utcnow(), longId))

test/script_scenario_basic

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,27 @@ function get_mock_port() {
2222
}
2323

2424
function get_mock_ip() {
25-
docker inspect -f='{{.NetworkSettings.IPAddress}}' grid_mock
25+
#if [ "$(uname)" == 'Darwin' ]; then
26+
# echo "192.168.65.1"
27+
#else
28+
docker inspect -f='{{.NetworkSettings.IPAddress}}' grid_mock
29+
#fi
2630
}
2731

2832
docker stop grid_mock || true
2933
docker rm grid_mock || true
3034

3135
export MOCK_SERVER_PORT=8080
3236

33-
docker run -d -t --name=grid_mock -e MOCK_SERVER_PORT elgalu/google_adwords_mock
37+
docker run -d -t --name=grid_mock -e MOCK_SERVER_PORT \
38+
-p $MOCK_SERVER_PORT:$MOCK_SERVER_PORT \
39+
elgalu/google_adwords_mock
40+
3441
docker attach grid_mock &
3542

3643
export MOCK_SERVER_HOST=$(get_mock_ip)
3744

38-
while ! curl -s "http://${MOCK_SERVER_HOST}:${MOCK_SERVER_PORT}/adwords"; do
45+
while ! curl -s "http://localhost:${MOCK_SERVER_PORT}/adwords"; do
3946
echo -n '.'
4047
sleep 0.2
4148
done

0 commit comments

Comments
 (0)