Integrating with Selenium

Selenium WebDriver is a test tool that allows you to write automated web application user interface tests in any programming language against any HTTP website using any mainstream JavaScript-enabled browser. Selenium WebDriver comes in two parts.

An API or library for the web browser itself that is used to allow external applications to connect to the web browser and instruct it to perform certain operations.

Client libraries for various computer languages - these are referred to as 'language bindings.

Therefore to use SpiraTest with Selenium WebDriver (hereafter referred to as just Selenium), you need to decide which client driver you want to use, and then use the appropriate integration between SpiraTest and that driver's underlying platform/language. Any unit test framework listed in this guide can be used with Selenium (in addition to just running unit tests), we have some examples below for .NET, Java and Python.

Using the .NET Driver

To use the .NET driver for Selenium with SpiraTest, you will need to run your Selenium tests within the context of an NUnit test fixture. Once you have configured NUnit for use with SpriaTest, there is one change that needs to be made to the SpiraTest NUnit configuration so that the Selenium tests report back to SpiraTest as 'Selenium' rather than "NUnit' so you can distinguish between them.

Supplied with the SpiraTest NUnit add-in is a sample test for using Selenium with SpiraTest:

using System;
using NUnit.Framework;
using Inflectra.SpiraTest.AddOns.SpiraTestNUnitAddIn.SpiraTestFramework;
using Selenium;

namespace SeleniumSampleTest
{
    /// <summary>
    /// Sample test fixture that tests the NUnit SpiraTest integration with the
    /// Selenium-RC .NET Driver
    /// </summary>
    [
    TestFixture,
    SpiraTestConfiguration("http://localhost/SpiraTest", "fredbloggs", "fredbloggs", 1, 1, 2, SpiraTestConfigurationAttribute.RunnerName.Selenium)
    ]
    public class GoogleTest
    {
        private static ISelenium selenium;

        [SetUp]
        public void SetupTest()
        {
            //Instantiate the selenium .NET proxy
            selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://www.google.com");
            selenium.Start();
        }

        [TearDown]
        public void TeardownTest()
        {
            selenium.Stop();
        }

        /// <summary>
        /// Sample test that searches on Google, passes correctly
        /// </summary>
        [
        Test,
        SpiraTestCase (5)
        ]
        public void GoogleSearch()
        {
            //Opens up Google
            selenium.Open("http://www.google.com/webhp");

            //Verifies that the title matches
            Assert.AreEqual("Google", selenium.GetTitle());
            selenium.Type("q", "Selenium OpenQA");

            //Verifies that it can find the Selenium website
            Assert.AreEqual("Selenium OpenQA", selenium.GetValue("q"));
            selenium.Click("btnG");
            selenium.WaitForPageToLoad("5000");
            Assert.IsTrue(selenium.IsTextPresent("www.openqa.org"));
            Assert.AreEqual("Selenium OpenQA - Google Search", selenium.GetTitle());
        }   
    }
}

The details of the sample itself are described in more detail on the Selenium website, and you can see that we have added the SpiraTest specific attributes onto the test case and test methods to indicate that they need to report back to SpiraTest.

However there is one change that has been made to the SpiraTestConfiguration attribute applied to the test fixture -- an extra SpiraTestConfigurationAttribute.RunnerName.Selenium parameter has been specified. This tells the SpiraTest NUnit add-in to report the results back as being generated by Selenium rather than NUnit.

Using the Java Driver

To use the Java driver for Selenium with SpiraTest, you will need to run your Selenium tests within the context of a TestNG test fixture. Once you have configured TestNG for use with SpriaTest, there is one change that needs to be made to the SpiraTest TestNG configuration so that the Selenium tests report back to SpiraTest as 'Selenium' rather than "TestNG' so you can distinguish between them.

Supplied with the SpiraTest TestNG listener is a sample test for using Selenium with SpiraTest:

The details of the sample itself are described in more detail on the Selenium website, and you can see that we have added the SpiraTest specific attributes onto the test case and test methods to indicate that they need to report back to SpiraTest.

package com.inflectra.spiratest.addons.testnglistener.samples;

import org.testng.annotations.*;
import static org.testng.AssertJUnit.*;
import com.thoughtworks.selenium.*;

import com.inflectra.spiratest.addons.testnglistener.*;

/**
 * A sample Selenium test using the ability to return results back to SpiraTest
 * 
 * @author      Inflectra Corporation
 * @version     2.3.0
 *
 */
