Selenium IDE

Selenium IDE is an record and playback tool.Selenium is is the plugin for Firefox Browser Extension. It saves time for the tester from coding.The simple record and run is very familiar with novice user. By only clicking the record button “Selenium IDE” starts recording the activities and write the command by itself.Novice without heaving any programming language can easily understand and interact with Selenium IDE.

The Feature of Selenium IDE

Very easy record and playback the recording.
Auto select the web element in according with complicity,It take IDx ,names, or Xpaths as needed.
Auto complete all selenium commands.
Very easy for tester to walk through tests.
Debug and set break point.
It support Selenium user-extensions.js file.
We can save the test as HTML ,Ruby Scripts or any other formats.
We can set assert the title of a page.
Easy customize through plugins.

Selenium IDE- Menu Bar

Through menu bar we can do operation in file,Options and menus.

Selenium IDE- File Menu

We can create open save and close the tests.
All the tests are saved in html format.
Useful feature of selenium IDE is we can export the tests into our suitable language.

We can export the Selenium IDE test cases to the following source code formats.

.cs(C#)
.java(Java)
.py(Python)
.rb(Ruby)

 

Selenium IDE-Edit Menu
As usual like other menu we can also Undo, Redo, Cut, Copy, Paste, Delete, and Select All.
Excluding this their are two more commands where we can “Insert New Command” and “Insert New Comment“.

The inserted command or comment will be placed on the top of the current selected line in Selenium IDE.

Introduction to Selenium IDE

Selenium IDE-Option Menu

Option menu is to provide various Clipboard format option.
HTML is the default selection.

Selenium IDE Option Dialog Box

Selenium IDE use 3 steps to write script
Command
Target
Values

What is web element-Any element present on the webpage known as web element .

Example of Selenium IDE Script

COMMAND                             TARGET                             VALUES
Open                                          http://facebook.com
SendKeys                                  name=usename                 adminSendKeys                                  name=password                pass123
Click&Wait                                id=LoginButton

FireBug-Firebug is used to identify or view the html source code of a web element present in the web page.

Batch Execution-Collection of multiple test cases is known as batch and executing multiple test cases in a single click is known as batch execution.

Test Suite-Location where we are storing multiple test cases.

Steps to create Test Suite-
Navigate to the file click on new test suite
Right click on the test suite click on new test cases
Select the test case and click on open like this we can add multiple test cases
Click on play entire test suite.
Advantages of Selenium IDE

1.Record and playback tools.
2.Easy to write & execute the script.

Disadvantages of Selenium IDE
1.Support only html language .
2.It supports only firefox browser.
3.It does not support for loop and if condition.
4.Selenium IDE works with open browser.
5.It will not open lunch new browser to run the test script.

How to Identify Static And Dynamic Object in Selenium

Finding the location of the element is the main and important task in selenium to find out the element we need Object Identification in Selenium by the different properties.

Target/Locator
All the element present on the wave page will be identified using target or locator.Target or locator are used to identify the element present in the web page.

In selenium we can identify the object through following attribute.
id
Id is the attribute attached to the html web element
for each element the id associated with the element is different.To find a element through target or locator id is the most unique way to locate the element.
name
Name might be same for different element so to identify the element through name sometimes makes difficulty.
CSS
To identify the element through CSS we might find difficulties because of same CSS might be used in the different location in the webpage.
XPATH
XPATH is the most convenient way to locate the target or locator.Whatever the situation is we can find the element by XPATH very uniquely.
LinkText()

Partial Link Text()
Class Name()
tagname()

 

If you have any further clarification on this topic please give comment below How to Identify Static And Dynamic Object in Selenium

How to find the XPATH of an element to automate the element

XPATH is a way to identify the element by navigating the entire html source code and identify the element using back-end attribute.

Syntax of XPATH

// To go to entire html source code

/   Navigating from parent to child

[ ]  Attribute Declaration

[ ]   Navigating from child to parent

@   Attribute annonation

We can give the xpath of an element either by absolute or relative.

Absolute XPATH
Whenever the XPATH is written from parent to child it is known as absolute XPATH. In real time we will not move for absolute XPATH because if the requirement got changed  or location of the element got changed it will fail to identify the element.

Relative XPATH
//htmltag[@attribute=’value’]

Whenever the xpath is written by using back end attribute of the element it is known as relative xpath.

Using one attribute when we will get multiple matching node we will take help of other attribute present in the html source code.

Sometime using attribute it fails to identify the element so we will go and take of parent and first we will identify the parent and then navigate to its child.

Function in XPATH

text()

Syntax- //html tag[text()=’value’]
eg- //h[text()=’username’]

These are the advance function to write the xpath whenever their is no attribute given in html source code only visible text is their we will go for text().

contains()

It is used to handle the dynamic element present on the web page.
Generally is used to handle ajax application.
Ajax is a java framework which is used to develop dynamic element.

Syntax// htmltag[contains(test(),’value’)]

eg-//a[contains(@title,’com’)]

With the help of part of a string we will write the XPATH to handle dynamic element.
Contains function automatically remove the space present before the starting and after the string.

normalize-space

It is used to remove the space present before the string and after the string present  in visible test or attribute value and compare the string with string  and return the .It is the supporting function of text function.
Syntax//htmltag[normalize-space(text()/@attribute)=’value’]
eg//td[normalize-space(text())=’Open Task’]

preceding sibling

It is used to identify the previous element of a web element .

When to use the following or preceding sibling

When the element attribute is dynamic and we are not able to identify the element using attribute we go for following or preceding sibling.

following sibling

Following sibling is used to identify the next element of a web element.

descendant

We use descendant to skip some html tag.

ancestor

Ancestor lets you select any ancestors [e.g., Parent and Grandparent] of the current node.
//*[@id=’regular-expression-syntax’]/ancestor::div[5]/div[2]
//*[@id=’regular-expression-syntax’]/ancestor::*

 ancestor-or-self

Ancestor-or-self lets you select any ancestors [e.g., Parent and Grandparent] of the current                      node including the current node.
//*[@id=’regular-expression-syntax’]/ancestor-or-self::div[1]
//*[@id=’regular-expression-syntax’]/ancestor-or-self::*

attribute

Attribute returns all the attributes in the current node.
//*[@class=’sphinxsidebarwrapper’]/attribute::*
//*[@id=’sidebarbutton’]/attribute::title

child

Child returns all the children in the current node.
//*[@class=’sphinxsidebarwrapper’]/child::*
//*//child::h3

 following

Following returns all in the document after the closing tag of the current node.
//*[@class=’clearer’]/following::*

 namespace

Namespace returns all namespace nodes in the current node.
TBD

parent

Parent returns the parent of the current node.
//*[@class=’sphinxsidebar’]/parent::node()
//*[@class=’bodywrapper’]/parent::*
//*[@class=’bodywrapper’]/parent::div[1]
//*[@class=’bodywrapper’]/parent::div

preceding

Preceding is a reverse of Following;  Preceding returns all in the document before the current node
//*[@class=’bodywrapper’]/preceding::*

self

Self returns the current node.
//*[@class=’footer’]/self::*
//*[@class=’footer’]/self::div

Selenium WebDriver

WebDriver is an interface present in selenium jar file which consists of some browser specific method to perform  operation on browser.

Advantages

Supports multiple browser
Support secure network
Supports secure network
Supports multiple OS.
Supports http protocol.

Disadvantages

Does not supports windows application.
Selenium web driver consists of jar files jar files consists of core java classes and interface.

Following are the methods are available in the webdriver

1.get()

driver.get() method is used to open an website in the webdriver.It takes argument as string in website link format.

2.findelement()

driver.findelement() method used to find the location of an element in side the webpage.

3.findelements()

driver.findelements() method used t used to get all the elements ts present in the webpage.

4.navigate().to()

driver.navigate().to()  Takes webpage  as string and open through the driver.

driver.navigate().back() Click the back button of the browser.

driver.navigate().forward() Click the forward button of the browser.

driver.navigate().refresh() Cllick the refresh page of the browser.

5.getcurrenturl()

Returns current url as a string.

6.getpagesource()

7.switchto
window()
frame()
8.windows maximize()
9,manage
maximize()
delete cookies()
10.getwindowhandles()
11.close()
12.quit()
13.gettitle()

1.get()

Get method is used to pass the url in the browser.

2.findelemnet()

Used to identify the single element present in the UI.

3.navigate.to()

It is used to pass the url

4.gettitle()

Method is used to capture the title of the current web page.

5.getCurretnUrl()

It is used to capture th current url.

6.getPageSource()

Method is used to get the html source code of current web page.

7.Manage()

When webdriver open an webpage sometimes it open in minimize mode.This method is used to maximize the windows lunch by selenium webdriver.

8.manage().DeleteCookies()

To delete all cookies present in browser.

9.driver.close()

It will close the current driver windows lunch by selenium webdriver.

10.Quit()

It will close all the windows by selenium driver.

To run selenium in firefox browser check the following post.
1.Selenium gecko driver setup to run Firefox in Ubuntu or Windows

Methods of the Selenium Web Element

Any element present on the web page is known as web element.Using find element we can identify element present in the web page.Once it is identified we need to perform operation on web element .Web element is a interface which contains some specific method  to perform operation on web element.

The following method are available in selenium web element.

click()
clear()
sendKeys()getText()
isDisplayed()
isSelected()
isEnabled()
getTagname()
getcssValue()

We can get following css value of an web element color,font,size.
getattribute()
getlocation()

This method will find the location  value of an element in x and y axis.
getcssvalue()
getAttiribute()

It is used to find out back end attribute of any web element.

Selenium Actions class By the help of Webdriver to Perform User Actions

 Selenium Actions present in Selenium WebDriver class which is used to perform  user interaction operations.Like mouse mouse click ,double click drag and drop.Many a time we found that these operation are needed by Selenium WebDriver.To use this action class first we need to transfer the driver control to this class and then to perform action operation.

To create an object of action class we need to create an object of action class for creating an object the following code be written

Actions act=new Actions(driver);

To perform operation on an Web Element we needs to do user defined operations

    public static void main(String ar[]) {
        System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        driver.get("http://google.co.in");
        Actions act = new Actions(driver);
        WebElement we = driver.findElement(By.xpath(".//*[@id='hplogo']"));
        act.moveToElement(we).perform();
        act.contextClick(we).perform();
    }

NOTE-It will show the exception in Selenium 3 instead you can use Java Robot Class.

Modifier and Type Method and Description
Action build()

Generates a composite action containing all actions so far, ready to be performed (and resets the internal builder state, so subsequent calls to build() will contain fresh sequences).

Actions click()

Clicks at the current mouse location.

Actions click(WebElement onElement)

Clicks in the middle of the given element.

Actions clickAndHold()

Clicks (without releasing) at the current mouse location.

Actions clickAndHold(WebElement onElement)

Clicks (without releasing) in the middle of the given element.

Actions contextClick()

Performs a context-click at the current mouse location.

Actions contextClick(WebElement onElement)

Performs a context-click at middle of the given element.

Actions doubleClick()

Performs a double-click at the current mouse location.

Actions doubleClick(WebElement onElement)

Performs a double-click at middle of the given element.

Actions dragAndDrop(WebElement source, WebElement target)

A convenience method that performs click-and-hold at the location of the source element, moves to the location of the target element, then releases the mouse.

Actions dragAndDropBy(WebElement source, int xOffset, int yOffset)

A convenience method that performs click-and-hold at the location of the source element, moves by a given offset, then releases the mouse.

Actions keyDown(Keys theKey)

Performs a modifier key press.

Actions keyDown(WebElement element, Keys theKey)

Performs a modifier key press after focusing on an element.

Actions keyUp(Keys theKey)

Performs a modifier key release.

Actions keyUp(WebElement element, Keys theKey)

Performs a modifier key release after focusing on an element.

Actions moveByOffset(int xOffset, int yOffset)

Moves the mouse from its current position (or 0,0) by the given offset.

Actions moveToElement(WebElement toElement)

Moves the mouse to the middle of the element.

Actions moveToElement(WebElement toElement, int xOffset, int yOffset)

Moves the mouse to an offset from the top-left corner of the element.

Actions pause(long pause)

Deprecated.

‘Pause’ is considered to be a bad design practice.

void perform()

A convenience method for performing the actions without calling build() first.

Actions release()

Releases the depressed left mouse button at the current mouse location.

Actions release(WebElement onElement)

Releases the depressed left mouse button, in the middle of the given element.

Actions sendKeys(java.lang.CharSequence… keysToSend)

Sends keys to the active element.

Actions sendKeys(WebElement element, java.lang.CharSequence… keysToSend)

Equivalent to calling: Actions.click(element).sendKeys(keysToSend). This method is different from WebElement.sendKeys(CharSequence…) – see sendKeys(CharSequence…) for details how.

dsdsd

Selenium gecko driver setup to run Firefox in Ubuntu or Windows

As Selenium is cross platform supported we can use it for the different environment as per our need .In this tutorial we will explain how to run the Firefox Browser Using Selenium.

Step:1

Download the gicko driver from the below linnk for your system environment.
Download the gecko driver from the following link.

Step:2

Extract the geckodriver from the zip and place in your local directory.
After placing the gickodriver everything is ready.Only we need to write the code in the editor to open the browser.

For Java

Copy and paste the code after importing  the external jar files of java selenium web driver.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
public class MyClass {
public static void main(String[] args) {
//Create a new instance of Firefox Browser
System.setProperty(“webdriver.gecko.driver”,”/home/shakti/Downloads/geckodriver”);

WebDriver driver = new FirefoxDriver();

}
}