How to work with PageObject using Playwright plugin for SpecfFlow?
I am using the Playwright plugin for SpecFlow and I have trouble organizing my code properly. I have followed the sample on the SpecFlow.Actions GitHub repository and created a BasePage and the pages that I want to test that inherit from it. The problem is that each page object creates a new browser instance.
For instance, if I have 3 pages in my web applications HomePage, LoginPage and DetailPage that inherits from my BasePage class:
- DetailPage is the page I want to test
- DetailPage can only be accessed from HomePage
- The user needs to be authenticated to access DetailPage and HomePage
- The user can be authenticated by entering its credentials on LoginPage.
To handle authentication:
- I created a Hook to manage the authentication for all my scenarios wher authentication is needed
- I created methods in LoginPage to fill the credentials fields in the page and get a token
To manage access to my DetailPage:
- I created a Hook that manage the navigation from my Home Page to the DetailPage for all my scenarios testing my DetailPage
- I created methods in HomePage that click on the buttons needed to navigate to the DetailPage
To test my DetailPage I created the methods in DetailPage with the things to test.
This way of doing things seem to follow the example but does not work because each page will create a browser. How should I do that ?
There is an issue about that in the Specflow.Actions github repo
-
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 -
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 -
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 -
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.
Comments
4 comments