我正在嘗試單擊一個復選框,但是當我運行該操作時,它不起作用。我使用相同的方式單擊按鈕和其他元素。我正在使用 data-testid 在視圖中查找復選框。我需要點擊復選框,點擊后執行下一步的操作。此復選框是一個反應組件,將在視圖中具有一個跨度。
context 'when the user agrees to charges by clicking a checkbox' do
before do
# sleep 1000
wait_before_clicking
page.find("span[data-testid='prepay-enrollment-modal-agreement-checkbox']").click
end
it 'renders new content for the user to review payment details' do
expect(page).to have_selector("[data-testid='review-step-container']")
expect(page).to have_content("Your Plan")
expect(page).to have_content("100 pages/ month")
expect(page).to have_content("Plan cost for 12 months, before discount")
expect(page).to have_content("Prepay discount (10%)")
expect(page).to have_content("Total Due:")
end
end

uj5u.com熱心網友回復:
該data-testid屬性位于input元素上而不是跨度上,因此find("span[data-testid='prepay-enrollment-modal-agreement-checkbox']")不起作用。如果您將 data-testid 屬性放在跨度而不是輸入上,那么它會起作用,但是有更好的方法。由于復選框有一個關聯的標簽元素,您可以告訴 Capybara 如果需要,它可以點擊標簽元素。首先,由于您使用的是測驗 ID,請設定
Capybara.test_id = 'data-testid'
在您的全域配置中,然后在您的測驗中執行
page.check('prepay-enrollment-modal-agreement-checkbox', allow_label_click: true)
注意:而不是expect(page).to have_selector("[data-testid='review-step-container']")你應該使用expect(page).to have_css("[data-testid='review-step-container']")它來更清楚地表明你知道你正在傳遞 CSS。同樣出于測驗速度的原因,您可能希望have_content使用正則運算式將所有呼叫合并為一個
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/489070.html
下一篇:洗掉Logstash中的中級欄位
