DEV Community

Sambathkumar Moorthy
Sambathkumar Moorthy

Posted on

Playwright java is unable to open browser in incognito window

Anything is missing in below code?

1) Am sharing the snippet which i used, able to open chrome browser but not opening in incognito window.
2) My requirement is application under testing url must open in incognito window, complete the authentication and do the testing. But its not happening, always its taking my windows sso credentials and throwing unauthorized access.

Note: I can't share application under testing url because the url will work inside the corporate network for example i have shared the google.com

Launchbrowser.java

package org.ex.pw;

import com.microsoft.playwright.*;

import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Launchbrowser {

    public static void main(String[] args) throws InterruptedException {
        // Create a Playwright instance
        Playwright playwright = Playwright.create();

        // Set the path to the Chrome driver executable
        String executablePath = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe";
        List browserArgs = new ArrayList<>(Arrays.asList("--auth-server-allowlist='*'"));
       //List browserArgs = new ArrayList<>(Arrays.asList("--incognito"));

        Browser browser = playwright.chromium().
                launch(new BrowserType
                        .LaunchOptions()
                        .setExecutablePath(Paths.get(executablePath))
                        .setArgs(browserArgs)
                        .setHeadless(false));
        BrowserContext context = browser.newContext();


        // Create a new page
        Page page = context.newPage();
        // Open a web page
       page.navigate("https://www.google.com");
        final String pageTitle = page.title();
        System.out.println("pageTitle="+pageTitle);
        Thread.sleep(25000);
        context.close();
        // Close the browser
        browser.close();
        // Close the Playwright instance
        playwright.close();
    }
}
Enter fullscreen mode Exit fullscreen mode

build.gradle:

plugins {
  id 'java'
}

group 'org.ex.pw'
version '1.0-SNAPSHOT'

repositories {
  mavenCentral()
}

dependencies {
  testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
  testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
  implementation 'com.microsoft.playwright:playwright:1.44.0'
}

test {
  useJUnitPlatform()
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)