Skip to content

Commit 6b59065

Browse files
committed
Merge pull request #457 from opinkerfi/selenium_rework
Selenium rework
2 parents a894a5f + d7acf64 commit 6b59065

4 files changed

Lines changed: 148 additions & 115 deletions

File tree

adagios/objectbrowser/tests.py

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import adagios.utils
2828
import adagios.objectbrowser.forms
2929
import re
30+
import adagios.seleniumtests
3031

3132
from adagios.objectbrowser.forms import PynagAutoCompleteField
3233

@@ -534,44 +535,46 @@ def test_set_prefix(self):
534535
field.set_prefix('')
535536
self.assertEqual('', field.get_prefix())
536537

537-
class SeleniumObjectBrowserTestCase(adagios.utils.SeleniumTestCase):
538+
class SeleniumObjectBrowserTestCase(adagios.seleniumtests.SeleniumTestCase):
538539
def test_contacts_loading(self):
539540
"""Test if contacts under configure loads"""
540-
self.driver.get(self.live_server_url + "/objectbrowser/#contact-tab_tab")
541+
for driver in self.drivers:
542+
driver.get(self.live_server_url + "/objectbrowser/#contact-tab_tab")
541543

542-
wait = WebDriverWait(self.driver, 10)
544+
wait = WebDriverWait(driver, 10)
543545

544-
try:
545-
# Get all host rows
546-
contact_table_rows = wait.until(
547-
EC.presence_of_all_elements_located((
548-
By.XPATH,
549-
"//table[contains(@id, 'contact-table')]/tbody/tr"))
550-
)
551-
except TimeoutException:
552-
self.assertTrue(False, "Timed out waiting for contact table to load")
553-
554-
self.assertTrue(len(contact_table_rows) > 0,
555-
"No table rows in contact table")
546+
try:
547+
# Get all host rows
548+
contact_table_rows = wait.until(
549+
EC.presence_of_all_elements_located((
550+
By.XPATH,
551+
"//table[contains(@id, 'contact-table')]/tbody/tr"))
552+
)
553+
except TimeoutException:
554+
self.assertTrue(False, "Timed out waiting for contact table to load")
555+
556+
self.assertTrue(len(contact_table_rows) > 0,
557+
"No table rows in contact table")
556558

557559
def test_hosts_loading(self):
558560
"""Test if hosts under configure loads"""
559-
self.driver.get(self.live_server_url + "/objectbrowser")
560-
561-
wait = WebDriverWait(self.driver, 10)
562-
563-
try:
564-
# Get all host rows
565-
host_table_rows = wait.until(
566-
EC.presence_of_all_elements_located((
567-
By.XPATH,
568-
"//table[contains(@id, 'host-table')]/tbody/tr"))
569-
)
570-
except TimeoutException:
571-
self.assertTrue(False, "Timed out waiting for host table to load")
572-
573-
self.assertTrue(len(host_table_rows) > 0,
574-
"No table rows in host-table")
561+
for driver in self.drivers:
562+
driver.get(self.live_server_url + "/objectbrowser")
563+
564+
wait = WebDriverWait(driver, 10)
565+
566+
try:
567+
# Get all host rows
568+
host_table_rows = wait.until(
569+
EC.presence_of_all_elements_located((
570+
By.XPATH,
571+
"//table[contains(@id, 'host-table')]/tbody/tr"))
572+
)
573+
except TimeoutException:
574+
self.assertTrue(False, "Timed out waiting for host table to load")
575+
576+
self.assertTrue(len(host_table_rows) > 0,
577+
"No table rows in host-table")
575578

