Saturday, March 16, 2013

4 Extending features of Selenium-IDE

For features that are not available in the core(Selenium-IDE.js) of IDE, we have wonderful option of extending the functionality of Selenium IDE with help of User Extensions. Extensions are nothing but JavaScript files containing code to do some special tasks that IDE doesn't support. Ex: We don't have support for usage of if statement inside IDE, but we can achieve this with help of user extensions.

Achieving If statement and While loop through user extension

One smart user of Selenium IDE created a user extension with features of If statement and While loop.Thanks to him that he shared his code with us for free. 

  • Go to Github (Click Here) and download sideflow.js. This is the user extension that contain code for IF statement and While loop. 
  • Open selenium IDE and select Options -> Options from menu bar.

  • Copy the location of the file "sideflow.js" that you downloaded previously and paste it over Selenium core extension input field of options dialog and click on OK.


  • Restart Selenium IDE and Firefox.
  • Now that you configured user extension, refer to page: Example usage(Click me) for documentation on how to call the functions declared inside "sideflow.js".
  • Here is a snapshot of Selenium IDE program that is pretty much self explanatory and makes use of While, If and Goto statements.


Explanation:
  • Store command discussed at Echo, StoreEval and StoredVars module (click here).
  • Command: label, declares a label to which we can jump directly from any part of the program. It takes one parameter and that is name of the label.
  • Command: gotolabel, jumps the control to specified label inside Target parameter. 
  • Command: runScript, run the java script code passed at parameter Target.
  • Command: gotoIf, validates expression at the Target field and if it is true, control jumps to the label passed at Value field. If expression is a failure, control simply flows to the next statement.
  • Command: while, evaluates expression in the Target field and when expression results in a true condition, control flow is iterated between statements among While and endWhile. It exits out of the loop when expression results in false condition.
  • I recommend interested users to try opening "sideflow.js" and see the code inside it to identify functions, its parameters and definition. 
Building your own user extensions and JavaScript functions

So far, we understood the concept of user extensions and implemented it with help of a user extension file created by some smart user. Its time for us to get smarter and create our own functions and user extensions. To achieve this I will consider a user requirement case that is not supported by Selenium IDE.

Requirement: Create a function to generate a random number and type it over an element that is passed as a parameter to the function. 
Let us recall structure of JavaScript function declared inside Selenium-IDE.js
Selenium.prototype.doFUNCTION_NAME = function(Parameter1, Parameter2, ... ) 
{
  //Function definition
};

Follow the steps below to achieve the requirement:
1. Open sideflow.js that you already integrated with Selenium IDE.
2. At the bottom of the page add the following code: 
 Selenium.prototype.doTypeRandomNumber = function(locator)
 {
    var RandomNumber = Math.floor(Math.random()*11)
    selenium.doType(locator,RandomNumber)
 }; 
3. Save the file.
4. Restart Selenium IDE
5. Create a new test case and as you type string "Type" in the command field, you should see a auto suggestion drop down with option  "TypeRandomNumber". This behavior confirms that you did everything right till step 4 and was able to extend the functionality of Selenium IDE. Congrats.   

6. Testing the function: Create a new test case(refer snapshot below) to check if we meet the requirement with function "TypeRandomNumber"


7. Run the program and observe that every time you run this program, a random number is entered into email id field of gmail.com website.

Output


Notes about the function TypeRandomNumber( )
  • Math.floor(Math.random()*11)  
  1. Here "11" specifies that we are trying to generate a random number between 0 - 10. In case if you would like to generate a number between 0 and 100, change 10 to 101.
  2. Math.random() - returns a decimal number between specified range. In our case 0-10.
  3. Math.floor() - round a number downward towards its nearest number. Ex: 4.68 to 4.

  • selenium.doType(locator,RandomNumber)
  1. "selenium" is an object created by Selenium Core : Selnium-IDE.js and we can use it any where inside user extension file.
  2. We tried to call "doType" function present inside Selenium-IDE.js/core of selenium IDE, to type a random number into an element pointed by variable "locator". Note that "locator" is a parameter received by TypeRandomNumber( ).

Summary: We can create any number of java script functions based on the requirement to extend the functionality of Selenium IDE. I recommend you to try create your own functions and practice as much as possible before moving to next module.

Hope you enjoyed reading this post. I will be glad to see your comments below. See you again in the next module. Thank you.

4 comments:

 

QuestionSelenium Copyright @Shiva Krishna