2

我正在使用标准示例: cucumber-jvm-selenium-example

当我运行测试时:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.michalvich.cucumber.selenium.example.GoogleSearchTest
Feature: Search on Google
  As an user
  I want to search on Google
  So that I can see results

  Scenario: results are shown                       # com\michalvich\cucumber\selenium\example\GoogleSearch.feature:6
    Given the page is open "http://www.google.com"  # GoogleSearchScenario.the_page_is_open(String)
[1A    Given the page is open "http://www.google.com"  # GoogleSearchScenario.the_page_is_open(String)
    When I search for "Cucumber"                    # GoogleSearchScenario.I_search_for(String)
[1A    When I search for "Cucumber"                    # GoogleSearchScenario.I_search_for(String)
      org.openqa.selenium.NoSuchElementException: Unable to locate element with name: q
      For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
      Build info: version: '2.28.0', revision: '18309', time: '2012-12-11 15:53:30'
      System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_17'
      Driver info: driver.version: HtmlUnitDriver
        at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByName(HtmlUnitDriver.java:749)
        at org.openqa.selenium.By$ByName.findElement(By.java:292)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1247)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1244)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:987)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:1244)
        at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:393)
        at com.michalvich.cucumber.selenium.example.GoogleSearchScenario.I_search_for(GoogleSearchScenario.java:26)
        at ?.When I search for "Cucumber"(com\michalvich\cucumber\selenium\example\GoogleSearch.feature:8)

但是,如果我更改驱动程序:

System.setProperty("webdriver.chrome.driver", "path\\to\\chromedriver.exe");
driver = new ChromeDriver();

有用。

4

1 回答 1

0

你需要等待谷歌ajax刷新,否则你将只有Google返回作为页面标题

@When("^I search for \"([^\"]*)\"$")
public void I_search_for(String search) throws Throwable {
    WebElement element = driver.findElement(By.name("q"));
    element.sendKeys(search);
    element.submit();

    // Google ajax wait for 5 seconds
    long end = System.currentTimeMillis() + 5000;
    while (System.currentTimeMillis() < end) {
        WebElement resultsDiv = driver.findElement(By.className("gssb_e"));

        // If results have been returned, the results are displayed in a drop down.
        if (resultsDiv.isDisplayed()) {
          break;
        }
    }
}
于 2014-01-15T00:32:26.827 回答