如何等待下一页加载Capybara和Selenium?

时间:2017-06-01 00:07:25

标签: ruby selenium selenium-webdriver capybara

假设您要提交表单以创建帖子,等待创建帖子,从数据库中获取帖子并检查帖子中的图像是否显示在下一页:

visit url
fill_in the_form
click_on 'Create'
post = Post.first
img = page.find '.post .image'
assert_equal post.file.thumb.url, URI(img[:src]).path

但是这样,post往往会niltest 'create post' do visit url fill_in the_form click_on 'Create' assert_selector '.post' post = Post.first img = page.find '.post .image' assert_equal post.file.thumb.url, URI(img[:src]).path end 。从您从数据库中获取它时,它可能尚未创建。如何确保下一页已加载?

2 个答案:

答案 0 :(得分:2)

find将等待Capybara.default_max_wait_time秒,以便在屏幕上显示匹配的元素。因此,要执行您要求的操作,请在从数据库加载项目之前检查您希望在下一页上显示的内容

visit url
fill_in the_form
click_on 'Create'
img = page.find '.post .image'
post = Post.first
assert_equal post.file.thumb.url, URI(img[:src]).path

如果按此顺序完成,page.find将等待元素显示在页面上,以确保已保存帖子,以便您可以加载它。

答案 1 :(得分:-1)

不幸的是,您不想等待页面重新加载。例如,因为现在帖子在页面重新加载后出现。有一段时间它可能会出现没有完整页面重新加载。如果你这样做,请解释你的情况。

我不幸地说,如果我们忽略上述内容,如果我们真的需要等待页面重新加载由于某种原因,旧答案中的想法比我在互联网中看到的任何其他想法都要好。 / p>

所以,你关心的是帖子已经出现在页面上。这给我们带来了以下代码:

wait_until

旧回答

受这些great articlelinkcomment的启发。根据{{​​3}},这是他在Capybara听到def wait_until Timeout.timeout(Capybara.default_max_wait_time) do sleep(0.1) until value = yield value end end def wait_for_page_reload id = find('html').native.ref yield wait_until { find('html').native.ref != id } end test 'create post' do visit url fill_in the_form wait_for_page_load do click_on 'Create' end post = Post.first img = page.find '.post .image' assert_equal post.file.thumb.url, URI(img[:src]).path end 的唯一合法用例。断言模型对象,即。

html

这有点黑客。但它的确有效。它等待var scene = new THREE.Scene(); var camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 1000); camera.position.set(-30, 60, -120); var renderer = new THREE.WebGLRenderer(); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement); var controls = new THREE.OrbitControls(camera, renderer.domElement); var r = 20, verticalLines = 32, /* must be divisible by 4! */ horizontalLines = 16; /* must be divisible by 2! */ var quadSphere_threeQuarters = createQuadSphere(r, 3 * verticalLines / 4, horizontalLines, { color: 0x0000ff }, 0, 3 * Math.PI / 2); scene.add(quadSphere_threeQuarters); var quadSphere_oneEighth = createQuadSphere(r, verticalLines / 4, horizontalLines / 2, { color: 0xff0000 }, 3 * Math.PI / 2, Math.PI / 2, Math.PI / 2, Math.PI / 2); quadSphere_oneEighth.position.x += 50; scene.add(quadSphere_oneEighth); var quadSphere_threeQuarters_right = createQuadSphere(r, 3 * verticalLines / 4, horizontalLines, { color: 0xff00ff }, 0, 3 * Math.PI / 2); quadSphere_threeQuarters_right.position.x -= 50; scene.add(quadSphere_threeQuarters_right); var quadSphere_oneEighth_right = createQuadSphere(r, verticalLines / 4, horizontalLines / 2, { color: 0xff00ff }, 3 * Math.PI / 2, Math.PI / 2, Math.PI / 2, Math.PI / 2); quadSphere_oneEighth_right.position.x -= 50; scene.add(quadSphere_oneEighth_right); function createQuadSphere(r, widthSegments, heightSegments, atts, phiStart, phiLen, thetaStart, thetaLen) { var sphere = new THREE.Group(), material = new THREE.LineBasicMaterial(atts); if (phiStart === undefined) { phiStart = Math.PI / 2; } if (phiLen === undefined) { phiLen = 2 * Math.PI; } if (thetaStart === undefined) { thetaStart = 0; } if (thetaLen === undefined) { thetaLen = Math.PI; } /* width segments (longitude) */ for (var phiDelta = phiLen / widthSegments, phi = phiStart, arc = createVerticalArc(r, heightSegments, thetaStart + Math.PI / 2, thetaLen); phi <= phiStart + phiLen + phiDelta; phi += phiDelta) { var arcTmpGeometry = arc.clone(); arcTmpGeometry.rotateY(phi); var arcLine = new THREE.Line(arcTmpGeometry, material); sphere.add(arcLine); } /* height segments (latitude) */ for (var thetaDelta = thetaLen / heightSegments, theta = thetaStart; theta < thetaStart + thetaLen - thetaDelta; theta += thetaDelta) { if (theta === 0) { continue; } var arcR = r * Math.sin(theta), arcH = r * Math.cos(theta), arcTmpGeometry = createHorizontalArc(arcR, widthSegments, phiStart, phiLen); arcTmpGeometry.rotateX(Math.PI / 2); arcTmpGeometry.rotateY(Math.PI / 2); arcTmpGeometry.translate(0, arcH, 0); var arcLine = new THREE.Line(arcTmpGeometry, material); sphere.add(arcLine); } return sphere; } function createVerticalArc(r, segments, thetaStart, thetaLen) { var geometry = new THREE.CircleGeometry(r, segments, thetaStart, thetaLen); geometry.vertices.shift(); return geometry; } function createHorizontalArc(r, segments, phiStart, phiLen) { var geometry = new THREE.CircleGeometry(r, segments, phiStart, phiLen); geometry.vertices.shift(); if (phiLen >= 2 * Math.PI) { geometry.vertices.push(geometry.vertices[0].clone()); } return geometry; } render(); function render() { requestAnimationFrame(render); renderer.render(scene, camera); }元素的内部ID更改。