Selenium + Java: How to start

Selenium + Java: How to start

Why Selenium Webdriver + Java is a great combination for web service integration testing?

Note: This note is about using Selenium Webdriver with Java. You can also read about using Selenium with Nightwatch JS in this post.

Selenium Webdriver is an amazing tool allowing you to interact with browsers installed in your system. It allows you to create test scenarios like “open this page – check element is visible – click on this button – check something happened”. This testing approach is called behavior-driven development (BDD). In other words – you make a program which will do the same work you do while performing tests during the development process.

Selenium itself is a tool to control a browser. To write testing scenarios you should use a programming language like JS or Java. Full list of supported languages can be found on this page.

Why Java? Java is a very popular programming language having a great library of modules which allow you to interact with databases, SSH servers, etc. Due to this you will create extremely powerful testing scenarios.

Creating a Java Selenium project with Maven

The easiest way to write your first Selenium test scenario is to create a Java Maven project and include Selenium WebDriver as a dependency. Fortunately Maven supports archetypes – predefined project templates which contain minimal starting project structure.

To create your first Selenium Java project using Intellij Idea IDE follow steps below:

  1. Open IDE and press Ctrl+Shift+N. In the popup window select “Maven”.
  2. Select “Create from archetype” checkbox.
  3. Click on “Add archetype” button.
  4. In “Add archetype” dialog window fill inputs:
    • GroupId: ru.stqa.selenium
    • ArtifactId: webdriver-testng-archetype
    • Version: 4.0
  5. Select new archetype in the list and go “Next”
  6. Fill your project’s Maven parameters:
    1. GroupId: com.mysite //Fill this input with your site’s domain name. Following the convention you should start with the first-level domain, then second-level domain, etc.
    2. ArtifactId: selenium-tests
  7. Go “Next” until the master’s finish.
  8. Open the new project in a new window.
  9. Click on “Import Changes” in the bottom right popup and wait while IDE will import dependencies.

Some editions before the start

Your project already contains demo test classes, so you can try running tests out of the box. But before you run your first test you should edit TestNgTestBase.initWebDriver method. Replace

driver = WebDriverPool.DEFAULT.getDriver(gridHubUrl, capabilities);

with

if (null == gridHubUrl || "".equals(gridHubUrl)) {
      driver = WebDriverPool.DEFAULT.getDriver(capabilities);
      System.out.println("If you need Selenium Grid, please start Selenium " +
              "Server standalone and check file debug.properties and URL of grid.url.");
    } else {
      driver = WebDriverPool.DEFAULT.getDriver(gridHubUrl, capabilities);
    }

Customizing testing properties

Testing configuration is defined in the pom.xml file. Open the file and find this lines:

<plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.19.1</version>
        <configuration>
          <suiteXmlFiles>
            <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
          </suiteXmlFiles>
          <systemPropertyVariables>
            <application.properties>/application.properties</application.properties>
          </systemPropertyVariables>
        </configuration>
      </plugin>

As you can see the testing properties file is defined here. If you open the file you will see parameters with placeholder values, e.g.

capabilities=${capabilities}
site.url=${site.url}
grid.url=${grid.url}

These values are defined in pom.xml file in <profiles> section:

<profile>
      <id>firefox</id>
      <properties>
        <capabilities>/firefox.capabilities</capabilities>
      </properties>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
    </profile>

Firefox profile is used by default. If you want to use another profile you should specify it in Maven test command, e.g.

mvn -P chrome,localhost,nogrid test

To do it in IntelliJ Idea open Maven project structure by clicking on “Maven Projects” button:

Expand “Run Configuration” and right click on “seleniumtests”. In context menu press on “Edit Run Configuration”. In the popup window specify profile you want:

 

Runnig tests

Now you are ready to run your first test. Expand “Run Configuration”  in the Maven project tree and right click on “selenium tests”. In context menu press on “Run”.

 

Author: vlad