Selenideを使用して、w3schools.com(here)にサンプル入力フォームを開きます。新たに開いたタブ/ウィンドウでセレン化テストが正しい要素を読み込めない
テストではフォームに名前を入力してsubmitをクリックすると、別のタブ/ウィンドウに新しいページが開き、divにリクエストパラメータが表示されます。
次の私のテストコードです:
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のドキュメントでは、then
のshouldHave()
メソッド呼び出しが自動的に流暢な待機を行うはずだとは言えますが、他の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をつかんだ、ということは明らかです:
この
は私のテストケースの出力です。私は行方不明または間違っていることがありますか?新しいページごとに新しいオブジェクトや何かをインスタンス化する必要がありますか?