Can I stop an RSpec test run in the middle? - ruby

Sometimes after I start the rspec tests, I realized that I wanted to change something; and if it's a long run I would love to be able to stop it in the middle, change whatever it was, and rerun the tests. Would someone tell me if that is possible and if it is, how to do it?

One can stop the RSpec test in the middle by pressing ctrl-C twice.

As #zetetic said, press Control-C. Or, if you're running from an IDE, click the stop button for the test (not the server or other code you're testing!)

Related

WebStorm Mocha Testing: tests never finish

I'm using WebStorm 2017.2.2 to develop and run some mocha tests.
All is going pretty well, but the tests never finish. It gets done with running all of my test files, then just sits there spinning saying "Running Tests."
If I hover over the red box, it shows a tooltip that says: Stop 'Full Tests' (⌘F2). It also shows "Stop Process" at the bottom of the IDE. So, I click it, and the red box turns into a skull.
Now, if I hover over the skull, it shows a tooltip that says: Stop 'Full Tests' (⌘F2) (same as before). It also shows "Kill Process" (different) at the bottom of the IDE. So, I click it, and the IDE stops saying "Running Tests" and now says "Test Results."
How do I configure WebStorm so that it automatically ties up those loose ends when it reaches the end of my test suite?
To answer this, the mocha docs state that autoexit is not a feature in v4, basically something is stopping the tests from exiting, to revert back to the v3+ way of exiting when your tests pass, use the arg --exit in your test configs under Extra mocha options
https://boneskull.com/mocha-v4-nears-release/#mochawontforceexit

Cucumber test with Selenium and Ruby in Jenkins server fails but passes in local machine

I have some cucumber tests made using the gem 'cucumber-rails'.
This tests use another gem called 'page-objects' (from cheezy) and in my tests I use Selenium and Chrome-driver.
To make the tests run in the Jenkins server (that has no X) I use the gem 'headless' and I've configured the server to use Xfvb (my local machine runs Elementary OS Freya 0.3.2 and the server runs Ubuntu 15.10).
When I run these tests in my local machine (also using headless), they all pass flawlessly every time. The same happens when I run the tests in Jenkins' server from the Terminal.
But when I run them on Jenkins CI, some fail (the error is "didn't find an element" - when the element is there).
Both tests fail because it cannot find an element (the element exists and when it fails I print the screen just to check what's happening. The page print show that it is on the right page and displays the element).
The strange thing is that they both fail at the second step (the first one is going to the login page and authenticate, which it does correctly). Also, there's another test that runs first that always passes (the tests have pretty much the same structure).
Both tests run over the same server (the webpage they access is the same and the data is the same).
I have looked at this over and over and I can't find any explanation. I know the problem is not very detailed, so if there's anything missing that might be important, just tell me and I'll update the post.
If you happen to know any reason why the tests might fail on a 'Xless' server, I would greatly appreciate it!
Thanks a lot for the help!
Edit:
The failures are both "element not found" and the element is present.
I've also made the Jenkins user part of the sudoers.
Any ideas?
Thanks a lot
The only way to solve these issues is to debug the error received. To debug, we can add screenshots at different places in the scenario to identify the cause of the failure. If this is failing as page not completely loaded, then to solve this issue, is to increase the default wait time to open the particular page or to locate an element. Once the page loads or element is located, set the default time to its original value.
Example:
1. You can add screen shot in you scenario:
Scenario: Click on Sign In Link
Given Joe opens "www.yahoo.com" page
When Joe takes screenshot with file name "ScreenShot1"
When Joe clicks "Sign In" link
The second line "When Joe takes screenshot with file name "ScreenShot1" can be used to take the screen shot
And(/^(\S*) takes screenshot with file name "([^"]*)"$/) do |user, file_name|
takes_screenshot_with_file_name(file_name, "ScreenShot")
end
def takes_screenshot_with_file_name(file_name, message)
page.save_screenshot("../../target/#{file_name}.png", :full => true)
end
Default wait time can be increased in below mentioned way:
The second line "And Joe waits for "Sign In" link" can be used to increase the default wait time
Scenario: Click on Sign In Link
Given Joe opens "www.yahoo.com" page
And Joe waits for "Sign In" link
When Joe clicks "Sign In" link
And(/^Joe waits for "([^"]*)" link$/) do |linkName|
Capybara.default_wait_time = 120
page.should have_content(linkName)
Capybara.default_wait_time = DEFAULT_WAIT_TIME
end
Things you can try:
Reduce the test to the very basics: Instead of running it all, just that concrete page and try to find that element. Check if that works. It probably won't...but you can pinpoint better the problem.
Add some waits in the code (sleep <number of seconds>), to see if your code is trying to find something "too fast". This happens time to time.
Instead of using headless, try to use something else and run it in a browser (with some gems like capybara you can switch between headless, firefox or chrome).
Can you find some sort of JS logs, to see if there are any errors?
Do Jenkins and your environment have exactly the same gem versions?
Another thing you can do to debug it is use something like byebug and set a breakpoint just before it fails. You can then use the interactive console to inspect the page, and run different commands to try to find the element. You would have to do this while in jenkins, but as a last resort...
Ok, so I found out the problem and the solution.
The problem was that the elements were still not loaded when performing their lookup. The solution was to use PageObject.wait_until to wait for them.
I realized that this was the problem after noticing the errors were sometimes these ones: Stale Element Reference
But there's a question that remains, still: why does this only happen in Jenkins CI? (whoever finds the solution will earn the bounty :) )

