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"PageCheck.java" 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.

page_item.get("http://testsheepnz.blogspot.co.nz/2016/07/im-hoping-that-this-blog-will-have-most.html");

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(By.id("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

2 comments:

  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

    ReplyDelete