Wednesday, December 31, 2014

Introduction to Selenium IDE

Brief Introduction Selenium IDE
Selenium IDE is the simplest of the tools in the Selenium suite, and the quickest way to get started with creating scripts; however it only supports record and playback within Firefox. For automating other browsers you’ll need to use one of the more advanced Selenium tools, such as Selenium RC or Selenium 2 (or Selenium Grid if you want to run your tests in parallel), but let’s not get ahead of ourselves! 


Selenium Integrated Development Environment (IDE) is the simplest framework in the Selenium suite and is the easiest one to learn. It is a Firefox plugin that you can install as easily as you can with other plugins. However, because of its simplicity, Selenium IDE should only be used as a prototyping tool. If you want to create more advanced test cases, you will need to use either Selenium RC or WebDriver.


First, let’s install Selenium IDE, which is really just an add-on for Firefox.


The Selenium-IDE (Integrated Development Environment) is an easy-to-use Firefox plug-in to develop Selenium test cases. It provides a Graphical User Interface for recording user actions using Firefox which is used to learn and use Selenium, but it can only be used with Firefox browser as other browsers are not supported.
However, the recorded scripts can be converted into various programming languages supported by Selenium and the scripts can be executed on other browsers as well.
The following table lists the sections that we are going to cover in this chapter.
TitleDescription
This section deals with how to download and configure Selenium IDE.
This section deals with the features available in Selenium IDE.
This section deals with how to create IDE tests using recording feature.
This section deals with debugging the Selenium IDE script.
This section describes how to insert verification points in Selenium IDE.
This section deals with how to work with regular expressions using IDE.
The Java script that allows users to customize or add new functionality.
This section deals with how to execute Selenium IDE scripts on different browsers.

Selenium IDE - Different Browser

Selenium scripts can run tests only against Firefox as the tool IDE itself is a plugin of Firefox. Tests developed using Selenium IDE can be executed against other browsers by saving it as Selenium WebDriver or Selenium Remote Control Script.
Step 1 : Open any saved Test in Selenium IDE.
Step 2 : Navigate to "File" menu and select "Export Test Suite As" and the options would be listed.
Selenium IDE 28
Step 3 : Now let us export the script to "WebDriver" and save it with a name.
Step 4 : The saved WebDriver file is displayed as shown below.
Selenium IDE 29

Selenium - User Extensions

It is easy to extend Selenium IDE by adding customized actions, assertions, and locator-strategies. It is done with the help of JavaScript by adding methods to the Selenium object prototype. On startup, Selenium will automatically look through the methods on these prototypes, using name patterns to recognize which ones are actions, assertions, and locators.
Let us add a 'while' Loop in Selenium IDE with the help of JavaScript.
Step 1 : To add the js file, first navigate to https://github.com/darrenderidder/sideflow/blob/master/sideflow.js and copy the script and place save it as 'sideflow.js' in your local folder as shown below.
Selenium IDE 24
Step 2 : Now launch 'Selenium IDE' and navigate to "Options" >> "Options" as shown below.
Selenium IDE 25
Step 3 : Click the 'Browse' button under 'Selenium Core Extensions' area and point to the js file that we have saved in Step 1.
Selenium IDE 26
Step 4 : Restart Selenium IDE.
Step 5 : Now you will have access to a few more commands such as "Label", "While" etc.
Step 6 : Now we will be able to create a While loop within Selenium IDE and it will execute as shown below.
Selenium IDE 27


Selenium - IDE Pattern Matching

Like locators, patterns are a type of parameter frequently used by Selenium. It allows users to describe patterns with the help of special characters. Many a time, the text that we would like to verify are dynamic; in that case, pattern matching is very useful.
Pattern matching is used with all the verification point commands - verifyTextPresent, verifyTitle, verifyAlert, assertConfirmation, verifyText, and verifyPrompt.
There are three ways to define a pattern:
  • globbing
  • regular expressions, and
  • exact patterns.

Globbing

Most techies who have used file matching patterns in Linux or Windows while searching for a certain file type like *.doc or *.jpg. would be familiar with term "globbing"
Globbing in Selenium supports only three special characters: *, ?, and [ ].
  • * - matches any number of characters.
  • ? - matches a single character.
  • [ ] - called a character class, lets you match any single character found within the brackets. [0-9] matches any digit.
