Testing with Cucumber and Selenium: how can I see what it sees in the browser?
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开发者_Go百科 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?
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.
Check this page: "Pausing Cucumber Scenarios to Debug"
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.
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 After
definition 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.
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.
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.
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
精彩评论