Feng erdong's Blog

Life is beautiful

FAQ of Selenium

| Comments

  • How to simulate press ENTER key
1
2
3
4
@And("^i press enter key$")
public void i_press_enter_key() throws Throwable {
  driver.findElement(By.id("gbqfq")).sendKeys(Keys.ENTER);
}
  • How can wait until a element display or clickable.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Then("^i should see at least (\\d+) results related to my search keyword$")
public void i_should_see_at_least_results_related_to_my_search_keyword(int amount) throws Throwable {
  WebDriverWait wait = new WebDriverWait(driver, 10);
  wait.until(ExpectedConditions.elementToBeClickable(By.id("pnnext")));

  List<WebElement> results = driver.findElements(By.cssSelector("li.g"));

  assertThat(results.size()).isGreaterThan(amount);

  List<String> resultTexts = Lambda.extract(results, on(WebElement.class).getText());
  for (String text : resultTexts) {
    assertThat(text).containsIgnoringCase(keyword);
  }
}

Comments