My First TestNG TestCase


A TestNG testsuite comprises of number of tests. A TestNG  ‘test’ comprises of one or more classes, and a TestNG class consists of multiple ‘Test Methods’.

Here i’m sharing a sample code for the following testcases.

Testcase 1 : Verifying Sign-On link

Testcase 2 : Get the source code

Testcase 3 : Validate the source code

import java.io.BufferedWriter;
import java.io.FileWriter;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Testng1 {
public String url = “http://newtours.demoaut.com/”;
public String expected;
public String actual;
public String sourcecode;
public WebDriver fd;
@BeforeTest
public void launchbrowser() {
fd = new FirefoxDriver();
fd.get(url);
}

@BeforeMethod
public void verifytitle() {
expected = “Welcome: Mercury Tours”;
actual = fd.getTitle();
Assert.assertEquals(actual, expected);
System.out.println(actual);
Reporter.log(“<font color=’green’> <b>title is verified</b></font>”);

}

@Test
public void login() throws Exception {
fd.findElement(By.linkText(“SIGN-ON”)).click();
expected = “Sign-on: Mercury Tours”;
actual = fd.getTitle();
Assert.assertEquals(actual, expected);
System.out.println(actual);
Reporter.log(“<font color=’green’> <b>contact is verified</b></font>”);
}

@Test
public void getsourcecode() throws Exception {
sourcecode = fd.getPageSource();
FileWriter fn = new FileWriter(“e:\\source.txt”);
BufferedWriter br = new BufferedWriter(fn);
br.write(sourcecode);
br.close();
}

@AfterMethod
public void homelink() {
fd.findElement(By.linkText(“Home”)).click();

}

@AfterTest
public void close() {
fd.quit();
}

}

 If you refresh your project, there will be a folder ‘test-output’ generated.In that folder you will ‘index.html’. This is the report which testng generates by default.
If you open index.html in a browser, you can the test cases which are passed and which are failed.
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s