Selenium Tutorials

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:

Importance of Automation Testing

Companies not only want to test software adequately, but also as quickly and thoroughly as possible. To accomplish this goal, organizations are turning to automated testing.

To increase the test coverage

Reduces the need for manual testing and discovers defects manual testing cannot expose and also manual testing is error prone and a time consuming process.

Running the tests again and again gives us the confidence that the new work we added to the system did not break the code that used to work and also to make sure that the changes we introduced are working.

Why Automation Testing is required?

Automated software testing is the best way to increase the effectiveness, efficiency and coverage of your software testing. Every software development group tests its products, yet delivered software always has defects.

Test engineers strive to catch them before the product is released but they always creep in and they often reappear, even with the best manual testing processes.

Defining Firefox Profile preferences using Selenium Webdriver

Some times, you may need to update the preferences within Firefox. We can do this by instantiating a Firefox Profile object and then update the settings.

We will then need to pass this object into FirefoxDriver which will load the profile with your defined settings.

You can check them using command "about:config" in the borwser. It will display a warning message and then asks you to proceed if required. Becarefull when doing any changes.

How to handle javascript alerts, confirmation and prompts?

Generally JavaScript popups are generated by web application and hence they can be easily controlled by the browser.

Webdriver offers the ability to cope with javascript alerts using Alerts APIClick here to view Alert API Details

// Get a handle to the open alert, prompt or confirmation
Alert alert = driver.switchTo().alert();

Alert is an interface. There below are the methods that are used

//Will Click on OK button.
alert.accept();