The warning you're getting about page.current_path
not available when attempting to screenshot is probably because your RSpec after hooks are installed in the wrong order (session is being reset before the screenshot call is made).
As for the failing test, it's really hard to tell exactly what you're doing but a number of things stand out as potential issues
I'm assuming the element ids are named #task_<task project order>
If it's not and instead it's #task_<task id>
then you need to rethink how you're doing the test
tbody:nth-child(2)
finds the tbody that is a second child, not the second child of the tbody element. I'm guessing you wanted tbody tr:nth-child(2) ...
There is no need to find the element before doing within
since within
will find the element (with retry/waiting)
The saving of the new order has to be committed before you call the second visit
- hopefully your check for the element being in a different row does indicate the order has been saved.
Applying those 3 I end up with
it "can re-order a task", :js do
visit(project_path(project))
within("#task_3") do
click_on("Up")
end
expect(page).to have_selector(
"tbody tr:nth-child(2) .name", text: "Take Notes")
#END:P1
#START:P2
visit(project_path(project))
within("#task_2") do
expect(page).to have_selector(".name", text: "Take Notes")
end
end
If fixing the selector there doesn't fix your test (hopefully it should sync the browser so the second visit doesn't occur until after the order has actually been changed), then you need to come up with something visible that actually indicates the new order has been saved and wait for that before calling the second visit.
If using a relatively recent version of Capybara you could also use the location based filters to actually verify what I assume you're looking for (that the vertical order of the text changes) - something like
notes_row = find('tr', text: 'Take Notes')
expect(page).to have_selector('tr', text: 'Use Telescope', above: notes_row)
notes_row.click_on('Up')
expect(page).to have_selector('tr', text: 'Use Telescope', below: notes_row)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…