Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Selenium Essentials

You're reading from   Selenium Essentials Get to grips with automated web testing with the amazing power of Selenium WebDriver

Arrow left icon
Product type Paperback
Published in Mar 2015
Publisher
ISBN-13 9781784394332
Length 194 pages
Edition 1st Edition
Arrow right icon
Author (1):
Arrow left icon
Prashanth Sams Prashanth Sams
Author Profile Icon Prashanth Sams
Prashanth Sams
Arrow right icon
View More author details
Toc

Selenium IDE JavaScript functions

In addition to user-defined JavaScript commands introduced through user-extensions.js, the Selenium IDE allows the user to create JavaScript queries or functions directly in the Target field. For example, let's run a Google search by getting a random number between 1 and 100, as follows:

Selenium IDE JavaScript functions

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>

Simple JavaScript execution

The predefined Selenium IDE JavaScript command runScript is a very powerful command that lets the user execute simple JavaScript functions directly from the IDE, for example, javascript{alert("Hello!")}.

Let's see how we can disable an active textbox and enable an inactive textbox using the following code snippet:

document.getElementsByName('****')[0].setAttribute('disabled', '')
document.getElementsByName('****')[0].removeAttribute('disabled');
Simple JavaScript execution

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:

Mouse scroll

Parameterization using arrays

The Selenium IDE command storeEval is used to store values in a variable while running scripts, whereas storedVars is a JavaScript associate array with string indexes containing variables. In the following example, storeEval reserves the list of rivers in an array, and getEval is a command for initiating and incrementing the values. Some of the commands used in this section are purely user-defined, such as while, endWhile, and so on. Here, the endWhile command is used to break the loop once the value inside the array reaches the maximum limit. The following screenshot gives us a clear idea of what is being discussed here:

Parameterization using arrays

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:

Parameterization using arrays

In this example, the values are pushed into the array manually, and it does a Google search one by one.

You have been reading a chapter from
Selenium Essentials
Published in: Mar 2015
Publisher:
ISBN-13: 9781784394332
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image