Skip to content
freynaud edited this page Nov 16, 2011 · 9 revisions

Writing a test containing web steps

A java method will be considered a test for testNG if you annotate it with @Test. It becomes a web test if you annotate it with @WebTest.

It's as simple as that.No need to extend a base class etc.

  @Test
  @WebTest
   public void test() {
     // WebDriver protocol allow the webdriver API
     webdriver().get("http://www.ebay.co.uk");
     // but also the selenium legacy API
   }

annotating a method with @WebTest will make sure you have a browser assigned to you before the method start, and released when it finishes.

You can access the driver for your test using webdriver().XXX

using web driver protocol

By default, @WebTest creates a WebDriver driver.

@WebTest is equivalent to @WebTest(protocol = SeleniumProtocol.WebDriver)

Check Demo1.java to have an example.

using the selenium legacy protocol

@WebTest(protocol = SeleniumProtocol.Selenium)

check [Demo2.java] (https://github.com/freynaud/testng-support/blob/master/src/test/java/Demo2.java) to have an example.

Running the tests locally

Running tests locally is done by starting a grid hub locally and attaching a local node to it. This has several advantages compared to starting the the local driver.

  • the architecture will be the same for remote runs. The matching between the requests and the browser are different if you're using grid or a local driver. By starting a local grid you get rid of that difference, avoid surprises later.
  • a local server do not have a limit of browsers it can start. If you're debugging a test with a dataprovider of 100 objects, you will end up having 100 browsers running in parallel locally. The debugging won't be pleasant :)

configuring the local grid

The local vs remote grid is handled by testNG listener LocalGrid.java It's plugged in in template.xml <listener class-name="org.openqa.selenium.support.testng.LocalGrid"></listener>

To start a grid locally rather than using a remote one, just comment out <!-- <parameter name="url" value="http://mygridhub:4444/wd/hub"></parameter>--> in template.xml.

If you do so, you need to also specify what browser you have installed on your system, and what protocol you want to use for them. That is documented in the node.json config file.

Running the tests against a selenium grid

Uncomment <parameter name="url" value="http://mygridhub:4444/wd/hub"></parameter> and specify the url of your grid.

Writing a real test using PageObjects.

See Demo3.java

next> integrate with Jenkins