class ListStationsVisibleOnMapTestCase(SeleniumTestCase): button_limit_to_map = PageElement(By.ID, "limit-to-map") td_komboti = PageElement(By.XPATH, '//td//a[text()="Komboti"]') td_agios_athanasios = PageElement(By.XPATH, '//td//a[text()="Agios Athanasios"]') td_tharbad = PageElement(By.XPATH, '//td//a[text()="Tharbad"]') def setUp(self): mommy.make( Station, name="Komboti", geom=Point(x=21.06071, y=39.09518, srid=4326), original_srid=4326, ) mommy.make( Station, name="Agios Athanasios", geom=Point(x=21.60121, y=39.22440, srid=4326), original_srid=4326, ) mommy.make( Station, name="Tharbad", geom=Point(x=-176.48368, y=0.19377, srid=4326), original_srid=4326, ) def test_list_stations_visible_on_map(self): # Visit site and wait until three stations are shown self.selenium.get(self.live_server_url) self.td_komboti.wait_until_is_displayed() self.td_agios_athanasios.wait_until_is_displayed() self.td_tharbad.wait_until_is_displayed() # Zoom station to an area that covers only two of these stations. self.selenium.execute_script( """ enhydris.map.fitBounds([[39.0, 21.0], [40.0, 22.0]]); """ ) # Click on "List stations visible on map" self.button_limit_to_map.click() # Now only two stations should be displayed self.td_komboti.wait_until_is_displayed() self.td_agios_athanasios.wait_until_is_displayed() self.assertFalse(self.td_tharbad.exists())
class ShowStationOnStationDetailMapTestCase(SeleniumTestCase): markers = PageElement(By.CSS_SELECTOR, ".leaflet-marker-pane") def setUp(self): mommy.make(Station, name="West", geom=Point(x=23.0, y=38.0, srid=4326)) self.station = mommy.make( Station, name="Middle", geom=Point(x=23.001, y=38.0, srid=4326) ) mommy.make(Station, name="East", geom=Point(x=23.002, y=38.0, srid=4326)) def test_shows_a_single_station_in_station_detail(self): self.selenium.get( "{}/stations/{}/".format(self.live_server_url, self.station.id) ) num_stations_shown = self._get_num_stations_shown() self.assertEqual(num_stations_shown, 1) def _get_num_stations_shown(self): self.markers.wait_until_exists() for i in range(6): result = len(self.markers.find_elements_by_tag_name("img")) if result: return result sleep(0.5) return 0
class ShowOnlySearchedForStationsOnMapTestCase(SeleniumTestCase): markers = PageElement(By.CSS_SELECTOR, ".leaflet-marker-pane") def setUp(self): mommy.make(Station, name="West", geom=Point(x=23.0, y=38.0, srid=4326)) mommy.make(Station, name="Middle", geom=Point(x=23.1, y=38.0, srid=4326)) mommy.make(Station, name="East", geom=Point(x=23.2, y=38.0, srid=4326)) def test_list_stations_visible_on_map(self): # Visit site and wait until three stations are shown self.selenium.get(self.live_server_url) num_stations_shown = self._get_num_stations_shown() self.assertEqual(num_stations_shown, 3) # Search so that only two stations will be found in an area that could include # all three stations, and verify only two stations are shown self.selenium.get(self.live_server_url + "?q=st") num_stations_shown = self._get_num_stations_shown() self.assertEqual(num_stations_shown, 2) def _get_num_stations_shown(self): self.markers.wait_until_exists() for i in range(6): result = len(self.markers.find_elements_by_tag_name("img")) if result: return result sleep(0.5)
class DailyMonthlyToggleButtonTestCase(SeleniumTestCase): toggle_button = PageElement(By.ID, "timestampSelectorBtn") def test_daily_monthly_toggle(self): self.selenium.get(self.live_server_url) self.toggle_button.wait_until_exists() self.assertEqual(self.toggle_button.text, "Switch to monthly") self.toggle_button.click() sleep(0.1) self.assertEqual(self.toggle_button.text, "Switch to daily")
class MapTestCase(SeleniumTestCase): komboti_div_icon = PageElement( By.XPATH, '//div[contains(@class, "leaflet-div-icon") and .//a/text()="Komboti"]', ) layer_control = PageElement(By.XPATH, '//a[@class="leaflet-control-layers-toggle"]') layer_control_rain = PageElement( By.XPATH, ('//label[input[@class="leaflet-control-layers-selector"] ' 'and span/text()=" Rain"]'), ) layer_control_temperature = PageElement( By.XPATH, ('//label[input[@class="leaflet-control-layers-selector"] ' 'and span/text()=" Air temperature"]'), ) layer_control_wind_gust = PageElement( By.XPATH, ('//label[input[@class="leaflet-control-layers-selector"] ' 'and span/text()=" Wind (gust)"]'), ) def setUp(self): self.data = TestData() settings.TEST_MATPLOTLIB = True self._setup_synoptic_root() def tearDown(self): self._teardown_synoptic_root() settings.TEST_MATPLOTLIB = False def _setup_synoptic_root(self): # We create synoptic root inside static files so that it will be served by # the live server during testing (otherwise relative links to js/css/etc won't # work) this_dir = os.path.dirname(os.path.abspath(__file__)) parent_dir = os.path.dirname(this_dir) static_dir = os.path.join(parent_dir, "static") self.synoptic_root = os.path.join(static_dir, "synoptic") if os.path.exists(self.synoptic_root): raise Exception(( "Directory {} exists; cowardly refusing to remove it. Delete it " "before running the unit tests.").format(self.synoptic_root)) self.saved_synoptic_root = settings.ENHYDRIS_SYNOPTIC_ROOT settings.ENHYDRIS_SYNOPTIC_ROOT = self.synoptic_root def _teardown_synoptic_root(self): settings.ENHYDRIS_SYNOPTIC_ROOT = self.saved_synoptic_root shutil.rmtree(self.synoptic_root) @freeze_time("2015-10-22 14:20:01") def test_outdated_date_shows_red(self): create_static_files() self.selenium.get("{}/static/synoptic/{}/index.html".format( self.live_server_url, self.data.sg1.slug)) self.komboti_div_icon.wait_until_is_displayed() date = self.komboti_div_icon.find_element_by_tag_name("span") self.assertEqual(date.get_attribute("class"), "date old") @freeze_time("2015-10-22 14:19:59") def test_up_to_date_date_shows_green(self): create_static_files() self.selenium.get("{}/static/synoptic/{}/index.html".format( self.live_server_url, self.data.sg1.slug)) self.komboti_div_icon.wait_until_is_displayed() date = self.komboti_div_icon.find_element_by_tag_name("span") self.assertEqual(date.get_attribute("class"), "date recent") @freeze_time("2015-10-22 14:19:59") def test_date_format(self): create_static_files() self.selenium.get("{}/static/synoptic/{}/index.html".format( self.live_server_url, self.data.sg1.slug)) self.komboti_div_icon.wait_until_is_displayed() date = self.komboti_div_icon.find_element_by_tag_name("span") self.assertEqual(date.text, "22 Oct 2015 14:20") def test_value_status(self): create_static_files() self.selenium.get("{}/static/synoptic/{}/index.html".format( self.live_server_url, self.data.sg1.slug)) self.layer_control.wait_until_is_displayed() self.layer_control.click() self.layer_control_rain.wait_until_is_displayed() # Rain should be ok self.layer_control_rain.click() value = self.komboti_div_icon.find_elements_by_tag_name("span")[1] self.assertEqual(value.get_attribute("class"), "value ok") # Wind gust should be high self.layer_control_wind_gust.click() value = self.komboti_div_icon.find_elements_by_tag_name("span")[1] self.assertEqual(value.get_attribute("class"), "value high") # Temperature should be low self.layer_control_temperature.click() value = self.komboti_div_icon.find_elements_by_tag_name("span")[1] self.assertEqual(value.get_attribute("class"), "value low")
class DjangoSeleniumCleanTestCase(SeleniumTestCase): heading_earth = PageElement(By.ID, "earth") heading_world = PageElement(By.ID, "world") user_info = PageElement(By.ID, "user") button_toggle_heading = PageElement(By.ID, "toggle-heading") button_open_window = PageElement(By.ID, "open-window") button_toggle_element = PageElement(By.ID, "toggle-element") button_toggle_message = PageElement(By.ID, "toggle-message") togglable = PageElement(By.ID, "togglable") message = PageElement(By.ID, "message") def test_toggle(self): self.selenium.get(self.live_server_url) # Check that "Greetings to earth" is visible self.assertTrue(self.heading_earth.is_displayed()) self.assertFalse(self.heading_world.is_displayed()) # Toggle and check that "Hello world" is visible self.button_toggle_heading.click() self.heading_world.wait_until_is_displayed() self.assertFalse(self.heading_earth.is_displayed()) self.assertTrue(self.heading_world.is_displayed()) # Toggle again and re-check self.button_toggle_heading.click() self.heading_earth.wait_until_is_displayed() self.assertTrue(self.heading_earth.is_displayed()) self.assertFalse(self.heading_world.is_displayed()) def test_login(self): from django.contrib.auth.hashers import make_password from django.contrib.auth.models import User cap = self.selenium.capabilities if cap["browserName"] == "phantomjs" and cap["version"] == "2.1.1": raise SkipTest("https://github.com/ariya/phantomjs/issues/14228") User.objects.create(username="******", password=make_password("topsecret"), is_active=True) # Verify we aren't logged on self.selenium.get(self.live_server_url) self.user_info.wait_until_is_displayed() self.assertEqual(self.user_info.text, "No user is logged on.") # Log on r = self.selenium.login(username="******", password="******") self.assertTrue(r) # Verify we are logged on self.selenium.get(self.live_server_url) self.user_info.wait_until_is_displayed() self.assertEqual(self.user_info.text, "The logged on user is alice.") # Log out self.selenium.logout() # Verify we are logged out self.selenium.get(self.live_server_url) self.user_info.wait_until_is_displayed() self.assertEqual(self.user_info.text, "No user is logged on.") def test_wait_until_n_windows(self): self.selenium.get(self.live_server_url) # Waiting for two windows should fail as there's only one with self.assertRaises(AssertionError): self.selenium.wait_until_n_windows(n=2, timeout=1) # Open a window and check again self.button_open_window.click() self.selenium.wait_until_n_windows(n=2, timeout=1) # Close the window self.selenium.switch_to_window(self.selenium.window_handles[1]) self.selenium.close() self.selenium.switch_to_window(self.selenium.window_handles[0]) def test_exists(self): self.selenium.get(self.live_server_url) # Element with id=togglable does not exist. Check that the various # waits and asserts are ok. self.togglable.wait_until_not_exists() self.assertFalse(self.togglable.exists()) with self.assertRaises(TimeoutException): self.togglable.wait_until_exists(timeout=1) # Create element with id=togglable... self.button_toggle_element.click() # ...and check things are opposite self.togglable.wait_until_exists() self.assertTrue(self.togglable.exists()) with self.assertRaises(TimeoutException): self.togglable.wait_until_not_exists(timeout=1) # Destroy the element... self.button_toggle_element.click() # ...and check once more self.togglable.wait_until_not_exists() self.assertFalse(self.togglable.exists()) with self.assertRaises(TimeoutException): self.togglable.wait_until_exists(timeout=1) def test_is_displayed(self): self.selenium.get(self.live_server_url) # Element with id=world is not displayed (but it exists). Check that # the various waits and asserts are ok. self.heading_world.wait_until_exists() self.heading_world.wait_until_not_displayed() with self.assertRaises(TimeoutException): self.heading_world.wait_until_not_exists(timeout=1) with self.assertRaises(TimeoutException): self.heading_world.wait_until_is_displayed(timeout=1) self.assertTrue(self.heading_world.exists()) self.assertFalse(self.heading_world.is_displayed()) # Show the element self.button_toggle_heading.click() # ...and check things are opposite self.heading_world.wait_until_exists() self.heading_world.wait_until_is_displayed() with self.assertRaises(TimeoutException): self.heading_world.wait_until_not_exists(timeout=1) with self.assertRaises(TimeoutException): self.heading_world.wait_until_not_displayed(timeout=1) self.assertTrue(self.heading_world.exists()) self.assertTrue(self.heading_world.is_displayed()) # Hide again... self.button_toggle_heading.click() # ...and check once more self.heading_world.wait_until_exists() self.heading_world.wait_until_not_displayed() with self.assertRaises(TimeoutException): self.heading_world.wait_until_not_exists(timeout=1) with self.assertRaises(TimeoutException): self.heading_world.wait_until_is_displayed(timeout=1) self.assertTrue(self.heading_world.exists()) self.assertFalse(self.heading_world.is_displayed()) def test_contains(self): self.selenium.get(self.live_server_url) # Message does not contain 'world'. Check that the various waits # and asserts are OK. self.message.wait_until_contains("earth") self.message.wait_until_not_contains("world") with self.assertRaises(TimeoutException): self.message.wait_until_contains("world", timeout=1) with self.assertRaises(TimeoutException): self.message.wait_until_not_contains("earth", timeout=1) self.assertTrue("earth" in self.message.text) self.assertFalse("world" in self.message.text) # Toggle... self.button_toggle_message.click() # and check things are opposite self.message.wait_until_contains("world") self.message.wait_until_not_contains("earth") with self.assertRaises(TimeoutException): self.message.wait_until_contains("earth", timeout=1) with self.assertRaises(TimeoutException): self.message.wait_until_not_contains("world", timeout=1) self.assertTrue("world" in self.message.text) self.assertFalse("earth" in self.message.text)