In this blog we will be utilizing Playwright Java to launch non-incognito browser windows.

Topics that we will cover:

  • Launch browser in incognito (private) mode
  • Launch Non-Incognito (normal) Chromium Browser
  • Launch Non-Incognito Chrome Browser
  • Launch Non-Incognito Chrome Browser Default Profile
  • Source code (incognito mode)
  • Source code (Non-Incognito Chromium Browser)
  • Source code (Non-Incognito Chrome Browser)
  • Source code (Non-Incognito Chrome Browser Default Profile)

Launch browser in incognito (private) mode

So far we have been launching a browser in an incognito (private browser) mode. 

This is the normal code that we have been running so far

 Let us run this code to launch the private chromium browser. You can see 'incognito' mode in top right hand corner

 Launch Non-Incognito (normal) “Chromium” Browser

At times we do NOT want to run a test in a private (incognito) browser. Instead we want to run the test in a "normal (non-incognito)" browser. 

Let us see how to launch a chromium browser in a non-incognito window.

In order to launch a normal (non-incognito) browser, first of all what we need to do is, instead of "launch", we have to use "launchPersistentContext" method. This method accepts 2 parameters : one is the path viz the browser profile that you need to give. If we don’t want to give path, we can keep it as blank as well:   

Execute and note that a non-incognito window has been launched, it is not a private browser

 Launch Non-Incognito “Chrome” Browser

Same thing we can do with chrome browser as well. We just need to setup the executable file path.

We can say .setExecutablePath(Paths.get("")) and give path of chrome exe: "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe" 

 Launch Non-Incognito Chrome Browser Default Profile

In case you want to launch default chrome profile, than you can give the chrome profile path over here

Execute

As seen above, we now see the entire history as well. So this is not the new profile. This is the same profile that you are running it by default.

So this is how you can play around with profiles, you can launch multiple browser types (chrome, chromium etc) in non-incognito window using playwright.

Source code (incognito mode)
package com.rsa.playwrightjava;

import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;

public class Blog25_Incognito {
public static void main(String[] args) {
Playwright playwright = Playwright.create(); 
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));

Page page = browser.newPage(); 
page.navigate("https://rahulshettyacademy.com/");
page.pause();
}
}

Source code (Non-Incognito Chromium Browser)
package com.rsa.playwrightjava;

import java.nio.file.Paths;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;

public class Blog25_NonIncognitoChromiumBrowser {
public static void main(String[] args) {
Playwright playwright = Playwright.create(); 
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));

BrowserContext browserContext= playwright.chromium().launchPersistentContext(Paths.get(""), new BrowserType.LaunchPersistentContextOptions().setHeadless(false));
Page page = browserContext.newPage();
//Page page = browser.newPage();
page.navigate("https://courses.rahulshettyacademy.com/courses/");
page.pause();
}
}

Source code (Non-Incognito Chrome Browser)
package com.rsa.playwrightjava;

import java.nio.file.Paths;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;

public class Blog25_NonIncognitoChromeBrowser {
public static void main(String[] args) {
Playwright playwright = Playwright.create(); 
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));

BrowserContext browserContext= playwright.chromium().launchPersistentContext(Paths.get(""), new BrowserType.LaunchPersistentContextOptions().setHeadless(false).setExecutablePath(Paths.get("C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe")));
Page page = browserContext.newPage();
//Page page = browser.newPage();
page.navigate("https://rahulshettyacademy.com/lifetime-access");
page.pause();
}
}

Source code (Non-Incognito Chrome Browser Default Profile)
package com.rsa.playwrightjava;

import java.nio.file.Paths;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;

public class Blog25_NonIncognitoChromeBrowserDefaultProfile {
public static void main(String[] args) {
Playwright playwright = Playwright.create(); 
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));

BrowserContext browserContext= playwright.chromium().launchPersistentContext(Paths.get("C:\\Users\\DELL\\AppData\\Local\\Google\\Chrome\\User Data\\Default"), new BrowserType.LaunchPersistentContextOptions().setHeadless(false).setExecutablePath(Paths.get("C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe")));
Page page = browserContext.newPage();
//Page page = browser.newPage();
page.navigate("https://rahulshettyacademy.com/lifetime-access");
page.pause();
}
}

Thanks.

Tags


You may also like

Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}