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
I’m thinking you should, in no particular order…
Continue reading A few developer principles that testers should follow
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
Writing proper code in your IDE is made easier when using IntelliJ, due to its’ code analysis features. One option to perform a comprehensive coding issue search is to use the Inspect Code feature IntelliJ provides. From the class you want to check for issues, right click (inside the class, in the editor) and choose Analyze –> Inspect Code. Continue reading Improving your code by using IntelliJ’s Inspections feature
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 you need to run the same test a number of times, by changing only a few variable values, instead of writing several identical tests, you can use the dataProvider functionality offered by TestNG. What you have to do is declare a dataProvider method that returns a list of lists of objects, pass it to the test method, change the test method’s signature to correspond to the types of objects found in each inner list and run the test. Continue reading DataProviders for TestNG tests

When running TestNG tests, one could want to perform some common actions – after each test has finished successfully, after each failed test, after each skipped test, or after all the tests have finished running, no matter their result. To apply such a common behavior to a group of tests, a custom listener can be created, that implements TestNG’s ITestListener interface. There are two types of methods to implement: a set of them are related to a single test’s run (onTestStart, onTestSuccess, onTestFailure, onTestSkipped, onTestFailedButWithinSuccessPercentage), the other to the whole suite’s run (onStart, onFinish). These methods, depending on their type, have a parameter passed to them: result, which is of type ITestResult – for the test run, and context, which is of type ITestContext, for the suite run. These types offer different information, for example: ITestResult can tell you when a test began to run, when it finished running, what status the test had (whether failed, passed), whereas ITestContext can tell you the list of all the passed tests, all the failed ones, and so on.