Search code examples
c#.netcompiler-errorsnunitplaywright

Interface Does not contain a definition for METHOD and no extension METHOD accepting a first argument of type INTERFACE could be found


I have created a IBrowserWrapper interface and BrowserWrapper class that implements the interface

public class BrowserWrapper : IBrowserWrapper
{
    private readonly IBrowser _browser;
    private readonly IPage _page;
    private readonly IBrowserContext _context;
    private readonly TracingManager _tracingManager;

    public IPage Page => _page;

    private BrowserWrapper(IBrowser browser, IBrowserContext context, IPage page, TracingManager tracingManager)
    {
        _browser = browser;
        _context = context;
        _page = page;
        _tracingManager = tracingManager;
    }

    public static async Task<IBrowserWrapper> CreateAsync(IConfiguration configuration)
    {
        var browserName = configuration["Browser:Name"];
        var headless = bool.Parse(configuration["Browser:Headless"]);
        var incognito = bool.Parse(configuration["Browser:Incognito"]);
        var viewportWidth = int.Parse(configuration["Browser:ViewportWidth"]);
        var viewportHeight = int.Parse(configuration["Browser:ViewportHeight"]);
        var slowMo = int.Parse(configuration["Browser:SlowMo"]);
        var tracingEnabled = bool.Parse(configuration["Browser:Tracing"]);

        var playwright = await Playwright.CreateAsync();
        IBrowser browser;

        if (browserName.ToLower() == "firefox")
        {
            browser = await playwright.Firefox.LaunchAsync(new BrowserTypeLaunchOptions { Headless = headless, SlowMo = slowMo });
        }
        else if (browserName.ToLower() == "webkit")
        {
            browser = await playwright.Webkit.LaunchAsync(new BrowserTypeLaunchOptions { Headless = headless, SlowMo = slowMo });
        }
        else
        {
            browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions { Headless = headless, SlowMo = slowMo });
        }

        var contextOptions = new BrowserNewContextOptions
        {
            ViewportSize = new ViewportSize
            {
                Width = viewportWidth,
                Height = viewportHeight
            }
        };

        var context = incognito ? await browser.NewContextAsync(contextOptions) : await browser.NewContextAsync();
        var tracingManager = new TracingManager(context, tracingEnabled);

        if (tracingEnabled)
        {
            await tracingManager.StartTracingAsync();
        }

        var page = await context.NewPageAsync();

        return new BrowserWrapper(browser, context, page, tracingManager);
    }
}

I have even created a BaseTest.cs file with below line

public class BaseTest
{
    protected IConfiguration Configuration { get; private set; }
    protected IBrowserWrapper BrowserWrapper { get; private set; }


    [SetUp]
    public async Task SetUp()
    {
        // Load configuration
        Configuration = ConfigurationLoader.LoadConfiguration();
        // Initialize the browser and page
        BrowserWrapper = await BrowserWrapper.CreateAsync(Configuration);
    }
}

At line BrowserWrapper = await BrowserWrapper.CreateAsync(Configuration); in the BaseTest I am getting the below error CS1061 'IBrowserWrapper' does not contain a definition for 'CreateAsync' and no accessible extension method 'CreateAsync' accepting a first argument of type 'IBrowserWrapper' could be found (are you missing a using directive or an assembly reference?)

What wrong I am doing here?


Solution

  • Change your BaseTest to have a full name (with a namespace) for Browser wrapper:

    My.Namespace.BrowserWrapper.CreateAsync

    You have a local field called BrowserWrapper which is clashing with the class name

    You can rename this field to something else, this will resolve the issue as well