GithubHelp home page GithubHelp logo

sajnikanth / holmium.core Goto Github PK

View Code? Open in Web Editor NEW

This project forked from alisaifee/holmium.core

0.0 1.0 0.0 214 KB

holmium core

Home Page: https://holmiumcore.readthedocs.org/en/latest/

License: MIT License

holmium.core's Introduction

holmium.core travis-ci coverall

Introduction

holmium.core provides utility classes to simplify writing pageobjects for webpages using selenium.

Nothing beats an example. Conventionally automated tests integrating with python-selenium are written similarly to the following code block (using seleniumhq.org).

import selenium.webdriver
import unittest

class SeleniumHQTest(unittest.TestCase):
    def setUp(self):
        self.driver = selenium.webdriver.Firefox()
        self.url = "http://seleniumhq.org"
    def test_header_links(self):
        self.driver.get(self.url)
        elements = self.driver.find_elements_by_css_selector("div#header ul>li")
        self.assertTrue(len(elements) > 0)
        expected_link_list = ["Projects", "Download", "Documentation", "Support", "About"]
        actual_link_list = [el.text for el in elements]
        self.assertEquals( sorted(expected_link_list), sorted(actual_link_list))

    def test_about_selenium_heading(self):
        self.driver.get(self.url)
        about_link = self.driver.find_element_by_css_selector("div#header ul>li#menu_about>a")
        about_link.click()
        heading = self.driver.find_element_by_css_selector("#mainContent>h2")
        self.assertEquals(heading.text, "About Selenium")

    def tearDown(self):
        if self.driver:
            self.driver.quit()

if __name__ == "__main__":
    unittest.main()

The above example does what most selenium tests do:

  • initialize a webdriver upon setUp
  • query for one or more web elements using either class name, id, css_selector or xpath
  • assert on the number of occurances / value of certain elements.
  • tear down the webdriver after each test case

It suffers from the typical web development problem of coupling the test case with the HTML plumbing of the page its testing rather than the functionality its meant to excercise. The concept of PageObjects reduces this coupling and allow for test authors to separate the layout of the page under test and the functional behavior being tested. This separation also results in more maintainable test code (i.e. if an element name changes - all tests dont have to be updated, just the pageobject).

Lets take the above test case for a spin with holmium. Take note of the following:

  • The initialization and reset of the webdriver is delegated to the TestCase base class (alternatively the class could subclass unittest.TestCase and be run with the holmium nose plugin.
  • the page elements are accessed in the test only via Element & ElementMap.
from holmium.core import TestCase, Page, Element, Locators, ElementMap
import unittest

class SeleniumHQPage(Page):
    nav_links = ElementMap( Locators.CSS_SELECTOR
                                        , "div#header ul>li"
                                        , key = lambda element : element.find_element_by_tag_name("a").text
                                        , value = lambda element: element.find_element_by_tag_name("a") )

    header_text = Element(Locators.CSS_SELECTOR, "#mainContent>h2")


class SeleniumHQTest(TestCase):
    def setUp(self):
        self.page = SeleniumHQPage(self.driver, "http://seleniumhq.org")

    def test_header_links(self):
        self.assertTrue( len(self.page.nav_links) > 0 )
        self.assertEquals( sorted(["Projects", "Download", "Documentation", "Support", "About"])
                        ,  sorted(self.page.nav_links.keys() ) )

    def test_about_selenium_heading(self):
        self.page.nav_links["About"].click()
        self.assertEquals(self.page.header_text.text, "About Selenium")

if __name__ == "__main__":
    unittest.main()

Which can then be executed in a few different ways as shown below.

# if using TestCase as the base class run as:
export HO_BROWSER=firefox;nosetests test_selenium_hq.py
# or..
export HO_BROWSER=firefox;python test_selenium_hq.py
# if using unittest.TestCase as the base class run as:
nosetests test_selenium_hq.py --holmium-browser=firefox

holmium.core's People

Contributors

alisaifee avatar sajnikanth avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.