Getting Started in Selenium and Java in IntelliJ Idea, Part 2

Read part one.
Get the source

 

Introduction

In part one of this article, we looked at how to use the Selenium IDE to record browser actions. We then used Firebug to locate a page element, and added a simple assertion to verify that a page element had the text we expected.

In part two of the series, we want to look at how to begin to work with out Selenium Scripts using Java. We’ll explore how easy it is to do a simple
export to a JUnit test, and we’ll get you the Maven and IntelliJ project files you’ll need to get started.

Set up a Selenium Test Project Using Maven and IntelliJ Idea

When setting up new projects in IntelliJ Idea, I almost always start with a Maven POM file first and then import it, since I find that pasting Maven
dependencies is the simplest way to get the jars I need. If you prefer to skip past this section, all the source including POM file and the finished project for this article are in the CodeSolid Tutorials repository on Github, in the Selenium Starter project. However, the steps are pretty simple:

  • Copy the pom file (see below) into a directory.

  • Set up a place for files to go. In the same directory where you copied your pom file, run the following at the command prompt: _mkdir src\test\java\com\codesolid\tutorials\seleniumstarter_” The “src\test\java” portion is a maven standard directory layout for test files and should
    be kept as is. In contrast, the “com\codesolid\tutorials\seleniumstarter” portion of that path will be our package structure, and you can modify
    it to fit your project’s package structure as needed.
  • In IntelliJ Idea, “File / Import Project”, then point to your directory. When prompted in the Import Project dialog, make sure “Import project from external
    model” and “Maven” are selected.
  • In the next Import Project dialog (below), I generally leave the defaults with the exception of “Import Maven Projects Automatically”. This helps to make sure that any changes you make to the POM are also reflected in your IntelliJ idea.

    importmaven

The rest of the dialogs are pretty straightforward. I generally skip importing any modules if prompted for one, choose my favorite JDK when prompted,
and provide a name for the project that matches the name I’m prompted for in the POM.

If you need more details on working with Idea and Maven projects, check out the IntelliJ documentation on working with Maven.

Exporting Selenium to Java

Once you have a project set up to house your tests, exporting Selenium tests to a JUnit test case is generally quite straightforward. To export a single test to JUnit, simply select “Export Test Case As”, and select the type of test case you want to create. Let’s select Java / JUnit 4 / WebDriver, as shown here:

exportjunittestcase

What you’ll want to do is export it to the package directory you created above, in other words, the directory where you copied the pom file plus src\test\java\com\codesolid\tutorials\seleniumstarter and give it a name, such as FirstSeleniumTest.java. The final step before running it in Idea is to make sure
your package name is set up correctly — chances are Selenium exported something like “package com.example”. Find the package directive at the top of the file
and change it to read:

At this point you should be able to right-click on the file in IntelliJ Idea and click on “Debug FirstSeleniumTest”. You should see Firefox launch and your test should pass. If you run into trouble, try grabbing the project from Github. Enjoy!

Leave a Reply