To specify a glob in a Selenium command, prefix the pattern with the keyword 'glob:'. For example, if you would like to search for the texts "tax year 2013" or "tax year 2014", then you can use the golb "tax year *" as shown below.
However the usage of "glob:" is optional while specifying a text pattern because globbing patterns are the default in Selenium.
CommandTargetValue
clickAndWaitlink=search
verifyTextPresentglob: tax year *

Exact Patterns

Patterns with the prefix 'exact:' will match the given text as it is. Let us say, the user wants an exact match with the value string, i.e., without the glob operator doing its work, one can use the 'exact' pattern as shown below. In this example the operator '*' will work as a normal character rather than a pattern-matching wildcard character.
CommandTargetValue
clickAndWaitlink=search
verifyValueexact: *.doc

Regular Expressions

Regular expressions are the most useful among the pattern matching techniques available. Selenium supports the complete set of regular expression patterns that Javascript supports. Hence the users are no longer limited by *, ? and [] globbing patterns.
To use RegEx patterns, we need to prefix with either "regexp:" or "regexpi:". The prefix "regexpi" is case-insensitive. The glob: and the exact: patterns are the subsets of the Regular Expression patterns. Everything that is done with glob: or exact: can be accomplished with the help of RegExp.

Example

For example, the following will test if an input field with the id 'name' contains the string 'tax year', 'Tax Year', or 'tax Year'.
CommandTargetValue
clickAndWaitlink=search
verifyValueid=nameregexp:[Tt]ax ([Yy]ear)

Selenium - IDE Verification Points

The test cases that we develop also need to check the properties of a web page. It requires assert and verify commands. There are two ways to insert verification points into the script.
To insert a verification point in recording mode, "Right click" on the element and choose "Show all Available Commands" as shown below.
selenium ide 21
We can also insert a command by performing a "Right-Click" and choosing "Insert New Command".
selenium ide 22
After inserting a new command, click 'Command' dropdown and select appropriate verification point from the available list of commands as shown below.
selenium ide 23
Given below are the mostly used verification commands that help us check if a particular step has passed or failed.
  • verifyElementPresent
  • assertElementPresent
  • verifyElementNotPresent
  • assertElementNotPresent
  • verifyText
  • assertText
  • verifyAttribute
  • assertAttribute
  • verifyChecked
  • assertChecked
  • verifyAlert
  • assertAlert
  • verifyTitle
  • assertTitle

Synchronization Points

During script execution, the application might respond based on server load, hence it is required for the application and script to be in sync. Given below are few a commands that we can use to ensure that the script and application are in sync.
  • waitForAlertNotPresent
  • waitForAlertPresent
  • waitForElementPresent
  • waitForElementNotPresent
  • waitForTextPresent
  • waitForTextNotPresent
  • waitForPageToLoad
  • waitForFrameToLoad

Selenium - IDE Debugging

Debugging is the process of finding and fixing errors in the test script. It is a common step in any script development. To make the process more robust, we can make use a plugin "Power Debugger" for Selenium IDE.
Step 1 : To install Power Debugger for Selenium IDE, navigate to https://addons.mozilla.org/en-US/firefox/addon/power-debugger-selenium-ide/ and click "Add to Firefox" as shown below.
Selenium IDE 16
Step 2 : Now launch 'Selenium IDE' and you will notice a new icon, "Pause on Fail" on recording toolbar as shown below. Click it to turn it ON. Upon clicking again, it would be turned "OFF".
Selenium IDE 17
Step 3 : Users can turn "pause on fail" on or off any time even when the test is running.
Step 4 : Once the test case pauses due to a failed step, you can use the resume/step buttons to continue the test execution. The execution will NOT be paused if the failure is on the last command of any test case.
Step 5 : We can also use breakpoints to understand what exactly happens during the step. To insert a breakpoint on a particular step, "Right Click" and select "Toggle Breakpoint" from the context-sensitive menu.
Selenium IDE 18
Step 6 : Upon inserting the breakpoint, the particular step is displayed with a pause icon as shown below.
Selenium IDE 19
Step 7 : When we execute the script, the script execution is paused where the breakpoint is inserted. This will help the user to evaluate the value/presence of an element when the execution is in progress.
Selenium IDE 20

Selenium - IDE Test Creation

The following steps are involved in creating Selenium tests using IDE:
  • Recording and adding commands in a test
  • Saving the recorded test
  • Saving the test suite
  • Executing the recorded test

Recording and Adding Commands in a Test

We will use www.ncalculators.com to demonstrate the features of Selenium.
Step 1 : Launch the Firefox browser and navigate to the website - http://www.ncalculators.com/
Step 2 : Open Selenium IDE from the Tools menu and press the record button that is on the top-right corner.
Selenium IDE 7
Step 3 : Navigate to "Math Calculator" >> "Percent Calculator >> enter "10" as number1 and 50 as number2 and click "calculate".
Selenium IDE 7
Step 4 : User can then insert a checkpoint by right clicking on the webelement and select "Show all available commands" >> select "assert text css=b 5"
Selenium IDE 8
Step 5 : The recorded script is generated and the script is displayed as shown below.
Selenium IDE 9

Saving the Recorded Test

Step 1 : Save the Test Case by navigating to "File" >> "Save Test" and save the file in the location of your choice. The file is saved as .HTML as default.
The test can also be saved with an extension htm, shtml, and xhtml.
Selenium IDE 9

Saving the Test Suite

A test suite is a collection of tests that can be executed as a single entity.
Step 1 : Create a test suite by navigating to "File" >> "New Test Suite" as shown below.
Selenium IDE 13
Step 2 : The tests can be recorded one by one by choosing the option "New Test Case" from the "File" Menu.
Step 3 : The individual tests are saved with a name along with saving a "Test Suite".
Selenium IDE 14

Executing the Recorded Test

The recorded scripts can then be executed either by clicking "Play entire suite" or "Play current test" button in the toolbar.
Step 1 : The Run status can be seen in the status pane that displays the number of tests passed and failed.
Step 2 : Once a step is executed, the user can see the result in the "Log" Pane.
Step 3 : After executing each step, the background of the test step turns "Green" if passed and "Red" if failed as shown below.
Selenium IDE 15

Selenium - IDE Tool Features

The following image shows the features of Selenium IDE with the help of a simple tool-tip.
Selenium IDE 4
The features of the record tool bar are explained below.
Selenium IDE 11

Selenium - IDE Download

Step 1 : Launch Firefox and navigate to the following URL- http://seleniumhq.org/download/.
Under the Selenium IDE section, click on the link that shows the current version number as shown below.
Selenium IDE 1
Step 2 : Firefox add-ons notifier pops up with allow and disallow options. User has to allow the installation.
Selenium IDE 2
Step 3 : The add-ons installer warns the user about untrusted add-ons. Click 'Install Now'.
Selenium IDE 3
Step 4 : The Selenium IDE can now be accessed by navigating to Tools >>Selenium IDE.
Selenium IDE 5
Step 5 : The Selenium IDE can also be accessed directly from the quick access menu bar as shown below.
Selenium IDE 4

Java Basic Operators

Java Basic Operators


Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:
  • Arithmetic Operators
  • Relational Operators
  • Bitwise Operators
  • Logical Operators
  • Assignment Operators
  • Misc Operators

The Arithmetic Operators:

Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra. The following table lists the arithmetic operators:
Assume integer variable A holds 10 and variable B holds 20, then:
OperatorDescriptionExample
+Addition - Adds values on either side of the operatorA + B will give 30
-Subtraction - Subtracts right hand operand from left hand operandA - B will give -10
*Multiplication - Multiplies values on either side of the operatorA * B will give 200
/Division - Divides left hand operand by right hand operandB / A will give 2
%Modulus - Divides left hand operand by right hand operand and returns remainderB % A will give 0
++Increment - Increases the value of operand by 1B++ gives 21
--Decrement - Decreases the value of operand by 1B-- gives 19

The Relational Operators:

There are following relational operators supported by Java language
Assume variable A holds 10 and variable B holds 20, then:
OperatorDescriptionExample
==Checks if the values of two operands are equal or not, if yes then condition becomes true.(A == B) is not true.
!=Checks if the values of two operands are equal or not, if values are not equal then condition becomes true.(A != B) is true.
>Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.(A > B) is not true.
<Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.(A < B) is true.
>=Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.(A >= B) is not true.
<=Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.(A <= B) is true.

The Bitwise Operators:

