Feel free to clone the example here:

git clone https://zerosimms@bitbucket.org/zerosimms/sense.git

Switch to the branch: WebDriver

Selenium

Hopefully your reading this post because you know what Selenium is about, so I’ll just carry on:)

I wanted to show and help others learn how to get an automation framework of the ground by starting from the beginning. It’s also good practice for me to document what I know because Frameworks on an enterprise level quickly become massive mazes!

So, from the beginning, fresh start with the absolute minimal code and working examples along the way!

The first stage, getting Selenium running!

To begin with, all my examples will be done in IntelliJ.

There are a number of reasons why but the biggest reason comes later, when we introduce Cucumber. Out of the box IntelliJ has better support for this and hell, they have a free version (which I am using) so why the Devil not?

 

The Code

I always create a new Maven project so that I can take advantage of the POM, using a POM is the easiest way to manage and update your dependencies. The very first one being Selenium itself :)

Add selenium as a dependency to your project.

[codesyntax lang=”php”]

   <dependencies>
      <dependency>
         <groupId>org.seleniumhq.selenium</groupId>
         <artifactId>selenium-java</artifactId>
         <version>2.42.2</version>
      </dependency>
</dependencies>

[/codesyntax]

Now, create a class and add the following to get you started:
[codesyntax lang=”java”]

public class ExampleTest {
   public static void main(String[] args) throws InterruptedException {
      WebDriver senseDriver = new FirefoxDriver();
      WebDriverWait wait = new WebDriverWait(senseDriver, 10);

      senseDriver.get("http://www.google.com");
      WebElement feelingLuckyButton = wait.until(ExpectedConditions.elementToBeClickable(By.id("gbqfbb")));
      feelingLuckyButton.click();
      senseDriver.quit();
   }
}

[/codesyntax]

Right click on your class and select Run:

Screen Shot 2014-09-20 at 21.07.21

That’s it, Selenium in under 10 minuets running!

The tiny test launches Firefox, heads to Google and then clicks the ‘Feeling Lucky’ button.