使用 Cucumber 和 Selenium 进行测试:如何查看浏览器中看到的内容?
我正在用 Cucumber 在 Rails 中测试一个场景,它返回说找不到要单击的编辑链接。如果我自己去该页面,它就在那里。显然我做错了什么,但我看不到。
如果我在场景之前添加 @selenium 标签,它将在 Firefox 中执行测试。但如果我看到浏览器打开和关闭,除非它需要我的交互(例如确认删除),否则它会在我看到它在做什么之前就过去了。
有没有办法查看浏览器中看到的内容并逐步进行?
I'm testing a scenario in Rails with Cucumber, and it comes back saying it can't find the edit link to click on. If I go to the page myself, it's there. Obviously I'm doing something wrong, but I can't see it.
If I add the @selenium tag before my scenario, it executes the tests in Firefox. But it I see the browser open and close, and unless it needs interaction from me (like to confirm a delete), it passes by before I can see what it's doing.
Is there a way to see what it's seeing in the browser and move through step-by-step?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
当您想查看 selenium 看到的内容时,请将以下内容添加到您的功能中:
使其工作。您需要在黄瓜加载路径中的某处进行以下
After
定义:在此处查看示例
瞧:现在浏览器窗口将保持打开状态,直到您在控制台中按 Enter 键。您可以单击并检查所有内容。
When you want to see what selenium sees add the following to your feature:
To make it work. you need the following
After
definition somewhere in your cucumber load path:see an example here
Voilá: Now the browser window stays open until you hit enter in the console. You can click around and inspect everytthing.
为什么不设置一个屏幕录制工具并针对任何显示网络驱动程序运行它?对我来说似乎是一个很好的低技术解决方案。
Why not set up a screen recording tool and run it against whatever display web driver is on? Seems like a good low tech solution to me.
我通常使用 Ask 方法来暂停操作,以便我可以看到发生了什么。我将在一切都暂停的情况下使用 Firebug 来探索这个问题。请参阅此处获取允许您使用 Firebug 和 Cucumber 来查看 CSS/JS 的 gem,等等。ask
方法的示例:
如果某些内容看起来不正确,您还可以编写ask 行来实际结束测试,但会根据返回值进行操作。
另一种选择是使用 ruby 调试器并检查正在运行的所有内容。 这里是一个解释其工作原理的问题。
I usually use the ask method to pause the action so I can see what is going on. I will use Firebug to explore the issue while everything is paused. See here for a gem that allows you to use Firebug with Cucumber to look at CSS/JS, etc.
Example of ask method:
You can also write the ask line to actually end the test if something does not look right but acting on the return value.
Another option is to use the ruby debugger and inspect everything running. Here is a SO question explaining how this works.
使用 Ruby,您可以添加 pry-nav gem - https://github.com/nixme/pry-nav< /a>
将以下内容添加到您的 gemfile 中:
宝石“撬”
gem '撬远程'
gem 'pry-nav'
然后将行 'binding.pry' 添加到您要中断的步骤定义中
然后您可以使用 step, next, continue 从命令行逐步执行代码。您还可以获得控制台功能,例如。你可以评估表达式
With Ruby you can add the pry-nav gem - https://github.com/nixme/pry-nav
Add the following to your gemfile:
gem 'pry'
gem 'pry-remote'
gem 'pry-nav'
Then add the line 'binding.pry' to your step definition, where you want to break
Then you can use step, next, continue from the command line to step through your code. You also get console functionality eg. you can evaluate expressions
要查看特定时间点浏览器中显示的内容,请将此步骤添加到您的场景中:
您可能无法获得所有样式,这取决于情况。
您还可以尝试另一件事:添加一个如下所示的步骤:
当页面显示在浏览器中时,您应该有 30 秒的时间来查看该页面。
To see what is displayed in the browser at a specific point in time, add this step to your scenario:
You might not get all the styling, it depends.
There's another thing you could try too: add a step that looks something like this:
That should give you 30 seconds to look at the page while its displayed in the browser.
检查此页面:“暂停 Cucumber 场景进行调试”
Check this page: "Pausing Cucumber Scenarios to Debug"
我通过实施
Cucumber
步骤解决了这个问题,与标签解决方案非常相似,但噪音较小。这样我就可以随时停止测试执行。
I solved it by implementing
Cucumber
stepVery similar to tags solution, but less noisy. This way I am able to halt test execution at any time.