Tuesday, June 24, 2014

Learning Selenium WebDriver



Selenium IDE Introduction

Selenium IDE (Integrated Development Environment) is a tool that we can use to develop automated test scripts. It has record-and-playback functionality, but we can also write the test scripts ourselves by manually entering the commands. It comes with a browser context menu with the most frequently used Selenium commands. It saves test execution time and is a good way to get familiar with test automation.

Further references

All the Selenium IDE commands are explained on the following location: http://release.seleniumhq.org/selenium-core/1.0.1/reference.html

Using Selenium IDE for the first time

Selenium IDE comes as a Firefox plug-in and has an easy-to-use user interface. Although the interface is very intuitive the most frequently used parts are explained in this section.

Prerequisites

As we are going to use Selenium IDE for the first time, the first thing we have to do is to make sure that Firefox and Selenium IDE are installed properly.
  1. Go to http://getfirefox.com and install Firefox.
  2. Go to http://seleniumhq.org/download/ in the Firefox browser and install Selenium IDE.
  3. Restart Firefox if requested.

In practice

We can now open Selenium IDE by using the following actions:
  1. Open Firefox
  2. Open Selenium IDE, via the Tools menu.
SeleniumIDE_edit
We just opened Selenium IDE and we can start recording test scripts by default..

There is more…

Selenium IDE is divided into sections which are numbered in the previous screenshot. Each section is explained below:
  1. Menubar
    The menubar has options to addopen and save test scripts and test suites. It also has some general edit options and it is even possible to specify a preferred programming language.
  2. Base URL
    Allows test scripts to be run across different domains.
  3. Toolbar
    The buttons in the toolbar are used to control the test scripts. Each button is explained below (starting from the left):
    • Speed Control: Controls how fast your test script runs.
    • Run All: Run all test scripts in Selenium IDE.
    • Run: Run a single test script in Selenium IDE.
    • Pause/Resume: Pause a running test script.
    • Step: Step through the test script once it has been paused.
    • Apply Rollup Rules: This allows us to group a sequence of actions into a single action.
    • Record: The record button. It will be enabled when Selenium IDE is recording.
  4. test script pane
    The test script commands are displayed in the test script pane. There is one tab which shows us the script in the three-column table format. The other tab shows us the script in a preferred language.
    The Command, Target and value textfields are corresponding to the three-column table structure. It is possible to edit the values by clicking on a specific row. Those three fields are explained below:
    • Command: This select box has a list of all the Selenium commands. It is possible to type into it to use the auto complete functionality or use it as a dropdown. Some reference information is shown in the reference pane at the bottom once a specific action is selected.
    • Action: The first parameter specified for a command in the Reference tab of the bottom pane always goes in the Target field.
    • Value: If a second parameter is specified by the Reference tab, it always goes in the Value field.
  5. Test suite pane
    Shows the test scripts in the current test-suite along with the results.
  6. Log/Reference pane
    The bottom pane is used for four different functions divided in separate tabs, like: LogReferenceUI-Element, and Rollup information. Each function is explained below:
    • Log: Error messages and information messages are shown while the test is running.
    • Reference: Displays the documentation of the command actually selected.
    • UI-Element: Displays the UI-element mappings actually in use.
    • Rollup: Shows rollup rules.

Record a test script

In this tutorial we are going to record a test script with Selenium IDE.

Prerequisites

Ensure that the record button is enabled. Selenium IDE automatically opens in record mode, otherwise you need to click on the (red) record button in the right top corner.

In practice

  1. Open the website under test in the Firefox browser. In this case we will use: http://selenium.polteq.com/testshop/.
  2. Open Selenium IDE, via the Tools menu.
  3. Start recording by pressing the record button in the right top corner, if needed.
  4. Search for ‘iPod’ and click on the search button.
  5. Validate that the text ‘iPod shuffle’ is present in the search results list. We can do that by selecting the text and opening the context menu (right-click) and select assertText.
The test script will end up like this:
COMMAND

open
type

clickAndWait
assertText

Selenese test cases are HTML based, so do not forget to add the HTML extension once you save the test case.
We can change the Locator Builder settings in Options -> Options… -> Locator Builders tab. It’s a good practice to put CSS on top.

