使用 Cucumber 和 Selenium 进行测试:如何查看浏览器中看到的内容?

发布于 2024-10-01 19:42:14 字数 219 浏览 2 评论 0原文

我正在用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

烟酒忠诚 2024-10-08 19:42:15

当您想查看 selenium 看到的内容时,请将以下内容添加到您的功能中:

@leave_the_window_open
@javascript
Feature: The failing feature you want to click through
(...)

使其工作。您需要在黄瓜加载路径中的某处进行以下After定义:

After('@leave_the_window_open') do |scenario|
  if scenario.respond_to?(:status) && scenario.status == :failed
    print "Step Failed. Press Return to close browser"
    STDIN.getc
  end 
end

在此处查看示例

瞧:现在浏览器窗口将保持打开状态,直到您在控制台中按 Enter 键。您可以单击并检查所有内容。

When you want to see what selenium sees add the following to your feature:

@leave_the_window_open
@javascript
Feature: The failing feature you want to click through
(...)

To make it work. you need the following Afterdefinition somewhere in your cucumber load path:

After('@leave_the_window_open') do |scenario|
  if scenario.respond_to?(:status) && scenario.status == :failed
    print "Step Failed. Press Return to close browser"
    STDIN.getc
  end 
end

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.

神也荒唐 2024-10-08 19:42:15

为什么不设置一个屏幕录制工具并针对任何显示网络驱动程序运行它?对我来说似乎是一个很好的低技术解决方案。

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.

朕就是辣么酷 2024-10-08 19:42:15

我通常使用 Ask 方法来暂停操作,以便我可以看到发生了什么。我将在一切都暂停的情况下使用 Firebug 来探索这个问题。请参阅此处获取允许您使用 Firebug 和 Cucumber 来查看 CSS/JS 的 gem,等等。ask

方法的示例:

res = ask('Where is that error coming from??')

如果某些内容看起来不正确,您还可以编写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:

res = ask('Where is that error coming from??')

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.

笑梦风尘 2024-10-08 19:42:15

使用 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

寻找我们的幸福 2024-10-08 19:42:14

要查看特定时间点浏览器中显示的内容,请将此步骤添加到您的场景中:

Then show me the page

您可能无法获得所有样式,这取决于情况。

您还可以尝试另一件事:添加一个如下所示的步骤:

Then /^I pause for a while$/ do
  sleep 30
end

当页面显示在浏览器中时,您应该有 30 秒的时间来查看该页面。

To see what is displayed in the browser at a specific point in time, add this step to your scenario:

Then show me the page

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:

Then /^I pause for a while$/ do
  sleep 30
end

That should give you 30 seconds to look at the page while its displayed in the browser.

水中月 2024-10-08 19:42:14

我通过实施 Cucumber 步骤解决了这个问题,

Then /^Wait for keypress$/ do
  STDIN.getc 
end

与标签解决方案非常相似,但噪音较小。这样我就可以随时停止测试执行。

I solved it by implementing Cucumber step

Then /^Wait for keypress$/ do
  STDIN.getc 
end

Very similar to tags solution, but less noisy. This way I am able to halt test execution at any time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文