extent-framework / extentreports-testng-adapter

TestNG Adapter for Extent Framework
http://extentreports.com/docs/versions/4/java/testng.html
Apache License 2.0
50 stars 14 forks source link

Info(msg) is not working in before method/class #5

Closed Rahul-QA closed 4 years ago

Rahul-QA commented 5 years ago

Hi Anshoo,

I am getting nullpointer exception for using info in @beforemethod

ExtentTestManager.getTest().info("inside beforeMethod");

beforemethod

anshooarora commented 4 years ago

Which listener? Complete stacktrace?

Ashwini1980 commented 4 years ago

Hi,

I am also getting null pointer exceptions while using ExtentTestManager.getTest().info("inside beforeMethod");.

I am using the ExtentITestListenerClassAdapter class.

Code:

public class Test4 {

WebDriver driver;

@BeforeSuite
public void setUp() {

    ExtentService.getInstance().setSystemInfo("Dev Name", "Ashwin Pattanayak");
    ExtentService.getInstance().setSystemInfo("Broser Name", "Chrome");
    ExtentService.getInstance().setSystemInfo("OS", "WINDOWS 10");
}

@BeforeMethod
public void login() {

    System.setProperty("webdriver.chrome.driver", 

System.getProperty("user.dir")+"/src/test/resources/executables/chromedriver.exe"); driver = new ChromeDriver(); System.out.println("Launching Chrome driver"); ExtentTestManager.getTest().info("Launching Chrome driver"); }

@Test
public void getTitle() {

    ExtentTestManager.getTest().info("Test Execution started.");
    driver.get("https://www.google.com");
    String title = driver.getTitle();
    System.out.println("Title is: "+title);
}

Exception:

image

It seems the problem is while creating the method on the listener clas: ExtentTestManager.createMethod(result, true);

Please correct me if I am wrong. Please give us a solution please so that we will not get null pointer exception.

Ashwini1980 commented 4 years ago

Hi Anshoo,

I am getting nullpointer exception for using info in @beforemethod

ExtentTestManager.getTest().info("inside beforeMethod");

beforemethod

Hi Rahul..

Were you able to resolve this issue?

Ashwini1980 commented 4 years ago

Hi Anshoo,

Can you please guide me where I went wrong? Should We do something onStart method in the listener class?

anshooarora commented 4 years ago

@Ashwini1980 This is because the test is not created yet, it gets created when onTestStart executes which is why you are getting a NPE in your BeforeMethod.

anshooarora commented 4 years ago

I have attempted to fix this, please test using version 1.0.8-SNAPSHOT. link

<groupId>com.aventstack</groupId>
<artifactId>extentreports-testng-adapter</artifactId>
<version>1.0.8-SNAPSHOT</version>
Ashwini1980 commented 4 years ago

I tried with the maven and seems artifact is missing and I am getting: image

Hence I download the JAR file manually and add it to the Java build path but still I am facing the same issue i.e. getting null pointer exception:

image

image

anshooarora commented 4 years ago

You would need to enable snapshots.

See here https://stackoverflow.com/a/7717234/10251989

Ashwini1980 commented 4 years ago

Thanks. Now no errors on pom.xml now. But earlier as I mentioned still getting null pointer exceptions.

Ashwini1980 commented 4 years ago

Hi Anshoo,

Did you get a chance to look into this issue?

Thanks, Ashwin

anshooarora commented 4 years ago

Can you share a small project which reproduces this? I am unable to on my end.. thank you.

Ashwini1980 commented 4 years ago

https://github.com/Ashwini1980/Repo4/tree/master/ExtentReportIssue

Ashwini1980 commented 4 years ago

Another observation. I upgraded to latest version i.e. 4.1.5. (Earlier I used to use 4.0.9 where I was getting nullpointer exception) Now I am not getting null pointer exceptions when I am using ExtentTestManager.getTest().info("Launching Chrome driver") @BeforeMethod. But Now I am getting null pointer exception while flushing the extent report:

image

image

anshooarora commented 4 years ago

You are not using the adapter: https://github.com/Ashwini1980/Repo4/blob/master/ExtentReportIssue/pom.xml

Is this an issue you are experiencing with the adapter or ExtentReports?

Ashwini1980 commented 4 years ago

Sorry for the confusion. Please use https://github.com/Ashwini1980/Repo5.git

Note: It seems beforeMethod is working fine in the latest build but beforeTest and BeforeSuite is not working. For beforeTest and suite still I am getting null pointer exceptions.

Ashwini1980 commented 4 years ago

Hi Anshoo,

Did you get a chance to look into this? Still @beforeTest and @BeforeSuite is throwing null pointer exceptions while using ExtentTestManager.getTest().info("").

Ashwini1980 commented 4 years ago

Hi Anshoo,

Did you get a chance to look into this?

Thanks

Ashwini1980 commented 4 years ago

Hi Anshoo,

Did you get a chance to look into this?

Just an update: For @BeforeMethod, it is working fine but for @BeforeTest/@BeforeClass/@BeforeSuite it is not working...getting same null pointer exception. Code is @ https://github.com/Ashwini1980/Repo5.git

Thanks

anshooarora commented 4 years ago

@Ashwini1980 I looked at your repo, and it seems to me you want to mix custom implementation with the listener. That may not be straight-forward, if even possible. The listener is meant to be used as-is without any custom logging, by letting the listener do the job of reporting. I have sent you a pull request to show the intended usage - please check.

Ashwini1980 commented 4 years ago

@anshooarora Thanks Anshoo. I got the usage details. But I am trying to automate the following scenarios:

Scenario: In the before suite, I want to login to a URL and in the Extent report I want to show login was successful. Ex: ExtentTestManager.getTest().pass(Before suite started..."); ExtentTestManager.getTest().pass("Login to URL was successful.");

In the Before test/class, I want to download a file after login and in the Extent report I want to show File download was successful. Ex: ExtentTestManager.getTest().pass(Before Test/Class started..."); ExtentTestManager.getTest().pass("File download was successful.");

In Before Method, I want to do something with the download file and start the test case. Ex: ExtentTestManager.getTest().pass("Started Amazon service."); ExtentTestManager.getTest().pass("Test Execution started."); So in the final report it show all the logging as mentioned above.

Can I do the same by using extentreports-testng-adapter alone? I tried and it did not work and getting same null pointer exception as mentioned in the original thread. If we can do it by using this testNg adapter please let me know.

As this did not work, hence I mixed custom implementation with the listener and tried to do it.

    <!-- dependency>
        <groupId>com.aventstack</groupId>
        <artifactId>extentreports</artifactId>
        <version>4.1.7</version>
    </dependency-->+ the TestNG Lister

In the above version, If I am trying to do logging on @BeforeMethod and @test and @after..., it is working fine. But while doing with @BeforeSuite, @Beforetest and @Beforeclass, it is throwing same null pointer exception. I think it is expected as we are trying to initialize the create method during onTestStart or beforeInvocation method as follows: ExtentTestManager.createMethod(result);

As you mentioned it is very straight forward to implement with custom implementation, is there any other way to get the Extent report with all the logging(as mention in the above mentioned scenarios)?

Thank in Advance!!

Regards, Ashwini

anshooarora commented 4 years ago

@Ashwini1980 What you are trying to do cannot be achieved directly by the adapter as it will use the pre-defined logic to build the report. You would need to create a custom implementation to achieve the desired behavior..