Learning Selenium – part 1

Basics of test automation with Selenium – Part 1big-logo

I’ve seen a lot of posters in forums asking about a good source to learn test automation with Selenium.

I decided to write my own, based on my experiences. I didn’t expect this series will be the best tutorial you ever see, but, I hope this can guide the most of the beginners on Selenium.

I’ll use Java, Selenium webDriver, Maven, and Junit as tools for this work. My personal environment is a Ubuntu Desktop OS and Netbeans IDE, but this is optional and you use what you want.

First, understand what Selenium is and what you can do with it.

Everyone thinks selenium is a tool for test automation. No, it’s not. In the official page of selenium you get its description:

Selenium automates browsers. That’s it! What you do with that power is entirely up to you. Primarily, it is for automating web applications for testing purposes, but is certainly not limited to just that. Boring web-based administration tasks can (and should!) also be automated as well.

As the page says, That’s it! Stop thinking you can just use only selenium to write your tests. Selenium interact with your browser. With it, you can click, type, get and some other features you used to with your mouse and keyboard.  You won  use Selenium to do assertions or assumptions. In the past, selenium 1.0 have methods to do that. Today, I don’t know and prefer still not knowing. Why? Just because I think you have to learn a test automation framework, like Junit or TestNG. In your life of test automator, you won’t automate only web pages.

Selenium is not only a tool to automate browsers. You will see a lot of references about Selenium Grid. Grid lets you create a grid of machines running your tests. I will talk about it in a future post. You can integrate your grid to a continuous integration tool, like Jenkins, and run several tests at same time. I will cover this on the future.

But, for now, lets start with the basics of selenium. We have to download the selenium jar to start writing our code. You will see references about selenium RC. Don’t go this way. Selenium RC does not have the same implementation of webDriver. And it is deprecated. Start from the fresh and new.

In the downloads page of selenium, you see the references about selenium server (ignore it, is for selenium RC), selenium IDE, IE Driver and Selenium Client & WebDriver Language Bindings. Go to this last part and download the latest selenium-java.zip. This gives you a zip with all jars you need to start implementing.

But, how to do better?

I suggest you use maven dependencies in your projects. I will not tell all pros of maven, but you have to consider the great feature of a centralized repository of artifacts. You will see, during your selenium automator life, that your always face firefox update vs. selenium updates a challenge. Every time mozilla release a new version of its browser, something strange occurs with your test execution. You will search about the error and you see that is about the update. And selenium releases a new one. Seriously? You have to do the download process, update manually your automated project (contradictory, huh?) to things get running again?

With maven, you can set a dependency and, every time you get your code running, it will get the latest artifact you need. In this official page of selenium, you get the maven dependency tag. To get the last version, always, do something like that:

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>[2.39.0,)</version>
</dependency>

The tag <version> with the value [2.39,) allows maven to get the 2.39 version and up. If you get your browser constantly updated, keep your selenium updated too.

Your first code

I assume that you have some knowledge in your IDE and how to create a maven project, so I’ll skip those steps. If not, comment bellow, and I write a post about how to do that.

With your project created and ready to code, you need to instance the WebDriver class to open a browser and a URL:

Java:

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");

And Vois lá! With this simple lines of code, you open the google page on your firefox browser!

Let’s do something. Write this code:

WebElement searchField = driver.findElement(By.id("gbqfq"));
WebElement searchButton = driver.findElement(By.name("btnG"));

searchField.sendKeys("automation");
searchButton.click();

Boolean messageExists = driver.getPageSource().
               contains("Wikipedia, the free encyclopedia");

if (messageExists) {
System.out.println("Yeah, this works!");
} else {
System.out.println(
               "Well, that did not work as I expected...");
}

driver.close();

First, the if/else I use is just to follow basic steps. There are better ways (maybe not better, but correct) to assert a test. I will discuss this on other post.

And, when you run the code, you get the message:

“Well, that did not work as I expected… :S”

Frustrating. Why this happen? Well, the line who did the verification was executed before the page finish to load. So, the driver can’t see it.

Ok, let’s put a Thread.sleep with 10 seconds and see what happens. Every page has to be loaded until 10 seconds, right?

No! Don’t do that. If your page open before the 10 seconds, you will have to wait. Do this several times and your test will take long time to run.

What you have to do is make webdriver wait until an expected condition occurs in a certain lapse of time. In this example, when you search something on google, your query becomes the page title. So, let’s rewrite our code:

WebElement searchField = driver.findElement(By.id("gbqfq"));
WebElement searchButton = driver.findElement(By.name("btnG"));

searchField.sendKeys("automation");
searchButton.click();

WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.titleContains("automation"));

Boolean messageExists = driver.getPageSource().
               contains("Wikipedia, the free encyclopedia");

if (messageExists) {
System.out.println("Yeah, this works!");
} else {
System.out.println(
               "Well, that did not work as I expected...");
}

driver.close();

Then the message printed on console is:

“Yeah, this works!”

Great!

Now, my first post about selenium training is finished. You can see a lot of improvements you can apply on the code.

On the next posts, I will cover about how to modularize your code and how to make the code above more simpler, with Page Objects Pattern and the FindBy annotations. I will teach how to do correct validations, instead the if/else above!

Thanks!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s