Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.1 kB
1
Indexable
Never
package com.lambdatest.factory;

import com.google.gson.JsonObject;
import com.microsoft.playwright.*;

public class PlaywrightFactory {
    private Playwright playwright;
    private Page page;
    private Browser browser;
    private BrowserContext context;

    public Page initBrowser(String browserName) {
        playwright = Playwright.create();
        JsonObject capabilities = new JsonObject();
        JsonObject ltOptions = new JsonObject();
        String user = System.getenv("LT_USERNAME");
        String accessKey = System.getenv("LT_ACCESS_KEY");
        ltOptions.addProperty("platform", "Windows 11");
        ltOptions.addProperty("build", "Playwright Java Build");
        ltOptions.addProperty("user", user);
        ltOptions.addProperty("accessKey", accessKey);
        capabilities.add("LT:Options", ltOptions);
        String cdpUrl = "wss://cdp.lambdatest.com/playwright?capabilities=" + capabilities;

        switch (browserName.toLowerCase()) {
            case "chromium":
                browser = playwright.chromium().c(new BrowserType.LaunchOptions().setHeadless(false));
                break;
            case "firefox":
                browser = playwright.firefox().launch(new BrowserType.LaunchOptions().setHeadless(false));
                break;
            case "chrome":
                browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setChannel("chrome").setHeadless(false));
                break;
            case "safari":
                browser = playwright.webkit().launch(new BrowserType.LaunchOptions().setHeadless(false));
                break;

            default:
                System.out.println("Please input the browser name");
                break;
        }
        context = browser.newContext(); // Set the context value
        page = context.newPage(); // Set the page value within the context
        page.navigate(Constants.LAMBDATEST_URL);
        return page;
    }

    public String getCurrentURL(){
        return page.url();
    }
}