我已經檢查了我能找到的與此問題相關的所有 SO 問題。
如果我洗掉了 PlanTemplate 必須具有類別的驗證,則表單的其余部分可以作業并且測驗通過。
我在其他地方的測驗腳本中創建了一個名為“行星”的類別。
我剛剛讓 Capybara 螢屏截圖作業,現在已經更新它以包含文本 - 它似乎沒有出現在 Capybara 中,但出現在本地 - 我無法從我的代碼中看出它為什么會被隱藏?
完整錯誤:
1) PlanTemplate can be created by admin admin can create new plan
Failure/Error: expect(page).to have_content('Planets')
expected to find text "Planets" in "window.addEventListener(\"load\", function(){ window.cookieconsent.initialise({ \"palette\": { \"popup\": { \"background\": \"#252e39\" }, \"button\": { \"background\": \"#428bca\" } }, \"theme\": \"classic\", \"content\": { \"href\": \"Toggle navigation Communities Communities People Mates Weekly Activity view Recent Activity list view All Mate Goals Manage Mates Plans Goals All Goals Manage My Goals New Goal Admin Admin Account Profile Email Settings Manage Mates My Interests Log out New Plan Template Plan name: Plan description: What additional information is required? (optional) What is the delivery medium & requirements of plan? (optional) What should you do once the plan is complete? (optional) How many hours per week does this goal take to complete? How many days in total does this take to complete? /7 for weeks. This will be used to calculate start times What level of user is this plan for? beginner novice intermediate experienced expert What category is this goal in? Feed My Goals My Mates All goals Home Sign Up Log in Browse Users Feedback ? Browse Goals Privacy TOS Cookies window.setTimeout(function() { $(\".alert\").fadeTo(500, 0).slideUp(500, function() { $(this).remove(); }); }, 6000);"
我迄今為止嘗試過的事情在下面被注釋掉(也嘗試過其他人并將其洗掉):
scenario 'admin can create new plan' do
login_as(create(:admin))
visit("/plans/new")
find('#plan-name').set('A test plan name')
find('#plan-desc').set('A test plan description, I think these need to be longish')
find('#plan-weekly-hours').set(1)
# page.check('Planets')
# find("label[for='Planets']").click
# find_field(['plan_template_category_ids_1']).check
# find('.checkbox').check
# check('Planets')
# find(:label, 'Planets').click
# check('.checkbox', allow_label_click: true)
# find(:label, 'plan_template_category_ids_1').click
# find('#plan_template_category_ids_1', visible: false).trigger('click')
find('#plan-days-to-complete').set(35)
find('#submit-new-plan').click
expect(page).to have_content('Plan template was successfully created.')
expect(page).to have_content('A test plan description, I think these need to be longish')
end
我還添加到 rails_helper:
Capybara.automatic_label_click = true
Capybara.ignore_hidden_elements = false
有問題的表單部分代碼:
<%= form.label "What category is this goal in?" %><br>
<%= form.collection_check_boxes :category_ids, Category.all, :id, :name do |cb| %>
<% cb.label(class: "checkbox-inline input_checkbox") {cb.check_box(class: "checkbox") cb.text} %>
<% end %>
Capybara 螢屏截圖中的 html 快照 - 必須將其粘貼為影像,因為 SO 不喜歡在代碼中使用它。

它在 DEV 瀏覽器中的顯示方式 *未測驗:
<div class="col-md-12 goalform">
<label>What category is this goal in?</label><br>
<input type="hidden" name="plan_template[category_ids][]" value=""><label class="checkbox-inline input_checkbox" for="plan_template_category_ids_2">
<input class="checkbox" type="checkbox" value="2" name="plan_template[category_ids][]" id="plan_template_category_ids_2">Dream Chasing</label>
<label class="checkbox-inline input_checkbox" for="plan_template_category_ids_1"><input class="checkbox" type="checkbox" value="1" name="plan_template[category_ids][]" id="plan_template_category_ids_1">Weightloss</label>
<label class="checkbox-inline input_checkbox" for="plan_template_category_ids_4"><input class="checkbox" type="checkbox" value="4" name="plan_template[category_ids][]" id="plan_template_category_ids_4">Productivity</label>
<label class="checkbox-inline input_checkbox" for="plan_template_category_ids_11"><input class="checkbox" type="checkbox" value="11" name="plan_template[category_ids][]" id="plan_template_category_ids_11">Popular</label>
<label class="checkbox-inline input_checkbox" for="plan_template_category_ids_3"><input class="checkbox" type="checkbox" value="3" name="plan_template[category_ids][]" id="plan_template_category_ids_3">Fitness</label>
<label class="checkbox-inline input_checkbox" for="plan_template_category_ids_12"><input class="checkbox" type="checkbox" value="12" name="plan_template[category_ids][]" id="plan_template_category_ids_12">Health</label>
</div>
uj5u.com熱心網友回復:
首先,在您顯示的 HTML 中沒有提到“行星”,因此 Capybara 找不到該復選框也就不足為奇了。如果您嘗試選擇值為 1 的復選框,則根據 HTML,其標簽內容為“Weightloss”。
其次,假設您使用的欄位set是input元素,請停止執行find(...).set(...)并使用fill_in.
這些更改將使您的測驗代碼
scenario 'admin can create new plan' do
login_as(create(:admin))
visit("/plans/new")
fill_in('plan-name', with: 'A test plan name')
fill_in('plan-desc', with: 'A test plan description, I think these need to be longish')
find('#plan-weekly-hours').set(1) # I don't know what type of element this is? If a select use `choose`
# check accepts the id, unique name, or associated label text as a locator
check('Weightloss')
find('#plan-days-to-complete').set(35) # same as above
click('submit-new-plan')
expect(page).to have_content('Plan template was successfully created.')
expect(page).to have_content('A test plan description, I think these need to be longish')
end
如果這對您不起作用,請復制您收到的完整錯誤并將其添加到您的問題中。
此外,Capybara.ignore_hidden_elements = false在撰寫測驗時永遠不要設定。從用戶的角度來看,這樣做會使您的測驗無效,并且基本上毫無意義。該設定可能有意義的唯一時間是網頁抓取。
uj5u.com熱心網友回復:
我遇到的錯誤是因為我使用 let(:category) { create :category, name: 'Planets', id: 99 } & let(:category) { create :category } 作為創建類別的一種方式,但是它們在測驗中無法使用。
我在測驗中沒有使用工廠就創建了它們,它作業正常。
Category.create(name:"Category 1")
我還認為我的 PlanTemplate 工廠會創建一個可以像我在工廠類別 { [create(:category)] } 中使用的類別,但這并不像我預期的那樣可用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/464254.html
