Hi Im stuck in a situation where I have declared my driver object in the test class and I have some wrappers developed in another class , all the respective variables are not static including the driver object , so now I want to take a screenshot on failure using testng listerners (I have created a custom listener class implementing to ITestListener) and when I refer to the driver object it throws a null exception probably due to driver not been static , how can I still achieve to take a screenshot without making the driver static , any testng related work around will also be helpful
this is the code line in my custom listenerclass
import com.rationaleemotions.threadsafe.TestClassExample.ExampleListener;
import java.util.Objects;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;
import org.testng.Assert;
import org.testng.ITestListener;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
@Listeners(ExampleListener.class)
public class TestClassExample {
private static final String ATTRIBUTE = "test.data";
@BeforeMethod
public void beforeMethod(ITestResult itr) {
//Replace this with your webdriver instantiation logic
Data data = new Data(UUID.randomUUID().toString());
itr.setAttribute(ATTRIBUTE, data);
ExampleListener.prettyPrint("BeforeMethod", itr);
}
@Test
public void passingTest() {
ITestResult itr = Reporter.getCurrentTestResult();
ExampleListener.prettyPrint("TestCase", itr);
}
@Test
public void failingTest() {
ITestResult itr = Reporter.getCurrentTestResult();
ExampleListener.prettyPrint("TestCase", itr);
Assert.fail("Simulating a failure");
}
public static class ExampleListener implements ITestListener {
@Override
public void onTestSuccess(ITestResult result) {
Data data = prettyPrint("onTestSuccess", result);
// The close() method is similar to you doing driver.quit()
data.close();
}
@Override
public void onTestFailure(ITestResult result) {
Data data = prettyPrint("onTestFailure", result);
// The close() method is similar to you doing driver.quit()
data.close();
}
public static Data prettyPrint(String prefix, ITestResult itr) {
Data data = (Data) itr.getAttribute(ATTRIBUTE);
Objects.requireNonNull(data, "Should have had a valid data object");
String msg = String.format("[%s] %s() ==> %s", prefix, itr.getMethod().getQualifiedName(), data);
System.err.println( msg);
return data;
}
}
@AllArgsConstructor
@Getter
@ToString
public static class Data {
private final String content;
public void close() {
System.err.println("Simulating a close operation");
}
}
}--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/5bd6048c-765a-410a-bd1b-92497f5a7119n%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/testng-users/6bb3987f-00f2-4cc5-8956-c1017cb813cdn%40googlegroups.com.