Tutorial: a simple Selenium test from Hiptest

In this short tutorial, we will show how easy it is to integrate Selenium to the tests generated from Hiptest. In order to reach as many people as possible, we will use Java as the export language, but it should work the same with any of the languages that are supported by Selenium. To keep it simple, we will just use Google as the system under test, so we will not need to setup an application.

Prerequisite

You will need a few tools installed to get through this tutorial:

  • Java (at least version 7)
  • Maven
  • Firefox (tested with version 41.0.2)
  • An Hiptest account

Setting up the project in Hiptest

Create a new project in Hiptest, called “Selenium Tutorial “:

Project creation - Selenium tutorial by Hiptest

Go to the project settings and turn the BDD mode on:

Activate BDD mode

Create a new scenario “Finding a site on Google ” that will describe the behavior we want to test: when typing “Hiptest” in Google, the application website is shown in the results:

Sélection_805

When you write the steps, do not forget to add quotes around “Google”, “Hiptest” and “Hiptest: Agile test management tool & behavior driven development”. This way, the system will create action words with parameters and you will be able to use those for other tests (using another search engine, searching another site, etc.).

If you go to the action words list, you should see the 3 action words previously created:

Create action words

Your project is now ready to be automated using Selenium Webdriver.

Getting the base project

Download the zip archive of this project and extract it (or clone it from the Github project of this tutorial).

Fetching code from Hiptest content

To speed up this tutorial, we will skip the installation of hiptest-publisher and use the service version provided inside Hiptest to get the skeleton of your test.
Open the “automation” tab of the project then click on the Java/JUnit link:

Download your tests with Hiptest Publisher services

You should get a zip file containing two files: one for the test (ProjectTest.java) and one for the action words (Actionwords.java). Copy both of these files and paste them in the base project you obtained previously, in the sub-directory “src/test/java/com/example/”.
You might notice a third file (actionwords_signature.yaml) in the archive, but we will not discuss it in this tutorial, so you do not have to copy it.

Now, open a terminal, go to the root of the project and run “mvn test”. Everything should work fine and you should get a “Build successful” message. However, the test has not been really run, because we have not implemented it. Let’s fix this.

Implementing the action words

Let’s have a look at our two files. The first one (ProjectTest.java) will contain the test we wrote previously in Hiptest:

package com.example;
import junit.framework.TestCase;

public class ProjectTest extends TestCase {

    public Actionwords actionwords = new Actionwords();

    public void testFindingASiteOnGoogle() {
        actionwords.iOpenP1("http://google.com");
        actionwords.iSearchForP1("Hiptest");
        actionwords.aLinkToP1IsShownInTheResults("Hiptest: Agile test " +
                "management tool & behavior driven development");
     }
}

Good news: you don’t have to change anything to this file. Actually it is a good habit to never manually update the test file, as this would create differences between the implemented version which is in your code repository and the reference version stored in your Hiptest project. So close it and switch to the “Actionwords.java” file:

package com.example;

public class Actionwords {
    public void iOpenP1(String p1) {
    }

    public void iSearchForP1(String p1) {
    }

    public void aLinkToP1IsShownInTheResults(String p1) {
    }
}

As you can see, the code is pretty empty and does not contain any references to Selenium. The first thing to do is to add the packages we need from Selenium. Below “package com.example”, add the following lines:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedCondition;

The second thing we will need to do is to get an access to the browser from the “Actionwords” class. We will create an instance of the driver object in the constructor. Just after the line “public class Actionwords”, add the following code:

private WebDriver driver;

Actionwords() {
     driver = new FirefoxDriver();
}

Now, if you run the tests again (using “mvn test”), you should see a Firefox window opening.

We’re almost done. We just have to fill the code of each action word.

Our first action word (iOpenP1) is supposed to tell the driver to open a web site. That’s pretty easy to do:

 public void iOpenP1(String p1) {
     driver.get(p1);
 }

The second action word will run the search. To do so, we will find the search input on the Google homepage (which is named “q”), clear the text that might be there, fill it with our own query and submit the form.

 public void iSearchForP1(String p1) {
     WebElement element = driver.findElement(By.name("q"));
     element.clear();
     element.sendKeys(p1);
     element.submit();
 }

The last action word to implement will ensure that Hiptest appears in the search results. The test runner will wait until a link with the correct name appears in the page. If the link with the correct name is not found, an error will be raised and the test will fail.

public void aLinkToP1IsShownInTheResults(String p1) {
    final String matcher = p1;

    (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver d) {
        return d.findElements(By.linkText(matcher)).size() != 0;
        }
    });
}