Modify a test script

In this tutorial we will modify a test script by adding some commands manually.

Prerequisites

We can use the test script created in the previous tutorial, but now we want to change the language without recording the test again.

In practice

  1. Open the previous recorded test script.
  2. Select the row where we want to put an extra command.
  3. Ensure that record mode is enabled.
  4. Click on a specific language.
  5. Disable record mode.
The test script will end up like this:
COMMAND

open
click
type

clickAndWait
assertText

We just added an extra command to our previous recorded test script.

Store information from the page

With Selenium IDE we can store data and use it later. There are a couple of actions to store data, like: storestoreValuestoreText.

In practice

We like to store the text from a specific link and print it to the log panel. Obviously you can also use the text (which is stored in a variable) in the next test step.
  1. Open the previously recorded test script.
  2. Select the last row.
  3. Insert two new rows by opening the context menu (right-click) and select Insert New Command
  4. Add a storeText command to store the text of a given element.
  5. Add a echo command to display the content of the variable in the log panel.
The test script will end up like this:
COMMAND

open
type

clickAndWait
storeText

echo
assertText

The output of the script above is: [info] echo: iPod shuffle
Selenium uses a JavaScript map called storedVars to store the data.

Dealing with AJAX functionality

Some websites are using AJAX to modify the graphical user interface (GUI) without being reloaded. In this tutorial we are going to record website functionality which is using AJAX. We can use waitFor commands, like: waitForAlertNotPresentwaitForAlertPresentwaitForElementPresentwaitForElementNotPresentwaitForTextPresent, etc.

In practice

  1. Open the website under test in the Firefox browser. In this case we will use: http://selenium.polteq.com/testshop/.
  2. Open Selenium IDE, via the Tools menu.
  3. Start recording by pressing the record button in the right top corner, if needed.
  4. Add a product to the cart by performing the following actions: navigate to the ‘laptop’ category, view an item and click on the ‘add to cart’ button.
  5. Verify that the product is added to the cart, by right clicking on the items name in the cart and select waitForText.
  6. Delete the item from the cart, by clicking on the remove icon next to the item name.
  7. Verify that the text ‘no product’ is present in the cart, we can do that by selecting the text and opening the context menu (right-click) and select waitForElementPresent.
  8. Stop recording by clicking on the record button and try to execute the recorded test script by clicking on the play button.
The test script will end up like this:
COMMAND

open
clickAndWait
clickAndWait
click
waitForText

click
waitForElementPresent
Selenese test cases are HTML based, so do not forget to add the HTML extension once you save the test case.

Create test suites

A test suite is a collection of test scripts. In this tutorial we are going to take a closer look on how to create a test suite.

In practice

  1. Create a test script as described in the previous tutorials.
  2. Select File -> New Test Case to create a new test script. (or open the context menu from the left panel and select New Test Case)
  3. Repeat step 1 and 2 as many times you want.
  4. Select File -> Save Test Suite to save all test scripts.
Selenese test suites are HTML based, so do not forget to add the HTML extension once you save the test case.

Exexute test scripts in different browsers

In this tutorial we are going to execute test scripts in different browsers.

Prerequisites

It is necessary to download the Selenium standalone serverIEDriverServer and chromedriver. (http://code.google.com/p/selenium/downloads/list)

In practice

  1. We need to start Selenium standalone server by entering the following command in the commandline:
    1
    2
    3
    java -jar selenium-server-standalone-2.39.0.jar 
    -Dwebdriver.ie.driver=IEDriverServer.exe 
    -Dwebdriver.chrome.driver=chromedriver.exe`
    
  2. We need to enable WebDriver Playback in Selenium IDE. Options -> Options... -> WebDriver tab and check Enable WebDriver Playback
  3. Specify in which browser we like to run our test scripts Options -> Options... -> WebDriver tab and specify the browser in the textfield. (chrome, firefox, internet explorer and so on)
Now you can run your test script with WebDriver in the specified browser.
- See more at: http://selenium.polteq.com/en/category/01-selenium-ide/#sthash.DtgxDgq3.dpuf              
                                                                                                                                                                                                        

No comments:

Post a Comment