Permalink
Browse files

Migrate to pytest-selenium

  • Loading branch information...
1 parent 036c610 commit aadb70ac9ff287b4aba8cb37d6b8af3039bc7fc5 @davehunt davehunt committed Nov 4, 2015
View
@@ -1,3 +0,0 @@
-[DEFAULT]
-api = webdriver
-baseurl = https://mozillians-dev.allizom.org
View
@@ -25,7 +25,7 @@ class Base(Page):
@property
def page_title(self):
- WebDriverWait(self.selenium, 10).until(lambda s: self.selenium.title)
+ WebDriverWait(self.selenium, self.timeout).until(lambda s: self.selenium.title)
return self.selenium.title
@property
@@ -54,32 +54,32 @@ def login(self, email, password):
from browserid import BrowserID
pop_up = BrowserID(self.selenium, self.timeout)
pop_up.sign_in(email, password)
- WebDriverWait(self.selenium, 20).until(lambda s: self.is_user_loggedin)
+ WebDriverWait(self.selenium, self.timeout).until(lambda s: self.is_user_loggedin)
def create_new_user(self, email, password):
self.click_browserid_login()
from browserid import BrowserID
pop_up = BrowserID(self.selenium, self.timeout)
pop_up.sign_in(email, password)
- WebDriverWait(self.selenium, 20).until(lambda s: self.is_user_loggedin)
+ WebDriverWait(self.selenium, self.timeout).until(lambda s: self.is_user_loggedin)
from pages.register import Register
- return Register(self.testsetup)
+ return Register(self.base_url, self.selenium)
# Logged in
@property
def header(self):
- return self.Header(self.testsetup)
+ return self.Header(self.base_url, self.selenium)
@property
def footer(self):
- return self.Footer(self.testsetup)
+ return self.Footer(self.base_url, self.selenium)
def open_user_profile(self, username):
self.get_relative_path(u'/u/%s' % username)
from pages.profile import Profile
- return Profile(self.testsetup)
+ return Profile(self.base_url, self.selenium)
def logout_using_url(self):
self.get_relative_path(u'/logout')
@@ -105,7 +105,7 @@ def search_for(self, search_term):
search_field.send_keys(search_term)
search_field.send_keys(Keys.RETURN)
from pages.search import Search
- return Search(self.testsetup)
+ return Search(self.base_url, self.selenium)
def click_options(self):
self.selenium.find_element(*self._profile_menu_locator).click()
@@ -120,19 +120,19 @@ def click_view_profile_menu_item(self):
self.click_options()
self.selenium.find_element(*self._view_profile_menu_item_locator).click()
from pages.profile import Profile
- return Profile(self.testsetup)
+ return Profile(self.base_url, self.selenium)
def click_invite_menu_item(self):
self.click_options()
self.selenium.find_element(*self._invite_menu_item_locator).click()
from pages.invite import Invite
- return Invite(self.testsetup)
+ return Invite(self.base_url, self.selenium)
def click_edit_profile_menu_item(self):
self.click_options()
self.selenium.find_element(*self._edit_profile_menu_item_locator).click()
from pages.edit_profile import EditProfile
- return EditProfile(self.testsetup)
+ return EditProfile(self.base_url, self.selenium)
def click_logout_menu_item(self):
self.click_options()
@@ -148,7 +148,7 @@ class Footer(Page):
def click_about_link(self):
self.selenium.find_element(*self._about_mozillians_link_locator).click()
from pages.about import About
- return About(self.testsetup)
+ return About(self.base_url, self.selenium)
def select_language(self, lang_code):
element = self.selenium.find_element(*self._language_selector_locator)
@@ -42,14 +42,14 @@ class EditProfile(Base):
def click_update_button(self):
self.selenium.find_element(*self._update_button_locator).click()
- return Profile(self.testsetup)
+ return Profile(self.base_url, self.selenium)
def click_cancel_button(self):
self.selenium.find_element(*self._cancel_button_locator).click()
def click_find_group_link(self):
self.selenium.find_element(*self._find_group_page).click()
- return GroupsPage(self.testsetup)
+ return GroupsPage(self.base_url, self.selenium)
def set_full_name(self, full_name):
element = self.selenium.find_element(*self._full_name_field_locator)
@@ -87,7 +87,7 @@ def click_delete_profile_button(self):
self.selenium.find_element(*self._acknowledge_deletion_checkbox_locator).click()
self.selenium.find_element(*self._delete_profile_button_locator).click()
from pages.confirm_profile_delete import ConfirmProfileDelete
- return ConfirmProfileDelete(self.testsetup)
+ return ConfirmProfileDelete(self.base_url, self.selenium)
def select_month(self, option_month):
element = self.selenium.find_element(*self._select_month_locator)
@@ -17,4 +17,4 @@ class GroupInfoPage(Base):
def delete_group(self):
self.selenium.find_element(*self._delete_group_button).click()
from pages.groups_page import GroupsPage
- return GroupsPage(self.testsetup)
+ return GroupsPage(self.base_url, self.selenium)
@@ -17,7 +17,7 @@ class GroupsPage(Base):
def click_create_group_main_button(self):
self.selenium.find_element(*self._create_group_main_button).click()
- return CreateGroupPage(self.testsetup)
+ return CreateGroupPage(self.base_url, self.selenium)
def wait_for_alert_message(self):
self.wait_for_element_visible(*self._alert_message_locator)
View
@@ -10,21 +10,20 @@
class Home(Base):
- def __init__(self, testsetup, open_url=True):
- Base.__init__(self, testsetup)
- self.maximize_window()
+ def __init__(self, base_url, selenium, open_url=True):
+ Base.__init__(self, base_url, selenium)
if open_url:
self.selenium.get(self.base_url)
def go_to_localized_edit_profile_page(self, non_US):
self.get_relative_path("/" + non_US + "/user/edit/")
from pages.edit_profile import EditProfile
- return EditProfile(self.testsetup)
+ return EditProfile(self.base_url, self.selenium)
def wait_for_user_login(self):
# waits to see if user gets logged back in
# if not then all ok
try:
- WebDriverWait(self.selenium, 20).until(lambda s: self.is_user_loggedin)
+ WebDriverWait(self.selenium, self.timeout).until(lambda s: self.is_user_loggedin)
except Exception:
pass
View
@@ -28,4 +28,4 @@ def invite(self, email, reason=''):
reason_field.send_keys(reason)
self.selenium.find_element(*self._send_invite_button_locator).click()
from pages.invite_success import InviteSuccess
- return InviteSuccess(self.testsetup)
+ return InviteSuccess(self.base_url, self.selenium)
@@ -8,10 +8,11 @@
from BeautifulSoup import BeautifulSoup
from unittestzero import Assert
-from pages.page import Page
+class LinkCrawler(object):
-class LinkCrawler(Page):
+ def __init__(self, base_url):
+ self.base_url = base_url
def collect_links(self, url, relative=True, name=True, **kwargs):
"""Collects links for given page URL.
@@ -19,16 +19,16 @@ class LocationSearchResults(Base):
@property
def title(self):
- return self.find_element(*self._results_title_locator).text
+ return self.selenium.find_element(*self._results_title_locator).text
@property
def results_count(self):
- return len(self.find_elements(*self._result_item_locator))
+ return len(self.selenium.find_elements(*self._result_item_locator))
@property
def search_results(self):
- return [self.SearchResult(self.testsetup, web_element)
- for web_element in self.find_elements(*self._result_item_locator)]
+ return [self.SearchResult(self.base_url, self.selenium, el) for el in
+ self.selenium.find_elements(*self._result_item_locator)]
def get_random_profile(self):
random_index = randrange(self.results_count)
@@ -39,6 +39,6 @@ class SearchResult(PageRegion):
_profile_page_link_locator = (By.CSS_SELECTOR, 'a')
def open_profile_page(self):
- self.find_element(*self._profile_page_link_locator).click()
+ self._root_element.find_element(*self._profile_page_link_locator).click()
from pages.profile import Profile
- return Profile(self.testsetup)
+ return Profile(self.base_url, self.selenium)
View
@@ -1,5 +1,3 @@
-#!/usr/bin/env python
-
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
@@ -9,35 +7,17 @@
from unittestzero import Assert
from selenium.webdriver.support.ui import WebDriverWait
-from selenium.common.exceptions import WebDriverException
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import ElementNotVisibleException
class Page(object):
- """
- Base class for all Pages.
- """
-
- def __init__(self, testsetup, **kwargs):
- """
- Constructor
- """
- self.testsetup = testsetup
- self.base_url = testsetup.base_url
- self.selenium = testsetup.selenium
- self.timeout = testsetup.timeout
- self._selenium_root = hasattr(self, '_root_element') and self._root_element or self.selenium
-
- for key, value in kwargs.items():
- setattr(self, key, value)
-
- def maximize_window(self):
- try:
- self.selenium.maximize_window()
- except WebDriverException:
- pass
+
+ def __init__(self, base_url, selenium):
+ self.base_url = base_url
+ self.selenium = selenium
+ self.timeout = 60
@property
def is_the_current_page(self):
@@ -56,13 +36,13 @@ def get_url_current_page(self):
def is_element_present(self, *locator):
self.selenium.implicitly_wait(0)
try:
- self._selenium_root.find_element(*locator)
+ self.selenium.find_element(*locator)
return True
except NoSuchElementException:
return False
finally:
# set back to where you once belonged
- self.selenium.implicitly_wait(self.testsetup.default_implicit_wait)
+ self.selenium.implicitly_wait(10)
def wait_for_element_visible(self, *locator):
count = 0
@@ -84,28 +64,30 @@ def wait_for_element_present(self, *locator):
"""Wait for an element to become present."""
self.selenium.implicitly_wait(0)
try:
- WebDriverWait(self.selenium, 10).until(lambda s: self._selenium_root.find_element(*locator))
+ WebDriverWait(self.selenium, self.timeout).until(
+ lambda s: s.find_element(*locator))
except TimeoutException:
Assert.fail(TimeoutException)
finally:
# set back to where you once belonged
- self.selenium.implicitly_wait(self.testsetup.default_implicit_wait)
+ self.selenium.implicitly_wait(10)
def wait_for_element_not_present(self, *locator):
"""Wait for an element to become not present."""
self.selenium.implicitly_wait(0)
try:
- WebDriverWait(self.selenium, 10).until(lambda s: len(self._selenium_root.find_elements(*locator)) < 1)
+ WebDriverWait(self.selenium, self.timeout).until(
+ lambda s: len(s.find_elements(*locator)) < 1)
return True
except TimeoutException:
return False
finally:
# set back to where you once belonged
- self.selenium.implicitly_wait(self.testsetup.default_implicit_wait)
+ self.selenium.implicitly_wait(10)
def is_element_visible(self, *locator):
try:
- return self._selenium_root.find_element(*locator).is_displayed()
+ return self.selenium.find_element(*locator).is_displayed()
except (NoSuchElementException, ElementNotVisibleException):
return False
@@ -115,15 +97,9 @@ def return_to_previous_page(self):
def get_relative_path(self, url):
self.selenium.get(u'%s%s' % (self.base_url, url))
- def find_element(self, *locator):
- return self._selenium_root.find_element(*locator)
-
- def find_elements(self, *locator):
- return self._selenium_root.find_elements(*locator)
-
class PageRegion(Page):
- def __init__(self, testsetup, element):
- self._root_element = element
- Page.__init__(self, testsetup)
+ def __init__(self, base_url, selenium, root_element):
+ Page.__init__(self, base_url, selenium)
+ self._root_element = root_element
View
@@ -30,8 +30,8 @@ class Profile(Base):
_profile_message_locator = (By.CSS_SELECTOR, '.alert')
_view_as_locator = (By.ID, 'view-privacy-mode')
- def __init__(self, testsetup):
- Base.__init__(self, testsetup)
+ def __init__(self, base_url, selenium):
+ Base.__init__(self, base_url, selenium)
WebDriverWait(self.selenium, self.timeout).until(
lambda s: self.is_element_visible(*self._profile_photo_locator))
@@ -74,30 +74,30 @@ def location(self):
@property
def city(self):
- return self.find_element(*self._city_locator).text
+ return self.selenium.find_element(*self._city_locator).text
@property
def region(self):
- return self.find_element(*self._region_locator).text
+ return self.selenium.find_element(*self._region_locator).text
@property
def country(self):
- return self.find_element(*self._country_locator).text
+ return self.selenium.find_element(*self._country_locator).text
def click_profile_city_filter(self):
- self.find_element(*self._city_locator).click()
+ self.selenium.find_element(*self._city_locator).click()
from location_search_results import LocationSearchResults
- return LocationSearchResults(self.testsetup)
+ return LocationSearchResults(self.base_url, self.selenium)
def click_profile_region_filter(self,):
- self.find_element(*self._region_locator).click()
+ self.selenium.find_element(*self._region_locator).click()
from location_search_results import LocationSearchResults
- return LocationSearchResults(self.testsetup)
+ return LocationSearchResults(self.base_url, self.selenium)
def click_profile_country_filter(self):
- self.find_element(*self._country_locator).click()
+ self.selenium.find_element(*self._country_locator).click()
from location_search_results import LocationSearchResults
- return LocationSearchResults(self.testsetup)
+ return LocationSearchResults(self.base_url, self.selenium)
@property
def languages(self):
View
@@ -37,7 +37,7 @@ def set_location(self, location):
element.send_keys(location)
element.send_keys(Keys.RETURN)
WebDriverWait(self.selenium, self.timeout).until(
- lambda s: self._selenium_root.find_element(*self._country_locator).text != "")
+ lambda s: s.find_element(*self._country_locator).text != "")
def set_full_name(self, full_name):
element = self.selenium.find_element(*self._full_name_field_locator)
@@ -88,4 +88,4 @@ def check_privacy(self):
def click_create_profile_button(self):
self.selenium.find_element(*self._create_profile_button_locator).click()
from pages.profile import Profile
- return Profile(self.testsetup)
+ return Profile(self.base_url, self.selenium)
Oops, something went wrong.

0 comments on commit aadb70a

Please sign in to comment.