Email Subscription Form

Tuesday, January 21, 2014

Installing Webdriver

Webdriver is an API that enables testers to find and use web elements in their automated tests.  To get set up, you will need to find out what the latest version of Webdriver is.  Navigate to this link:  http://docs.seleniumhq.org/download and in the section called Selenium Client and Webdriver Language Bindings, find the Java entry and make a note of the latest client version.

Next, you will need to add this text to your project's POM file:
<dependency>
   <groupId>org.seleniumhq.selenium</groupId>
   <artifactId>selenium-java</artifactId>
   <version>2.39.0</version>  //replace the version number here with the most current version number
 </dependency>

Add the text after the JUnit dependency, so that the dependencies section now looks like this:

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.1</version>
    </dependency>
    <dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.39.0</version>
</dependency>
  </dependencies>

In the command line, navigate to your project folder.  Then type:
clean install
This will download all of the dependencies needed to run Webdriver in your project.

Let's set up a new test using Webdriver:

1. Right-click on the myFirstTests package and select New->Class
2. Give the class a name of secondTest
3. The SecondTest class should open in the center pane, and should look like this:
package myFirstTests;
public class SecondTest {
}

4. In between the brackets, add the following:
@Test
public void test() {
}

5. Add the following to the import section:
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

6.  In between the brackets following test(), add this code:
driver = (WebDriver) new FirefoxDriver();
String URL = "http://www.google.com";
driver.get(URL);
driver.quit();

7.  Right-click on the test name secondTest and choose Run As-> JUnit Test.  You should see a Firefox browser open, navigate to Google, and close again, and on the JUnit tab of Eclipse, you should see that the test has passed.

Congratulations!  You are now all set up to begin writing your own automated Webdriver tests!  In my next post, I will discuss the Webdriver syntax.













Monday, January 20, 2014

Understanding the POM

A POM file is necessary for any automated testing with Maven.  It can seem daunting at first, especially if you don't usually work with XML files, but it's actually very simple.  

POM stands for "Project Object Model".  It contains instructions about where to look for the source code for your tests, and what versions of JUnit, Maven, and Webdriver to use.  

Here is an example of a POM file, which should look very similar to the file you created for your First Java Project:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>MyFirstJavaProject</groupId>
  <artifactId>MyFirstJavaProject</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.1</version>
    </dependency>
    <dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.32.0</version>
</dependency>
  </dependencies>
</project>

Let's look at the file one section at a time:  

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
</project>

This is information that describes the schema for the Maven project.  This will be automatically generated for you.

 <modelVersion>4.0.0</modelVersion>
 <groupId>MyFirstJavaProject</groupId>
 <artifactId>MyFirstJavaProject</artifactId>
 <version>0.0.1-SNAPSHOT</version>

This is required information for every POM file-
The version number of Maven
The groupId (can be any name you like)
The artifactId (should match the name of your project)
The version number of your project (can be any number you like, and it's easy to just leave it at the default of 0.0.1-SNAPSHOT)

<build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
    </plugins>
</build>

The build section defines what will be necessary to build the project-
The sourceDirectory defines where Maven should look for your source files.  If you ever have trouble with a build, check to make sure this value is correct.  In this case, the source files will be found in the src folder.
The plugins section defines what plugins are necessary in order for your tests to run.

<plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
</plugin>

The plugin that is needed in this case is the Maven compiler plugin.  The compiler version is specified, and the source and target Java versions are specified.  

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.1</version>
    </dependency>
    <dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.32.0</version>
     </dependency>
  </dependencies>

Finally we need the dependencies; any extra software that the code needs in order to run properly.  In the case of this example, we need JUnit and Selenium to run our tests.  

As you become adept at test automation, you will probably run into situations where a test that ran without difficulty suddenly won't run at all.  This is often because of browser/Selenium incompatibility; for instance, a new version of Firefox may have been released, and you will need the latest version of Selenium in order for your tests to run in the new browser.  Rather than downloading and replacing files yourself, all you need to do is alter the version number of Selenium in your POM file, and the next time you do a Maven build, the project will fetch and download the new files for you!  

This is far from an exhaustive list of all the information that a POM file can contain; it is merely a brief description to help demystify what the file does and how it can be used.  For more information, see these helpful links:





  








New Blog Location!

I've moved!  I've really enjoyed using Blogger for my blog, but it didn't integrate with my website in the way I wanted.  So I&#...