Skip to content

Commit

Permalink
Merge pull request #441 from opinkerfi/selenium_tests
Browse files Browse the repository at this point in the history
Selenium tests
  • Loading branch information
palli committed Oct 29, 2014
2 parents ac91cd0 + 82f6b82 commit 67c7014
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ install:
- pip install $DJANGO_VERSION
- pip install simplejson
- pip install paramiko
- pip install selenium
- pip install https://github.com/pynag/pynag/zipball/master
- python setup.py build
- python setup.py install
Expand All @@ -59,3 +60,8 @@ install:
- sudo chmod -R 777 /var/lib/nagios3
notifications:
email: false
addons:
sauce_connect:
username: tommi
access_key:
secure: "GZUO7uiH0Q5/oXAn3CiDjDo9/CeNTeRPI2z1uERAOX6aK0D6XnSpn+ot/KXYN90c/spPqyi4e6MdgfFxuZrt7IK7cMpxEPYYjbB9+vFbODaVvo6/1J3SxpcGJtn/bciEF4bTiEcmL4gfuc6+5FedocvFIpAmHuANOzfv1B26F0o="
46 changes: 46 additions & 0 deletions adagios/objectbrowser/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@

pynag.Model.cfg_file = adagios.settings.nagios_config

try:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
except ImportError:
# selenium tests are skipped if selenium is not available
pass

class TestObjectBrowser(unittest.TestCase):

Expand Down Expand Up @@ -526,6 +534,44 @@ def test_set_prefix(self):
field.set_prefix('')
self.assertEqual('', field.get_prefix())

class SeleniumObjectBrowserTestCase(adagios.utils.SeleniumTestCase):
def test_contacts_loading(self):
"""Test if contacts under configure loads"""
self.driver.get(self.live_server_url + "/objectbrowser/#contact-tab_tab")

wait = WebDriverWait(self.driver, 10)

try:
# Get all host rows
contact_table_rows = wait.until(
EC.presence_of_all_elements_located((
By.XPATH,
"//table[contains(@id, 'contact-table')]/tbody/tr"))
)
except TimeoutException:
self.assertTrue(False, "Timed out waiting for contact table to load")

self.assertTrue(len(contact_table_rows) > 0,
"No table rows in contact table")

def test_hosts_loading(self):
"""Test if hosts under configure loads"""
self.driver.get(self.live_server_url + "/objectbrowser")

wait = WebDriverWait(self.driver, 10)

try:
# Get all host rows
host_table_rows = wait.until(
EC.presence_of_all_elements_located((
By.XPATH,
"//table[contains(@id, 'host-table')]/tbody/tr"))
)
except TimeoutException:
self.assertTrue(False, "Timed out waiting for host table to load")

self.assertTrue(len(host_table_rows) > 0,
"No table rows in host-table")

