microsoft / playwright-dotnet

.NET version of the Playwright testing and automation library.
https://playwright.dev/dotnet/
MIT License
2.47k stars 235 forks source link

How to port PlaywrightSharp code to playwright-dotnet for Proxy usage #1534

Closed zydjohnHotmail closed 3 years ago

zydjohnHotmail commented 3 years ago

Hello: I have used PlaywrightSharp (0.192.0) until yesterday. But suddenly, I found this package is out of support. Then I want to change my code to use playwright-dotnet. I read some articles, but I can't figure out how to launch Playwright (Chromium) using proxy server. With PlaywrightSharp, I can write code like this:

    public static readonly LaunchOptions proxy_launch_option = new()
    {
        Headless = false,
        IgnoreHTTPSErrors = true,
        Proxy = new ProxySettings()
        { Server = "1.2.3.4:5678" },
    };

But when I crete a C# project using playwright-dotnet (1.12.1), I can't use the above launch option. I get error messages: Error CS0246 The type or namespace name 'LaunchOptions' could not be found (are you missing a using directive or an assembly reference?) I think it could be only some kind of syntax issue, but I can't find any correct launch options for using proxy. Please share some code example on this issue. Thanks,

avodovnik commented 3 years ago

Considering you're using IgnoreHTTPSErros, you should probably be looking to create a BrowserTypeLaunchPersistentContextOptions object, or shorthand:

var context = await pw.Chromium.LaunchPersistentContextAsync("C:/temp/data", new()
            {
                Headless = false,
                IgnoreHTTPSErrors = true,
                Proxy = new()
                {
                    Server = "1.2.3.4:5678"
                }
            });

If you're not looking at a persistent context, you can instantiate a BrowserTypeLaunchOptions.

zydjohnHotmail commented 3 years ago

Hello: I am totally lost in your code. Is it possible that you give me a rather complete code sample, like how to use the option, and launch a page to visit one specific web page, like: www.bing.com For me, it looks different from the way I used PlaywrightSharp. Thanks,

avodovnik commented 3 years ago

The Getting started section of the docs has a pretty complete example that does exactly that. Hope that helps!