Getting Started with WebDriver (a Browser Automation Tool)
Posted At : May 17, 2010 11:08 AM | Posted By : Bob Silverberg
Related Categories: WebDriver
A few weeks ago during MXUnit Office Hours, Bill Shelton demonstrated using WebDriver to create functional tests in Java. It was a pretty cool demo and I meant to give it a whirl for a while, but only got around to doing it this weekend. I had to refer back to the recording of the session a number of times, so I thought it would be worthwhile to document the steps to get a project up and running in Eclipse that uses WebDriver. Let's start with some background.
What is WebDriver?
WebDriver is a tool that allows you to write code, in a number of languages, that automates a browser and allows you to interrogate what is rendered by the browser. You can use these features to created automated tests of a web application. You may be familiar with a tool called Selenium, which does similar things, but works within the context of a web browser itself. The Selenium project and the WebDriver project are merging to create Selenium 2.0. That project is a work in progress, but there's nothing to stop you from experimenting with WebDriver right now.
For a bit more of an official definition, here's some info from the Selenium 2.0 and WebDriver page at SeleniumHQ:
WebDriver is a tool for automating testing web applications, and in particular to verify that they work as expected. It aims to provide a friendly API that's easy to explore and understand, which will help make your tests easier to read and maintain. It's not tied to any particular test framework, so it can be used equally well with JUnit, TestNG or from a plain old "main" method.
WebDriver uses a different underlying framework from Selenium's javascript Selenium-Core. It also provides an alternative API with functionality not supported in Selenium-RC. WebDriver does not depend on a javascript core embedded within the browser, therefore it is able to avoid some long-running Selenium limitations.
WebDriver's goal is to provide an API that establishes
- A well-designed standard programming interface for web-app testing.
- Improved consistency between browsers.
- Additional functionality addressing testing problems not well-supported in Selenium 1.0.
I hope to write a number of posts about working with WebDriver. This first post will be a quick guide to setting up an Eclipse project and getting WebDriver to interact with a web page. Let's get started.
Have you seen Badboy (http://www.badboysoftware.biz/)?
It does something similar but uses a graphical interface. Badboy is very easy to use (actually darned simple - just open up BB, navigate through the site or test case and that's it).
Badboy is also scriptable, using jScript. The chief downside to it that you have to have IE on your machine.
I use it to develop test cases etc. One thing that makes it very useful is that you can export your badboy saved files to use as jMeter load testing xml configuration file.
Currently I use it to automate a download data files from a site that uses password protection.
regards,
larry
Hey, one minor point (and it was my shortcomming) ... the URL for the Javadoc should omit the 'index.html' and be : http://selenium.googlecode.com/svn/trunk/docs/api/... . Eclipse still says the location "might be valid" or some such.
-bill
Larry, thanks for the info to Badboy! This might be a winner for one particular Win32/IE project I'm working on. Though not open source, it appears to be quite reasonable at 45USD or less.
-bill
and configure a Remote Server?
When I run this test I get A NoSuchElementFoundException. Th
The google.com website is not even been requested.
Please let me know what extra configuration is required.
Thanks in advance
I wrote a webdriver script which will do some update
on UI and then it needs to verify the oracle datadase to
check whether the update is successful or not in a
particular DB table . How can I check
the DB part using webdriver .
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
FirefoxDriver cannot be resolved to a type
at test.test.main(test.java:11)
what is the problem ??
import org.openqa.selenium.firefox.FirefoxDriver;
WebDriver driver = new InternetExplorerDriver();
Of course you have to import the corresponding library...
Tried chrome driver but it just kills the CPU, consumes too much resources...
I know selenium 2 with web driver is ready now, but their documentation sucks... Yesterday I tried every possible way to make it work on windows 7 but it was impossible, the maven connection never worked for me and the tutorial they have just tells you to get Maven information somewhere else...
I canĀ“t believe someone releases a tool with such a crappy documentation for the developers!
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Cannot instantiate the type WebDriver
at test.Test.main(Test.java:11)
could you provide more details on the error? Initially it look like missing references.
Do you have some documentation on how to create a build file using ant or maven?
Kudos!!!
import org.openqa.selenium.firefox.FirefoxDriver;
as posted by Bill Mackie.
Many thanks!
"Protected mode (enable or disable)". Not sure what it is
This was pretty good piece of information.
What amaze me is the leverage of power of Java into test Automation.
I had been working around for 2 days just to evoke "FirefoxWebDriver" class. And I guess I was missing somewhere.
I came across this blog and my problem got resolved.
I am QTP professional and my interest is inclined towards Java.
I'll be happy to know if have all the features of QTP.
Thank you once again and keep writing.
i've followed all the instructions and added the:
import org.openqa.selenium.firefox.FirefoxDriver;
but i'm getting the following message:
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/base/Function
at webdriverpackage.Test.main(Test.java:12)
Caused by: java.lang.ClassNotFoundException: com.google.common.base.Function
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
i'm a total novice so any help would be greatly appreciated
its working now and ran successfully
Thanks!
Bill Mackie's above comment regarding adding the import "import org.openqa.selenium.firefox.FirefoxDriver;" should be added in, seems like it doesn't work without that.
Also might be helpful to note that if someone simply copies and pastes your code (as I did), the line numbers must be removed before it will work.
~Chaitanya
Your blog helped me solve the path to java doc identifying problem, its now all setup correctly and I am not seeing any errors
But I am trying to automate using IE webdriver and when I run following code on eclipse I am getting error
package webtest2;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class thirdprogram {
public static void main(String[] args) {
// TODO Auto-generated method stub
// System.setProperty("webdriver.ie.driver", "C:\\IEDriverServer_x64_2.35.3\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
driver.close();
driver.quit();
}
}
Started InternetExplorerDriver server (64-bit)
2.35.3.0
Listening on port 13277
Exception in thread "main" org.openqa.selenium.remote.SessionNotFoundException: Unexpected error launching Internet Explorer. Could not get document from window handle (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 8.89 seconds
Build info: version: '2.37.0', revision: 'a7c61cb', time: '2013-10-18 17:15:02'
System info: host: 'PRADEEPM-PC', ip: '192.168.2.173', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_45'
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:151)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:216)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:201)
at org.openqa.selenium.ie.InternetExplorerDriver.run(InternetExplorerDriver.java:224)
at org.openqa.selenium.ie.InternetExplorerDriver.<init>(InternetExplorerDriver.java:214)
at org.openqa.selenium.ie.InternetExplorerDriver.<init>(InternetExplorerDriver.java:180)
at webtest2.thirdprogram.main(thirdprogram.java:13)
thanks