First Steps With Selenium
This is a short guide to getting started with Selenium - an automated website testing library.
What you will learn
You will learn how to automatically test a simple web form using Selenium. Examples will be created using Java and Maven. If you would like to follow along with this tutorial, you can download this project from github (look for the package called selenium1
).
What is Selenium?
Selenium is a library for interacting with a web browser. It enables you to simulate how users may use your site, like filling up forms for example. What is important about Selenium - it can test JavaScript code. You can open up a browser of your choice and enter any site, even with advanced scripting. In the next sections we will dive into details how to do it.
Maven Dependency
First you need to add a dependency to the pom.xml
:
Creating a WebDriver
Next you should create a WebDriver
object. WebDriver
is an interface representing a general browser. For specific browsers you have to use concrete implementations. Currently supported out-of-the-box are:
- Chrome
- Internet Explorer
- Firefox
- Opera
For starting Firefox you would write:
To open a specific page use the get
method:
Simple Scenario
Supposedly, we have a form like:
To mimic a user logging into our application we could use this code:
Three important things happen here:
- entering text into fields using
sendKeys
method - clicking the "Log in" button with
click
method - looking up web elements in the page structure (in the DOM model to be specific)
Entering text and clicking is quite straightforward, however finding web elements can be quite complex. You can specify search criteria by:
By.id(String id)
By.name(String name)
By.tagName(String tagName)
By.className(String className)
By.linkText(String linkText)
- will search for the "a
" HTML element with the given textBy.partialLinkText(String linkText)
- same as above, but even partial match will succeedBy.xpath(String xpathExpression)
By.cssSelector(String selector)
For simple look ups, choice of a specific method will be quite obvious. Most of the time you will use By.id
, By.name
or By.className
.
For the non-trivial cases you will have to choose between xpath
and css
selectors. It is best to decide based on your knowledge and complexity of the result query. Sometimes xpath will be simpler, sometimes a css selector will work out better.
The End
Here ends this quick tour on Selenium basics. In further articles I will try to expand the topic with more complex scenarios.