JUNIT Annotations, Commands and Assertions


The following annotations are used frequently in Selenium scripts:

@Ignore - test methods that use this annotation will be ignored at run time

@Test - identifies a test method; methods that don't have @Test won' be executed at run time 

@Before - method will be run before every JUnit @Test method

@After - method will be run after every JUnit @Test method

@BeforeClass - method will be run once before any methods in that class

@AfterClass - method will be run once after all methods in the class


Some of the commands may return one or more results depending on the number of HTML elements     matched by the XPATH locator.

selenium.open(page's relative path)

usage: opens the page that has the relative path provided as argument
example: selenium.open("/selenium/search.php");

selenium.type(XPATH locator of the HTML element, value)

usage: types the value in the HTML element found through the XPATH value
example: selenium.type("xpath=//input[@name='q']", value);

selenium.click(XPATH locator of the HTML element)

usage: clicks the HTML element found through the XPATH value
example: selenium.click("//a[@title = 'a12345']");


usage: wait for some time for the page to load


usage: gets the value of the title of the current page

selenium.getText(XPATH locator of the HTML element)

usage: gets the text of the HTML element found through the locator
example: selenium.getText("//p[@class = 'abcdef']");

selenium.submit(XPATH locator of the form)

usage: submits a HTML form found through the locator
example: selenium.submit("//form[@id='HTMLFormElements']");

selenium.check(XPATH locator of the checkbox)

usage: checks a checkbox found through the locator
example: selenium.check("//input[@name='checkboxes[]' and @value='cb2']");

selenium.uncheck(XPATH locator for the checkbox)

usage: un-checks a checkbox found through the locator
example: selenium.uncheck("//input[@name='checkboxes[]' and @value='cb3']");

selenium.getXPathCount(XPATH locator)

usage: returns the number of the HTML elements matched by the XPATH locator


The most important assertions for Selenium are assertTrue and assertEquals.

assertTrue(text displayed if the condition is false, condition)

usage: returns true if the condition returns true
example: assertTrue("No homepage URL found ",matchingCountTotal>0)

assertEquals(text displayed if expected value <> actual value, expected value, actual value)

usage: returns true if the expected value is equal with the actual value
example: assertEquals("the title is not correct", "HTML Form Elements",selenium.getTitle());

Find HTML Elements With XPATH

NEXT - Selenium Basics 6 - Annotations, Commands And Assertions

VIDEO - Find HTML elements using XPATH 1

VIDEO - Find HTML elements using XPATH 2

This post is dedicated to XPATH which will be used for finding HTML elements in the browser DOM.

SELENIUM has one more way for finding HTML elements called CSS Selector.

The following will be investigated for XPATH:

- the structure of the browser DOM

- element tags, attributes and values

- finding elements using the absolute path

- searching for elements using attribute values

- searching for elements using XPATH functions

- XPATH locators that return more than one result

Selenium RC Setup

1. install FIREFOX

2. install the FireBug add-on for FIREFOX (from https://addons.mozilla.org/en-US/firefox/addon/firebug/)

3. install the FirePath add-on for FIREFOX (from https://addons.mozilla.org/en-US/firefox/addon/firepath/)

4. install the Selenium IDE for FIREFOX (from http://release.seleniumhq.org/selenium-ide/2.5.0/selenium-ide-2.5.0.xpi)

5. install Java (from http://java.com/en/)

6. download the Selenium Server (from http://selenium-release.storage.googleapis.com/2.41/selenium-server-standalone-2.41.0.jar)

7. download the Selenium client driver (from http://selenium-release.storage.googleapis.com/2.41/selenium-java-2.41.0.zip)

8. create a Selenium folder in your C drive (or in any other location);
   copy the Selenium Server jar file in the Selenium folder;
   extract the Selenium client driver zip file in the Selenium folder;

9. start the Selenium server from the command line:
   start Command Prompt
   cd c:\
   cd Selenium
   java -jar selenium-server-standalone-2.41.0
10. install Eclipse IDE for Java developers (from http://www.eclipse.org/downloads/); make sure that you install the one for your operating system (32 bit or 64 bit)

11. start Eclipse

12. create a new Java project in Eclipse and call it VPL

Selenium Installation Video