playwright-community / playwright-go

Playwright for Go a browser automation library to control Chromium, Firefox and WebKit with a single API.
https://playwright-community.github.io/playwright-go/
MIT License
2.11k stars 156 forks source link

Viewport definition issue #403

Closed drbornot closed 6 months ago

drbornot commented 8 months ago

I'm facing an issue when extending my browser-based desktop tests to mobile devices. If I set the device Viewport, it fails: 1- when chromium, tests always fail and running in browser mode I see the page start moving when the click action is executed 2- when webkit, tests pass only if browser mode (headless true)...otherwise they fail

If I don't set Viewport then tests pass

Below is my code, if I disable the Viewport setting then it is a success:

        runOption := &playwright.RunOptions{
        Browsers: []string{"chromium", "webkit"},
    }

    err = playwright.Install(runOption)
    if err != nil {
        log.Fatalf("could not install playwright dependencies: %v", err)
    }

    pw, err = playwright.Run()
    if err != nil {
        log.Fatalf("could not start Playwright: %v", err)
    }

        pwDevice = pw.Devices["Pixel 7"]

    switch pwDevice.DefaultBrowserType {
    case "chromium":
        testBrowser, err = pw.Chromium.Launch(playwright.BrowserTypeLaunchOptions{
            Channel:  playwright.String("chromium"),
            Headless: playwright.Bool(Headless),
            Args: []string{
                "--disable-features=IsolateOrigins,site-per-process",
                "--disable-setuid-sandbox",
                "--disable-web-security",
                "--disable-blink-features",
                "--disable-blink-features=AutomationControlled",
                "--incognito",
                "--disable-application-cache",
                "--no-sandbox",
                "--disable-dev-shm-usage",
                "--enable-automation",
                "--remote-allow-origins=*",
            },
        })
        if err != nil {
            log.Fatalf("could not launch: %v", err)
        }
    case "webkit":
        testBrowser, err = pw.WebKit.Launch(playwright.BrowserTypeLaunchOptions{
            Channel:  playwright.String("webkit"),
            Headless: playwright.Bool(Headless),
        })
        if err != nil {
            log.Fatalf("could not launch: %v", err)
        }
    }

        ctx, err = testBrowser.NewContext(playwright.BrowserNewContextOptions{
            Viewport:          pwDevice.Viewport,
            UserAgent:         playwright.String(pwDevice.UserAgent),
            DeviceScaleFactor: playwright.Float(pwDevice.DeviceScaleFactor),
            IsMobile:          playwright.Bool(pwDevice.IsMobile),
            HasTouch:          playwright.Bool(pwDevice.HasTouch),
        })
        require.NoError(t, err)