Skip to content

Commit

Permalink
Moves around all driver creation functions into one place
Browse files Browse the repository at this point in the history
We now have the functional/functional_test.py to handle all
driver creation logic. For the pages-layout tests, we are creating
only a Firefox driver to connect to the local container itself.

This logic does not work against any external server as of now.
  • Loading branch information
kushaldas authored and msheiny committed Aug 15, 2018
1 parent 93c06cf commit 626945f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 29 deletions.
52 changes: 48 additions & 4 deletions securedrop/tests/functional/functional_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
NoAlertPresentException)
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from tbselenium.tbdriver import TorBrowserDriver
from tbselenium.utils import start_xvfb, stop_xvfb

Expand Down Expand Up @@ -126,8 +128,7 @@ def _create_webdriver(self, profile=None):
time.sleep(connrefused_retry_interval)
raise

def _create_secondary_firefox_driver(self):
profile = None
def _create_secondary_firefox_driver(self, profile=None):
self.f_profile_path = "/tmp/testprofile"
if os.path.exists(self.f_profile_path):
shutil.rmtree(self.f_profile_path)
Expand All @@ -147,6 +148,42 @@ def _create_secondary_firefox_driver(self):
firefox_profile=profile)
self.second_driver.implicitly_wait(15)

def _create_webdriver2(self, profile=None):
# Only for layout tests
# see https://review.openstack.org/#/c/375258/ and the
# associated issues for background on why this is necessary
connrefused_retry_count = 3
connrefused_retry_interval = 5
binpath = '/usr/lib/firefox-esr/firefox-esr'
for i in range(connrefused_retry_count + 1):
try:
driver = webdriver.Firefox(firefox_binary=binpath,
firefox_profile=profile)
if i > 0:
# i==0 is normal behavior without connection refused.
print('NOTE: Retried {} time(s) due to '
'connection refused.'.format(i))
return driver
except socket.error as socket_error:
if (socket_error.errno == errno.ECONNREFUSED
and i < connrefused_retry_count):
time.sleep(connrefused_retry_interval)
continue
raise

def _javascript_toggle(self):
# the following is a noop for some reason, workaround it
# profile.set_preference("javascript.enabled", False)
# https://stackoverflow.com/a/36782979/837471
self.driver.get("about:config")
actions = ActionChains(self.driver)
actions.send_keys(Keys.RETURN)
actions.send_keys("javascript.enabled")
actions.perform()
actions.send_keys(Keys.TAB)
actions.send_keys(Keys.RETURN)
actions.perform()

def swap_drivers(self):
if not self.second_driver:
self._create_secondary_firefox_driver()
Expand Down Expand Up @@ -239,6 +276,7 @@ def setup(self, session_expiration=30):
self.source_app = source_app.create_app(config)
self.journalist_app = journalist_app.create_app(config)

# This user is required for our tests cases to login
self.admin_user = {
"name": "journalist",
"password": "WEjwn8ZyczDhQSK24YKM8C9a",
Expand Down Expand Up @@ -286,8 +324,14 @@ def start_journalist_server(app):
self.session_expiration = session_expiration

self.xvfb_display = start_xvfb()
self._create_secondary_firefox_driver()
self.driver = self._create_webdriver()
if not hasattr(self, 'override_driver'):
# Means this is not pages-layout tests
self._create_secondary_firefox_driver()
self.driver = self._create_webdriver()
else:
# We will use a normal firefox esr for the pages-layout tests
self.driver = self._create_webdriver2(self.new_profile) # pylint: disable=no-member # noqa
self._javascript_toggle()

# Polls the DOM to wait for elements. To read more about why
# this is necessary:
Expand Down
27 changes: 2 additions & 25 deletions securedrop/tests/pages-layout/functional_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
from os.path import abspath, dirname, realpath
import pytest

from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

from selenium import webdriver

from tests.functional import functional_test
Expand All @@ -46,34 +43,14 @@ def webdriver_fixture(self, request):
'screenshots', self.accept_languages))
if not os.path.exists(self.log_dir):
os.makedirs(self.log_dir)
firefox = self._prepare_webdriver()
profile = webdriver.FirefoxProfile()
profile.set_preference("intl.accept_languages", self.accept_languages)
self.new_profile = webdriver.FirefoxProfile()
self.new_profile.set_preference("intl.accept_languages", self.accept_languages)
self.override_driver = True
self.driver = self._create_webdriver(firefox, profile)
self._javascript_toggle()

yield None

self.driver.quit()

def _screenshot(self, filename):
self.driver.set_window_size(1024, 500) # Trim size of images for docs
self.driver.save_screenshot(os.path.join(self.log_dir, filename))

def _javascript_toggle(self):
# the following is a noop for some reason, workaround it
# profile.set_preference("javascript.enabled", False)
# https://stackoverflow.com/a/36782979/837471
self.driver.get("about:config")
actions = ActionChains(self.driver)
actions.send_keys(Keys.RETURN)
actions.send_keys("javascript.enabled")
actions.perform()
actions.send_keys(Keys.TAB)
actions.send_keys(Keys.RETURN)
actions.perform()

def _save_alert(self, filename):
fd = io.open(os.path.join(self.log_dir, filename), 'wb')
fd.write(self.driver.switch_to.alert.text.encode('utf-8'))

0 comments on commit 626945f

Please sign in to comment.