Monday, July 18, 2016

AUTOMATION 18 - GUI 6, Running our first Selenium WebDriver script

Technical level: ****

To get the most out of this post, you will need a very basic level of Java (I hope to write a short series on this, but I have to get this epic series finished first).

If you're followed the preceeding steps, then you've installed all the software you need, and set up your Java project area with links to the Selenium WebDriver Jar files.

Now you're ready to try your first script!

There is a great simple script here, which I've seen a lot of sites use, and is the equivalent of "Hello World" for Selenium.

I actually wanted to have a script that did and checked for something, so created Script 1, which you can find on a previous post here.  Open it up, and copy into Eclipse in your Java class (overriding what's there).

What we'll do is start by looking at what it does when run, and then taking a look at all the commands inside...

Hit run!

To make it run, hit the green play button here which will compile and build the code before running it ...

A Firefox browser pops up, open at the surreal entry I added about aardvarks last week ...

The output in Eclipse says this ...

If you have any problems, my advice is,
  • Make sure Selenium WebDriver and Firefox are up to date.  
  • Check consistent class name - should be"" and "public class PageCheck" match. Anything else, Google the error message.  This is pretty much a developer masterclass in how they solve problems.

The script does the following,
  • It opens Firefox and my Aardvark post
  • It captures the page title, and prints to the screen
  • It checks the whole page for the phase "blue aardvark" and prints it's found it - I mention "blue aardvark" in the article.
  • It checks the whole page for the phase "red aardvark" and prints it's found it - I mention "red aardvark" in the comment.
  • It checks the original post content for the phase "blue aardvark" and prints it's found it.
  • It checks the original post content for the phase "red aardvark", finds nothing (it's in the comments), so prints nothing.

Although I've done a good job commenting it to make it readable, I'm now going to go through the commands used, and explain what they're all about, so you can understand how this script holds together.

import org.openqa.selenium.*;

There is a whole series of import commands, which brings import the parts of the Selenium JAR libraries that I need for our scripts.

In actual fact, I don't need the following right now, but likely to need them in script 2 - so left them in (a bad habit I know).

If I didn't use these import commands, I'd get all sorts of errors of items not find ... in fact I've commented it out and hit run, and I got ...

public class PageChecker

This declares the class we're using.  To be honest with something so simple we're not really using it as a class, just so it can run right now.  Classes are an important concept in modern languages like C# and Java, but too complicated to explore right now.

public static void main(String[] args)

This is the main function - that is the first thing which will be triggered when we hit run and the Java has compiled.

WebDriver page_item = new FirefoxDriver();

This sets an instance of a Firefox driver that we can use.  Most code declares it as "WebDriver driver", but I felt that "WebDriver page_item" would be easier to follow - we can read the page content by looking at the data within page_item, and can likewise manipulate the page by calling page_item, methods.

It's on this command that a Firefox browser instance is triggered on your computer.


This causes the Firefox page_item to go to a web address, the address of my blog post.

System.out.println("Page title is: " + page_item.getTitle());

The command println prints to the command line.  It prints out part of the page_item data, relating to the page title.

if ( page_item.getPageSource().contains("blue aardvark"))

This calls a page_item method, where it checks the page content for the text provided - "blue aardvark".  If it's found it prints to screen it's been found.

The "red aardvark" if-statement is the same, just using a different search string.

WebElement element = page_item.findElement("post-body-8711143007795160191"));

A web element is a part of the page - we've declared it to be by id "post-body-8711143007795160191", which corresponds to the original post - not the title or the comments.

There are several alternative methods we could have used to declare the web element - such as by name or class ID.  But we've already covered via our previous work using developer tools how to do this.

Check out these two posts here and here for more background information

if ( element.getText().contains("blue aardvark"))

Very similar to the previous if-statement, only this time we're looking for the text string in the element, not the whole page.

Hence this time we'll find "blue aardvark" in the original post, but not "red aardvark".

Next time we'll look at how you can explore more Selenium commands, and we'll create a more complicated script.

Extension material
  • Write a check for "See it run"
  • Add a comment to that blog, and create a check for it
  • Google other examples of Selenity commands that you can perform


  1. Nicely explained. Here you described the well written article from your in-depth knowledge. Truly impressive and nice information

    Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

    Java Online Training Java Online Training JavaEE Training in Chennai Java EE Training in Chennai

  2. Gclub To play casino games online is not unusual.

    G club As a leading online betting website, bets on online casino games are no longer an issue because online betting is not playing today. For fun to play games, online gambling is to generate millions of dollars in just a few minutes.

    Playing online casino games nowadays is considered to be one of the options that allow players to play online games without boring fun and online betting on the site of our online casino is. Fun to make money with the player and the easiest way to do it. By playing online gambling games with our online casino site, it is a fun new alternative to making your online gambling addictive a boring and challenging one. You go by default with me.

    To play online gambling games. Every time you start placing bets, the odds are that you can raise and raise from 100 to become millions in just a few minutes. You can become rich millionaire already. So if you think about online betting, we want you to think about the services of the website. Gclub มือถือ

  3. The information which you have provided is very good. It is very useful who is looking for selenium Online Training

  4. 먹튀뉴스 베팅커머스는 회원분들께 항상 새로운 정보를 드리도록 노력하고있습니다.