Java defines several bitwise operators, which can be applied to the integer types, long, int, short, char, and byte.
Bitwise operator works on bits and performs bit-by-bit operation. Assume if a = 60; and b = 13; now in binary format they will be as follows:
a = 0011 1100
b = 0000 1101
-----------------
a&b = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a  = 1100 0011
The following table lists the bitwise operators:
Assume integer variable A holds 60 and variable B holds 13 then:
OperatorDescriptionExample
&Binary AND Operator copies a bit to the result if it exists in both operands.(A & B) will give 12 which is 0000 1100
|Binary OR Operator copies a bit if it exists in either operand.(A | B) will give 61 which is 0011 1101
^Binary XOR Operator copies the bit if it is set in one operand but not both.(A ^ B) will give 49 which is 0011 0001
~Binary Ones Complement Operator is unary and has the effect of 'flipping' bits.(~A ) will give -61 which is 1100 0011 in 2's complement form due to a signed binary number.
<<Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.A << 2 will give 240 which is 1111 0000
>>Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.A >> 2 will give 15 which is 1111
>>>Shift right zero fill operator. The left operands value is moved right by the number of bits specified by the right operand and shifted values are filled up with zeros.A >>>2 will give 15 which is 0000 1111

The Logical Operators:

The following table lists the logical operators:
Assume Boolean variables A holds true and variable B holds false, then:
OperatorDescriptionExample
&&Called Logical AND operator. If both the operands are non-zero, then the condition becomes true.(A && B) is false.
||Called Logical OR Operator. If any of the two operands are non-zero, then the condition becomes true.(A || B) is true.
!Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.!(A && B) is true.

The Assignment Operators:

There are following assignment operators supported by Java language:
OperatorDescriptionExample
=Simple assignment operator, Assigns values from right side operands to left side operandC = A + B will assign value of A + B into C
+=Add AND assignment operator, It adds right operand to the left operand and assign the result to left operandC += A is equivalent to C = C + A
-=Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operandC -= A is equivalent to C = C - A
*=Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operandC *= A is equivalent to C = C * A
/=Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operandC /= A is equivalent to C = C / A
%=Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operandC %= A is equivalent to C = C % A
<<=Left shift AND assignment operatorC <<= 2 is same as C = C << 2
>>=Right shift AND assignment operatorC >>= 2 is same as C = C >> 2
&=Bitwise AND assignment operatorC &= 2 is same as C = C & 2
^=bitwise exclusive OR and assignment operatorC ^= 2 is same as C = C ^ 2
|=bitwise inclusive OR and assignment operatorC |= 2 is same as C = C | 2

Misc Operators

There are few other operators supported by Java Language.

Conditional Operator ( ? : ):

Conditional operator is also known as the ternary operator. This operator consists of three operands and is used to evaluate Boolean expressions. The goal of the operator is to decide which value should be assigned to the variable. The operator is written as:
variable x = (expression) ? value if true : value if false
Following is the example:
public class Test {

   public static void main(String args[]){
      int a , b;
      a = 10;
      b = (a == 1) ? 20: 30;
      System.out.println( "Value of b is : " +  b );

      b = (a == 10) ? 20: 30;
      System.out.println( "Value of b is : " + b );
   }
}
This would produce the following result:
Value of b is : 30
Value of b is : 20

instanceof Operator:

This operator is used only for object reference variables. The operator checks whether the object is of a particular type(class type or interface type). instanceof operator is wriiten as:
( Object reference variable ) instanceof  (class/interface type)
If the object referred by the variable on the left side of the operator passes the IS-A check for the class/interface type on the right side, then the result will be true. Following is the example:
public class Test {

   public static void main(String args[]){
      String name = "James";
      // following will return true since name is type of String
      boolean result = name instanceof String;  
      System.out.println( result );
   }
}
This would produce the following result:
true
This operator will still return true if the object being compared is the assignment compatible with the type on the right. Following is one more example:
class Vehicle {}

public class Car extends Vehicle {
   public static void main(String args[]){
      Vehicle a = new Car();
      boolean result =  a instanceof Car;
      System.out.println( result );
   }
}
This would produce the following result:
true

Precedence of Java Operators:

Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator:
For example, x = 7 + 3 * 2; here x is assigned 13, not 20 because operator * has higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7.
Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first.
Category Operator Associativity 
Postfix () [] . (dot operator)Left to right 
Unary ++ - - ! ~Right to left 
Multiplicative  * / % Left to right 
Additive  + - Left to right 
Shift  >> >>> <<  Left to right 
Relational  > >= < <=  Left to right 
Equality  == != Left to right 
Bitwise AND Left to right 
Bitwise XOR Left to right 
Bitwise OR Left to right 
Logical AND && Left to right 
Logical OR || Left to right 
Conditional ?: Right to left 
Assignment = += -= *= /= %= >>= <<= &= ^= |= Right to left 
Comma Left to right