Now run the tests again and you should see a Firefox windows open and the steps of the test be run in front of you.

Forewords and conclusion

As you can see, integrating Selenium Webdriver to automate your tests is pretty simple and it takes only a few minutes to get an integration test running (as long as the Maven configuration is already there).

There is still some areas we have not covered during this short tutorial, but we’ll explain them in later blog posts:

  • how to use hiptest-publisher instead of the service version to handle updates in the scenarios and action words
  • how to customize the output of the test code

Get started for free - Hiptest

11 thoughts on “Tutorial: a simple Selenium test from Hiptest”

    1. Sorry, that got sent too quickly. Btw, I’m using SpecFlow and Selenium. I imported the scenarios generated from HipTest. But I’m wondering how does it interconnect with Visual Studio? Can we fire up the automated test via HipTest? Will be happy to work this out with you as we are very interested with HipTest

  1. The tutorial doesn’t seem to work. I got as far as the step which opens up Firefox but then none of the Google steps execute. The log/error report is not really that intuitive/easyo to interpret. Any ideas?

    Here’s my error log:

    ——————————————————————————-
    Test set: com.example.ProjectTest
    ——————————————————————————-
    Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 47.735 sec <<< FAILURE!
    warning(junit.framework.TestSuite$1) Time elapsed: 0.015 sec <<< FAILURE!
    junit.framework.AssertionFailedError: Exception in constructor: testFindingASiteOnGoogle (org.openqa.selenium.WebDriverException: Failed to connect to binary FirefoxBinary(C:\Program Files (x86)\Mozilla Firefox\firefox.exe) on port 7055; process output follows:
    ","version":"1.2","type":"extension","internalName":null,"updateURL":null,"updateKey":null,"optionsURL":null,"optionsType":null,"aboutURL":null,"icons":{},"iconURL":null,"icon64URL":null,"defaultLocale":{"name":"Adobe Acrobat – Create PDF","description":"Converts a web page to PDF","creator":"Adobe Systems Incorporated","homepageURL":"http://www.adobe.com/&quot;},"visible":true,"active":false,"userDisabled":true,"appDisabled":false,"descriptor":"C:\\Program Files (x86)\\Adobe\\Acrobat 10.0\\Acrobat\\Browser\\WCFirefoxExtn","installDate":1472034165723,"updateDate":1472034165723,"applyBackgroundUpdates":1,"bootstrap":false,"skinnable":false,"size":516364,"sourceURI":null,"releaseNotesURI":null,"softDisabled":false,"foreignInstall":true,"hasBinaryComponents":false,"strictCompatibility":false,"locales":[],"targetApplications":[{"id":"{ec8030f7-c20a-464f-9b0e-13a3a9e97384}","minVersion":"3.6","maxVersion":"*.*"}],"targetPlatforms":[],"multiprocessCompatible":false,"signedState":0,"seen":true}
    1476371957577 DeferredSave.extensions.json DEBUG Save changes
    1476371957577 addons.xpi DEBUG Updating database with changes to installed add-ons
    1476371957577 addons.xpi-utils DEBUG Updating add-on states
    1476371957582 addons.xpi-utils DEBUG Writing add-ons list
    1476371957591 addons.xpi DEBUG Registering manifest for C:\Program Files (x86)\Mozilla Firefox\browser\features\e10srollout@mozilla.org.xpi
    1476371957591 addons.xpi DEBUG Calling bootstrap method startup on e10srollout@mozilla.org version 1.0
    1476371957591 addons.xpi DEBUG Registering manifest for C:\Program Files (x86)\Mozilla Firefox\browser\features\firefox@getpocket.com.xpi
    1476371957592 addons.xpi DEBUG Calling bootstrap method startup on firefox@getpocket.com version 1.0.4
    1476371957593 addons.xpi DEBUG Registering manifest for C:\Program Files (x86)\Mozilla Firefox\browser\features\loop@mozilla.org.xpi
    1476371957594 addons.xpi DEBUG Calling bootstrap method startup on loop@mozilla.org version 1.4.3
    1476371957611 addons.manager DEBUG Registering shutdown blocker for XPIProvider
    1476371957611 addons.manager DEBUG Provider finished startup: XPIProvider
    1476371957611 addons.manager DEBUG Starting provider: LightweightThemeManager
    1476371957611 addons.manager DEBUG Registering shutdown blocker for LightweightThemeManager
    1476371957611 addons.manager DEBUG Provider finished startup: LightweightThemeManager
    1476371957612 addons.manager DEBUG Starting provider: GMPProvider
    1476371957619 addons.manager DEBUG Registering shutdown blocker for GMPProvider
    1476371957619 addons.manager DEBUG Provider finished startup: GMPProvider
    1476371957619 addons.manager DEBUG Starting provider: PluginProvider
    1476371957619 addons.manager DEBUG Registering shutdown blocker for PluginProvider
    1476371957619 addons.manager DEBUG Provider finished startup: PluginProvider
    1476371957619 addons.manager DEBUG Completed startup sequence
    1476371958370 addons.manager DEBUG Starting provider:
    1476371958371 addons.manager DEBUG Registering shutdown blocker for
    1476371958371 addons.manager DEBUG Provider finished startup:
    1476371958732 DeferredSave.extensions.json DEBUG Write succeeded
    1476371958732 addons.xpi-utils DEBUG XPI Database saved, setting schema version preference to 17
    1476371958733 DeferredSave.extensions.json DEBUG Starting timer
    1476371958764 DeferredSave.extensions.json DEBUG Starting write
    1476371958783 addons.repository DEBUG No addons.json found.
    1476371958784 DeferredSave.addons.json DEBUG Save changes
    1476371958786 DeferredSave.addons.json DEBUG Starting timer
    1476371958844 addons.manager DEBUG Starting provider: PreviousExperimentProvider
    1476371958844 addons.manager DEBUG Registering shutdown blocker for PreviousExperimentProvider
    1476371958844 addons.manager DEBUG Provider finished startup: PreviousExperimentProvider
    1476371958887 DeferredSave.addons.json DEBUG Starting write
    1476371958889 DeferredSave.extensions.json DEBUG Write succeeded
    1476371958906 DeferredSave.addons.json DEBUG Write succeeded

    Build info: version: ‘2.48.2’, revision: ’41bccdd10cf2c0560f637404c2d96164b67d9d67′, time: ‘2015-10-09 13:08:06’
    System info: host: ‘UKMRE048’, ip: ‘10.0.2.8’, os.name: ‘Windows 7’, os.arch: ‘amd64’, os.version: ‘6.1’, java.version: ‘1.8.0_101’
    Driver info: driver.version: FirefoxDriver
    at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:135)
    at org.openqa.selenium.firefox.FirefoxDriver.startClient(FirefoxDriver.java:271)
    at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:117)
    at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:216)
    at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:211)
    at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:207)
    at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:120)
    at com.example.Actionwords.(Actionwords.java:16)
    at com.example.ProjectTest.(ProjectTest.java:7)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at junit.framework.TestSuite.createTest(TestSuite.java:61)
    at junit.framework.TestSuite.addTestMethod(TestSuite.java:294)
    at junit.framework.TestSuite.addTestsFromTestCase(TestSuite.java:150)
    at junit.framework.TestSuite.(TestSuite.java:129)
    at org.junit.internal.runners.JUnit38ClassRunner.(JUnit38ClassRunner.java:71)
    at org.junit.internal.builders.JUnit3Builder.runnerForClass(JUnit3Builder.java:14)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:250)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
    Caused by: org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output:
    “,”version”:”1.2″,”type”:”extension”,”internalName”:null,”updateURL”:null,”updateKey”:null,”optionsURL”:null,”optionsType”:null,”aboutURL”:null,”icons”:{},”iconURL”:null,”icon64URL”:null,”defaultLocale”:{“name”:”Adobe Acrobat – Create PDF”,”description”:”Converts a web page to PDF”,”creator”:”Adobe Systems Incorporated”,”homepageURL”:”http://www.adobe.com/”},”visible”:true,”active”:false,”userDisabled”:true,”appDisabled”:false,”descriptor”:”C:\\Program Files (x86)\\Adobe\\Acrobat 10.0\\Acrobat\\Browser\\WCFirefoxExtn”,”installDate”:1472034165723,”updateDate”:1472034165723,”applyBackgroundUpdates”:1,”bootstrap”:false,”skinnable”:false,”size”:516364,”sourceURI”:null,”releaseNotesURI”:null,”softDisabled”:false,”foreignInstall”:true,”hasBinaryComponents”:false,”strictCompatibility”:false,”locales”:[],”targetApplications”:[{“id”:”{ec8030f7-c20a-464f-9b0e-13a3a9e97384}”,”minVersion”:”3.6″,”maxVersion”:”*.*”}],”targetPlatforms”:[],”multiprocessCompatible”:false,”signedState”:0,”seen”:true}
    1476371957577 DeferredSave.extensions.json DEBUG Save changes
    1476371957577 addons.xpi DEBUG Updating database with changes to installed add-ons
    1476371957577 addons.xpi-utils DEBUG Updating add-on states
    1476371957582 addons.xpi-utils DEBUG Writing add-ons list
    1476371957591 addons.xpi DEBUG Registering manifest for C:\Program Files (x86)\Mozilla Firefox\browser\features\e10srollout@mozilla.org.xpi
    1476371957591 addons.xpi DEBUG Calling bootstrap method startup on e10srollout@mozilla.org version 1.0
    1476371957591 addons.xpi DEBUG Registering manifest for C:\Program Files (x86)\Mozilla Firefox\browser\features\firefox@getpocket.com.xpi
    1476371957592 addons.xpi DEBUG Calling bootstrap method startup on firefox@getpocket.com version 1.0.4
    1476371957593 addons.xpi DEBUG Registering manifest for C:\Program Files (x86)\Mozilla Firefox\browser\features\loop@mozilla.org.xpi
    1476371957594 addons.xpi DEBUG Calling bootstrap method startup on loop@mozilla.org version 1.4.3
    1476371957611 addons.manager DEBUG Registering shutdown blocker for XPIProvider
    1476371957611 addons.manager DEBUG Provider finished startup: XPIProvider
    1476371957611 addons.manager DEBUG Starting provider: LightweightThemeManager
    1476371957611 addons.manager DEBUG Registering shutdown blocker for LightweightThemeManager
    1476371957611 addons.manager DEBUG Provider finished startup: LightweightThemeManager
    1476371957612 addons.manager DEBUG Starting provider: GMPProvider
    1476371957619 addons.manager DEBUG Registering shutdown blocker for GMPProvider
    1476371957619 addons.manager DEBUG Provider finished startup: GMPProvider
    1476371957619 addons.manager DEBUG Starting provider: PluginProvider
    1476371957619 addons.manager DEBUG Registering shutdown blocker for PluginProvider
    1476371957619 addons.manager DEBUG Provider finished startup: PluginProvider
    1476371957619 addons.manager DEBUG Completed startup sequence
    1476371958370 addons.manager DEBUG Starting provider:
    1476371958371 addons.manager DEBUG Registering shutdown blocker for
    1476371958371 addons.manager DEBUG Provider finished startup:
    1476371958732 DeferredSave.extensions.json DEBUG Write succeeded
    1476371958732 addons.xpi-utils DEBUG XPI Database saved, setting schema version preference to 17
    1476371958733 DeferredSave.extensions.json DEBUG Starting timer
    1476371958764 DeferredSave.extensions.json DEBUG Starting write
    1476371958783 addons.repository DEBUG No addons.json found.
    1476371958784 DeferredSave.addons.json DEBUG Save changes
    1476371958786 DeferredSave.addons.json DEBUG Starting timer
    1476371958844 addons.manager DEBUG Starting provider: PreviousExperimentProvider
    1476371958844 addons.manager DEBUG Registering shutdown blocker for PreviousExperimentProvider
    1476371958844 addons.manager DEBUG Provider finished startup: PreviousExperimentProvider
    1476371958887 DeferredSave.addons.json DEBUG Starting write
    1476371958889 DeferredSave.extensions.json DEBUG Write succeeded
    1476371958906 DeferredSave.addons.json DEBUG Write succeeded

    at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:123)
    … 34 more
    )
    at junit.framework.Assert.fail(Assert.java:50)
    at junit.framework.TestSuite$1.runTest(TestSuite.java:97)
    at junit.framework.TestCase.runBare(TestCase.java:134)
    at junit.framework.TestResult$1.protect(TestResult.java:110)
    at junit.framework.TestResult.runProtected(TestResult.java:128)
    at junit.framework.TestResult.run(TestResult.java:113)
    at junit.framework.TestCase.run(TestCase.java:124)
    at junit.framework.TestSuite.runTest(TestSuite.java:243)
    at junit.framework.TestSuite.run(TestSuite.java:238)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)

  2. I am having problems with where your action words file was edited for ruby/rspec which was loaded back into hiptest to automate thetest.

  3. If don’t mind, can you send me a screen short of how and what you changed in the action word file to be able to update the test script? thank you

  4. I have few selenium scripts created using maven and TestNG, can I launch my scripts from hiptest and pass parameters? If yes how?

  5. Well, here are sharing such a great and helpful information regarding selenium Testing. Thanks for the publish great ideas in this blog!

  6. Pingback: How to execute your BDD scenarios with CrossBrowserTesting – Get to market faster with the product you imagined

Leave a Reply

Your email address will not be published. Required fields are marked *