Stop running a cucumber test feature

Is it possible to cancel a cucumber test while it is running? I have tried Ctrl + C and esc, but to no avail. The only way I can stop cucumber once I have started running tests is to wait for the tests to finish or to manually kill the process using the operating system. Is there a better way?
Pressing Ctrl + C and Ctrl + C twice. It should stop immediately.
Try Ctrl + Z. It stops the test but messes a few things up because I think it thinks the process is still going. If you try rake db:test:prepare after you've stopped the test, that usually works. It takes a minute or so, but it's faster than waiting for a long test with lots of JavaScript.
CTRL+Z on mac
Pressing twice CTRL+C on windows
will interrupt the cucumber tests

Can you close all open windows in capybara?

So when I run my tests individually, they work great. However, when I run my feature (multiple tests) my code is failing.
This is because when capybara exists, not all of my windows are closing and therefore my selenium drivers don't know which window to use.
Basically, what is happening is that my test is opening multiple windows. This is happening because my tests are clicking links which open new windows and checking the content in the new windows. Even though I am saying page.quit at the end of each test, this function isn't closing all open windows (closes active window but not the original window). When the 2nd test runs, it uses the originally create window, but when it goes to open the new pop up, it uses the wrong window.
How can I ensure that a new browser is being used during each test and all windows from the previous test are closed.
I am currently using page.reset! and page.quit.
Not sure, but can I say session.quit to close all open browser windows?
This code works:
page.execute_script "window.close();"
I just execute this while in the window I want to close.
I use this code piece to access popups and close them after. It should not be much difference
within_window(page.driver.browser.window_handles.last) do
click buttons and stuff...
...
page.driver.browser.close #closes popup
end
Hope it helps
page.driver.browser.window_handles.each do |handle|
page.driver.browser.switch_to.window(handle)
page.execute_script "window.close()"
end
Did the charm for me. Combination of #Jason and #Justin's answers.
Thanks!
Try the following:
page.driver.browser.window_handles.each do |handle|
page.driver.browser.switch_to.window(handle)
page.quit
end
(I think it will work, but I have not had a chance to test it.)
Try this:
tab_id = page.driver.find_window("http://google.com.au")
page.driver.browser.switch_to.window(tab_id)
page.driver.quit
This worked for me.
In order to have all assertions in spec file, I passed control to new page (window), after executing assertions I closed new page and passed control to original page as bellow:
after(:each) do
expect(#static_page.has_logo?).to eq(true) # assertion in new page
page.execute_script('window.close()') # close new page
switch_to_window(windows.first) # return control to first page
end
Adding this here because this page comes up within the first few Google results for "capybara close all windows".
We had some mysterious feature spec failures that we tracked down to a few that opened app links in second tabs -- since the tabs were just left open, every so often they'd fire a failing AJAX request and the current test would bomb out with a completely unrelated error. The way we fixed that was to add a hook that made sure to close all but the current window after every feature spec:
Rspec.configure do |config|
config.after(type: :feature) do
# Make sure all browser windows except one are closed
windows.reject(&:current?).each(&:close)
end
end
This worked like a charm for us, using Capybara's headless_chrome driver.
Add this to your feature tests, it'll reset the session cookies and start the test on a blank page:
# Window washing - use a clean window before every example
before(:example) do
page.reset!
end

Running tests using the menu vs. shortcut keys

In VS2010, if I click on
Test->Run->All Tests in Solution (CTRL+R,A)
All of my tests will run and I'll be able to see what tests passed and what tests failed.
If I use the shortcut (Ctrl+R,A), then it will break when an exception is thrown.
Why is this experience different? How can I run all my tests through no matter what without going to the menu bar?
The chord "CTRL-R, A" executes
Test->Run->All Tests in Solution (CTRL+R,A)
The chord "CTRL-R, CTRL-A" executes
Test->Debug->All Tests in Solution (CTRL+R,CTRL-A)
So the fact is that you aren't using the shortcut you think you are.

Resources