Untitled
unknown
java
a year ago
3.6 kB
2
Indexable
Never
package com.lambdatest.factory; import com.google.gson.JsonObject; import com.microsoft.playwright.*; import java.io.*; import java.net.URLEncoder; import java.util.Properties; public class PlaywrightFactory { private Playwright playwright; private Page page; private Browser browser; private BrowserContext context; private String LAMBDATEST_URL, configFileName; private Properties properties; private FileInputStream configFile; private String rootFolder = System.getProperty("user.dir"); public Page initBrowser(String browserName) { playwright = Playwright.create(); configFileName = "accountInfo.properties"; properties = new Properties(); try { configFile = new FileInputStream(rootFolder + File.separator + configFileName); properties.load(configFile); configFile.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } JsonObject capabilities = new JsonObject(); JsonObject ltOptions = new JsonObject(); String user = getAccount("username"); String accessKey = getAccount("access_key"); capabilities.addProperty("browserVersion", "latest"); ltOptions.addProperty("platform", "Windows 10"); ltOptions.addProperty("build", "Playwright Java Build"); ltOptions.addProperty("user", user); ltOptions.addProperty("accessKey", accessKey); ltOptions.addProperty("seCdp", true); ltOptions.addProperty("selenium_version", "4.0.0"); capabilities.add("options", ltOptions); // Convert capabilities to a string and encode it String caps = capabilities.toString(); String encodedCaps; try { encodedCaps = URLEncoder.encode(caps, "utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return null; // Handle the error appropriately } String cdpUrl = "wss://cdp.lambdatest.com/playwright?capabilities=" + encodedCaps; LAMBDATEST_URL = "https://www.lambdatest.com/selenium-playground/"; switch (browserName.toLowerCase()) { case "firefox": browser = playwright.firefox().connect(cdpUrl); break; case "chrome": browser = playwright.chromium().connect(cdpUrl); break; case "chromium": browser = playwright.chromium().connect(cdpUrl); 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(LAMBDATEST_URL); return page; } public String getAccount(String keyword){ if(keyword.equalsIgnoreCase("username")){ return properties.getProperty("username"); } else if (keyword.equalsIgnoreCase("access_key")){ return properties.getProperty("access_key"); } return null; } public static void setTestStatus(String status, String remark, Page page) { Object result; result = page.evaluate("_ => {}", "lambdatest_action: { \"action\": \"setTestStatus\", \"arguments\": { \"status\": \"" + status + "\", \"remark\": \"" + remark + "\"}}"); } }