Professional Documents
Culture Documents
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.
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
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
3 of 5
7/22/2008 3:10 PM
http://10.3.245.204/mediawiki/index.php/Selenium_-_How_To
4 of 5
7/22/2008 3:10 PM
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"
5 of 5
7/22/2008 3:10 PM