576579
_TEST_SERVICE = """
577580
define service {

adagios/seleniumtests.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
from django.test import LiveServerTestCase
2+
from django.utils import unittest
3+
import adagios.settings
4+
import adagios.utils
5+
import os
6+
7+
try:
8+
from selenium import webdriver
9+
from selenium.common.exceptions import WebDriverException
10+
except ImportError:
11+
webdriver = None
12+
13+
def get_remote_webdriver(capabilities=None):
14+
"""Get remote webdriver. Configured using environment variables
15+
to setup where the remote webdriver is. options could be a local
16+
install or using the setup at saucelabs"""
17+
18+
if not capabilities:
19+
capabilities = webdriver.DesiredCapabilities.FIREFOX
20+
21+
# Saucelabs setup,
22+
capabilities["build"] = os.environ.get("TRAVIS_BUILD_NUMBER")
23+
capabilities["tags"] = [os.environ.get("TRAVIS_PYTHON_VERSION"), "CI"]
24+
capabilities["tunnel-identifier"] = os.environ.get("TRAVIS_JOB_NUMBER")
25+
26+
username = os.environ.get("SAUCE_USERNAME")
27+
access_key = os.environ.get("SAUCE_ACCESS_KEY")
28+
hub_url = os.environ.get('SAUCE_HUBURL', "ondemand.saucelabs.com/wd/hub")
29+
30+
if username and access_key:
31+
hub_url = "%s:%s@%s" % (username, access_key, hub_url)
32+
33+
return webdriver.Remote(desired_capabilities=capabilities,
34+
command_executor="http://%s" % hub_url)
35+
36+
class SeleniumTestCase(LiveServerTestCase):
37+
environment = None
38+
39+
@classmethod
40+
def setUpClass(cls):
41+
if not webdriver:
42+
raise unittest.SkipTest("No selenium installed")
43+
44+
# Tests for pull requests from forks do not get the SAUCE_USERNAME
45+
# exposed because of security considerations. Skip selenium tests for
46+
# those tests
47+
if os.environ.get('TRAVIS_BUILD_NUMBER') and \
48+
not os.environ.get('SAUCE_USERNAME'):
49+
raise unittest.SkipTest("Travis with no sauce username, skipping")
50+
51+
super(SeleniumTestCase, cls).setUpClass()
52+
53+
cls.drivers = []
54+
55+
try:
56+
if 'SELENIUM_REMOTE_TESTS' in os.environ or \
57+
'TRAVIS_BUILD_NUMBER' in os.environ:
58+
# Fire up remote webdriver
59+
remote = get_remote_webdriver()
60+
cls.drivers.append(remote)
61+
else:
62+
# Use the firefox webdriver
63+
firefox = webdriver.Firefox()
64+
cls.drivers.append(firefox)
65+
except WebDriverException as error:
66+
raise unittest.SkipTest("Exception in running webdriver, skipping " \
67+
"selenium tests: %s" % str(error))
68+
69+
cls.nagios_config = adagios.settings.nagios_config
70+
cls.environment = adagios.utils.FakeAdagiosEnvironment()
71+
cls.environment.create_minimal_environment()
72+
cls.environment.configure_livestatus()
73+
cls.environment.update_adagios_global_variables()
74+
cls.environment.start()
75+
cls.livestatus = cls.environment.get_livestatus()
76+
77+
@classmethod
78+
def tearDownClass(cls):
79+
cls.environment.terminate()
80+
for driver in cls.drivers:
81+
driver.quit()
82+
super(SeleniumTestCase, cls).tearDownClass()
83+

adagios/status/tests.py

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import adagios.settings
3232
import adagios.utils
3333
import simplejson as json
34+
import adagios.seleniumtests
3435

3536
try:
3637
from selenium.webdriver.common.by import By
@@ -179,14 +180,15 @@ def test_get(self):
179180
self.assertTrue('packetloss' in result[0]['metrics'])
180181

181182

182-
class SeleniumStatusTestCase(adagios.utils.SeleniumTestCase):
183+
class SeleniumStatusTestCase(adagios.seleniumtests.SeleniumTestCase):
183184
def test_network_parents(self):
184185
"""Status Overview, Network Parents should show an integer"""
185-
self.driver.get(self.live_server_url + "/status")
186+
for driver in self.drivers:
187+
driver.get(self.live_server_url + "/status")
186188

187-
# Second link is Network Parents in overview
188-
self.assertEqual(self.driver.find_elements(By.XPATH,
189-
"//a[@href='/status/parents']")[1].text.isdigit(), True)
189+
# Second link is Network Parents in overview
190+
self.assertEqual(driver.find_elements(By.XPATH,
191+
"//a[@href='/status/parents']")[1].text.isdigit(), True)
190192

191193
def test_services_select_all(self):
192194
"""Loads services list and tries to select everything
@@ -197,34 +199,35 @@ def test_services_select_all(self):
197199
Look for statustable rows
198200
Assert that all rows are checked"""
199201

200-
self.driver.get(self.live_server_url + "/status/services")
202+
for driver in self.drivers:
203+
driver.get(self.live_server_url + "/status/services")
201204

202-
self.driver.find_element_by_xpath("//input[@class='select_many']").click()
203-
self.driver.find_element_by_xpath("//a[@class='select_all']").click()
205+
driver.find_element_by_xpath("//input[@class='select_many']").click()
206+
driver.find_element_by_xpath("//a[@class='select_all']").click()
204207

205-
# Get all statustable rows
206-
status_table_rows = self.driver.find_element_by_xpath(
207-
"//table[contains(@class, 'statustable')]"
208-
).find_elements(By.XPATH, "//tbody/tr[contains(@class, 'mainrow')]")
208+
# Get all statustable rows
209+
status_table_rows = driver.find_element_by_xpath(
210+
"//table[contains(@class, 'statustable')]"
211+
).find_elements(By.XPATH, "//tbody/tr[contains(@class, 'mainrow')]")
209212

210-
# Sub-select non-selected
211-
for row in status_table_rows:
212-
self.assertTrue('row_selected' in row.get_attribute('class'),
213-
"Non selected row found after selecting all: " + \
214-
row.text)
213+
# Sub-select non-selected
214+
for row in status_table_rows:
215+
self.assertTrue('row_selected' in row.get_attribute('class'),
216+
"Non selected row found after selecting all: " + \
217+
row.text)
215218

216219
def test_status_overview_top_alert_producers(self):
217220
"""Check the top alert producers part of status overview"""
221+
for driver in self.drivers:
222+
driver.get(self.live_server_url + "/status")
218223

219-
self.driver.get(self.live_server_url + "/status")
224+
top_alert_table_rows = driver.find_elements(By.XPATH,
225+
"//table[@id='top_alert_producers']/tbody/tr"
226+
)
220227

221-
top_alert_table_rows = self.driver.find_elements(By.XPATH,
222-
"//table[@id='top_alert_producers']/tbody/tr"
223-
)
228+
count = 0
229+
for row in top_alert_table_rows:
230+
if 'display' not in row.get_attribute('style'):
231+
count += 1
224232

225-
count = 0
226-
for row in top_alert_table_rows:
227-
if 'display' not in row.get_attribute('style'):
228-
count += 1
229-
230-
self.assertTrue(count <= 3, "Top alert producers returns too many rows")
233+
self.assertTrue(count <= 3, "Top alert producers returns too many rows")

adagios/utils.py

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,14 @@
1717
# You should have received a copy of the GNU Affero General Public License
1818
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1919

20-
import multiprocessing
2120
import adagios.status.utils
22-
import time
2321
import adagios
2422
import pynag.Model
2523
import adagios.exceptions
2624
import adagios.settings
2725
import os
2826
import pynag.Utils.misc
29-
from django.test import LiveServerTestCase
30-
from django.utils import unittest
31-
import atexit
32-
27+
from multiprocessing.pool import ThreadPool
3328
from django.utils.translation import ugettext as _
3429

3530
SELENIUM_DRIVER = None
@@ -56,7 +51,6 @@ def wait_for_service(host_name, service_description, condition='last_check >= 0'
5651
WaitObject=waitobject
5752
)
5853

59-
from multiprocessing.pool import ThreadPool
6054

6155

6256
class Task(object):
@@ -175,53 +169,3 @@ def terminate(self):
175169
super(FakeAdagiosEnvironment, self).terminate()
176170

177171

178-
class SeleniumTestCase(LiveServerTestCase):
179-
driver = None
180-
environment = None
181-
182-
@classmethod
183-
def setUpClass(cls):
184-
global SELENIUM_DRIVER
185-
try:
186-
from selenium import webdriver
187-
except ImportError:
188-
raise unittest.SkipTest("No selenium installed")
189-
190-
super(SeleniumTestCase, cls).setUpClass()
191-
192-
cls.nagios_config = adagios.settings.nagios_config
193-
cls.environment = adagios.utils.FakeAdagiosEnvironment()
194-
cls.environment.create_minimal_environment()
195-
cls.environment.configure_livestatus()
196-
cls.environment.update_adagios_global_variables()
197-
cls.environment.start()
198-
cls.livestatus = cls.environment.get_livestatus()
199-
200-
if not SELENIUM_DRIVER:
201-
if 'TRAVIS' in os.environ:
202-
capabilities = webdriver.DesiredCapabilities.CHROME
203-
capabilities["build"] = os.environ["TRAVIS_BUILD_NUMBER"]
204-
capabilities["tags"] = [os.environ["TRAVIS_PYTHON_VERSION"], "CI"]
205-
capabilities["tunnel-identifier"] = os.environ["TRAVIS_JOB_NUMBER"]
206-
capabilities['platform'] = "Windows 8.1"
207-
capabilities['version'] = "31"
208-
209-
username = os.environ["SAUCE_USERNAME"]
210-
access_key = os.environ["SAUCE_ACCESS_KEY"]
211-
212-
hub_url = "%s:%s@ondemand.saucelabs.com/wd/hub" % (username, access_key)
213-
SELENIUM_DRIVER = webdriver.Remote(desired_capabilities=capabilities, command_executor="http://%s" % hub_url)
214-
else:
215-
SELENIUM_DRIVER = webdriver.Firefox()
216-
# Exit browser when all tests are done
217-
atexit.register(SELENIUM_DRIVER.quit)
218-
219-
220-
221-
cls.driver = SELENIUM_DRIVER
222-
223-
@classmethod
224-
def tearDownClass(cls):
225-
cls.environment.terminate()
226-
super(SeleniumTestCase, cls).tearDownClass()
227-

0 commit comments

Comments
 (0)