2

I am using standart exsample: cucumber-jvm-selenium-example

When I run test:

-------------------------------------------------------
 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)

But if I change driver:

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

It works.

MikO
  • 18,243
  • 12
  • 77
  • 109
kotygoroshko
  • 342
  • 1
  • 6
  • 18

1 Answers1

0

You need to wait for Google ajax to refresh, otherwise you will only have Google return as the page title

@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;
        }
    }
}
Zeno Yu
  • 169
  • 2
  • 4
  • this is true, but waiting a fixed amount of time is bad design, consider something like `fluentWait` which is discussed here: https://stackoverflow.com/questions/12858972/how-can-i-ask-the-selenium-webdriver-to-wait-for-few-seconds – Dude May 13 '15 at 07:21
  • I believe issue is not with the wait;it is with the driver object which is being set to htmlunit by default. – Vishal Aggarwal Oct 30 '15 at 22:32
  • The problem seems to me something with the specific implementation of HtmlUnitDriver to handle in this scenario ,which is working fine with Chrome driver implementation. – Vishal Aggarwal Oct 30 '15 at 22:50