转换为Webdriver后遇到Selenium Grid问题

时间:2014-02-19 18:44:17

标签: java selenium-webdriver selenium-grid

从RC切换到Selenium Webdriver后,Selenium Grid不再有效。请注意,我的大多数测试仍然在RC中,但是一次只能转换为Webdriver,因此仍然需要Selenium实例。看起来我的驱动程序和/或浏览器(Selenium)实例在并行运行时会被覆盖。

这是我的代码:

public class SeleniumTestSupport
{

    private static Properties singleSharedProperties;
    private static Selenium webmailsingleSharedBrowser;
    protected Selenium webmailbrowser;
    protected WebDriver singleSharedDriver;

    protected WebDriver driver;
    protected Selenium browser;//was protected, now public
    protected static String domain;
    Integer flag = 0;

    @BeforeSuite(alwaysRun = true)
    public void startSeleniumClient() {
    }

    @BeforeTest(alwaysRun = true)
    public void distributeTests(){
    }

    @BeforeClass(alwaysRun = true)
    public void initBrowser() {
    }

    @BeforeMethod(alwaysRun = true)
    public void logIn() {

        singleSharedProperties = new Properties(System.getProperties());
        try {
            singleSharedProperties.load(getClass().getClassLoader().getResourceAsStream("selenium.properties"));
            } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
            } catch (IOException e) {
            throw new RuntimeException(e);
        }

        String serverHost = singleSharedProperties.getProperty("selenium.serverHost", "localhost");
        String serverPortText = singleSharedProperties.getProperty("selenium.serverPort", "4444");
        int serverPort;
        try {
            serverPort = Integer.parseInt(serverPortText);
            } catch (NumberFormatException e) {
            throw new RuntimeException("Unable to parse selenium.serverPort '" + serverPortText + "'");
        }
        String browserStartCommand = singleSharedProperties.getProperty("selenium.browserStartCommand");

        System.out.println("serverhost=" + serverHost);
        System.out.println("serverport=" + serverPort);
        System.out.println("browserStartCommand=" + browserStartCommand);
        System.out.println("url=" + singleSharedProperties.getProperty("teamconnect.url"));


        DesiredCapabilities capability = DesiredCapabilities.firefox();
        singleSharedDriver = new RemoteWebDriver(capability);
        driver=singleSharedDriver;
        browser = new WebDriverBackedSelenium(singleSharedDriver, singleSharedProperties.getProperty("teamconnect.url"));


        String usernamePassword = singleSharedProperties.getProperty("teamconnect.user." + getUserGroup());
        String username = StringUtils.substringBefore(usernamePassword, "/");
        String password = StringUtils.substringAfter(usernamePassword, "/");



        driver.get(singleSharedProperties.getProperty("teamconnect.url"));


        //This code is some setup for each Test, basically, logging into the application...
        LoginPage loginPage = new LoginPage(browser);
        loginPage.setUsername(username);
        loginPage.setPassword(password);
        loginPage.clickLogIn();
        //more code later, removed for brevity


    }

以下是更多信息:

以下是测试的java代码:

包XXXXXXXXXXXXXX; 导入XXXXXXXXXXXXXX;

@Test(groups = {“admin”}) 公共类EN1200_SR0050_DesignerRightsTest扩展了SeleniumTestSupport {

@Test
public void testAllowAllAndDenyAllGroupDesignerRights2() {

    GlobalNavigationPage globalNavigationPage = new GlobalNavigationPage(browser);


    // Click Admin tab from global navigation bar.
    globalNavigationPage.clickAdminTab();

这是GlobalNavigationPage java代码:

包XXXXXXXXXXXX;

导入XXXXXXXXXXXXXX;

公共类GlobalNavigationPage扩展了EntityPage {

Reusable_Actions reusable_Actions = new Reusable_Actions();
Page page = new Page(browser);

public GlobalNavigationPage(Selenium browser) {
    super(browser);
}


public void clickAdminTab() {

    long start = System.currentTimeMillis();

    while (System.currentTimeMillis() - start < WAIT_TIME_IN_SECONDS * 2000) {

        // if element is present return
        if (browser.isElementPresent(LNK_ADMIN)) {
            return;
        }
        // wait for 1/10 of a second
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }}

    driver.findElement(By.id(LNK_ADMIN)).click();
    browser.setTimeout(Integer.toString(WAIT_TIME_IN_SECONDS * 2000));
    browser.waitForPageToLoad(Integer.toString(WAIT_TIME_IN_SECONDS * 2000));
    assertTextNotPresent("System Error Has Occured!");
}

1 个答案:

答案 0 :(得分:1)

我的猜测是你有一个关于如何运行测试的自定义配置。

您的集线器配置最有可能说明:

  

仅运行RC测试。

如果您想运行WebDriver测试,则需要指定要使用的WebDriver浏览器的数量。

{
  "capabilities":
      [
        {
          "browserName": "*firefox",
          "maxInstances": 5,
          "seleniumProtocol": "Selenium"
        },
        {
          "browserName": "*googlechrome",
          "maxInstances": 5,
          "seleniumProtocol": "Selenium"
        },
        {
          "browserName": "*iexplore",
          "maxInstances": 1,
          "seleniumProtocol": "Selenium"
        },
        {
          "browserName": "firefox",
          "maxInstances": 5,
          "seleniumProtocol": "WebDriver"
        },
        {
          "browserName": "chrome",
          "maxInstances": 5,
          "seleniumProtocol": "WebDriver"
        },
        {
          "browserName": "internet explorer",
          "maxInstances": 1,
          "seleniumProtocol": "WebDriver"
        }
      ],
  "configuration":
  {
    "proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
    "maxSession": 5,
    "port": 5555,
    "host": ip,
    "register": true,
    "registerCycle": 5000,
    "hubPort": 4444,
    "hubHost": ip
  }
}

您可以看到我们可以为RC测试指定"Selenium",为Selenium 2测试指定"WebDriver"