Testing is Rocket Science Not Brain Surgery

Almost all of the World's Greatest Accomplishments Were The Result of Great Planning!

Testing is Rocket Science Not Brain Surgery header image 2

“Getting Started with Selenium” Part One of Many by Bobby Washington

June 6th, 2011 · 2 Comments · Uncategorized

If you have been exposed to test automation for any length of time then I suspect you have heard about an open source tool called Selenium. If there is any question in your mind regarding the validity of this set of tools simply because it is open source then I encourage you to perform a job search on Dice.com using “Selenium” as the search term. At the time of this writing the search yielded 474 results. A similar Monster.com search yielded 245 results with pretty impressive salary ranges I might add. In addition to this, if you navigate to the Selenium “Support” page you will find a listing of at least 12 companies/individuals that provide expert support for Selenium. Suffice to say Selenium is an automation tool you should become familiar with. Getting the tool set up is not difficult so I really would prefer not to spend a lot of time on this aspect of Selenium. Basically if you have installed an add-on for Firefox then you can install Selenium. As you peruse this site you will undoubtedly read about concepts that are essential to successfully using LoadRunner. Selenium is no different in that it has its own set of concepts that I believe are essential for a Selenium user. While using this tool, the following three areas that have aided me in creating Selenium test scripts:
• Common Selenium commands
• Use of web development tool Firebug
• Understanding of Xpath
Part one of this two part blog series will focus on Selenium commands that I find myself using repeatedly throughout my tests scripts. Part two will focus on using Xpath queries for element/object identification with the assistance of an additional Firefox add on development tool called Firebug.
Today many websites implement some type of controls for interacting with the site. Whatever the implementation you can be sure that at any given point you will be selecting, displaying, and/or entering data. So it should come as no surprise that you will find yourself using a core/common set of Selenium commands in your suite of test scripts for manipulating and interacting with these web controls. Understanding these commands and their arguments is critical to creating automated test scripts. Selenium commands can be grouped into one of three categories: Actions, Accessors and Assertions.
Actions are commands that generally manipulate the state of the application. They do things like “click this link” and “select that option”. If an Action fails, or has an error, the execution of the current test is stopped.
Accessors examine the state of the application and store the results in variables, e.g. “StoreTitle”. They are also used to automatically generate Assertions.
Assertions are like Accessors, but they verify that the state of the application conforms to what is expected. Examples include “make sure the page title is X” and “verify that this checkbox is checked”.
The items that follow are common Action, Accessors, and Assertion commands I have found myself repeatedly including in my scripts. The examples were derived from this very site so you can copy them into your Selenium scripts and execute them while reading this blog.
Common Action commands –
Script Focus: Opens the testrocket.org website and selects an item from a drop-down menu. Also performs a search by entering search criteria.
1. Command: open() – Opens a webpage in the test frame.
Target: http://testrocket.org/Blog/
Value:
2. Command: selectAndWait () – Select an option from a drop-down using an option locator. If the action causes a new page to load, call waitForPageToLoad
Target: cat
Value: label=regexp:\\s+Software Gems\\s+\\(4\\)
3. Command: type() – Sets the value of an input field, as though you typed it in.
Target: s
Value: label= Software Gems
Script Focus: Navigates to another page and clicks in a checkbox. Also takes a screenshot!
1. Command: open() – Opens a webpage in the test frame.
Target: http://testrocket.org/Blog/
Value:

2. Command: clickAndWait() – Clicks on a link, button, checkbox or radio button. If the click action causes a new page to load (like a link usually does), call waitForPageToLoad.
Target: link=Subscribe
Value:

3. Command: click() – Clicks on a link, button, checkbox or radio button. If the click action causes a new page to load (like a link usually does).
Target: alwaysUse
Value:

4. Command: captureEntirePageScreenshot() – Saves the entire contents of the current window
Target: C:\screenshot.png
Value:

Common Accessor commands –
Script Focus: Returns all values associated with a drop-down.
1. Command: open() – Opens a webpage in the test frame.
Target: http://testrocket.org/Blog/
Value:
2. Command: storeSelectOptions() – Gets all option labels in the specified select drop-down.
Target: cat
Value: foo
3. Command: echo – Prints the specified message into the third table cell in your Selenese tables. Useful for debugging.
Target: The store value is ${foo}
Value:

***Note** While echo is not considered an Accessor command I wanted to include it to allow you to verify the expected value was actually captured. It is handy command to have knowledge of. Also when calling a variable the syntax is ${value name}

Script Focus: Stores the value typed in an input field.
1. Command: open() – Opens a webpage in the test frame.
Target: http://testrocket.org/Blog/
Value:
2. Command: type () – Sets the value of an input field, as though you typed it in.
Target: s
Value: Software Gems
3. Command: storeValue() – Gets the (whitespace-trimmed) value of an input field (or anything else with a value parameter).
Target: s
Value: typevalstore
4. Command: echo – Prints the specified message into the third table cell in your Selenese tables. Useful for debugging.
Target: The store value is ${typeval store}
Value:

Script Focus: Stores whether an option has been selected from a list.

1. Command: open() – Opens a webpage in the test frame.
Target: http://testrocket.org/Blog/
Value:
2. Command: selectAndWait () – Select an option from a drop-down using an option locator. If the action causes a new page to load, call waitForPageToLoad
Target: cat
Value: label=regexp:\\s+Software Gems\\s+\\(4\\)
3. Command: storeSomethingSelected() – Determines whether some option in a drop-down menu is selected.
Target: cat
Value: selectval
4. Command: echo() – Prints the specified message into the third table cell in your Selenese tables. Useful for debugging.
Target: The store value is ${selectval}
Value:

