1

Overview

Hi In ItestContexts.setAttribute() the method Page Object does not get overridden by the new pages created , I want to dynamically update the the page value in ItestContext on run time

Question

I'm using Playwright with Java to develop a wrapper framework and I have created a custom listener class implementing ITestListener to take screenshot on failure using playwright methods, I only use one Page object in my test class which I overrides with new pages as wanted , but the ItestContext page reference does not get overridden by the latest page value I have set in my test method , how can I achieve ItestContext.setAttribute to update the latest page value on run time, as per my Test Method line 3

@BeforeClass
    public void init(ITestContext iTestContext) {
        iTestContext.setAttribute("Page_Reference", page); //page comes from my inherited BaseClass
    }


@Test(description = "Testing Screenshot by Overriding Practice")
    public void navDishLandPage() {
        page.goTo("https://github.com/"); //page reference comes from my base class - 1
        Page originalPage = page; //stores page reference to later access - 2
        page = getPage("User"); //Overrides the previous page reference by new Page ; getPage("Key") is a wrapper method -3
        page.goTo("https://github.com/cbeust/testng/issues/2905");
        page = originalPage; //switching back to the stored page reference
        System.out.println(page.getTitle());
        Assert.assertEquals(page.getTitle(), "TTT");
    }

Inside My onTestFailureMethod

        Page page = (Page) iTestResult.getTestContext().getAttribute("Page_Reference");
        byte[] bufferImage=page.screenShot();
Arctodus
  • 5,743
  • 3
  • 32
  • 44

1 Answers1

0

You need to do the following:

  • You use ITestResult.setAttribute() (this is one object per @Test method) instead of using ITestContext.setAttribute() (which is one object for the entire <test> tag). One or more @Test methods come under a <test> tag.
  • Since you getPage("User") is your own custom method, I would suggest that you add the below lines within that method
public Page getPage(String text) {
    Page page; //Your logic goes here to find the page to be returned.
    
    //Get the currently running "@Test" method's result
    ITestResult itr = Reporter.getCurrentTestResult();

    // override the attribute with a reference to the latest page object being returned
    itr.setAttribute("Page_Reference", page);
    return page;
}
Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66