javaselenium-webdrivertestngextentreportsselenium-extent-report

null values passed when passing ITestContext attribute value to onTestStart method


I want to pass the variables that I set using ITestContext to onTestStart

   public void login(ITestContext Test){
        Test.setAttribute("nodeName","05 test");
        Test.setAttribute("nodeDetails","05 This belongs to regresion test");
   }

I used below code but only null values get printed.

 @Override
    public void onTestStart(ITestResult result) {
        //get the node name
        String node= (String) result.getTestContext().getAttribute("nodeName");
        String nodeDetails= (String) result.getTestContext().getAttribute("nodeDetails");
        System.out.println("onTestStart node is "+node);
        System.out.println("onTestStart nodeDetails is* "+nodeDetails);
}

However, I did notice that when I put it to onTestSuccess method.

My original requirement is to pass the node name and node details for extent's report node creation in onTestStart method. Kindly help.

test = report.createTest(result.getMethod().getMethodName()).createNode(node).pass(nodeDetails);

Solution

  • onStart: This method is invoked before any test method gets executed. You can use this to set your extra attributes if you need to.

    onTestStart: This method is invoked before any tests method is invoked. This can be used to indicate that the particular test method has been started.

       @Override
       public void onStart(ITestContext context) {
    
            context.setAttribute("nodeName","05 test");
            context.setAttribute("nodeDetails","05 This belongs to regresion test");
       }
    

    === Edited

    If you want to add some attributes to the ITestContext before (ALL Tests are run), then use @BeforeTest

    Use @BeforeTest, this will be run once before all other tests are run.

       @BeforeTest
       public void setData(ITestContext context)
       {
          context.setAttribute("nodeName","05 test");
       }
    

    If you want to do some logic that is supposed to run before (EACH) test method, then use

    @BeforeMethod
    public void beforeMethod(ItestContext testContext) {
        // Do testContext related processing
    }
    

    If neither of the above, and you want to pass custom data to each @Test case, then DataProvider is what you are looking for.

    An example:

        @DataProvider(name = "node-05-provider")
        public Object[][] dataProvider(){
                return new Object[][]{
                    {"node-05"}};
        }
    
        @Test(dataProvider="node-05-provider")
        public void search(String data){
         
             System.out.println(data); // "node-05"
    
    
        }