Tony Dugay

About Tony Dugay

Tony is a Developer/Tester based in the UK. He had over 5 years working professionally as a Java Web Developer in the airline industry and then later as a contractor. Since then he has mainly worked on software test automation using tools such as SilkTest, QTP and of course Java/Selenium.

A Selenium/WebDriver example in Java

A couple of years back, I was pitching for some work and the client wanted to see how I would tackle a real world problem. They asked me to automate some tasks on the woot.com web site.

The task was to go to various woot web sites and to read the product name and price of the offer of the day.

I wrote a little bit of Selenium code and thought I’d post it here in case any of it is useful to anyone.

I got the job – so it can’t be too bad.

First up I defined an interface to represent a woot page:

package uk.co.doogle;

import com.thoughtworks.selenium.Selenium;

/**

* This interface defines the methods we must implement for classes

* of type Woot. Woot web sites have one item for sale every 24 hours.

* @author Tony

*/

public interface Woot {

/**

* Defines the interface of the method we use to get the price

* of the item for sale on a Woot website

* @param selenium the selenium object we pass in which is used to interact

* with the browser/web page

* @return String representation of the price of the item for sale

*/

public String getPrice(Selenium selenium);

/**

* Defines the interface of the method we use to get the product name

* of the item for sale on a Woot website

* @param selenium the selenium object we pass in which is used to interact

* with the browser/web page

* @return String representation of the product name of the item for sale

*/

public String getProductName(Selenium selenium);

}

Then I implemented this interface a few times to represent the actual behaviour of the various woot pages – here for example if the winewoot page:

public class WineWoot extends BaseWoot {

/**

* Constructor

* @param url pass in the url of the web site

*/

public WineWoot(String url) {

super(url);

}

/**

* Implementation of the method to get the price of the object for sale on

* the Woot web site.

*/

public String getPrice(Selenium selenium) {

//if you need to update the xpath to the piece of text of interest - use xpather firefox plugin

String xPath = '//html/body/header/nav/ul/li[8]/section/div/a/div[3]/span';

selenium.waitForCondition('selenium.isElementPresent(\'xpath=' + xPath + '\');', '12000');

return selenium.getText(xPath) + ' ';

}

/**

* Implementation of the method to get the product name of the item for sale

* on the Woot web site

*

*/

public String getProductName(Selenium selenium) {

//if you need to update the xpath to the piece of text of interest - use xpather firefox plugin

String xPath = '//html/body/header/nav/ul/li[8]/section/div/a/div[2]';

selenium.waitForCondition('selenium.isElementPresent(\'xpath=' + xPath + '\');', '12000');

return selenium.getText(xPath) + ' ';

}

}

Note – back then I used the xPather plugin – this doesn’t work for recent versions of firefox, so now I use firebug.

Then I wrote the actual ‘test':

package uk.co.doogle;

import com.thoughtworks.selenium.*;

import java.io.BufferedWriter;

import java.io.FileWriter;

import java.util.ArrayList;

import java.util.List;

/**

* This class is where we define tests of the Woot web sites

* @author Tony

*

*/

public class TestWoots extends SeleneseTestCase {

/**

* Outputstream for our results file

*/

private BufferedWriter out;

/**

* Our list of Woot web sites we want to test

*/

private List<BaseWoot> sites = new ArrayList<BaseWoot>();

/**

* This is where we do any set up needed before our test(s) run.

* Here we add the list of Woot web sites we want to test and we create an

* output stream ready to write results to file

*/

public void setUp() throws Exception {

sites.add(new BaseWoot('http://www.woot.com/'));

sites.add(new ShirtWoot('http://shirt.woot.com/'));

sites.add(new WineWoot('http://wine.woot.com/'));

try {

//let's append to our file...

FileWriter fstream = new FileWriter('out.csv', true);

out = new BufferedWriter(fstream);

out.write('Site, Product Name, Product Price');

out.newLine();

} catch (Exception e) {

System.err.println('Error creating a file to write our results to: ' + e.getMessage());

}

}

/**

* Tests getting the item name and price for the item for sale on each Woot web site we test. We see the results of the test

* in std out in the form of a table and we also write the results to a csv file.

* If there are any errors getting the information, this is displayed instead.

*

* How to run me: open command prompt and from the directory where our selenium server is

* located type: java -jar selenium-server-standalone-2.0b3.jar (or equivalent) and wait for the server to start up.

* Then just run this unit test.

*/

public void testGetItemsAndPrices() throws Exception {

//for each Woot site in our list of sites we want to test

for (BaseWoot woot : sites) {

//let's put this in a try catch block as we want to try ALL the sites - some may be down or slow...

try {

selenium = new DefaultSelenium('localhost', 4444, '*firefox', woot.getUrl());

selenium.start();

selenium.open('/');

selenium.waitForPageToLoad('50000');

//add a new row for our table to std out

System.out.println();

//print out the information we need - the site, the title of the item for sale and the price

String siteUrl = woot.getUrl();

String productName = woot.getProductName(selenium);

String productPrice = woot.getPrice(selenium);

//sometimes there are commas which mess up our csv file - so

//we substitute with ;

productName = productName.replace(',', ';');

System.out.print('website: ' + siteUrl + ' ');

System.out.print('product name: ' + productName);

System.out.print('price: ' + productPrice);

out.write(siteUrl + ', ' + productName + ', ' + productPrice);

out.newLine();

} catch (Exception ex) {

//here may may see that the web site under test has changed and the xpath to the price or product name may need to

//be changed in the Woot class

System.out.print('problem getting the data for: ' + woot.getUrl()+ ' ' + ex.getMessage() + ' ');

} finally {

selenium.stop();

}

}

}

/**

* Any tear-down we need to do to cleanup after our test(s).

* Here we just stop selenium and close the output stream

*/

public void tearDown() throws Exception {

selenium.stop();

out.close();

}

}

I know this code worked for a couple of years, and I have made some minor changes to get it to work with the current woot.com web sites – all I had to do was get the latest selenium-server-standalone.jar for it to work with the latest firefox and also to update the xpaths to the price and product name information. That would be a good improvement to the code – to make it data driven – such that we could just update the xpaths in a config file rather than changing the hard-coded ones I have used here. That was the only feedback from the client actually.
 

Reference: A Selenium/WebDriver example in Java from our JCG partner Tony Dugay at the Doogle Ltd blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

6 Responses to "A Selenium/WebDriver example in Java"

  1. John says:

    How could this work? Your extending the interface’ BaseRoot’ but your interface is called Woot.
    Your calling woot.getURL() method which doesn’t exist. Also why create an interface and then create one implementation? Why not just create one class? And why pass in a selenium object into each method when you could simply declare a public selenium object. in the WineWoot class?

    It is an interesting implementation however.
    thanks!

  2. Harika says:

    Please find the below usefull link which provides selenium webdriver tutorials which will also guide you to build a good framework.
    http://seleniumeasy.com

  3. gobizen says:

    Thank you for sharing above code for selenium, I want to close browser fully what will be the command for it, share me Im beginner for selenium

  4. yuki says:

    i am not clear that what is BaseWood?

  5. yuki says:

    Please explain

Leave a Reply


7 − four =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close