Listners in Testng Framework
- @listner is a testNG annotation
· This is one very important annotation if you chose to have some default behavior for your test case when it fails, passes and skip etc.
· 2. You can either extend 'TestListenerAdapter' or implement Interface 'ITestListener' which is a listener for test running.
Eg : if you extend TestListenerAdapter class and write your implementation in a custom listner to override onTestFailure or onTestSuccess methods you will be able to do anything which you want when a test fails or passes, like taking screenshots,printing logs etc.
i.e) Suppose your test case is passed and you want to write something in console at that case just you need to override a method
onTestSuccess
(ITestResult tr)
Suppose your test case is failed and you want to take screenshot at that case just you need to override a method onTestFailure
(ITestResult tr)
Methods available in TestListnerAdapter
· 1. OnTestSuccess : Invoked each time a test succeeds.
· 2. OnTestFailure : Invoked each time a test fails
We can implement any logic that you want to do when a test fails, Normally most of them prefer taking screen shots when a test fails. Here in this method we can add a logic to take the screen shot and the name of the test as screenshot name.
We can implement any logic that you want to do when a test fails, Normally most of them prefer taking screen shots when a test fails. Here in this method we can add a logic to take the screen shot and the name of the test as screenshot name.
· OnTestSkipped : Invoked each time a test is skipped.
In this program :
- Navigating to gmail
driver.get("http://www.gmail.com");
2. Verifying the Heading
Assert.assertEquals(Heading, "One account. All of Google.");
3. Failing the testcase because there is no signin webelement
driver.findElement(By.name("signin")).click();
In listnerclass.java:
1. For Passed test am just printing like "Test Passed" in console
public void onTestSuccess(ITestResult tr) {
System.out.println("TestPassed");
}
2 . For Failed Test am taking Screenshot and store it in (D:\\failure.png) drive
public void onTestFailure(ITestResult itr)
{
File scrFile = ((TakesScreenshot)Properties.driver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
try {
FileUtils.copyFile(scrFile, new File("D:\\failure.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Taken screenshot because testcase failed");
}
Lets see the above things in action.
· 1. Create a new package name it as listener.
2. Create a new class properties.java
This is used to intialize the Webdriver.
Source code for properties.java
package listner;
import org.openqa.selenium.WebDriver;
public class Properties {
public static WebDriver driver;
}
3 Create a simple class named as Listner and extends TestListenerAdapter
3 . extends TestListenerAdapter
After extends TestListenerAdapter am going to override some methods.
* onTestSuccess(ITestResult tr)
* onTestFailure(ITestResult itr)
Source code for MyListner.java
package listner;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;
public class MyListner extends TestListenerAdapter{
public void onTestSuccess(ITestResult tr) {
System.out.println("TestPassed");
}
//Am going to take screenshot on failure
public void onTestFailure(ITestResult itr)
{
File scrFile = ((TakesScreenshot)Properties.driver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
try {
FileUtils.copyFile(scrFile, new File("D:\\failure.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Taken screenshot because testcase failed");
}
}
Make a new class named as Logintest in which your testcases are running and write the @Testng annotations and create two Testcases in this class
4. Create a new Class name it us Logintest
Source code for logintest.java
package listner;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;
public class Logintest extends Properties{
public WebDriver driver;
public Logintest()
{
Properties.driver = new FirefoxDriver();
driver = Properties.driver;
}
@Test(priority=0)
public void verifytextsucess()
{
driver.get("http://www.gmail.com");
//Verify the heading is correct
WebElement text=driver.findElement(By.xpath("html/body/div[1]/div[2]/div[1]/h1"));
String Heading=text.getText();
System.out.println(Heading);
Assert.assertEquals(Heading, "One account. All of Google.");
}
@Test(priority=1)
public void loginfail()
{
//This test will fail because there is no signin webelement
driver.get("http://www.gmail.com");
driver.findElement(By.name("Email")).sendKeys("kk.prashanth65@gmail.com");
driver.findElement(By.name("Passwd")).sendKeys("smartboys");
//signin webelement is not present
driver.findElement(By.name("signin")).click();
}
@AfterTest
public void endtest()
{
driver.quit();
}
}
5. The below is the Test.xml file.
In the above Test.xml file, We need to pass the class name where we have the test methods. And also we should define listeners in testng.xml file so that TestNG can use them to rewrite the annotations.
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="My Test Suite">
<listeners>
<listener class-name="listner.MyListner" />
</listeners>
<test name="Logintest">
<classes>
<class name="listner.Logintest" ></class>
</classes>
</test>
</suite>
Now right click in Test.xml - > RunAs -> TestNG Suit
Once you run. your Test output look like below.
It will also take the screenshot once its complete the Test
The screenshot looks like above..Because the Webelement button name is incorrect in Test.
No comments:
Post a Comment