Selenium Tutorials

Selenium Automation Framework Example

Introduction to Automation testing:

Testing is an essential part of a software development process. While testing intermediate versions of products/projects being developed, testing team needs to execute a number of test cases. In addition, prior to release every new version, it is mandatory that the version is passed through a set of “regression” and “smoke” tests. Most of all such tests are standard for every new version of product/project, and therefore can be automated in order to save human resources and time for executing them.

Set browser width and height in Selenium Webdriver

Selenium WebDriver allows resizing and maximizing window natively from its API. We use 'Dimension' class to resize the window.

Lets take the help of Selenium WebDrivers Dimension Class and declare object say 'd' by initializing it with width and height as 420X600 as shown below:

Remember, We need to import the statement 'import org.openqa.selenium.Dimension'

Handling keyboard events and mouse hover events using Webdriver

In Webdriver, handling keyboard events and mouse events (including actions such as Drag and Drop or clicking multiple elements With Control key) are done using the advanced user interactions API . It contains Actions and Action classes which are needed when performing these events.
In order to perform action events, we need to use org.openqa.selenium.interactions.Actions class.

Here is the sample code to work with Action Class

Webdriver Select with Multiple Attribute

WebDriver’s support classes called “Select”, which provides useful methods for interacting with select options. User can perform operations on a select dropdown and also de-select operation using the below methods.

User can also get the text of the values in the dropdown. Also can get the option which are selected by the user. To use these options, the Select Tag should have "multiple" attribute.

Webdriver SELECT Methods to work with Dropdowns

WebDriver’s support classes called “Select”, which provides useful methods for interacting with select options. User can perform operations on a select dropdown and also de-select operation using the below methods.

Method Name: selectByIndex 

Syntax: select.selectByIndex(Index);
Purpose:  To Select the option based on the index given by the user.
There is an attribute called "values" which will have the index values.

The below is the sample html code using index
Example
HTML Code

Taking Screenshot using Webdriver

Its very important to take screenshot when we execute a test script. When we execute huge number of test scripts, and if some test fails, we need to check why the test has failed.

It helps us to debug and identify the problem by seeing the screen shot.

In selenium webdriver, we can take the screen shot using the below command.
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

WebDriver Methods

We will look in detail for each of the webdriver methods that we have. The first that we use is 'driver.get(url)' after starting the browser.

When we get the driver object, the below are the methods that we can perform operation on a driver. In IDE like eclipse, when we add period (. ) you will show all the below methods.
get()
getCurrentUrl();
getTitle()
findElements()
findElement()
getPageSource()
close()
quit()

How to delete Cookies in Selenium Webdriver

Delete Cookie
Delete Cookie with Name
Delete All Cookies

User can delete a cookie from the browser's "cookie jar". The domain of the cookie will be ignored.

User can delete the named cookie from the current domain. This is equivalent to setting the named cookie's expiry date to sometime in the past.

User can also delete all the cookies for the current domain using driver.manage().deleteAllCookies();

Example:

Deleting the specific cookie with cookie name "--utmb"

How to addcookie with Selenium Webdriver

Using webdriver we can easily pass the cookie to the domain. In order to pass cookie, we should use a method named "addCookie(cookie)"

Method Name: addCookie(Cookie cookie)
Syntax:driver.manage().addCookie(arg0);
Purpose: To add a specific cookie into cookies. If the cookie's domain name is left blank, it is assumed that the cookie is meant for the domain of the current document.
Parameters: cookie - The name and value of the cookie to be add.

Example: