automatictester

my thoughts on test automation

SBT project in IntelliJ IDEA for Selenium WebDriver testing – tutorial

In this step-by-step tutorial you will learn how to create brand new project in IntelliJ IDEA with SBT as a build tool. This project will contain WebDriver, TestNG and Hamcrest libraries, which you will use to create Selenium tests. It is assumed you have prior Selenium knowledge and worked with IDE before. No prior experiences with SBT are required. This tutorial is based on IntelliJ IDEA 13.1.4 Community Edition and SBT 0.13.5, on OS X platform.

  • In IntelliJ, install Scala plugin and restart IDE
  • Create New Project / Scala / SBT
  • In Preferences / SBT, make sure you selected  Use auto-import  option
  • Add following dependencies to  build.sbt  file:
    libraryDependencies ++= Seq(
       "org.seleniumhq.selenium" % "selenium-java" % "2.42.2",
       "org.testng" % "testng" % "6.8.8",
       "org.hamcrest" % "hamcrest-all" % "1.3"
    )
  • Wait for IntelliJ to rebuild the project
  • Create your sample test:
    package com.wordpress.automatictester.examples;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.testng.annotations.AfterTest;
    import org.testng.annotations.BeforeTest;
    import org.testng.annotations.Test;
    
    import static org.hamcrest.CoreMatchers.is;
    import static org.hamcrest.MatcherAssert.assertThat;
    
    public class FirefoxTest {
    
        private WebDriver driver;
    
        private static final By ABOUT_TAB = By.linkText("About");
        private static final By ROADMAP_LINK = By.xpath("//a[text() = 'Roadmap']");
        private static final By HELP_LINK = By.xpath("//a[text() = 'Help']");
    
        @BeforeClass
        private void initialize() {
            driver = new FirefoxDriver();
            driver.navigate().to("http://www.seleniumhq.org/");
            driver.manage().window().maximize();
        }
    
        @Test
        public void verifySomething() {
            driver.findElement(ABOUT_TAB).click();
            assertThat(driver.findElement(ROADMAP_LINK).isDisplayed(), is(true));
            assertThat(driver.findElements(HELP_LINK).isEmpty(), is(true));
        }
    
        @AfterClass
        private void quit() {
            driver.quit();
        }
    }
  • If everything went fine, you should now be able to right-click class name and select  Run ‘FirefoxTest’  to start your test

That’s it, SBT-backed project is ready to go!


If you need more control over the build process, you can get SBT executable and work with command line.

  • Download  sbt-launch.jar
  • Copy this JAR to your project root directory
  • Create sbt text file in your project root directory with following content:
    SBT_OPTS="-Xms512M -Xmx1536M -Xss1M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=256M"
    java $SBT_OPTS -jar `dirname $0`/sbt-launch.jar "$@"
  • In root project directory run  chmod +x sbt  to make it executable
  • Add following plugin to  plugins.sbt  file:
    addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.6.0")

    This will enable sbt gen-idea

  • Go to project root directory and run:
    ./sbt clean compile test:compile gen-idea

This will take care of dependencies and regenerate IntelliJ project. You should be prompted to reload project when you get back to IntelliJ.

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

%d bloggers like this: