Automated Web Application Testing With Selenium 21750

Embed Size (px)

Citation preview

  • 7/29/2019 Automated Web Application Testing With Selenium 21750

    1/21

    BEA Confidential. | 1

    Automated Web ApplicationTesting with Selenium

    September 12, 2006

  • 7/29/2019 Automated Web Application Testing With Selenium 21750

    2/21

    BEA Confidential. | 2

    Outline

    What is Selenium?

    Writing Maintainable Selenium Tests

    Writing Testable Web Applications

  • 7/29/2019 Automated Web Application Testing With Selenium 21750

    3/21

    BEA Confidential. | 3

    What is Selenium?

    Open source test tool for web applications

    http://selenium.openqa.org

    Runs in any mainstream browser

    IE, Mozilla/Firefox, Opera, Safari, Konqueror

    Write tests in many languages

    Java, .NET, Perl, Python, Ruby

    Selenese (pure HTML, no Java/backend required)

    Record/playback (Selenium IDE)

    http://selenium.openqa.org/http://selenium.openqa.org/http://selenium.openqa.org/
  • 7/29/2019 Automated Web Application Testing With Selenium 21750

    4/21

    BEA Confidential. | 4

    Demo

    Record a test in Selenium IDE

    Demo same test written in Java

  • 7/29/2019 Automated Web Application Testing With Selenium 21750

    5/21

    BEA Confidential. | 5

    Java Test Example

    public void testGoogleTestSearch() throws Throwable

    {

    selenium.open("http://www.google.com/webhp");

    assertEquals("Google", selenium.getTitle());

    selenium.type("q", "Selenium OpenQA");

    selenium.click("btnG");

    selenium.waitForPageToLoad("5000");

    assertEquals("Selenium OpenQA - Google Search",

    selenium.getTitle());

    }

  • 7/29/2019 Automated Web Application Testing With Selenium 21750

    6/21

    BEA Confidential. | 6

    Java setUp/tearDown Example

    public void setUp() throws Exception {

    selenium = new DefaultSelenium("localhost",

    4444, "*firefox",

    "http://www.google.com");selenium.start();

    }

    protected void tearDown() throws Exception {

    selenium.stop();

    }

  • 7/29/2019 Automated Web Application Testing With Selenium 21750

    7/21BEA Confidential. | 7

    A Few Selenium Commands

    typeisTextPresentgetCursorPosition

    refreshgoBackfireEvent

    opengetValuedragdrop

    mouseOvergetTitlecreateCookie

    keyPressgetHtmlSourceclose

    isVisiblegetEvalclick

  • 7/29/2019 Automated Web Application Testing With Selenium 21750

    8/21

    BEA Confidential. | 8

    Element Locators

    selenium.click(____)

    ID: id=foo

    Name: name=foo

    Identifier (ID, then name): identifier=foo

    DOM: document.forms[myform].myDropdownXPath: //table[@id='table1']//tr[4]/td[2]

    Link Text: link=text

    CSS Selector: css=a[href=#id3]

    Default:

    DOM if it starts with document.

    XPath if it starts with //

    Identifier otherwise

  • 7/29/2019 Automated Web Application Testing With Selenium 21750

    9/21

    BEA Confidential. | 9

    How It Works

  • 7/29/2019 Automated Web Application Testing With Selenium 21750

    10/21

    BEA Confidential. | 10

    Outline

    What is Selenium?

    Writing Maintainable Selenium Tests

    Writing Testable Web Applications

  • 7/29/2019 Automated Web Application Testing With Selenium 21750

    11/21

    BEA Confidential. | 11

    Standard End-User Black Box Test

    1. Login as administrator

    2. Create a user

    3. Log out

    4. Login as that user5. Create a folder

    6. Create a thingy in that folder

    7. Search for that thingy in the search box

    8. Make sure your thingy shows up on the search results page

  • 7/29/2019 Automated Web Application Testing With Selenium 21750

    12/21

    BEA Confidential. | 12

    Fragile automated tests

    Exercising irrelevant features

    Logging in/Logging out

    Creating a folder

    Creating a thingy

    If the UI for any one of those features changes, your searchtest fails

  • 7/29/2019 Automated Web Application Testing With Selenium 21750

    13/21

    BEA Confidential. | 13

    Know When to Record a Test

    Recorded tests reuse no code

    Record & Tweak vs. Fire and Forget

    Slight change in Folder Creator means allof those tests haveto be re-recorded from scratch

    This happened to us with Mercurys Quick Test Pro

    Use the recorder to create reusable code

  • 7/29/2019 Automated Web Application Testing With Selenium 21750

    14/21

    BEA Confidential. | 14

    Unit Testing vs. Integration Testing

    Selenium tests are integrationtests, not unittests

    Functional/Acceptance/User/Compatibility

    Unit tests verify a unit in isolation

    If FooTest.java fails, the bugmust be in Foo.java

    A unit test should never fail because of a browser incompatibility

    Unit tests need to be completely isolated from each other

    Integration tests verify that units work well together

    Black Box tests

    Requires testing multiple configurations (browsers)

    Tend to build on the side-effects of earlier tests

  • 7/29/2019 Automated Web Application Testing With Selenium 21750

    15/21

    BEA Confidential. | 15

    JUnit and TestNG

    JUnit is opinionated software

    JUnit: Everything gets teared down after everytest method

    Order isolation [see next slide]

    Constantly starting/stopping browser

    Dependencies between tests is explicitly prevented

    Separate tests are testing separate units

    TestNG: dependsOnX

    Same test multiple times, different configurations?Java Properties, multiple runs

    TestNG parameters

  • 7/29/2019 Automated Web Application Testing With Selenium 21750

    16/21

    BEA Confidential. | 16

    JUnit Test Execution

    public void setUp() { log(setup!); }

    public void testFoo() { log(foo!); }

    public void testBar() { log(bar!); }

    public void tearDown() { log(teardown!); }

    Runs like this

    setup! foo! teardown! setup! bar! teardown!

    NOT like this

    setup! foo! bar! teardown!

  • 7/29/2019 Automated Web Application Testing With Selenium 21750

    17/21

    BEA Confidential. | 17

    Outline

    What is Selenium?

    Writing Maintainable Selenium Tests

    Writing Testable Web Applications

  • 7/29/2019 Automated Web Application Testing With Selenium 21750

    18/21

    BEA Confidential. | 18

    Testable HTML Pages

    Human-legible (hand-coded) element IDs

    id=obj3 is going to be fragile

    XPaths are veryfragile

    JSF: TLD true on ID elements

    Hidden flags: input fields, object properties

    waitForCondition is your AJAX testing tool

  • 7/29/2019 Automated Web Application Testing With Selenium 21750

    19/21

    BEA Confidential. | 19

    Model View Controller

    Model is separately testable

    Thats where your business logic is!

    Write Integration tests of those!

    Use Selenium to test your viewand controller (navigation)

    UI Testing

    API Testing Model

    Unit Testing Classes

  • 7/29/2019 Automated Web Application Testing With Selenium 21750

    20/21

    BEA Confidential. | 20

    Another Code Reuse Strategy

    Prepare your Search tests using Model API tests

    FolderBean fb = new FolderBean();

    fb.setParent(FolderBean.ROOT);

    fb.setName(foo);

    fb.createNewFolder(); adds a folder to the DB

    selenium.open(/search.faces);

    selenium.type(query, foo);

    selenium.click(search);

    assertTrue(selenium.isTextPresent(foo found);

    Writing your tests/webapp in same language

  • 7/29/2019 Automated Web Application Testing With Selenium 21750

    21/21

    BEA Confidential. | 21

    Self-testing web applications

    Create a special JSP that runs Selenium tests

    Configure Session-scoped beans