_TEST_SERVICE = """
define service {
Expand Down
4 changes: 4 additions & 0 deletions adagios/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
)

MANAGERS = ADMINS
STATIC_URL = "/media/"

DATABASES = {
'default': {
Expand Down Expand Up @@ -306,3 +307,6 @@ def get_random_string(length, stringset=string.ascii_letters + string.digits + s
'theme': THEME_DEFAULT,
'refresh_rate': refresh_rate
}

# Allow tests to run server on multiple ports
os.environ['DJANGO_LIVE_TEST_SERVER_ADDRESS'] = 'localhost:8000-9000'
59 changes: 58 additions & 1 deletion adagios/status/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,19 @@
import pynag.Parsers
import os
from django.test.client import RequestFactory
from django.test import LiveServerTestCase
import adagios.status
import adagios.status.utils
import adagios.status.graphite
import adagios.settings
import adagios.utils
import simplejson as json

try:
from selenium.webdriver.common.by import By
except ImportError:
pass


class LiveStatusTestCase(unittest.TestCase):

Expand Down Expand Up @@ -103,7 +109,7 @@ def test_status_detail(self):
tmp = self.loadPage('/status/detail?contactgroup_name=admins')
self.assertTrue('nagiosadmin' in tmp.content)


def testStateHistory(self):
request = self.factory.get('/status/state_history')
adagios.status.views.state_history(request)
Expand Down Expand Up @@ -171,3 +177,54 @@ def test_get(self):
self.assertTrue(len(result) == 1)
self.assertTrue('rta' in result[0]['metrics'])
self.assertTrue('packetloss' in result[0]['metrics'])


class SeleniumStatusTestCase(adagios.utils.SeleniumTestCase):
def test_network_parents(self):
"""Status Overview, Network Parents should show an integer"""
self.driver.get(self.live_server_url + "/status")

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

def test_services_select_all(self):
"""Loads services list and tries to select everything
Flow:
Load http://<url>/status/services
Click select all
Look for statustable rows
Assert that all rows are checked"""

self.driver.get(self.live_server_url + "/status/services")

self.driver.find_element_by_xpath("//input[@class='select_many']").click()
self.driver.find_element_by_xpath("//a[@class='select_all']").click()

# Get all statustable rows
status_table_rows = self.driver.find_element_by_xpath(
"//table[contains(@class, 'statustable')]"
).find_elements(By.XPATH, "//tbody/tr[contains(@class, 'mainrow')]")

# Sub-select non-selected
for row in status_table_rows:
self.assertTrue('row_selected' in row.get_attribute('class'),
"Non selected row found after selecting all: " + \
row.text)

def test_status_overview_top_alert_producers(self):
"""Check the top alert producers part of status overview"""

self.driver.get(self.live_server_url + "/status")

top_alert_table_rows = self.driver.find_elements(By.XPATH,
"//table[@id='top_alert_producers']/tbody/tr"
)

count = 0
for row in top_alert_table_rows:
if 'display' not in row.get_attribute('style'):
count += 1

self.assertTrue(count <= 3, "Top alert producers returns too many rows")
55 changes: 55 additions & 0 deletions adagios/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@
import adagios.settings
import os
import pynag.Utils.misc
from django.test import LiveServerTestCase
from django.utils import unittest
import atexit

from django.utils.translation import ugettext as _

SELENIUM_DRIVER = None

def wait(object_type, WaitObject, WaitCondition, WaitTrigger, **kwargs):
livestatus = adagios.status.utils.livestatus(None)
Expand Down Expand Up @@ -170,3 +174,54 @@ def terminate(self):
self.restore_adagios_global_variables()
super(FakeAdagiosEnvironment, self).terminate()


class SeleniumTestCase(LiveServerTestCase):
driver = None
environment = None

@classmethod
def setUpClass(cls):
global SELENIUM_DRIVER
try:
from selenium import webdriver
except ImportError:
raise unittest.SkipTest("No selenium installed")

super(SeleniumTestCase, cls).setUpClass()

cls.nagios_config = adagios.settings.nagios_config
cls.environment = adagios.utils.FakeAdagiosEnvironment()
cls.environment.create_minimal_environment()
cls.environment.configure_livestatus()
cls.environment.update_adagios_global_variables()
cls.environment.start()
cls.livestatus = cls.environment.get_livestatus()

if not SELENIUM_DRIVER:
if 'TRAVIS' in os.environ:
capabilities = webdriver.DesiredCapabilities.CHROME
capabilities["build"] = os.environ["TRAVIS_BUILD_NUMBER"]
capabilities["tags"] = [os.environ["TRAVIS_PYTHON_VERSION"], "CI"]
capabilities["tunnel-identifier"] = os.environ["TRAVIS_JOB_NUMBER"]
capabilities['platform'] = "Windows 8.1"
capabilities['version'] = "31"

username = os.environ["SAUCE_USERNAME"]
access_key = os.environ["SAUCE_ACCESS_KEY"]

hub_url = "%s:%[email protected]/wd/hub" % (username, access_key)
SELENIUM_DRIVER = webdriver.Remote(desired_capabilities=capabilities, command_executor="http://%s" % hub_url)
else:
SELENIUM_DRIVER = webdriver.Firefox()
# Exit browser when all tests are done
atexit.register(SELENIUM_DRIVER.quit)



cls.driver = SELENIUM_DRIVER

@classmethod
def tearDownClass(cls):
cls.environment.terminate()
super(SeleniumTestCase, cls).tearDownClass()

0 comments on commit 67c7014

Please sign in to comment.