Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Automatisierte Browser-Tests mit gocept.selenium

wosc
October 05, 2011

Automatisierte Browser-Tests mit gocept.selenium

wosc

October 05, 2011
Tweet

More Decks by wosc

Other Decks in Programming

Transcript

  1. Automatisierte Browser-Tests mit
    (gocept.)Selenium
    Wolfgang Schnerring
    gmbh & co. kg
    PyCon DE 2011, Leipzig
    Wolfgang Schnerring Selenium 07.10.2011 1 / 19

    View Slide

  2. View Slide

  3. Einführung
    Web 2.0 und JavaScript
    Wolfgang Schnerring Selenium 07.10.2011 3 / 19

    View Slide

  4. Einführung
    Browser-Fernsteuerung
    Wolfgang Schnerring Selenium 07.10.2011 4 / 19

    View Slide

  5. Selenium
    Architektur
    nach: http://seleniumhq.org/projects/remote-control/
    Wolfgang Schnerring Selenium 07.10.2011 5 / 19

    View Slide

  6. Selenium
    Funktionsumfang
    Wolfgang Schnerring Selenium 07.10.2011 6 / 19

    View Slide

  7. Selenium
    API
    open http://www.google.de/
    type name=q zope
    waitForVisible id=suggestions
    assertText //div[@id=’suggestions’])/ul/li[2] *fisch
    Wolfgang Schnerring Selenium 07.10.2011 7 / 19

    View Slide

  8. Selenium
    API
    open http://www.google.de/
    type name=q zope
    waitForVisible id=suggestions
    assertText //div[@id=’suggestions’])/ul/li[2] *fisch
    s = selenium.selenium(server, port, ’∗firefox’)
    s.start()
    s.open(’http://www.google.de/’)
    s.type(’name=q’, ’zope’)
    # won’t work because of asynchronity!
    assertTrue(s.is_visible(’id=suggestions’)))
    assertEqual(s.get_text(’//div[@id="suggestions"])/ul/li[2]’, ’fisch’)
    Wolfgang Schnerring Selenium 07.10.2011 7 / 19

    View Slide

  9. View Slide

  10. gocept.selenium
    Beispielcode
    class AutocompleteTest(gocept.selenium.wsgi.TestCase):
    def test_search_field_should_show_suggestions(self):
    s = self.selenium
    s.open(’http://www.google.de/’)
    s.type(’name=q’, ’zope’)
    s.waitForVisible(’id=suggestions’))
    s.assertText(’//div[@id="suggestions"])/ul/li[2]’, ’∗fisch’)
    Wolfgang Schnerring Selenium 07.10.2011 9 / 19

    View Slide

  11. gocept.selenium
    Befehlstypen
    Aktionen
    click(locator), type(), dragAndDrop()
    Abfragen
    getText(locator), getSelectedValues(), getHeight()
    Assertions
    assertText(locator, pattern), assertElementPresent()
    Asynchron
    waitForElementPresent(), waitForXpathCount()
    Wolfgang Schnerring Selenium 07.10.2011 10 / 19

    View Slide

  12. gocept.selenium
    Locators
    Element-ID
    getText(’id=navigation’)
    CSS
    getText(’css=div.navigation li a’)
    XPath
    getText(’//div[@id="suggestions"])/ul/li[2]’)
    Javascript
    getText(’document.forms[’myForm’].myDropdown’)
    Wolfgang Schnerring Selenium 07.10.2011 11 / 19

    View Slide

  13. Abstraktion
    click(’id=menu1’)
    waitForElementPresent(’css=div.lightbox’)
    waitForVisible(’css=div.lightbox’)
    click(’css=a.close’)
    waitForElementNotPresent(’css=div.lightbox’)
    Wolfgang Schnerring Selenium 07.10.2011 12 / 19

    View Slide

  14. Abstraktion
    click(’id=menu1’)
    waitForElementPresent(’css=div.lightbox’)
    waitForVisible(’css=div.lightbox’)
    click(’css=a.close’)
    waitForElementNotPresent(’css=div.lightbox’)
    click(’id=menu1’)
    assertLightboxOpens()
    click(’css=a.close’)
    assertLightboxCloses()
    Wolfgang Schnerring Selenium 07.10.2011 12 / 19

    View Slide

  15. View Slide

  16. Test-Integration
    Setup
    Wolfgang Schnerring Selenium 07.10.2011 14 / 19

    View Slide

  17. Test-Integration
    Layer
    class Layer(object):
    __bases__ = (OTHER_LAYER,)
    def setUp(self):
    # [start browser]
    def tearDown(self):
    # [kill browser]
    def testSetUp(self):
    def testTearDown(self):
    Wolfgang Schnerring Selenium 07.10.2011 15 / 19

    View Slide

  18. Test-Integration
    einfacher mit zope.testrunner
    from my.package.main import my_wsgi_callable
    class AutocompleteTest(gocept.selenium.wsgi.TestCase):
    layer = gocept.selenium.wsgi.Layer(my_wsgi_callable)
    def test_search_field_should_show_suggestions(self):
    s = self.selenium
    s.open(’http://%s:%s/foo’
    % (self.layer.host, self.layer.port)
    s.assertTextPresent(’Hello, world!’)
    Wolfgang Schnerring Selenium 07.10.2011 16 / 19

    View Slide

  19. View Slide

  20. Ausblick & Ressourcen
    Selenium-2.x (WebDriver-API)
    JsTestDriver
    http://seleniumhq.org
    http://release.seleniumhq.org/selenium-core/1.0/reference.html
    http://pypi.python.org/pypi/gocept.selenium
    Wolfgang Schnerring Selenium 07.10.2011 18 / 19

    View Slide

  21. Wir suchen Mitarbeiter in Halle!
    Softwareentwicklung
    Web
    Python
    TDD
    Systemadministration
    Gentoo/KVM
    Puppet
    Nagios
    Bei Interesse: mich ansprechen oder [email protected]
    Wolfgang Schnerring Selenium 07.10.2011 19 / 19

    View Slide