Selenium - How to - FrontEndWiki

download Selenium - How to - FrontEndWiki

of 5

Transcript of Selenium - How to - FrontEndWiki

  • 8/13/2019 Selenium - How to - FrontEndWiki

    1/5

    Selenium - How To

    From FrontEndWiki

    This page will help Developers to author Selenium test cases and to run them both in-browser for a sanity check and

    via JUnit for more formal and robust testing.

    Contents

    1 What is Selenium?

    2 Where Does it Fit?

    3 Where Does it NOT Fit?

    4 How Do I Create a T est?

    5 What do I Verify/Assert?

    6 How Do I Run Outside of the Firefox Plugin?

    6.1 Starting the Selenium RC Server6.2 Creating a JUnit Test

    hat is Selenium?

    From the website (Selenium WebSite (ht tp ://selenium-core.openqa.org/) )

    "Selenium Core is a test tool for web applications. Selenium Core tests run directly in a browser, just as real users do.

    nd they run in Internet Explorer, Mozilla and Firefox on Windows, Linux, and Macintosh."

    So, in a nutshell, it is a means to test webapps. As a added bonus, it can be used to execute the same test in multiplebrowsers. Depending on the nat ure of your test, that could be extremely valuable.

    here Does it Fit?

    Basically, Selenium is a good fit if:

    You need to test a piece of a webapp in the browser itself (example: DHTML pages).

    You want to create a regression test for a full flow.

    You want to create a simple test quickly.

    here Does it NOT Fit?

    You should NOT use Selenium for:

    Unit testing things like Servlets. ServletUnit and similar frameworks are far more appropriate for such

    scenarios.

    Arguably, flows that don't have DHTML components (some feel WebTest is better for this...but if it is

    Selenium or nothing, then go with Selenium!).

    How Do I Create a Test?

    ium - How To - FrontEndWiki http://10.3.245.204/mediawiki/index.php/Selenium_-_

    7/22/2008

  • 8/13/2019 Selenium - How to - FrontEndWiki

    2/5

    The easiest way to create a test is using the Firefox plugin of Selenium IDE.

    Selenium IDE 1.0 Beta Firefox Plugin (http://release.openqa.org/selenium-ide/1.0-beta-2/selenium-ide-1.0-beta-2.xpi)

    Note: I've linked to a Beta version as, at this t ime, 1.0 has not been released and 1.0 has many feature which we need

    or will find useful. Specifically for the IDE, 1.0 has the concept of a test suite which is quite handy for obvious

    reasons.

    Once you install the plugin (and restarted Firefox), writinga Selenium test is simple:

    1. Open Selenium IDE in Firefox via Tools > Selenium IDE

    2. Set Base URL to your App Login Page (ie. OLB's PreSignOn.cibc)

    3. Click the Record icon (Big Red Dot).

    4. Perform t he flow you wish t o t est.

    NOTE: you can simple record a Login and then make sure that is the first test in a suite if you want Login not to be

    a part of your test but is still required of the test to execute.

    5. When finished, click the Record icon again to stop.

    6. Play back the test to make sure the recording worked as expected (view the Table to see if any errors are

    reported).

    7. Celebrate, you just make a test (albeit fairly useless at this point).

    8. Save your test (select File-->Save T est Case) and (if desired) File-->Save T est Suite).

    So, now that you have a test written, you need to add your verifications and assertions. Verifications simply report

    an error if what you are verifying is not t rue. An assertion halts the t est if it is not true.

    There are a couple of ways to do this. The first way is to simply playback your test (slowly) and pause it where you

    want to add your tests. Then you can either right click the element of interest choose one of t he presented options

    (i.e. "verifyValue" for an input field), or click in the Selenium IDE table and choose to Insert New Command. If you

    choose to manuallyInsert New Command, you'll pick from the dropdown which command you want to use (i.e.

    assertValue) and then choose the two arguments: targetand value. T ypically targetis the field that you are verifying

    and valueis the expected value. This isn't always the case though. If, for example, your target is a JavaScript snippet

    (written asjavascript{...script to execute...}) then the value is the name name of t he variable to store t he result as.

    For example, if I execute astorecommand with a targetof javascript{'hello'}and a valueof Greeting, then there is a

    variable called Greetingcreated with the output of t he executed JavaScript . T his variable will be available insubsequent calls in the test via ${Greeting}or storedVars['Greeting']. Anyway, the best way to get the feel of these is

    to play around with some test cases.

    Just about any verification/assertion you would want to make is available. Just think about what it is you want to

    assert and typically you'll find a command with a very similar name. It is very intuitive. I've not found a good

    resource on the Web with all of them outlined so you'll have to make due and experiment a bit.

    That being said, there is one particular test that is missing which may cause you troubles. Say you want to verify that

    theHello Susan text on the screen actually has the user's name (Susan). Now, you can simple verify that the text

    ium - How To - FrontEndWiki http://10.3.245.204/mediawiki/index.php/Selenium_-_

    7/22/2008

  • 8/13/2019 Selenium - How to - FrontEndWiki

    3/5

    Susanis present or, in many cases, you can verify the table cell's contents. However, in some cases, either these

    tests aren't possible for whatever reason OR they aren't sufficient (i.e. Susan is written elsewhere on the page so

    simply verifying that Susan is on the page does NOT verify that Susan appears after Hello). T o test these scenarios,

    you can leverage the innerHTML of whatever element theHello Susan is wrapped in. Imagine t he simple case where

    that phrase is wrapped in a span with an ID of greetingSpan . You could test that with an assertEvalcommand with a

    targetof javascript{this.browserbot.findElem ent('greetingSpan').innerHTML == 'Hello Susan'}and a valueof true. I

    only mention this little trick since WebForms testing relies on this heavily to verify static text which is actually

    output by the WebForms clientside engine.

    hat do I Verify/Assert?

    Well, you can Verify/Assert pretty well anything you want. However, you do not want to make assumptions about

    the layout of t he page or any non-data related elements (i.e. the CSS of t he page). This stuff can change all the time

    so we want to make sure the t ests are thorough in t hat they verify that the page is functionally correct, but robust

    enough to not break simply because somebody reworked some of the layout.

    Examples of what to Verify/Assert:

    Prepopulation of Fields

    Echoing of User information (i.e. text showing the the card number)

    Expect ed cause/effect behaviour (i.e. Checking box A causes field B to appear) .

    Basically anything that is Data related

    Examples of what NOT to Verify/Assert:

    The whole page!

    Fonts

    Colors

    Basically non-Data related items

    How Do I Run Outside of the Firefox Plugin?

    The ability to run the tests outside of the IDE is provided by Selenium RC (Remote Control):

    Selenium RC (ht tp://selenium-rc.openqa.org/)

    Selenium RC comes in a variety of flavors...but we care about the Java one. So, start off by downloading the Java

    pieces. You'd be inclined to grab the Java package from t he site, but if you do that ...good luck getting IE to work.

    Instead, grab this package:

    Selenium RC 1.0 Beta 1 (http://archiva.openqa.org/repository/releases/org/openqa/selenium/selenium-remote-control

    /1.0-beta-1/selenium-remote-control-1.0-beta-1-dist.zip)

    Unpack the ZIP to some directory on your machine...it doesn't mat ter where (i.e. c:\seleniumRC).

    Note: Again we are using the 1.0 Beta as it is necessary to do cert ain t hings (i.e. t est in Int ernet Explorer).

    Starting the Selenium RC Server

    Note: I used Java 5 so you should do the same...

    Simply open a command prompt and navigate t o theselenium -server-1.0-beta-1 sub-directory (example:

    c:\seleniumRC\selenium-remote-control-1.0-beta-1\selenium-server-1.0-beta-1).

    ium - How To - FrontEndWiki http://10.3.245.204/mediawiki/index.php/Selenium_-_

    7/22/2008

  • 8/13/2019 Selenium - How to - FrontEndWiki

    4/5

    Start the server via:

    java -jar selenium-server.jar -interactive -trustAllSSLCertificates

    You should see Jetty start up and that's it!

    Creating a JUnit Test

    Selenium RC Java execution is done via JUnit extension. Specifically, by extending th e

    com.thoughtworks.selenium.SeleneseTestCase. The good old Selenium IDE helps in this by actually (pretty much)

    generating the test for you...but we'll get back to that.

    Let's assume you already have an Eclipse project (i.e. your onlineproject in OLB) and you just want to add some

    tests to it. T he first step is to update your project's Build Path t o cont ain the SeleniumTestCase(and related classes).

    To do this, simply include theselenium -java-client-driver.jarlocated in t heselenium -java-client-driver-1.0-beta-1

    sub-directory where you ext racted Selenium RC (example: c:\seleniumRC\selenium-remot e-cont rol-1.0-beta-

    1\selenium-java-client-driver-1.0-beta-1\selenium-java-client-driver.jar) in your Build Path. If this formally to

    become a part of the pro ject , you can just copy t he JAR to your project 's LIB directory which contains

    non-deployed JARS and then refresh the project (press F5 while selecting the Project) and then adding the JAR tothe Build Pat h by Right Clicking on t he P roject-->Build Pat h-->Configure Build Pat h-->Add JARs). I f t his is just your

    experimenting, then simply Right Clicking on the Project-->Build Path-->Configure Build Path-->Add External

    JARs-->\selenium-java-client -driver-1.0-beta-1\selenium-java-client-driver.jar will

    work just f ine.

    Note: I'm not gett ing overly detailed because I assume you know how to add/remove JARs from a project in Eclipse.

    Anyway, now we want to create a JUnit test. To do that we go back to Firefox and load up Selenium IDE. In

    Selenium IDE simply open your Test Case then click the Sourcetab. While viewing the Sourcetab, choose the

    Optionsmenu items and select Format-->Java. Copy that stuff into a new Java file in Eclipse. You'll most likely see

    some errors due to the format not being 100% perfect, but by simply looking at the errors you'll quickly see that

    they are trivial to resolve.

    Note: T he generat ed test cases aren't pretty by any stretch. You can do all kinds of stuff to make them nicer. An

    example is to have an AuthenticatedSeleniumTestCasebase class which, in the setup(), logs into your application. If

    you do this then you can just extend that t est case for any t est that needs Signon...prett y handy. Another way to

    clean things up is to replace the String names given to the browsers with an Enum. Also, things like the context root

    can/should be pulled into a properties file. The couple of WebForms Selenium tests done aren't pretty, but they do at

    least do these things.

    Now that you have your test writ ten, you just need t o write your setUp() and you'll be ready to roll. A quick example

    is:

    public void setUp() throws Exception {

    setUp("https://10.3.245.218:13110", "*iehta");

    }

    ...or if you wrote your own Enum (highly advised)...

    public void setUp() throws Exception {

    setUp("https://10.3.245.218:13110", Browser.IE);

    }

    ium - How To - FrontEndWiki http://10.3.245.204/mediawiki/index.php/Selenium_-_

    7/22/2008

  • 8/13/2019 Selenium - How to - FrontEndWiki

    5/5