2016-06-20 18 views
0

私はCucumberの自動化フレームワークで練習していますので、私は仕事中のプロジェクトに使用できます。私はブラウザと対話するためにSelenium WebDriverを使用しています。今私はちょうどGoogle検索が実際に正しい結果を返すことをテストしています。私のフィーチャーファイルはここにある:Selenium WebDriverはGoogleの結果ページの正しいタイトルを返しません

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クラスです:

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

それは、関連する結果を返すないことをテストするために、私はページのタイトルが含まれていることを主張しています検索クエリ。現在、driver.getTitle()は「馬 - Google検索」ではなく「Google」を返すため、最後のステップには失敗しています。

私はなぜこれをやっているのか分かりません。私は結果ページのHTMLをチェックしました。タイトルは私が期待したものです。しかし、セレンは正しい結果を返すわけではありません。なぜ誰かが私にそれを修正する方法を説明することができますか?

+0

は、それがのタイトルを返すアサーションの失敗を引き起こすかもしれない、ページのタイトルを主張する前に、いくつかの待機時間を追加する必要があります前のページ?ページ遷移が完了する前にページタイトルが返されている可能性があります。申し訳ありませんが、私はキュウリを知らないのでコードを提供することはできませんが、問題を解決するかどうかを待ってみてください。 – JeffC

答えて

-1

回答:いくつかの倍のドライバアクションはかなり速いですので

は、おそらくあなたは

@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)); 
    } 
+1

'.implicitlyWait()'は、あなたが思っていると思われることをしていません。その行はグローバル待ち時間を設定します...その場所では最大10秒間だけ待機します。 http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp#implicit-waits。 – JeffC

+0

私はCucumberに慣れていませんが、Java/C#ではWebDriverWaitが必要です。私はキュウリの可能な解決策を参照しているページを見つけました。 http://www.seleniumframework.com/cucumber-jvm-3/waits-and-synchronization/ – JeffC

+0

私は自分の答えを更新しました。ページタイトルをアサートする前に待ち時間を与えていたのですか? –

関連する問題