Set browser options
Learn how to set the required Chrome arguments and Firefox user preferences in your Playwright tests running on BrowserStack Automate
Playwright allows you to set browser arguments for Chromium based browsers and Firefox user preferences if the selected browser is Firefox. These arguments need to be set in your Playwright capabilities while testing on BrowserStack as well.
Set args for Chromium based browsers
You can set args
for Chromium based browsers within BrowserStack capabilities as shown in the example below:
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.microsoft.playwright.*;
import java.io.Console;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.stream.Collectors;
public class Example {
public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) {
BrowserType chromium = playwright.chromium();
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("browser","playwright-chromium");
jsonObject.addProperty("browser_version","latest");
jsonObject.addProperty("os","osx");
jsonObject.addProperty("os_version","catalina");
jsonObject.addProperty("browserstack.networkLogs", true);
jsonObject.addProperty("browserstack.local", true);
jsonObject.addProperty("browserstack.console", "errors");
jsonObject.addProperty("browserstack.username", "");
jsonObject.addProperty("browserstack.accessKey", "");
// Adding playwright arguments here
JsonArray arguments = new JsonArray();
arguments.add("--disable-print-preview");
jsonObject.add("args", arguments);
String caps = URLEncoder.encode(jsonObject.toString(), "utf-8");
String ws_endpoint = "wss://cdp.browserstack.com/playwright?caps=" + caps;
Browser browser = chromium.connect(ws_endpoint);
Page page = browser.newPage();
page.navigate("http://127.0.0.1:3002");
page.locator("[id=printBox]").click();
Thread.sleep(10000);
browser.close();
} catch (UnsupportedEncodingException | InterruptedException e) {
e.printStackTrace();
}
}
}
// Import Playwright dependencies
(async () => {
const caps = {
'browser': 'chrome',
'os': 'osx',
'os_version': 'catalina',
'name': 'My first playwright test',
'build': 'playwright-build-1',
'browserstack.username': process.env.BROWSERSTACK_USERNAME || 'YOUR_USERNAME',
'browserstack.accessKey': process.env.BROWSERSTACK_ACCESS_KEY || 'YOUR_ACCESS_KEY',
'args': ['--incognito'] // You can specify an array of strings here. Valid only if the browser is chromium based
};
// Add CDP endpoint here to connect to BrowserStack
const browser = await chromium.connect({
wsEndpoint: `wss://cdp.browserstack.com/playwright?caps=${encodeURIComponent(JSON.stringify(caps))}`,
});
// BrowserStack specific code ends here
// Insert your test script here
})();
You can specify any argument from the list of all supported Chromium arguments.
Set Firefox user preferences
You can set firefox_user_prefs
if the selected browser is playwright-firefox
using the capability firefox_user_prefs
as shown below:
// Import Playwright dependencies
(async () => {
const caps = {
'browser': 'chrome',
'browserstack.username': process.env.BROWSERSTACK_USERNAME || 'YOUR_USERNAME',
'browserstack.accessKey': process.env.BROWSERSTACK_ACCESS_KEY || 'YOUR_ACCESS_KEY',
'firefox_user_prefs': '<Object<string, string|number|boolean>>' // You can pass the capability in the same format as supported by Playwright in the launch arguments
};
// Add CDP endpoint here to connect to BrowserStack
const browser = await chromium.connect({
wsEndpoint: `wss://cdp.browserstack.com/playwright?caps=${encodeURIComponent(JSON.stringify(caps))}`,
});
// BrowserStack specific code ends here
// Insert your test script here
})();
The firefox_user_prefs
capability value can be passed in the same format as supported by Playwright in the launch arguments. You can pass any firefox_user_prefs
as supported by Playwright.
We're sorry to hear that. Please share your feedback so we can do better
Contact our Support team for immediate help while we work on improving our docs.
We're continuously improving our docs. We'd love to know what you liked
- RESOURCES
We're sorry to hear that. Please share your feedback so we can do better
Contact our Support team for immediate help while we work on improving our docs.
We're continuously improving our docs. We'd love to know what you liked
Thank you for your valuable feedback!