@SpiraTestConfiguration(
url="http://localhost/SpiraTest",
login="fredbloggs",
password="fredbloggs",
projectId=1,
releaseId=1,
testSetId=-1
runner=RunnerName.Selenium
)
@Test(groups={"seleniumtest"})
public class SeleniumTest
{
    private Selenium selenium;

    @BeforeClass
    public void setUp()
    {
        //Instantiate the selenium Java proxy
        String url = "http://www.google.com";
        selenium = new DefaultSelenium("localhost", 4444, "*firefox", url);
        selenium.start();
    }

    @AfterClass
    protected void tearDown()
    {
        selenium.stop();
    }

    // Sample test that searches on Google, passes correctly
    @Test(groups={"seleniumtest"})
    @SpiraTestCase(testCaseId=5)
    public void testGoogle()
    {
        //Opens up Google
        selenium.open("http://www.google.com/webhp?hl=en");

        //Verifies that the title matches
        assertEquals("Google", selenium.getTitle());
        selenium.type("q", "Selenium OpenQA");

        //Verifies that it can find the Selenium website
        assertEquals("Selenium OpenQA", selenium.getValue("q"));
        selenium.click("btnG");
        selenium.waitForPageToLoad("5000");
        assertEquals("Selenium OpenQA - Google Search", selenium.getTitle());
    }
}

However there is one change that has been made to the SpiraTestConfiguration attribute applied to the test fixture -- an extra runner=RunnerName.Selenium parameter has been specified. This tells the SpiraTest TestNG listener to report the results back as being generated by Selenium rather than TestNG.

Using the Python Driver

To use the Python driver for Selenium with SpiraTest, you will need to run your Selenium tests within the context of a PyUnit unit-test fixture. Once you have configured PyUnit for use with SpriaTest, there is one change that needs to be made to the SpiraTest PyUnit configuration so that the Selenium tests report back to SpiraTest as 'Selenium' rather than "PyUnit' so you can distinguish between them.

Supplied with the SpiraTest PyUnit extension is a sample test for using Selenium with SpiraTest:

from selenium import selenium
import unittest
import sys, time
import spiratestextension

#   A sample Selenium test using the ability to return results back to SpiraTest
#
#   Author      Inflectra Corporation
#   Version     2.3.0
#
class TestSeleniumSample(unittest.TestCase):

    seleniumHost = 'localhost'
    seleniumPort = str(4444)
    browserStartCommand = "*firefox"
    browserURL = "http://www.google.com"

    def setUp(self):
        print "Using selenium server at " + self.seleniumHost + ":" + self.seleniumPort
        self.selenium = selenium(self.seleniumHost, self.seleniumPort, self.browserStartCommand, self.browserURL)
        self.selenium.start()

    def testGoogle__4(self):
        selenium = self.selenium
        selenium.open("http://www.google.com/webhp?hl=en")

        #Verifies that the title matches
        self.assertEqual ("Google", selenium.get_title())
        selenium.type("q", "Selenium OpenQA")

        #Verifies that it can find the Selenium website
        self.assertEqual("Selenium OpenQA", selenium.get_value("q"))
        selenium.click("btnG")
        selenium.wait_for_page_to_load("5000")
        self.assertEqual("Selenium OpenQA - Google Search", selenium.get_title())

    def tearDown(self):
        self.selenium.stop()

suite = unittest.TestLoader().loadTestsFromTestCase(TestSeleniumSample)
testResult = unittest.TextTestRunner(verbosity=2).run(suite)
releaseId = 1
testSetId = -1
spiraTestExtension = spiratestextension.SpiraTestExtension()
spiraTestExtension.projectId = 1
spiraTestExtension.server = "localhost"
spiraTestExtension.port = 80
spiraTestExtension.path = "SpiraTest"
spiraTestExtension.userName = "fredbloggs"
spiraTestExtension.password = "fredbloggs"
spiraTestExtension.recordResults(TestSeleniumSample, testResult, releaseId, testSetId, "Selenium")

The details of the sample itself are described in more detail on the Selenium website, and you can see that we have added the SpiraTest specific test case suffixes and reporting code into the test methods to indicate that they need to report back to SpiraTest.

However there is one change that has been made to the spiraTestExtension.recordResults method called at the end of the test case. An extra string parameter has been specified that contains "Selenium". This tells the SpiraTest PyUnit extension to report the results back as being generated by Selenium rather than PyUnit.