5

我正在使用 Selenium Grid 和 TestNG 来并行运行我的测试。我有某些需要用户登录的测试,所以我配置了一个用于测试的用户帐户。

我一直小心翼翼地确保任何涉及以我的测试用户身份登录的测试都在单独的虚拟机上运行,​​以便会话不会中断,但是在调查了一些经常失败的测试之后,事实证明这还不够。因此,我要么需要:

  • 为每个测试设置一个新用户(我知道这可能是理想的解决方案,但配置/运行需要一些时间。

  • 拥有多个测试用户帐户,并确保一次只使用一个。

如果我可以识别运行测试的机器的主机,我可以相应地设置测试帐户。有什么办法可以做到这一点,还是有其他我没有想到的解决方案?

4

3 回答 3

1

我通过设置“我是谁页面”解决了识别遥控器的问题。在我的远程 webdriver(或 Selenium)工厂中,我访问该页面,将其存储在用于保存会话的类中,然后记录它。此页面报告主机名、IP、浏览器类型和版本。

于 2011-12-19T19:56:43.863 回答
0

我通过设置几个纯粹用于测试的唯一用户帐户解决了我的问题,所有帐户都以数字结尾(例如:selenium.test1)。这很容易使用 Selenium 作为一次性任务自动化。

我将这个数字存储在代码中,每次测试需要登录时,数字都会增加并构造用户名。

我确实考虑过“释放”并随后在测试完成时重用这些帐户,但我认为确保我有足够的测试帐户来进行测试会更容易。

于 2009-09-24T10:34:13.313 回答
0

这里提供的另一个解决方案:

https://groups.google.com/forum/#!topic/selenium-users/8eNRW9JnayQ

 public static String[] getHostNameAndPort(String hostName, int port, SessionId session) {
        String[] hostAndPort = new String[2];
        String errorMsg = "Failed to acquire remote webdriver node and port info. Root cause: ";

        try {
            HttpHost host = new HttpHost(hostName, port);
            DefaultHttpClient client = new DefaultHttpClient();
            URL sessionURL = new URL("http://" + hostName + ":" + port + "/grid/api/testsession?session=" + session);
            BasicHttpEntityEnclosingRequest r = new BasicHttpEntityEnclosingRequest("POST",
                    sessionURL.toExternalForm());
            HttpResponse response = client.execute(host, r);
            JSONObject object = extractObject(response);
            URL myURL = new URL(object.getString("proxyId"));
            if ((myURL.getHost() != null) && (myURL.getPort() != -1)) {
                hostAndPort[0] = myURL.getHost();
                hostAndPort[1] = Integer.toString(myURL.getPort());
            }
        } catch (Exception e) {
            logger.log(Level.SEVERE, errorMsg, e);
            throw new RuntimeException(errorMsg, e);            
        }
        return hostAndPort;
    }
于 2015-10-13T22:27:49.353 回答