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.
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.
The What
I have a bunch of tests that i would like to run faster, by making them execute in parallel. In my tests:
Continue reading Quick Tip: Running automated tests in parallel
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:
The task is to easily select / deselect an element from the dropdwon.
Continue reading Quick Tip: Selenium – select / deselect a value from a dropdown
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
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
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.
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:
Cookie cookie = new Cookie("testCookie", "testValue");
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:
driver.manage().getCookies();
You can save these values in a set, to use them later on:
Set<Cookie> cookies = driver.manage().getCookies();
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");
driver.manage().deleteAllCookies();
driver.manage().deleteCookieNamed("cookieName");
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.

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
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
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.
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