How to access page in AfterTestRun hook

54 Views Asked by At

I want to create after test run rook to clean data after run. For AfterScenario hooks I'm using ScenarioContext from injection:

public Hooks(ScenarioContext scenarioContext)
{
    _scenarioContext = scenarioContext;
}

later I can call Pages inside scenario hooks:

var creditPage = _scenarioContext.ScenarioContainer.Resolve<CreditPage>();

I dont know however how to access page inside AfterTestRun which has to be static.

1

There are 1 best solutions below

0
Gaurav Khurana On

you can create a custom variable _Page and give your page object to it at the start of your test via constructor of hooks

  [Binding]
  public class HooksForTest
   {
    public readonly ScenarioContext _scenarioContext;
    public IPage _Page;

    public HooksForTest (ScenarioContext scenarioContext)
    {
        _scenarioContext = scenarioContext;
        _Page = CurrentPage;
    }
 }

Current page is the page which will come from page initialisation. and now _Page will remain active and you can access it in after Test

Creating your own page object is better rather than one taking from PageTest