Tuesday 29 October 2013

Cleaning up tests in selenium - Killing processes and clearing temporary files

All selenium tests create profiles on the temp folder(temporary directory) and browsers apart from firefox spawn separate processes(.exe's) that are not tied with the java process that spawns selenium.

driver.quit() usually does the trick of clearing the temp folders and kills the processes associated with the tests.But there has been several issues around this and it does not work particularly with older versions of selenium webdriver.

As per Issue 1934 the firefox problem of deleting the temp folders seems to have been solved.But it does not work in standard manner for other browsers. Stackoverflow has many questions related to this and the only reasonable way to solve this is to clear the temp folder and kill the process by external means.

Again if we write boilerplate code using Java File I/O to delete files in temp folder, it is a pain to recurse and delete all the files and also the folders. Fortunately Apache Commons IO has a way to deal with this.I have a made a test cleanup method of my own which deletes all files from temp ignoring the ones that cannot be deleted and kills the process names that you specify to it.


Link to the code : 
https://github.com/Madusudanan/Selenium/blob/master/TestCleanup.java

Let me know in case of any suggestions or issues.


Tuesday 8 October 2013

Selenium Webdriver : Multiple Locators for a Single Element

We all have had dealings with Selenium locators and many times we face with issues with flaky scripts due to flaky locators. The flakiness of the scripts is directly dependent upon the Application Under Test (AUT) .

There might be some situations where one locator might work for an element and the other does not.The natural way that comes to our mind is to use a simple if-else construct and then choose the one that works. This will definitely work, but it's not an elegant solution.

Selenium webdriver has in-built methods to handle this situation and it works for CSS and XPath locator strategy.

XPath

Suppose we have an element with an id = 'login-button' and a name='login-homepage' and lets assume they come under the same div.The way we would write locators using Xpath are as follows.

Using ID : //div[@id='login-button']

Using Name: //div[@name='login-homepage']

Combining both : //div[@id='login-button' or @name='login-homepage']

Ok. It wasn't that hard.Now how it will work is that we have the parent element as the same.But we are not dependent on the locator of the parent Div , but we take the fact that Id , Name are unique for the same web element and work on that.

CSS

CSS also has provisions to combine two locators. Let us assume we have an image with the following HTML

<img src="images/selenium-ide-logo.png" class="icon" alt="Selenium IDE Logo" style="background-color: transparent;">

Using Class : img[class='icon']

Using alt     : img[alt='Selenium IDE Logo']

Combining both : img[class='icon'],img[alt='Selenium IDE Logo']

We can verify the functionality by purposely giving the first locator wrongly,then see whether the second locator works correctly or not and vice versa.

In this way we can combine many locators into one single place in our code such as

String Image_In_Home_Page_Locator = "img[class='icon'],img[alt='Selenium IDE Logo']";

This looks a lot more neater than using if-else conditions.