Tag Archives: selenium

Quick Tip: Selenium – Taking screenshots (of regular tests and test failures)

The What

During Selenium test execution, i want to store a screenshot of what is displayed in the browser to understand in what conditions the test ran.

Continue reading Quick Tip: Selenium – Taking screenshots (of regular tests and test failures)

Quick Tip: Running automated tests in parallel

The What

I have a bunch of tests that i would like to run faster, by making them execute in parallel. In my tests:

  1. I am not using a DataProvider and only want to make the same test run several times.
  2. I am using a DataProvider and want my test to run with the provided values from the provider, but in parallel.
  3. I am not using DataProviders, but my tests are ran by using the textng.xml file that specifies which tests to run (as per this article).

Continue reading Quick Tip: Running automated tests in parallel

Quick Tip: Selenium – select / deselect a value from a dropdown

The What

Having an HTML dropdown on a web page, i would like to select, via Selenium, an element from it, or deselect the selected one.

An example of an HTML representation of a dropdown can be found below – it displays a list of winter months:

<select id="winter">
 <option value="Dec">December</option>
 <option value="Jan">January</option>
 <option value="Feb">February</option>
 </select>

This element would look something like:

dropdown

The task is to easily select / deselect an element from the dropdwon.

The How

Continue reading Quick Tip: Selenium – select / deselect a value from a dropdown

Selenium tests, the Object Oriented Way

Some while ago i published an article in a local magazine on the topic of writing Selenium tests in an Object Oriented fashion.  As i believe this is a very useful way of reducing the number of asserts tests can include, and to expand to a bigger audience, here is a different approach to writing Selenium tests.

This approach is recommended when you have, for example, a big module whose properties you must compare to the expected ones, after performing an action or when it comes to testing the translation of the page. Continue reading Selenium tests, the Object Oriented Way

Page navigation with Selenium

While running tests with Selenium, you might want to navigate between the pages that are opening, without knowing or caring about the URL that is assigned to them. You would like to use the browser’s back and forward functionality, to just go back one page from the current one, or forward to the next page. You might also want to refresh the current page, without performing a driver.get(“someURL”). Continue reading Page navigation with Selenium

Working with cookies in Selenium

What are cookies

Cookies are text files stored on your computer, that contain information sites need to remember of your visit on them, like some of your preferences that the sites will persist while browsing their pages (for example: the language you are viewing the site in, the username you logged in with, the contents of your shopping cart, and so on). Cookies are used by your browser at the time you access the site. Each cookie has a name and a value, but also a scope, determined by the domain and path properties of the cookie (these are used to determine the site they belong to). A website can only set cookies valid on its’ domain and subdomains, not domains that do not belong to it. An expiry date can also be assigned to the cookie.
There are several types of cookies: session cookies are assigned to a website and are only valid while the site is being accessed. After leaving the site or closing the browser, these objects are deleted. Also, persistent cookies are used, those cookies that have an expiration date and that are valid beyond closing the browser.

Cookie objects supported by WebDriver

In the org.openqa.selenium module from the selenium-api library, you will find a class that emulates cookie objects, named of course, Cookie. A Cookie object can at most have the following attributes, as defined in this class: name; value; path; domain; expiry; isSecure. A Cookie object must have at least two attributes, namely name and value.
The Cookie object offers several constructors to build your cookie object, having the need to pass in the following parameters:

  • Minimal constructor with parameters: name, value. Example of creating such a Cookie object:
    Cookie cookie = new Cookie("testCookie", "testValue");
    
  • name, value, path
  • name, value, path, expiry
  • name, value, path, expiry, domain
  • name, value, path, expiry, domain, isSecure

Working with cookies in WebDriver

All methods related to cookies in WebDriver are accessible from the Options interface. First you must declare a variable for the webdriver, for example:

private WebDriver driver;

Then, you can use the cookie methods as described below:

  • Get all cookies: After opening a page whose cookies you want to find, use:
    driver.manage().getCookies();

    You can save these values in a set, to use them later on:

    Set<Cookie> cookies = driver.manage().getCookies();
  • Get a certain cookie: When you know the name of the cookie whose properties you want to get, you can use:
    driver.manage().getCookieNamed("cookieName");

    Storing the properties of this cookie is done by assigning the result of the above method to a Cookie object:

    Cookie theCookie = driver.manage().getCookieNamed("cookieName");
    
  • Deleting all the cookies on a page is done easily, by using:
    driver.manage().deleteAllCookies();
  • Deleting only a particular cookie, whose name you know, is done by using:
       driver.manage().deleteCookieNamed("cookieName");
  • Adding a new cookie: To add a new cookie to the browser, first you must create a Cookie object with all its’ needed properties. Afterwards, just use the following method:
    driver.manage().addCookie(cookieName);

    After adding the cookie, for it to be applied to the page you opened, a page refresh must be done right after adding it.

    Tip: In case you are not sure how to add the ‘domain’ property to a cookie, but you know what page it must be active on, you can: initially open that page; delete the cookie from the page, if it exists; create the Cookie object without setting the ‘domain’ property; add the cookie to the already opened page; refresh the page. The domain will be added automatically.

  • Editing cookies: You cannot directly edit the properties of a cookie. Instead, what you can do is: delete the existing cookie from the browser; create a new Cookie variable to reflect the new values you want for its’ properties ; add the new cookie to the browser; refresh the page.

Waiting for UI events


When running Selenium tests, in many situations one would like to wait to for some event to take place, before performing an action. For example, after clicking a button, one would need to wait for an element to be displayed until the test can use that element (the element being displayed is the event, using it is the action). Continue reading Waiting for UI events

Run tests on multiple browsers

Running tests on multiple browsers helps ensure that the behavior and look of your application is consistent for all your users. Selenium offers the possibility to use most common browsers to run your tests against. However, if your application needs to run also on mobile devices, from within their browsers (not from within native applications that is), there is an easy way to simulate the mobile devices your users would open your website on. You can use Chrome ‘s user agent capabilities. Also, you can run tests on your regular browsers by having some extensions active in the browser session (you might need some additional actions to be performed with the help of these extensions). Continue reading Run tests on multiple browsers

Creating the page objects

What is a page object

Simply put, a page object is an object that Selenium uses as a representation of an HTML element. Selenium tests will not interact with HTML code directly, but with objects that use selectors to refer to particular bits of the HTML code.

Defining page objects

You will need to create independent classes for declaring your page objects.  Such a class should group together all the page objects that belong to the same page, or to the same module that is about to be tested. They should be grouped logically and naturally. The tests and page objects should not belong to the same Java class (they should be independent one of the other). This has a great number of advantages, amongst them: avoiding redundant code (having a page object in only one place), availability of a page object to every test class that needs it (every test class that needs an object will access it from the same location), changing of the selector will be done in only one place if the HTML code changes.

A recommended way of declaring a page object is presented here: Continue reading Creating the page objects