Created
May 30, 2012 21:29
-
-
Save miohtama/2839086 to your computer and use it in GitHub Desktop.
Plone Selenium helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Some PSE 2012 Selenium notes | |
| * https://github.com/plone/plone.seleniumtesting | |
| * https://github.com/emanlove/pse2012 | |
| Selenium WebDriver API | |
| * http://code.google.com/p/selenium/source/browse/trunk/py/selenium/webdriver/remote/webdriver.py | |
| Selenium element match options | |
| * http://code.google.com/p/selenium/source/browse/trunk/py/selenium/webdriver/common/by.py | |
| Selenium element API (after find_xxx()) | |
| * http://code.google.com/p/selenium/source/browse/trunk/py/selenium/webdriver/remote/webelement.py | |
| """ | |
| from selenium.webdriver.common.by import By | |
| from plone.app.testing import selenium_layers | |
| class SeleniumHelper(object): | |
| """ | |
| Selenium convenience methods for Plone. | |
| Command Selenium browser to do common actions. | |
| This mainly curries and delegates to plone.app.testing.selenium_layers helper methods. | |
| More info: | |
| * https://github.com/plone/plone.app.testing/blob/master/plone/app/testing/selenium_layers.py | |
| """ | |
| def __init__(self, testcase): | |
| """ | |
| :param testcase: Yout test class instance | |
| """ | |
| self.testcase = testcase | |
| self.driver = testcase.layer['selenium'] | |
| self.portal = testcase.layer["portal"] | |
| def login(self, username, password): | |
| """ | |
| Perform login using Selenium test browser. | |
| """ | |
| selenium_layers.login(self.driver, self.portal, username, password) | |
| def logout(self): | |
| """ | |
| Perform logout using Selenium test browser. | |
| """ | |
| selenium_layers.logout(self.driver) | |
| def get_plone_page_title(self): | |
| """ | |
| Get Plone main <h1> contents as lowercase. | |
| XXX: Looks like Selenium API returns uppercase if there is text-transform: uppercase? | |
| :return: Empty string if there is no title on the page (convenience for string matching) | |
| """ | |
| title_elem = self.driver.find_element_by_class_name("documentFirstHeading") | |
| if not title_elem: | |
| return "" | |
| return title_elem.text.lower() | |
| def trap_error_log(self, orignal_page=None): | |
| """ | |
| Read error from the site error log and dump it to output. | |
| Makes debugging Selenium tests much more fun when you directly see | |
| the actual errors instead of OHO. | |
| :param orignal_page: Decorate the traceback with URL we tried to access. | |
| """ | |
| # http://svn.zope.org/Zope/trunk/src/Products/SiteErrorLog/SiteErrorLog.py?rev=96315&view=auto | |
| error_log = self.portal.error_log | |
| entries = error_log.getLogEntries() | |
| msg = "" | |
| if orignal_page: | |
| msg += "Plone logged an error when accessing page %s\n" % orignal_page | |
| # We can only fail on traceback | |
| if len(entries) >= 2: | |
| msg += "Several exceptions were logged.\n" | |
| entry = entries[0] | |
| raise AssertionError(msg + entry["tb_text"]) | |
| def is_error_page(self): | |
| """ | |
| Check that if the current page is Plone error page. | |
| """ | |
| return "but there seems to be an error" in self.get_plone_page_title() | |
| def is_unauthorized_page(self): | |
| """ | |
| Check that the page is not unauthorized page. | |
| ..note :: | |
| We cannot distingush login from unauthorized | |
| """ | |
| # require_login <-- auth redirect final target | |
| return "/require_login/" in self.driver.current_url | |
| def is_not_found_page(self): | |
| """ | |
| Check if we got 404 | |
| """ | |
| return "this page does not seem to exist" in self.get_plone_page_title() | |
| def open(self, url, check_error_log=True, check_sorry_error=True, check_unauthorized=True, check_not_found=True): | |
| """ | |
| Open an URL in Selenium browser. | |
| If url does not start with http:// assume it is a site root relative URL. | |
| :param check_error_log: If the page has created anything in Plone error log then dump this traceback out. | |
| :param check_sorry_error: Assert on Plone error response page | |
| :param check_unauthorized: Assert on Plone Unauthorized page (login dialog) | |
| """ | |
| # Convert to abs URL | |
| if not url.startswith("http://"): | |
| url = self.portal.absolute_url() + url | |
| selenium_layers.open(self.driver, url) | |
| if check_error_log: | |
| self.trap_error_log(url) | |
| if check_sorry_error: | |
| self.testcase.assertFalse(self.is_error_page(), "Got Plone error page for url: %s" % url) | |
| if check_unauthorized: | |
| self.testcase.assertFalse(self.is_unauthorized_page(), "Got Plone Unauthorized page for url: %s" % url) | |
| if check_not_found: | |
| self.testcase.assertFalse(self.is_not_found_page(), "Got Plone not found page for url: %s" % url) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment