Thursday, February 9, 2012

Webdriver: TestNG + Eclipse + Java

In the beginning it was enough to Run my tests from Eclipse just by clicking Run button. After some time I realized that I need to run tests with different setup - e.g. different users/passwords, different channels, different browsers, run particular classes/methods in groups etc. I found TestNG is what I need and even more. With TestNG it is possible to:
  • parametrize tests
  • run tests in separate sets grouped by classes or methods
  • run tests in multiple threads
  • generate test reports
  • use annotations
  • and more..

The installation and getting started with testng is very well described on their site testng.org - shortly:
  1. Open Eclipse
  2. Help > Install New Software...
  3. Add http://beust.com/eclipse repository and complete the proccess
  4. Window > Show View > Other.. select Java > TestNG > OK
  5. Create a new TestNG class for existing package (test): right-click on the package > New > TestNG class (name it TestExample)
  6. Copy the following code to the class

package test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.testng.annotations.Parameters;
 

public class TestExample {
 @BeforeTest
 @Parameters({"usrName", "pwd"})
 public void setUp(String usrName, String pwd) {
   // code that will be executed before tests
 }
 @Test
 public void test1() {
      // test1 steps
      System.out.println("test1 passed");
 }
 @Test
 public void test2() {
      // test2 steps
      System.out.println("test2 passed");
 }
}


     7. Modify the XML configureation (testng.xml by default) as follows

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
 <test name="TestName" preserve-order="true">
      <parameter name="usrName"  value="test"/>
      <parameter name="pwd"  value="1234"/>
     <classes>
                  <class name="test.TestExample">
                        <methods>
                              <include name="test1" />
                              <include name="test2" />
                        </methods>
                  </class>
            </classes>
   </test>
</suite>

    8. Run testng.xml

Console results:

[TestNG] Running:
  C:\workspace\webdriver\testng.xml

test1 passed
test2 passed

===============================================
Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

TestNG results:



1 comment:

  1. Hi buddy, I'm working with the same tools, but I have a little problem. I need to compile a test into an exe file, so I can distribute at least one test to my client in order to show them a running test, without giving the code. do you know any way yo create an exe test based on webdriver, testng and java?? thanks in advance!

    ReplyDelete