You are on page 1of 5

Selenium - How To - FrontEndWiki

http://10.3.245.204/mediawiki/index.php/Selenium_-_How_To

Selenium - How To
From FrontEndWiki
T his 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 2 3 4 5 6 What is Selenium? Where Does it Fit? Where Does it NOT Fit? How Do I Create a T est? What do I Verify/Assert? How Do I Run Outside of the Firefox Plugin? 6.1 Starting the Selenium RC Server 6.2 Creating a JUnit T est

What is Selenium?
From the website (Selenium WebSite (http://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. And 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 multiple browsers. Depending on the nature of your test, that could be extremely valuable.

Where Does it Fit?


Basically, Selenium is a good fit if: You need to test a piece of a webapp in the browser itself (example: DHT ML pages). You want to create a regression test for a full flow. You want to create a simple test quickly.

Where 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 DHT ML components (some feel WebT est is better for this...but if it is Selenium or nothing, then go with Selenium!).

How Do I Create a Test?


1 of 5 7/22/2008 3:10 PM

Selenium - How To - FrontEndWiki

http://10.3.245.204/mediawiki/index.php/Selenium_-_How_To

T he 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 time, 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), writing a Selenium test is simple: 1. Open Selenium IDE in Firefox via T ools > 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 the flow you wish to test. NOT E: 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 T able t o 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 true. An assertion halts the test if it is not true. T here are a couple of ways to do this. T he first way is to simply playback your test (slowly) and pause it where you want to add your tests. T hen you can either right click the element of interest choose one of the 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 manually Insert New Command , you'll pick from the dropdown which command you want to use (i.e. assertValue) and then choose the two arguments: target and value. T ypically target is the field that you are verifying and value is the expected value. T his isn't always the case though. If, for example, your target is a JavaScript snippet (written as javascript{...script to execute...}) then the value is the name name of the variable to store the result as. For example, if I execute a store command with a target of javascript{'hello'} and a value of Greeting , then there is a variable called Greeting created with the output of the executed JavaScript. T his variable will be available in subsequent 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. T hat being said, there is one particular test that is missing which may cause you troubles. Say you want to verify that the Hello Susan text on the screen actually has the user's name (Susan). Now, you can simple verify that the text

2 of 5

7/22/2008 3:10 PM

Selenium - How To - FrontEndWiki

http://10.3.245.204/mediawiki/index.php/Selenium_-_How_To

Susan is 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 innerHT ML of whatever element the Hello Susan is wrapped in. Imagine the simple case where that phrase is wrapped in a span with an ID of greetingSpan . You could test that with an assertEval command with a target of javascript{this.browserbot.findElement('greetingSpan').innerHTML == 'Hello Susan'} and a value of 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.

What do I Verify/Assert?
Well, you can Verify/Assert pretty well anything you want. However, you do not want t o make assumptions about the layout of the page or any non-data related elements (i.e. the CSS of the page). T his stuff can change all the time so we want to make sure the tests are thorough in that 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) Expected 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: T he whole page! Fonts Colors Basically non-Data related items

How Do I Run Outside of the Firefox Plugin?


T he ability to run the tests outside of the IDE is provided by Selenium RC (Remote Control): Selenium RC (http://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 the 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 matter where (i.e. c:\seleniumRC). Note: Again we are using the 1.0 Beta as it is necessary to do certain things (i.e. t est in Internet Explorer).

Starting the Selenium RC Server


Note: I used Java 5 so you should do the same... Simply open a command prompt and navigate to the selenium-server-1.0-beta-1 sub-directory (example: c:\seleniumRC\selenium-remote-control-1.0-beta-1\selenium-server-1.0-beta-1).

3 of 5

7/22/2008 3:10 PM

Selenium - How To - FrontEndWiki

http://10.3.245.204/mediawiki/index.php/Selenium_-_How_To

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 the com.thoughtworks.selenium.SeleneseTestCase. T he 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 online project in OLB) and you just want to add some tests to it. T he first step is to update your project's Build Path to contain the SeleniumTestCase (and related classes). T o do this, simply include the selenium-java-client-driver.jar located in the selenium-java-client-driver-1.0-beta-1 sub-directory where you extracted Selenium RC (example: c:\seleniumRC\selenium-remote-control-1.0-beta1\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 project, you can just copy the 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 to the Build Path by Right Clicking on the Project-->Build Path-->Configure Build Path-->Add JARs). If this is just your experimenting, then simply Right Clicking on the Project-->Build Path-->Configure Build Path-->Add External JARs--><selenium RC unzip directory>\selenium-java-client-driver-1.0-beta-1\selenium-java-client-driver.jar will work just fine. Note: I'm not getting 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. T o do that we go back to Firefox and load up Selenium IDE. In Selenium IDE simply open your T est Case then click the Source tab. While viewing the Source tab, choose the Options menu 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 generated 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 AuthenticatedSeleniumTestCase base class which, in the setup(), logs into your application. If you do this then you can just extend that test case for any test that needs Signon...pretty 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. T he couple of WebForms Selenium tests done aren't pretty, but they do at least do these things. Now that you have your test written, you just need to 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); }

4 of 5

7/22/2008 3:10 PM

Selenium - How To - FrontEndWiki

http://10.3.245.204/mediawiki/index.php/Selenium_-_How_To

T his just tells the system what the base URL to use is and what browser to use. We are somewhat limited in what Browsers we can test on. T he big reason for this is that our applications are HT T PS. However, I have managed to get the following browsers to work with the following Selenium browser Strings: Internet Explorer 7 *iehta Firefox 3 *chrome Netscape 9.2 *chrome C:\\Program Files\\Netscape\\Navigator 9\\navigator.exe INT ERNET EXPLORER NOT E: You'll need to do the following before you run a test: T ools-->Internet Options-->Privacy-->Advanced-->Check both checkboxes and both Accept radio buttons. Other browsers like Firefox 2 and IE6 almost certainly work...I've just not tried them. Safari 3 XP does NOT work nor does Netscape 7.2. Actually, if you try Safari XP it will mess up your IE7 proxy settings thus buggering IE7 not only for Selenium, but for everything else as well. T o fix that, in IE choose T ools-->Internet Options-->Connections-->LAN Settings-->uncheck everything! T hat's it! It should just magically run like any other JUnit test now. T he spawned browser may not gain focus so watch your taskbar to see if a new IE/Firefox instance spawns and is awaiting your at tention. You may, depending on a variety of things, be prompted about certificates etc..., but in general things should work just fine. If not, well, make sure the Selenium Server is up with the command line options shown and make sure your browser String in the test case is correct. If you are still having issues, find me and I'll help you out (david.workman@cibc.com). One side note, IE tests won't let you verify/assert JavaScript text (i.e. assertT extP resent('var foo = 1')). Such tests work fine in Firefox and Netscape but fail miserably in IE. As such, it is better to assert things which happen due to the script you are looking for rather than the script itself. WebForms has IDE and RC tests in CVS in CVS2 under WebForms. Retrieved from "http://10.3.245.204/mediawiki/index.php/Selenium_-_How_T o"

This page was last modified 13:05, 3 July 2008.

5 of 5

7/22/2008 3:10 PM

You might also like