- describe principles around Selenium testing
- learn how to write some simple Selenium tests
- introduce a reusable Maven
pom.xml
- completion of Introducing jUnit
- completion of TDD Fibonacci using jUnit
- completion of Introducing Maven
Selenium is a tool that allows us to write automated tests to check that a website functions as expected.
With Selenium, we can write Java programs that do the following:
- open a browser at a given URL
- find any HTML element on the page
- interact with a found element – for example click a button, enter text in an input field, etc
- wait for a specified element to appear on the page before proceeding
Before writing the tests, we will define some requirements.
Given my starting URL is http://www.google.co.uk:
- Then the
<title>tag should start with the textGoogle - When I search for the term
Sausages, then the<title>tag should start with the textSausages.
The starter code already contains an empty class for the test.
Open the
Selenium3ExampleIT.javaclass in your IDE.
You will see the following:
- several
imports - several
privatevariables
These will all be used once we have finished this lesson.
The @BeforeClass annotation allows us to specify code that is run once before any of the tests are run.
We can use this to create an instance of the Chrome browser. Add the following annotation and method before the end of the class:
@BeforeClass
public static void beforeAll() {
startUrl = "http://www.google.co.uk/";
driver = new ChromeDriver();
wait = new WebDriverWait(driver, 10);
}Note:
startUrlis the URL that will be used to start each test withdriverrepresents the web browser that will be created- we will use
waitto wait for a condition to occur on the web page
The @AfterClass annotation executes code after all the tests have been run.
So we want to close the browser that we previously opened. Add the following annotation and method before the end of the class:
@AfterClass
public static void afterAll() {
driver.close();
driver.quit();
}As we have done previously with jUnit, we can specify code to be executed before every test.
In our case, we want the browser to go back to the starting URL:
@Before
public void beforeEach() {
driver.get(startUrl);
}Note that the way we tell the browser to go to a specific URL is by calling the get() method on the driver instance, and pass in a Java String that contains the URL we want to go to.
Now that we have the scaffolding out of the way, we can add a simple test.
We are going to check to see if the <title> element on the web page is an expected value:
@Test
public void checkPageTitleOnInit() {
assertEquals("Google", driver.getTitle());
}Note that we are not actually directly specifying an HTML element to search for – the interface provided by Selenium contains a getTitle() method that does this for us.
Here's the command to run the integration tests from your terminal:
mvn clean verifyWe are now going to test what happens when we perform a search on Google:
- find the input box for the search
- simulate typing in a search term
- click the Google Search button
- check what happens to the
<title>element on the page
Here's the code to execute that test:
@Test
public void checkPageTitleAfterSearch() {
WebElement searchField = driver.findElement(By.name("q"));
searchField.sendKeys("Sausages!");
searchField.submit();
// Google's search is rendered dynamically with JavaScript, so wait for the page to load.
result = wait.until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
return driver.getTitle().toLowerCase().startsWith("sausages!");
}
});
assertTrue(result);
}Note the following:
- we locate the search field by calling
driver.findElement()with a parameter that defines where the search field is - this is found by calling
By.name("q")&name; which means that there is an input element that has anameattribute ofq(more on this later) - once we have found the search field, we can call the
sendKeys()method to enter text - and then call the
submit()method to submit the form - the next block of code looks complex, but is simple in concept: it is waiting until the
<title>contains an expected value - if that does not happen within the specified time, the test will fail
Run the tests again
So: how did we know how to find the HTML input element for the search box?
It's not possible to know just by looking at the website… we need to view the HTML elements.
To do this, just right-click inside the text input box, and one of the menu items should be Inspect – click this:
The developer toolbar should now be open in the Elements tab:
In the Elements tab on the right, you can see the <input> element highlighted, and on the next line (still highlighted) you can see name="q" – this tells us that the name attribute of the search field has a value of q (presumably short for "query").
The pom.xml file included in this repository is reusable in other projects – with minor modifications to the test class, you can use a different browser to test with.
- learned principles around Selenium testing
- learn how Selenium tests are written, and written some basic tests ourselves
- introduced a reusable Maven
pom.xml

