2018-06-20 04:10:02 +00:00
|
|
|
import time
|
2019-08-19 21:53:34 +00:00
|
|
|
from contextlib import contextmanager
|
2018-06-20 04:10:02 +00:00
|
|
|
|
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.test import LiveServerTestCase
|
|
|
|
from selenium import webdriver
|
|
|
|
from selenium.webdriver.common.by import By
|
2019-08-19 21:53:34 +00:00
|
|
|
from selenium.webdriver.support.expected_conditions import staleness_of
|
|
|
|
from selenium.webdriver.support.expected_conditions import visibility_of_element_located
|
2018-06-20 04:10:02 +00:00
|
|
|
from selenium.webdriver.support.select import Select
|
2019-08-19 21:53:34 +00:00
|
|
|
from selenium.webdriver.support.wait import WebDriverWait
|
2018-06-20 04:10:02 +00:00
|
|
|
|
|
|
|
from apps.map.models import CaseStudy
|
|
|
|
|
|
|
|
|
|
|
|
TIMEOUT = 8
|
|
|
|
|
|
|
|
|
|
|
|
class SeleniumTest(LiveServerTestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2019-08-19 21:52:27 +00:00
|
|
|
# To test with firefox, uncomment these lines and comment those pertaining
|
|
|
|
# to Chrome. However, it will not work with GitLab CI because the docker
|
|
|
|
# container does not have the geckodriver installed.
|
|
|
|
#
|
2018-09-26 15:18:57 +00:00
|
|
|
# profile = webdriver.FirefoxProfile()
|
|
|
|
# profile.set_preference("dom.forms.number", False)
|
|
|
|
# cls.sl = webdriver.Firefox(profile)
|
2018-09-26 14:38:56 +00:00
|
|
|
chrome_options = webdriver.ChromeOptions()
|
2019-08-19 21:37:32 +00:00
|
|
|
chrome_options.add_argument("--no-sandbox")
|
2018-09-26 15:18:57 +00:00
|
|
|
# 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.
|
2019-08-19 21:37:32 +00:00
|
|
|
chrome_options.add_argument("--headless")
|
|
|
|
chrome_options.add_argument("--disable-gpu")
|
2018-12-02 02:47:54 +00:00
|
|
|
cls.sl = webdriver.Chrome(options=chrome_options)
|
2018-06-20 04:10:02 +00:00
|
|
|
cls.sl.implicitly_wait(TIMEOUT)
|
|
|
|
super(SeleniumTest, cls).setUpClass()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
cls.sl.quit()
|
|
|
|
super(SeleniumTest, cls).tearDownClass()
|
|
|
|
|
|
|
|
def setUp(self):
|
2019-08-19 21:37:32 +00:00
|
|
|
self.user = User.objects.create_superuser(
|
|
|
|
username="test", password="test", email="test@example.com"
|
|
|
|
)
|
2018-06-20 04:10:02 +00:00
|
|
|
|
|
|
|
def _open(self, url):
|
2019-08-19 21:37:32 +00:00
|
|
|
self.sl.get("%s%s" % (self.live_server_url, url))
|
2018-06-20 04:10:02 +00:00
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
def wait_for_page_load(self, timeout=30):
|
2019-08-19 21:37:32 +00:00
|
|
|
old_page = self.sl.find_element_by_tag_name("html")
|
2018-06-20 04:10:02 +00:00
|
|
|
yield
|
2019-08-19 21:37:32 +00:00
|
|
|
WebDriverWait(self.sl, timeout).until(staleness_of(old_page))
|
2018-06-20 04:10:02 +00:00
|
|
|
|
2019-08-19 21:37:32 +00:00
|
|
|
def _select_option(self, select, option, optgroup=""):
|
2018-06-20 04:10:02 +00:00
|
|
|
if optgroup:
|
2019-08-19 21:37:32 +00:00
|
|
|
optgroup = "/optgroup"
|
2018-06-20 04:10:02 +00:00
|
|
|
self.sl.find_element_by_xpath(
|
|
|
|
"//select[@id='%s']%s/option[text()='%s']" % (select, optgroup, option)
|
|
|
|
).click()
|
|
|
|
|
|
|
|
|
|
|
|
class MapTest(SeleniumTest):
|
|
|
|
def setUp(self):
|
|
|
|
self.case_study = CaseStudy(
|
|
|
|
approved=True,
|
2019-08-19 21:37:32 +00:00
|
|
|
entry_name="test",
|
2018-06-20 04:10:02 +00:00
|
|
|
location='{"type": "Point", "coordinates": [0, 0]}',
|
2019-08-19 21:37:32 +00:00
|
|
|
sector_of_economy="RN",
|
|
|
|
positive_or_negative="P",
|
|
|
|
country="NZ",
|
|
|
|
area_of_land="100",
|
|
|
|
land_ownership="PRI",
|
|
|
|
location_context="URB",
|
|
|
|
describe_ecosystem="test",
|
|
|
|
project_status="EXSTNG",
|
|
|
|
synopsis="test",
|
|
|
|
full_description="test",
|
|
|
|
media_coverage_mainstream="test",
|
|
|
|
media_coverage_independent="test",
|
2018-06-20 04:10:02 +00:00
|
|
|
)
|
|
|
|
self.case_study.save()
|
|
|
|
|
2018-10-04 12:46:39 +00:00
|
|
|
def login(self):
|
2019-08-19 21:37:32 +00:00
|
|
|
self.user = User.objects.create_superuser(
|
|
|
|
username="test", password="test", email="test@example.com"
|
|
|
|
)
|
|
|
|
self.sl.find_element_by_id("id_username").send_keys("test")
|
|
|
|
self.sl.find_element_by_id("id_password").send_keys("test")
|
2018-10-04 08:43:11 +00:00
|
|
|
self.sl.find_element_by_css_selector('input[type="submit"]').click()
|
2019-08-19 21:37:32 +00:00
|
|
|
|
2018-10-04 12:46:39 +00:00
|
|
|
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.
|
2019-08-19 22:30:13 +00:00
|
|
|
zoom_in = self.sl.find_element_by_class_name("leaflet-control-zoom-in")
|
|
|
|
for i in range(0, 30):
|
|
|
|
zoom_in.click()
|
2019-08-19 21:37:32 +00:00
|
|
|
time.sleep(0.2)
|
2019-08-19 22:30:13 +00:00
|
|
|
|
2019-08-19 21:37:32 +00:00
|
|
|
self.sl.find_element_by_class_name("leaflet-draw-draw-marker").click()
|
|
|
|
self.sl.find_element_by_id("id_location-map").click()
|
2018-10-04 12:46:39 +00:00
|
|
|
|
|
|
|
def test_form_will_show_error(self):
|
2019-08-19 21:37:32 +00:00
|
|
|
self._open("/en-gb/case-study/create/long")
|
|
|
|
self.login()
|
2018-10-04 08:43:11 +00:00
|
|
|
# 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
|
2019-08-19 21:37:32 +00:00
|
|
|
self.sl.find_element_by_id("submit-id-submit").click()
|
|
|
|
error_message = self.sl.find_element_by_class_name("has-error")
|
2019-08-19 22:40:23 +00:00
|
|
|
assert error_message.is_displayed() is True
|
2018-10-04 08:43:11 +00:00
|
|
|
|
2018-10-04 10:30:21 +00:00
|
|
|
def test_short_form_will_submit(self):
|
2019-08-19 21:37:32 +00:00
|
|
|
self._open("/en-gb/case-study/create/short")
|
2018-10-04 12:46:39 +00:00
|
|
|
self.login()
|
|
|
|
self.place_marker()
|
2019-08-19 21:37:32 +00:00
|
|
|
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"
|
|
|
|
)
|
|
|
|
Select(
|
|
|
|
self.sl.find_element_by_id("id_location_context")
|
|
|
|
).select_by_visible_text("Urban")
|
|
|
|
self.sl.find_element_by_id("id_describe_ecosystem").send_keys("test")
|
|
|
|
Select(self.sl.find_element_by_id("id_project_status")).select_by_visible_text(
|
|
|
|
"In planning and design"
|
|
|
|
)
|
|
|
|
self.sl.find_element_by_css_selector(
|
|
|
|
"[name=sector_of_economy][value=ST]"
|
|
|
|
).click()
|
|
|
|
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(
|
|
|
|
"There is/was an organising process in favour of the project"
|
|
|
|
)
|
2018-10-04 12:46:39 +00:00
|
|
|
# you can submit the form on any field. Using this one because the submit button sometimes isn't visible
|
2019-08-19 21:37:32 +00:00
|
|
|
self.sl.find_element_by_id("id_area_of_land").submit()
|
2019-08-19 22:40:23 +00:00
|
|
|
assert "Thanks!" in self.sl.page_source, "Success message not shown"
|
2019-08-19 21:37:32 +00:00
|
|
|
self._open("/admin/map/casestudy/")
|
2019-08-19 22:40:23 +00:00
|
|
|
assert "Short Entry" in self.sl.page_source, "Case study not saved"
|
2018-10-04 10:30:21 +00:00
|
|
|
|
|
|
|
def test_long_form_will_submit(self):
|
2019-08-19 21:37:32 +00:00
|
|
|
self._open("/en-gb/case-study/create/long")
|
2018-10-04 12:46:39 +00:00
|
|
|
self.login()
|
|
|
|
self.place_marker()
|
2019-08-19 21:37:32 +00:00
|
|
|
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"
|
|
|
|
)
|
|
|
|
Select(
|
|
|
|
self.sl.find_element_by_id("id_location_context")
|
|
|
|
).select_by_visible_text("Urban")
|
|
|
|
self.sl.find_element_by_id("id_describe_ecosystem").send_keys("test")
|
|
|
|
Select(self.sl.find_element_by_id("id_project_status")).select_by_visible_text(
|
|
|
|
"In planning and design"
|
|
|
|
)
|
|
|
|
self.sl.find_element_by_id("id_synopsis").send_keys("test")
|
|
|
|
self.sl.find_element_by_id("id_full_description").send_keys("test")
|
|
|
|
self.sl.find_element_by_id("id_project_owners").send_keys("test")
|
|
|
|
self.sl.find_element_by_id("id_shareholders").send_keys("test")
|
|
|
|
self.sl.find_element_by_css_selector(
|
|
|
|
'a[href="#technical-and-economic-analysis"]'
|
|
|
|
).click()
|
|
|
|
self.sl.find_element_by_css_selector(
|
|
|
|
"[name=sector_of_economy][value=ST]"
|
|
|
|
).click()
|
|
|
|
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(
|
|
|
|
"There is/was an organising process in favour of the project"
|
|
|
|
)
|
2018-10-04 10:30:21 +00:00
|
|
|
self.sl.find_element_by_css_selector('a[href="#uploads"]').click()
|
2018-10-04 12:46:39 +00:00
|
|
|
# you can submit the form on any field. Using this one because the submit button sometimes isn't visible
|
2019-08-19 21:37:32 +00:00
|
|
|
self.sl.find_element_by_id("id_name_of_territory_or_area").submit()
|
2019-08-19 22:40:23 +00:00
|
|
|
assert "Thanks!" in self.sl.page_source, "Success message not shown"
|
2019-08-19 21:37:32 +00:00
|
|
|
self._open("/admin/map/casestudy/")
|
2019-08-19 22:40:23 +00:00
|
|
|
assert "Long Entry" in self.sl.page_source, "Case study not saved"
|
2018-10-04 10:30:21 +00:00
|
|
|
|
2018-06-20 04:10:02 +00:00
|
|
|
def test_map(self):
|
2019-08-19 21:37:32 +00:00
|
|
|
self._open("/")
|
2018-06-20 04:10:02 +00:00
|
|
|
WebDriverWait(self.sl, 5).until(
|
2019-08-19 21:37:32 +00:00
|
|
|
visibility_of_element_located((By.CSS_SELECTOR, ".hello--hide"))
|
2018-06-20 04:10:02 +00:00
|
|
|
)
|
2019-08-19 21:37:32 +00:00
|
|
|
self.sl.find_element_by_css_selector(".hello--hide").click()
|
2018-06-20 04:10:02 +00:00
|
|
|
|
|
|
|
WebDriverWait(self.sl, 5).until(
|
2019-08-19 21:37:32 +00:00
|
|
|
visibility_of_element_located((By.CSS_SELECTOR, ".leaflet-marker-icon"))
|
2018-06-20 04:10:02 +00:00
|
|
|
)
|
2019-08-19 21:37:32 +00:00
|
|
|
self.sl.find_element_by_css_selector(".leaflet-marker-icon").click()
|
2018-06-20 04:10:02 +00:00
|
|
|
|
2019-08-19 22:30:13 +00:00
|
|
|
WebDriverWait(self.sl, 5).until(
|
|
|
|
visibility_of_element_located(
|
|
|
|
(By.CSS_SELECTOR, ".leaflet-popup-content a.btn")
|
|
|
|
)
|
|
|
|
)
|
2019-08-19 21:37:32 +00:00
|
|
|
details_link = self.sl.find_element_by_css_selector(
|
|
|
|
".leaflet-popup-content a.btn"
|
|
|
|
)
|
2019-08-19 22:40:23 +00:00
|
|
|
assert details_link.is_displayed() is True
|
2018-06-20 04:10:02 +00:00
|
|
|
|
|
|
|
details_link.click()
|
|
|
|
|
2019-08-19 22:40:23 +00:00
|
|
|
assert self.sl.current_url.endswith("case-study/test") is True
|