2017-06-30 5 views
0

Selenideを使用して、w3schools.com(here)にサンプル入力フォームを開きます。新たに開いたタブ/ウィンドウでセレン化テストが正しい要素を読み込めない

テストではフォームに名前を入力してsubmitをクリックすると、別のタブ/ウィンドウに新しいページが開き、divにリクエストパラメータが表示されます。

Sample HTML form screenshot

Returned results screenshot

次の私のテストコードです:

import com.codeborne.selenide.Configuration 
import org.openqa.grid.internal.utils.configuration.StandaloneConfiguration 
import org.openqa.selenium.remote.server.SeleniumServer 
import spock.lang.Specification 

import static com.codeborne.selenide.Condition.* 
import static com.codeborne.selenide.Selectors.byName 
import static com.codeborne.selenide.Selectors.byTitle 
import static com.codeborne.selenide.Selenide.* 

class DummyTest extends Specification { 

    def "Test w3schools form submit"() { 
     given: "Server setup configurations" 
      Configuration.browser = 'chrome' 
      StandaloneConfiguration configuration = new StandaloneConfiguration() 
      SeleniumServer server = new SeleniumServer(configuration) 
      server.boot() 

     when: "Name info is submitted into the form" 
      open("http://www.w3schools.com/html/html_forms.asp") 
      $(byName("firstname")).setValue("Clark") 
      $(byName("lastname")).setValue("Kent") 
      $x('//*[@id="main"]/div[3]/div/form/input[3]').click() 

      // Maybe this will fix the erroneous element problem? 
      $(byTitle('HTML Forms')).waitUntil(disappears, 5000) 

     then: "New page should display the passed-in request params" 
      $x("/html/body/div[1]").shouldHave(text("firstname=Clark&lastname=Kent")) 
    } 
} 

... webdriver.chrome.driverが渡されている環境変数ポインティングchromedriver.exeの正しい場所に。

テストでは初期フォームページが開き、姓名を正しく入力します。送信ボタンをクリックすると、2ページ目が読み込まれ、リクエストパラメータが表示されるのを待ちます。

私のメソッド呼び出しが存在するかどうかに関係なく、エラーが発生します。 Selenideのドキュメントでは、thenshouldHave()メソッド呼び出しが自動的に流暢な待機を行うはずだとは言えますが、他のStackOverflow投稿の多くは、問題を解決するために何かを行うことを提案しています。

Condition failed with Exception: 

$x("/html/body/div[1]").shouldHave(text("firstname=Clark&lastname=Kent")) 
|      |   | 
|      |   text 'firstname=Clark&lastname=Kent' 
|      Element should have text 'firstname=Clark&lastname=Kent' {By.xpath: /html/body/div[1]} 
|      Element: '<div class="w3-container top">w3schools.com 
|      THE WORLD'S LARGEST WEB DEVELOPER SITE</div>' 
|      Screenshot: file:/C:/Workspace/IntelliJ/dummy-selenide-project/build/reports/tests/1498836183270.0.png 
|      Timeout: 4 s. 
NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[1]"} 

だから、それはテスト出力ごとに、それは最初のページではなく、2ページ目のXPathをつかんだ、ということは明らかです:

この

は私のテストケースの出力です。

私は行方不明または間違っていることがありますか?新しいページごとに新しいオブジェクトや何かをインスタンス化する必要がありますか?

答えて

0

ドキュメント内で答えを見つけることは困難でしたが、別のウィンドウに切り替える方法を呼び出す必要があるという質問の中で、hereが見つかりました。

SelenideTargetLocatorを返す静的メソッドSelenide#switchTo()を呼び出す必要があります。そのオブジェクトでは、0から始まるインデックス(int)または名前/ハンドル/タイトル(String)のいずれかを取るメソッドwindow()を呼び出すことができます。

だから、私のスポックのテストのための私のand句でswitchTo()方法を追加した後、作業の結果は次のようになります。

def "Test w3schools form submit"() { 
    given: "Server setup configurations" 
     Configuration.browser = 'chrome' 
     StandaloneConfiguration configuration = new StandaloneConfiguration() 
     SeleniumServer server = new SeleniumServer(configuration) 
     server.boot() 

    when: "Name info is submitted into the form" 
     open("http://www.w3schools.com/html/html_forms.asp") 
     $(byName("firstname")).setValue("Clark") 
     $(byName("lastname")).setValue("Kent") 
     $x('//*[@id="main"]/div[3]/div/form/input[3]').click() 

    and: "Switch to newly opened window" 
     switchTo().window(1) 

    then: "New page should display the passed-in request params" 
     $x("/html/body/div[1]").shouldHave(text("firstname=Clark&lastname=Kent")) 
} 

:私のテストコードのために、テストのみ開きますので、新しいタブが1つあり、switchTo(1)をChromeでうまく使用できますが、他のブラウザではウィンドウ/タブの順序が異なる場合があります。

関連する問題