Selenium Tutorials

If you are a beginner, please Proceed here to start from the beginning.

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:

Navigation Methods in Webdriver with Examples

Navigate.To(URL)

Method Name: navigate.to(URL)
Syntax: driver.navigate().to(URL);
Purpose: This methods Load a new web page in the current browser window. This is done using an HTTP GET operation, and the method will block until the load is complete.
Parameters: URL – It should be a fully qualified URL.

Example:

Difference between Webdriver get() and Webdriver navigate()

driver.get("URL")
The first thing you’ll want to do with WebDriver is navigate to a page. The normal way to do this is by calling get:

Syntax: get(java.lang.String url) url - The URL to load. It is always best to use a fully qualified URL

Example: driver.get("http://www.google.com"); which loads a new web page in the current browser window.

StaleElementReference Exceptions in Selenium Webdriver

This Exception occurs when driver is trying to perform action on the element which is no longer exists or not valid.

WebElement ele = driver.findElement(By.id("sample"));
// Before clicking some thing happened and DOM has changed due to page refresh, or element is removed and re-added
ele.click();

Now at this point, the element which you're clicking is no longer valid.

so it throws up its hands and gives control to user, who as the test/app author should know exactly what may or may not happen.

Mouse Hover Actions in Selenium Webdriver

In order to perform a 'mouse hover' action, we need to chain all of the actions that we want to achieve in one go. So move to the element that which has sub elements and click on the child item. It should the same way what we do normally to click on a sub menu item.

With the actions object you should first move the menu title, and then move to the sub menu item and click it.

First we need to create new action builder instance by passing the webdriver instance, then.

Below is the sample code to perform Mouse hover action

Example 1: