Skip to main content

How to work with PageObject using Playwright plugin for SpecfFlow?

Comments

4 comments

  • Andreas Willich

    Without code it is hard to say what it is exactly, but I assume that you are doing too much in your base class.

    Always be careful what you put in your baseclasses. It is very easy to get code that is executed multiple times.

    0
  • Alexandre Nedelec

    Thank you for your answer. Concerning the code, I am using the example of the SpecFlow.Actions Playwright Plugin github repository. In this example the baseclass is the following (not doing extra thing, just followed the official sample):

    using Microsoft.Playwright;
    using SpecFlow.Actions.Playwright;
    using System.Threading.Tasks;

    namespace Example.PageObjects
    {
    public class BasePage
    {
    public readonly Task<IPage> _page;
    public BasePage(BrowserDriver browserDriver)
    {
    _page = CreatePageAsync(browserDriver.Current);
    }

    private async Task<IPage> CreatePageAsync(Task<IBrowser> browser)
    {
    // Creates a new page instance
    return await (await browser).NewPageAsync();
    }
    0
  • Bao Dinh

    Hi Alexandre,

    It has been a month, maybe you already have fixed your problem.  Anyway....

    One thing that I notice in your question "...and the pages that I want to test that inherit from it".  I think only the first page should inherit from BasePage.  If you also inherit other pages with BasePage, new Blank browser will be created when that page is instantiated because I see in the BasePge's constructor, it calls NewPageAsync()

    Any pages after the first page, you can inject IPage with constructor injection.  Just remember to register the dependency IPage using IObjectContainer in your BeforeScenario hook.

    1
  • Alexandre Nedelec

    Thanks for our answer, not too late, I put aside this topic for the moment but I am still looking for good practices to use the Playwright plugin. Yes I think you are right, I should not make the other pages inherit from Base page.

    0

Please sign in to leave a comment.

Powered by Zendesk