Script Focus: Stores the label associated with an option selected from a drop-down menu.

1. Command: open() – Opens a webpage in the test frame.
Target: http://testrocket.org/Blog/
Value:
2. Command: selectAndWait () – Select an option from a drop-down using an option locator. If the action causes a new page to load, call waitForPageToLoad
Target: cat
Value: label=regexp: Performance Testing\\s+\\(12\\)
3. Command: storeSelectedLabel() – Gets option label (visible text) for selected option in the specified select element.
Target: cat
Value: selectval
5. Command: echo() – Prints the specified message into the third table cell in your Selenese tables. Useful for debugging.
Target: The store value is ${selectval}
Value:

Common Assertion(Verify) commands –
Script Focus: Demonstrates various verify commands supported by Selenium. If the verification fails Selenium will indicate this however script will continue to execute.

1. Command: open() – Opens a webpage in the test frame.
Target: http://testrocket.org/Blog/
Value:

2. Command: verifyText () – Gets the text of an element.
Target: //html/body/div/ul/li[3]/a
Value: Q&AA

***Note*** Selenium log will indicate the text was not found. Changing the value to “Q&A” will cause Selenium to return a message similar to:

[info] Executing: |verifyText | //html/body/div/ul/li[3]/a | Q&A |

3. Command: type() – Sets the value of an input field, as though you typed it in.
Target: s
Value: Software Gem

4. Command: verifyValue() – Gets the (whitespace-trimmed) value of an input field (or anything else with a value parameter.
Target: //*[@id=”s”]
Value: Value no where close to Software Gem

***Note*** Selenium log will indicate the text did not match. Changing the value to “Software Gem” will cause Selenium to return a message similar to:

[info] Executing: |verifyValue | //*[@id=”s”] | Software Gem |

5. Command: selectAndWait() – Select an option from a drop-down using an option locator.
Target: cat
Value: label=regexp:\\s+Performance Wilderness\\s+\\(3\\)

6. Command: verifySelectedValue – Gets option value (value attribute) for selected option in the specified select element.
Target: //*[@id=”cat”]
Value: 4

***Note*** Selenium log will indicate the option value did not match. Changing the value to “32” will cause Selenium to return a message similar to:

[info] Executing: |verifySelectedValue | //*[@id=”cat”] | 32 |

7. Command: verifyElementPresent – Verifies that the specified element is somewhere on the page.
Target: //html/body/div/div[2]/
Value:

***Note*** Selenium log will indicate the element was not present. Changing the target to “//html/body/div/div[2]/img” will cause Selenium to return a message similar to:

[info] Executing: |verifyElementPresent | //html/body/div/div[2]/img | |

Common Assertions commands (cont.)–
Script Focus: Demonstrates the assertion command assertText() supported by Selenium. If the assertions fails Selenium will indicate this and script will stop executing.

1. Command: open() – Opens a webpage in the test frame.
Target: http://testrocket.org/Blog/
Value:

2. Command: assertText () – Gets the text of an element.
Target: //html/body/div/ul/li[3]/a
Value: label=Q&AA

3. Command: type() – Sets the value of an input field, as though you typed it in.
Target: s
Value: Software Gem

***Note*** Selenium log will indicate the text was not found. Command #3 will not be executed. Changing the value to “Q&A” will cause Selenium to return a message similar to:

[info] Executing: |assertText | //html/body/div/ul/li[3]/a | Q&A |

Also Command #3 will be executed.

Script Focus: Demonstrates the assertion commands assertElementPresent and assertValue() supported by Selenium. If the assertions fails Selenium will indicate this and script will stop executing.

1. Command: open() – Opens a webpage in the test frame.
Target: http://testrocket.org/Blog/
Value:

2. Command: assertElementPresent() – Verifies that the specified element is somewhere on the page.
Target: //html/body/div/div[2]/imb
Value:

3. Command: type() – Sets the value of an input field, as though you typed it in.
Target: s
Value: Software Gem

4. Command: assertValue() – Sets the value of an input field, as though you typed it in.
Target: s
Value: Software Gem

***Note*** Selenium log will indicate the element was not present. Command #3 will not be executed. Changing the target to “//html/body/div/div[2]/img” will cause Selenium to return a message similar to:

[info] Executing: |assertElementPresent | //html/body/div/div[2]/img | |

Also Commands #3 and #4 will be executed.

In closing, these are just some of the common commands I find myself using when testing with Selenium. As I stated earlier in this blog, Selenium is a great test automation tool for testing web applications. According to the Selenium site http://seleniumhq.org/about/history.html development of the tool began in 2004. Selenium is by no means limited by the core command set. The ability to transform a recorded script into a number of supported languages such as C#, Perl, and Ruby only add to the tool’s capabilities. According to the website it is even possible to execute small JavaScript snippets within the tool. Selenium offers plenty of functionality to support very robust web-based automation testing. Selenium is truly a tool any software tester should be familiar with using. My hat’s off to the group responsible for developing and maintaining this great testing tool. Then only thing it will cost you is perhaps a couple of hours over a weekend. I assure it will be time well spent.

Tags:

2 Comments so far ↓

  • Kurt Buehler

    This is a great article. I look forward to more. I am currently searching for a Performance Test lead for Sacramento, CA. 12 mo contract job. And mostly LoadRunner. But Selenium is all over the place now. I find it impressive that you basically called it two years ago!!

    Call me anytime.

    Kurt Buehler
    408-449-8757

  • a

    Hi, just wanted to say, I enjoyed this article.
    It was inspiring. Keep on posting!

Leave a Comment