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);
// capabilities.add("LT:Options", ltOptions);
// try {
// String caps = URLEncoder.encode(capabilities.toString(), "utf-8");
// } catch (UnsupportedEncodingException e) {
// e.printStackTrace();
// }
// String cdpUrl = "wss://cdp.lambdatest.com/playwright?capabilities=" + capabilities;
LAMBDATEST_URL = "https://www.lambdatest.com/selenium-playground/";
switch (browserName.toLowerCase()) {
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;
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 + "\"}}");
}
}