From a061bbbb068e59a79f1324694c7a7d23380cd347 Mon Sep 17 00:00:00 2001 From: Naomi Date: Thu, 4 Oct 2018 13:46:39 +0100 Subject: [PATCH] Refactored tests, and added delay when placing map marker --- ojusomap/tests.py | 57 ++++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/ojusomap/tests.py b/ojusomap/tests.py index 33bafe5..d9d377f 100644 --- a/ojusomap/tests.py +++ b/ojusomap/tests.py @@ -32,7 +32,7 @@ class SeleniumTest(LiveServerTestCase): chrome_options.add_argument('--no-sandbox') # comment out this next line in order to see the tests running in a browser. # But be sure to put it back before comitting, or gitlab CI will fail. - #chrome_options.add_argument('--headless') + chrome_options.add_argument('--headless') chrome_options.add_argument('--disable-gpu') cls.sl = webdriver.Chrome(chrome_options=chrome_options) cls.sl.implicitly_wait(TIMEOUT) @@ -87,12 +87,24 @@ class MapTest(SeleniumTest): ) self.case_study.save() - def test_form_will_show_error(self): + def login(self): self.user = User.objects.create_superuser(username='test', password='test', email='test@example.com'); - self._open('/en-gb/case-study/create/long'); self.sl.find_element_by_id('id_username').send_keys('test'); self.sl.find_element_by_id('id_password').send_keys('test'); self.sl.find_element_by_css_selector('input[type="submit"]').click() + + def place_marker(self): + # click the zoom out button enough to bring up the thing to draw marker then use it. + # the headless browser needs time between each click or it does not show the draw-marker control. + for i in range(0, 15): + self.sl.find_element_by_class_name('leaflet-control-zoom-in').click() + time.sleep(0.2) + self.sl.find_element_by_class_name('leaflet-draw-draw-marker').click() + self.sl.find_element_by_id('id_location-map').click() + + def test_form_will_show_error(self): + self._open('/en-gb/case-study/create/long'); + self.login(); # navigate to the last tab, where the submit button is self.sl.find_element_by_css_selector('a[href="#uploads"]').click() # click submit without filling in any fields @@ -101,16 +113,10 @@ class MapTest(SeleniumTest): self.assertTrue(error_message.is_displayed()) def test_short_form_will_submit(self): - self.user = User.objects.create_superuser(username='test', password='test', email='test@example.com'); self._open('/en-gb/case-study/create/short'); - self.sl.find_element_by_id('id_username').send_keys('test'); - self.sl.find_element_by_id('id_password').send_keys('test'); - self.sl.find_element_by_css_selector('input[type="submit"]').click() - self.sl.find_element_by_id('id_entry_name').send_keys('test'); - for i in range(0, 20): - self.sl.find_element_by_class_name('leaflet-control-zoom-in').click() - self.sl.find_element_by_class_name('leaflet-draw-draw-marker').click() - self.sl.find_element_by_id('id_location-map').click() + self.login() + self.place_marker() + self.sl.find_element_by_id('id_entry_name').send_keys('Short Entry'); Select(self.sl.find_element_by_id('id_country')).select_by_visible_text('Albania'); self.sl.find_element_by_id('id_area_of_land').send_keys('123'); Select(self.sl.find_element_by_id('id_land_ownership')).select_by_visible_text('Private Land'); @@ -121,21 +127,17 @@ class MapTest(SeleniumTest): self.sl.find_element_by_id('id_synopsis').send_keys('test'); self.sl.find_element_by_id('id_full_description').send_keys('test'); Select(self.sl.find_element_by_id('id_positive_or_negative')).select_by_visible_text('Positive'); - self.sl.execute_script("window.scrollTo(0, document.body.scrollHeight);") - self.sl.find_element_by_id('submit-id-submit').click() - self.assertTrue("Thanks!" in self.sl.page_source, "Case study not successfully submitted") + # you can submit the form on any field. Using this one because the submit button sometimes isn't visible + self.sl.find_element_by_id('id_area_of_land').submit(); + self.assertTrue("Thanks!" in self.sl.page_source, "Success message not shown") + self._open('/admin/map/casestudy/'); + self.assertTrue("Short Entry" in self.sl.page_source, "Case study not saved") def test_long_form_will_submit(self): - self.user = User.objects.create_superuser(username='test', password='test', email='test@example.com'); self._open('/en-gb/case-study/create/long'); - self.sl.find_element_by_id('id_username').send_keys('test'); - self.sl.find_element_by_id('id_password').send_keys('test'); - self.sl.find_element_by_css_selector('input[type="submit"]').click() - self.sl.find_element_by_id('id_entry_name').send_keys('test'); - for i in range(0, 20): - self.sl.find_element_by_class_name('leaflet-control-zoom-in').click() - self.sl.find_element_by_class_name('leaflet-draw-draw-marker').click() - self.sl.find_element_by_id('id_location-map').click() + self.login() + self.place_marker() + self.sl.find_element_by_id('id_entry_name').send_keys('Long Entry'); Select(self.sl.find_element_by_id('id_country')).select_by_visible_text('Albania'); self.sl.find_element_by_id('id_area_of_land').send_keys('123'); Select(self.sl.find_element_by_id('id_land_ownership')).select_by_visible_text('Private Land'); @@ -151,8 +153,11 @@ class MapTest(SeleniumTest): self.sl.find_element_by_css_selector('a[href="#socio-environmental-analysis"]').click() Select(self.sl.find_element_by_id('id_positive_or_negative')).select_by_visible_text('Positive'); self.sl.find_element_by_css_selector('a[href="#uploads"]').click() - self.sl.find_element_by_id('submit-id-submit').click() - self.assertTrue("Thanks!" in self.sl.page_source, "Case study not successfully submitted") + # you can submit the form on any field. Using this one because the submit button sometimes isn't visible + self.sl.find_element_by_id('id_name_of_territory_or_area').submit(); + self.assertTrue("Thanks!" in self.sl.page_source, "Success message not shown") + self._open('/admin/map/casestudy/'); + self.assertTrue("Long Entry" in self.sl.page_source, "Case study not saved") def test_map(self): self._open('/')