Selenium WebDriver 不会 return 更正 Google 结果页面的标题
Selenium WebDriver won't return correct title of Google results page
我正在练习 Cucumber 自动化框架,因此我可以将它用于工作中的项目。我正在使用 Selenium WebDriver 与浏览器交互。现在我只是在测试 Google 搜索确实 return 正确的结果。我的功能文件在这里:
Feature: Google
Scenario: Google search
Given I am on the Google home page
When I search for "horse"
Then the results should relate to "horse"
这是我的 Java class 以及步骤定义:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.junit.Assert;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class StepDefinitions {
WebDriver driver = null;
@Given("^I am on the Google home page$")
public void i_am_on_the_Google_home_page() throws Throwable {
driver = new FirefoxDriver();
driver.get("https://www.google.com");
}
@When("^I search for \"([^\"]*)\"$")
public void i_search_for(String query) throws Throwable {
driver.findElement(By.name("q")).sendKeys(query);
driver.findElement(By.name("btnG")).click();
}
@Then("^the results should relate to \"([^\"]*)\"$")
public void the_results_should_relate_to(String result) throws Throwable {
System.out.println(driver.getTitle());
Assert.assertTrue(driver.getTitle().contains(result));
}
}
为了测试它是否提供 return 相关结果,我只是断言页面标题包含搜索查询。现在,最后一步失败了,因为 driver.getTitle()
正在 returning "Google",而不是预期的 "horse - Google Search".
我不确定为什么要这样做。我检查了结果页的HTML,标题是我所期望的。但是 Selenium 并不是 return 正确的结果。有人可以向我解释为什么以及如何解决它吗?
答案:
可能您需要在断言页面标题之前添加一些等待时间,因为有时 driver 操作非常快,这可能会导致断言失败
@Then("^the results should relate to \"([^\"]*)\"$")
public void the_results_should_relate_to(String result) throws Throwable {
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("some element in page"))));
System.out.println(driver.getTitle());
Assert.assertTrue(driver.getTitle().contains(result));
}
我正在练习 Cucumber 自动化框架,因此我可以将它用于工作中的项目。我正在使用 Selenium WebDriver 与浏览器交互。现在我只是在测试 Google 搜索确实 return 正确的结果。我的功能文件在这里:
Feature: Google
Scenario: Google search
Given I am on the Google home page
When I search for "horse"
Then the results should relate to "horse"
这是我的 Java class 以及步骤定义:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.junit.Assert;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class StepDefinitions {
WebDriver driver = null;
@Given("^I am on the Google home page$")
public void i_am_on_the_Google_home_page() throws Throwable {
driver = new FirefoxDriver();
driver.get("https://www.google.com");
}
@When("^I search for \"([^\"]*)\"$")
public void i_search_for(String query) throws Throwable {
driver.findElement(By.name("q")).sendKeys(query);
driver.findElement(By.name("btnG")).click();
}
@Then("^the results should relate to \"([^\"]*)\"$")
public void the_results_should_relate_to(String result) throws Throwable {
System.out.println(driver.getTitle());
Assert.assertTrue(driver.getTitle().contains(result));
}
}
为了测试它是否提供 return 相关结果,我只是断言页面标题包含搜索查询。现在,最后一步失败了,因为 driver.getTitle()
正在 returning "Google",而不是预期的 "horse - Google Search".
我不确定为什么要这样做。我检查了结果页的HTML,标题是我所期望的。但是 Selenium 并不是 return 正确的结果。有人可以向我解释为什么以及如何解决它吗?
答案:
可能您需要在断言页面标题之前添加一些等待时间,因为有时 driver 操作非常快,这可能会导致断言失败
@Then("^the results should relate to \"([^\"]*)\"$")
public void the_results_should_relate_to(String result) throws Throwable {
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("some element in page"))));
System.out.println(driver.getTitle());
Assert.assertTrue(driver.getTitle().contains(result));
}