Skip to content
Ondřej Košarko edited this page Jan 11, 2016 · 1 revision

Selenium

Selenium is a testing framework for automated testing of web applications. You can find more information about selenium here http://docs.seleniumhq.org/docs. We are using selenium with TestNG (http://testng.org), which is a testing framework inspired by JUnit. It gives us flexibility xml based configuration and automated reports. More about how to use Selenium and TestNG together can be found here http://testng.org/doc/selenium.html.

Configuration

Selenium has two versions Selenium 2 (WebDriver) and Selenium 1 (RemoteControl). We are using Selenium 2 but still using the old framework i.e Remote Control because the new version not yet support some of the functionality. For running Selenium Remote Control, a selenium host should be started that listens to clients (browsers) which runs the test cases. In our setup all of this is configured in the BaseTestCase java class.

There are two configuration files: testng.xml and local.conf.

testng.xml

It contains the configuration and sequence of test cases to run. It also contains the following three important parameters that needs to be configured to run Selenium.

  <parameter name="selenium.host" value="localhost" />
  <parameter name="selenium.port" value="8123" />
  <parameter name="selenium.browser" value="*firefox" />

The selenium will use the host and port parameters to start a listener and use the browser parameter to load a browser that will run the test cases.

Note that each test in testng.xml will load a new browser session.

local.conf

In local.conf define the following properties

homeURL = /xmlui
browseURL = /xmlui/browse
searchURL = /xmlui/discover
submissionURL = /xmlui/submissions
submitURL = /xmlui/submit
localLogin = /xmlui/password-login

UserID = 
Password = 
UserName = 

adminUserID = 
adminPassword = 
adminUserName = 

new users can be defined using a prefix with UserID, Password, and UserName properties e.g. an admin user is defined above. The user can be passed as a parameter to a test case in testng.xml. see the example below:

  <test name="Test Item Submission with Admin" preserve-order="true">
    <parameter name="user" value="admin" />  
    <classes>
      <class name="cz.cuni.mff.ufal.test.LoginTest">
        <methods>
          <include name="testLoginLocal" />
        </methods>
      </class>    
      <class name="cz.cuni.mff.ufal.test.SubmissionTest">
        <methods>
          <include name="testSubmissionWithAdmin" />
        </methods>
      </class>
    </classes>
  </test>

Creating a new Test Case

Create a new Java class and extends it with cz.cuni.mff.ufal.test.BaseTestCase. Create a new method and write the selenium commands for the test case in it. This method can also be generated using Selenium IDE plugin for firefox see http://docs.seleniumhq.org/docs/02_selenium_ide.jsp for details. We need to export the Selenium IDE test case to “Java / TestNG / Remote Control”

When using Selenium IDE to generate the code, only copy the code inside the method to the method you created and change all the assertion methods to org.testng.AssertJUnit methods. Selenium IDE is using a different wrapper class SeleneseTestNgHelper but that is not working properly with Remote Control.

Following is a sample test case for testing the UFAL Point home page.

public class HomePageTest extends BaseTestCase {

    @Test
    public void testHomePageLoad() {
        selenium.open(prop.getProperty("homeURL"));
        selenium.waitForPageToLoad("30000");
        AssertJUnit.assertEquals(selenium.getTitle(), "Home");
    }


}

Add the new Test to testng.xml.

<suite name="UFAL Point Test Setup" verbose="10">
  <test name="Test Home Page Loading" preserve-order="true">
    <classes>
      <class name="cz.cuni.mff.ufal.test.HomePageTest">
        <methods>
          <include name="testHomePageLoad" />
        </methods>
      </class>
  </test>
</suite>

Running tests from command line

From command line, the testng.xml can be run with ant using the “testng” ant task. see the build.xml file.

Running tests without displaying a browser

see http://www.alittlemadness.com/2008/03/05/running-selenium-headless

In our makefile scripts there is a target “selenium_tests” that will run all the tests in testng.xml without opening a browser.

Clone this wiki locally