在自动化中获取 Browserstack 空闲超时

Getting Browserstack Idle Timeout in Automation

提问人:kavya M 提问时间:11/16/2023 最后编辑:Robertkavya M 更新时间:11/16/2023 访问量:17

问:

在启动 URL 之前,我收到超时错误。在错误消息中,在此处输入图像描述

尝试了BrowserStack提供的分辨率。(https://www.browserstack.com/docs/automate/selenium/error-codes/browserstack-idle-timeout#increase-timeout-duration )即使问题尚未解决。

public class BrowserStackTestclass {
    public RemoteWebDriver driver;
    private Local l;
    public String sessionId;
    private String methodName;
    private DesiredCapabilities capabilities;
    public String username;
    public String accessKey;
    private static JSONObject jsonObject;
    public String browserName;

    @BeforeEach
    public void setUp(TestInfo testInfo) throws Exception {

        Map<String, String> env = System.getenv();

        for (Map.Entry<String, String> entry : env.entrySet()) {
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }

        List<Integer> taskIDs = new ArrayList<Integer>();
        String config = System.getProperty("config");
        if (StringUtils.isBlank(config)) {
            config = "chrome.json";
        } else {
            config = config + ".json";
        }
        System.out.println("Using config as " + config);
        browserName = config;
        JSONParser parser = new JSONParser();
        jsonObject = (JSONObject) parser.parse(new FileReader("src/test/resources/conf/" + config));
        
        JSONArray envs = (JSONArray) jsonObject.get("environments");

        DesiredCapabilities capabilities = new DesiredCapabilities();

        Map<String, String> envCapabilities = (Map<String, String>) envs.get(0);
        Iterator it = envCapabilities.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pair = (Map.Entry) it.next();
            capabilities.setCapability(pair.getKey().toString(), pair.getValue().toString());
        }
        Map<String, String> commonCapabilities = (Map<String, String>) jsonObject.get("capabilities");
        it = commonCapabilities.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pair = (Map.Entry) it.next();
            if (capabilities.getCapability(pair.getKey().toString()) == null) {
                capabilities.setCapability(pair.getKey().toString(), pair.getValue().toString());
            }
        }
        username = System.getProperty("BROWSERSTACK_USERNAME");

        if (StringUtils.isBlank(username)) {
            username = (String) jsonObject.get("user");
        }
        if (StringUtils.isBlank(username)) {
            username = System.getenv("BROWSERSTACK_USERNAME");
        }
        accessKey = System.getProperty("BROWSERSTACK_ACCESS_KEY");
        if (StringUtils.isBlank(accessKey)) {
            accessKey = (String) jsonObject.get("key");
        }
        if (StringUtils.isBlank(accessKey)) {
            accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY");
        }   
        String localIdentifier=UUID.randomUUID().toString();
        l = new Local();
        Map<String, String> options = new HashMap<String, String>();
        options.put("key", accessKey);
         capabilities.setCapability("browserstack.local", "true");

        
        if(System.getProperty("env", "qa").equalsIgnoreCase("qa")) {
             options.put("forcelocal", "true");
             options.put("localIdentifier", localIdentifier);
             capabilities.setCapability("browserstack.local", "true");
             l.start(options);
        }
        // Set Default Capabilities
        capabilities.setCapability("project", "ProjectName");
        capabilities.setCapability("browserstack.debug", "false");
        capabilities.setCapability("browserstack.safari.enablePopups", "true");
        capabilities.setCapability("browserstack.ie.enablePopups", "true");
        capabilities.setCapability("browserstack.video", "true");
        capabilities.setCapability("browserstack.console", "info");
        capabilities.setCapability("browserstack.networkLogs", "true");
        capabilities.setCapability("browserstack.idleTimeout", "240");
        capabilities.setCapability("acceptSslCerts", "true");
        capabilities.setCapability("browserstack.selenium_version", "3.141.59");
        capabilities.setCapability("browserstack.localIdentifier", localIdentifier);

        // capabilities.setCapability("browserstack.sendKeys", "true");

        // Set OS
        String os = getCapabilityByProperty("browser_os", "", capabilities);
        if (StringUtils.isNotBlank(os)) {
            capabilities.setCapability("browser_os", os);
        }
        // Set OS VERSION
        String osversion = getCapabilityByProperty("os_version", "", capabilities);
        if (StringUtils.isNotBlank(osversion)) {
            capabilities.setCapability("os_version", osversion);
        }
        // Set browser
        String browser = getCapabilityByProperty("browser", "", capabilities);
        if (StringUtils.isNotBlank(browser)) {
            capabilities.setCapability("browser", browser);
        }
        // Set browser version
        String browserversion = getCapabilityByProperty("browser_version", "", capabilities);
        if (StringUtils.isNotBlank(browserversion)) {
            capabilities.setCapability("browser_version", browserversion);
        }
        // Set device -used for mobile device
        String device = getCapabilityByProperty("device", "", capabilities);
        if (StringUtils.isNotBlank(device)) {
            capabilities.setCapability("device", device);
        }

        String buildNumber = System.getProperty("BUILD_NUMBER");

        // Set Build Name {{env}}-build-
        String testSuite = System.getProperty("TESTSUITE");
        if (StringUtils.isNotBlank(testSuite)) {
            capabilities.setCapability("build",
                    StringUtils.defaultString(buildNumber, "Local") + "-"
                            + convertToPascal(System.getProperty("env", "local")) + "-" + config + "-"
                            + convertToPascal(testSuite));
        } else {
            capabilities.setCapability("build", "Local Test Run");
        }
        
        methodName = testInfo.getDisplayName();
        // Set Test case name
        capabilities.setCapability("name", convertToPascal(getMethodName()));       
        System.out.println("Url for browserstack " +"https://" + username + ":" + accessKey + "@" + jsonObject.get("server") + "/wd/hub");
        driver = new RemoteWebDriver(
                new URL("https://" + username + ":" + accessKey + "@" + jsonObject.get("server") + "/wd/hub"),
                capabilities);
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        sessionId = driver.getSessionId().toString();
        System.out.println("Session id is "+sessionId);
        if (StringUtils.isBlank(sessionId)) {
            throw new RuntimeException("SessionId is blank");

        }

        WebDriverRunner.setWebDriver(driver);
    }
   
    private static String getCapabilityByProperty(String key, String defaultValue, Capabilities capabilities) {
        String value = System.getProperty(key);
        if (StringUtils.isBlank(value)) {
            value = (String) capabilities.getCapability(key);
            if (StringUtils.isBlank(value)) {
                value = defaultValue;
            }
        }
        return value;
    }

    public DesiredCapabilities getCapabilities() {
        return capabilities;
    }

    public void setCapabilities(DesiredCapabilities capabilities) {
        this.capabilities = capabilities;
    }

    @AfterEach
    public void tearDown() throws Exception {
        driver.quit();
        if (l != null)
            l.stop();
    }
    
    public String getUsername() {

        return username;
    }

    public String getAcccessKey() {

        return accessKey;
    }

    public String getSessionId() {

        return sessionId;
    }

    public String getMethodName() {
        return methodName.replaceAll("[()]","");
    }

    

    public static void mark(String username, String accessKey, String sessionId, String status, String reason)
            throws URISyntaxException, UnsupportedEncodingException, IOException {
        URI uri = new URI("https://" + username + ":" + accessKey + "@api.browserstack.com/automate/sessions/"
                + sessionId + ".json");
        HttpPut putRequest = new HttpPut(uri);
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add((new BasicNameValuePair("status", status)));
        nameValuePairs.add((new BasicNameValuePair("reason", reason)));
        putRequest.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpClientBuilder.create().build().execute(putRequest);
    }

    public static String convertToPascal(String word) {
        return StringUtils
                .capitalize(StringUtils.join(StringUtils.splitByCharacterTypeCamelCase(word), StringUtils.SPACE));
    }

}
java selenium-webdriver junit

评论

1赞 Robert 11/16/2023
Too much code. Can you please edit your question and bring the code down to a minimal reproducible example?
0赞 HomeLander 11/16/2023
Hi kindly please raise this as an issue at [email protected]

答: 暂无答案