Selenium Essentials - Sample Chapter
Selenium Essentials - Sample Chapter
Selenium Essentials - Sample Chapter
ee
Sa
pl
Selenium Essentials
Selenium WebDriver is an open source software-testing tool used to automate
web-based applications that is platform independent and that can be accessed
by any popular programming languages. It's been about a decade since Jason
Huggins started the Selenium project in 2004 at Thoughtworks. Later, in 2008,
Simon Stewart combined his work on WebDriver with Selenium to give a new
birth to Selenium WebDriver. Today, Selenium WebDriver is the most widely
used web-automation tool around the world.
This book provides guidance that will help readers grasp Selenium WebDriver
concepts fast. You will learn about the advanced features of the Selenium IDE
and Selenium Builder, followed by cross-browser tests, methods of Selenium
WebDriver, best practices involved, and extensive ideas to create a
Selenium framework.
Chapter 1
[1]
A new, untitled test case will be created in Left Pane after launching the Selenium
IDE. To start with a new test case, choose New Test Case from the File menu,
that is, File | New Test Case, or make use of Ctrl + N, the Windows shortcut.
To start recording test scripts, click on the round, red icon from the playback control
toolbar. By default, the record button will be active and the test scripts are recorded
in Selenese, a domain-specific language that is similar to the HTML format.
The playback control toolbar is shown in the following screenshot:
The Fast-Slow
slider adjusts the test speed execution; the Play All
button lets you run entire test cases as a test suite, where a test suite is a collection
of test cases; and the Play
button helps you to run the current test case. The
Pause/Resume button pauses test execution for a while and allows the user to
resume tests at their convenience.
The Test Case pane displays all the recorded steps with Command, Target, and
Value. The Command column instructs the IDE about what to do. It comes with
three different aspects, which are:
Actions
Accessors
Assertions
[2]
Chapter 1
The Selenium IDE has a list of built-in commands that let you drive tests as expected.
Adding user-defined commands to the Selenium IDE is quite feasible by extending
the external JavaScript methods. A command can be any one of the preceding
three types. In the IDE, these commands are easily editable and replaceable with
alternative commands while generating scripts.
Action commands manipulate the application state through some kind of actions,
can be either action or actionAndWait. Action commands that end with the suffix
AndWait allow the page to load fully before starting to execute the next command.
A few of the Action command examples are open, type, typeAndWait, select,
selectAndWait, check, checkAndWait, click, and clickAndWait.
Accessors detect the application state and store results in a variable; store, storeText,
and storeValue are the commands that are used to store values. For example, in the
following screenshot, search is a variable and prashanth sams is the search keyword.
Later on, the stored value is retrieved and is used as a parameter for an action,
${search}. The discussion in this paragraph is encapsulated in this screenshot:
[3]
The Log / Reference / UI-Element / Rollup pane is displayed at the bottom of the
IDE. This is shown in the following screenshot:
This pane allows the user to see the log information, command reference, UI-Element,
and Rollup, among others. On installing the Neustar plugin to the Selenium IDE, a
separate tab called Neustar Script Uploader will be shown along with the other tabs.
Neustar WPM (formerly called Browsermob) is a web performance management tool
for web page monitoring and load testing.
The log captures all the IDE test execution steps one by one and is mainly used for
debugging purposes. The Debug menu in the bottom pane contains a list of options,
namely Info, Debug, Warn, and Error. It lets you filter the explicit status, warning,
and error messages, and certainly reduces the verbosity level.
[4]
Chapter 1
The Reference tab gives a detailed explanation of the IDE commands upon clicking
on each row from the Test Case pane. In the case of user-defined commands,
the Reference tab will not include any information. Rollup executes a group of
commands in one step; it is reusable and can be used any number of times within
the test case. Refer to Help | UI-Element Documentation for more details about
UI-Element and Rollup.
While recording test scripts, the Selenium IDE provides UI-based options for
every mouse right-click on elements on a browser web page. To achieve this,
right-click on the web page and hover the mouse over Show All Available
Commands. The following screenshot is the result of this action:
WebDriver playback
The WebDriver playback feature in Selenium IDE lets you run tests in any one of
the most popular web browsers: Chrome, Firefox, HtmlUnit, Internet Explorer, and
Opera. By default, the WebDriver playback feature is turned off and is inactive. To run
Selenium IDE scripts through WebDriver, turn on the WebDriver playback settings.
[5]
Launch the Selenium IDE and choose Selenium IDE Options from the Options menu.
Switch to the WebDriver tab and select the Enable WebDriver checkbox. Now, restart
the Selenium IDE to enable the WebDriver playback feature. However, on changing
the browser name, restarting the IDE is not necessary. The idea discussed in these two
paragraphs is shown in the following screenshot:
Selenium Server can be initialized manually from the terminal or Command Prompt.
Open the terminal or Command Prompt, locate the Selenium Server JAR file, and
run the command using the following syntax:
java -jar selenium-server-standalone-<version-number>.jar
[6]
Chapter 1
[7]
On Mac:
1. Unzip/extract the zipped package (chromedriver_mac32.zip).
2. Copy and paste ChromeDriver to /usr/bin.
3. It will prompt you to enter the admin password; enter it to set the path.
Locator prioritization
Prioritization lets you prioritize locators while recording scripts. In general, this
feature helps the user by giving high priority to generate scripts with respect to
the user's preferred locators. For example, by changing the csslocator order from
the fifth to the first position, the further elements generated on the Selenium IDE's
target will be in CSS, that is, the locatorType will be set to CSS by default.
An example of this is CSS = argument.
Launch the Selenium IDE, choose Options... from the Options menu, and switch
to the Locator Builders tab. The left-hand pane will be mounted with a list of
available locator builders, such as, ui, id, link, name, css, dom:name, xpath:link,
xpath:img, xpath:attributes, xpath:idRelative, xpath:href, dom:index,
and xpath:position. The list of available locator builders is shown in the
following screenshot:
[8]
Chapter 1
Drag and drop locator builders on the left-hand side to change their order. Finally,
click on the OK button, and restart the Selenium IDE for the changes to take effect.
To reset the default settings of the Selenium IDE, click on the Reset option found in
the bottom-left corner of the Selenium IDE options window pane.
[9]
The tab will automatically switch to the Source view on disabling the Table toggle
button, as shown in the following screenshot:
Launch the Selenium IDE, hover your mouse over the Clipboard Format option
from the Options menu, and select the preferred combination format.
[ 10 ]
Chapter 1
Copy the following row from the Test Case pane (as shown in the preceding
screenshot) and paste it as a code snippet:
driver.findElement(By.id("gbqfq")).clear();
driver.findElement(By.id("gbqfq")).sendKeys("prashanthsams");
For example, create a JavaScript (.js) file (Datasource.js) that includes the
following keywords:
Search1 = "PrashanthSams"
Search2 = "Selenium Essentials"
[ 11 ]
Launch the Selenium IDE and choose Options from the Option menu,
Option | Options.... Now, browse through Selenium IDE extensions and select
the .js file created earlier (Datasource.js), as shown in the following screenshot:
Finally, restart the Selenium IDE to effect the changes. Initialize, store, and fetch
values from the .js file one by one using the storeEval command, as follows:
[ 12 ]
Chapter 1
Here, Search1 and Search2 are the two variables that retrieve the respective
keywords from the JavaScript file. These values are again stored in the new variables,
GoogleSearch1 and GoogleSearch2, as shown in the preceding screenshot.
datadriven.js
goto_sel_ide.js
user-extensions.js
2. Launch the Selenium IDE and choose Options from the Option menu,
Option | Options.... Now, navigate to Selenium Core extensions and
upload the JavaScript files (user-extensions.js, goto_sel_ide.js and
datadriven.js), as shown in the following screenshot. Finally, restart the
Selenium IDE for the changes to take effect.
[ 13 ]
3. In the Selenium IDE, the XML file is used as a data source to store values,
whereas Datadriven.js is designed to support the XML file format.
Here is the syntax for XML file formatted data source:
<testdata>
<test varname="value" />
<test varname="value" />
<test varname="value" />
</testdata>
Create an XML file with the extension .xml (data.xml). Here, varname is
the variable name, and value refers to the keyword under the <test> tag.
Let's create an XML file with the .xml extension (data.xml). For example,
refer to the following code snippet:
<testdata>
<test phrase="selenium essentials" />
<test phrase="seleniumworks.com" />
<test phrase="prashanthsams" />
</testdata>
[ 14 ]
Chapter 1
The following HTML source tags let you convert the steps into runnable test scripts:
<tr>
<td>store</td>
<td>javascript{'Random number ' + Math.floor(Math.random() * 100);}
</td>
<td>search</td>
</tr>
<tr>
<td>echo</td>
<td>${search}</td>
<td></td>
</tr>
<tr>
<td>open</td>
<td>/</td>
<td></td>
</tr>
<tr>
<td>type</td>
<td>id=gbqfq</td>
<td>${search}</td>
</tr>
[ 15 ]
Mouse scroll
The scroll event is currently unavailable in the Selenium IDE. However, the
user-extensions.js file includes a JavaScript method that lets you scroll the
mouse through the web page.
Refer to the Google site https://sites.google.com/site/seleniumworks/
selenium-ide-tricks to download user-extensions.js. This user extension
file includes IDE commands like while, endWhile, gotoIf, gotoLabel, and push.
Increase the value to 10 based upon the vertical length of the web page, as shown
in the following screenshot:
[ 16 ]
Chapter 1
Let's see another example of advanced parameterization concepts using the Selenium
IDE. Refer to the Google site https://sites.google.com/site/seleniumworks/
selenium-ide-tricks to download the user-extensions.js
file. The following screenshot captures the essence of this discussion:
[ 17 ]
In this example, the values are pushed into the array manually, and it does a Google
search one by one.
Selenium Builder
Selenium Builder is a record-and-playback tool similar to the Selenium IDE and is an
extension of the Firefox web browser. It has some unique features that the Selenium
IDE doesn't support, for example GitHub integration to export and commit test
suites/cases in GitHub and TestingBot integration to run tests in the cloud. It also
provides more language support than the Selenium IDE, including for languages
such as JSON, Java/TestNG, NodeJS WD, NodeJS Mocha, and NodeJS Protractor.
Selenium Builder is expected to be the future of the Selenium IDE, with advanced
features. Here's a screenshot of Selenium Builder:
[ 18 ]
Chapter 1
Choose Selenium Builder from the Tools menu, that is, Tools | Web
Developer | Launch Selenium Builder
Selenium Builder allows you to have control over the web app to start recording test
scripts. It allows the users to verify their tests using a button, Record a verification.
Clicking on the verification button highlights the text to be verified all over the page
on mouseover. The mouseover function is an excellent feature of Selenium Builder
that helps the user to add mouse hover functionality whenever needed. Select the
Record mouseovers checkbox to record mouseover actions. The following screenshot
shows this functionality:
Stop recording the scripts and export (File | Export) the tests in the preferred
combination format.
[ 19 ]
Selenium Builder comprises higher-level support for Data Driven testing that lets
you drive tests using the JSON, XML, and CSV file formats. CSV file formats can
be used to fetch data files with large volumes. Let's see some of the file formats
involving Data Driven tests.
[ 20 ]
Chapter 1
For example, create a JSON file with the .json extension (data.xml). Here, foo is
the variable name, whereas value is defined with the keywords prashanth and
sams, as follows:
[ {"foo": "prashanth"}, {"foo": "sams"}]
To address the values stored in a JSON file, it is recommended to call the variables,
for example, ${foo}, in the pre-recorded steps, as shown in the preceding screenshot.
For example, create an XML file with the .xml extension (data.xml). Here, search is
the variable name, and value refers to the keyword under the <test> tag, as shown
in the following code snippet:
<testdata>
<test search="selenium essentials" />
<test search="prashanthsams" />
[ 21 ]
[ 22 ]
Chapter 1
Obtain the Sauce access key after logging in to your Sauce account as a real user
or by clicking look up access key in Selenium Builder. The access key is unique for
each user.
Choose Run on Sauce OnDemand from the Run menu. Make sure that Sauce Settings
is furnished before running the test cases. All you need to do is enter the Sauce
Username, Sauce Access Key, Browser, and OS versions. Finally, log in to Sauce and
verify the test results. The tests are recorded in video and images for user preview.
Summary
In this chapter, you learned about the Selenium IDE functions, along with Selenium
Builder, and observed how to handle the Selenium IDE to automate simple tests.
In the next chapter, we will discuss advanced compatibility-testing techniques using
Selenium WebDriver. It lets you drive tests on different browsers.
[ 23 ]
www.PacktPub.com
Stay Connected: