capybara / Observe html5/history route change - tdd

as it is somehow possible to wait for ajax calls (here and there), would there be a way to wait for or observe a route change in capybara (html5/browser history) ? Does a route change fire an event that we can listen to ?

Pushing to or popping from the history should change the current URL so you can use The have_current_path matcher
expect(page).to have_current_path("/whatever")

Related

Capybara wait until button is enabled?

Surprised I actually haven't come across this, but I have a simple button that is disabled until a dropdown is selected. Sometimes the page isn't fast enough to "enable" the button to be clicked on after the previous dropdown is selected causing it to fail.
I could throw in a "sleep" of a second or two and fix this, but that seems like a lazy/poor way to do this.
Is there a way in capybara (or purely selenium) that I can make it wait until the button is actually enabled? I'd like to throw this is the page model method for this button (as im trying to avoid API specific methods/selenium/etc... in the actual test specs (Although I can if I need to).
FWIW this is specifically for Ruby's capybara framework but pure selenium is fine as well.
Assuming the button you're referring to is actually a button (<button> element, or <input> element with type submit, reset, image, or button) then Capybaras :button selector will (by default) wait for it to be non-disabled.
click_button('Something')
or
find_button('button_id').click
or
find(:button, 'button_value').click
If any of the finder or action methods aren't waiting long enough for a specific element you can always increase the maximum wait time for a specific finder/action by passing a :wait option
find(:button, 'Something', wait: 10).click
If you're not using selector types (if not, why not) and instead are just using raw CSS to locate the element then you can use the :enabled pseudo class along with your existing CSS and something like
find('#my_button:enabled', wait: 10).click
If the element you're calling a button isn't actually a button but some other type of element (<a> etc) styled to look like a button, then you'll need to explain exactly how you're disabling the "button".
In Python you can do something like this:
def wait_until_clickable(driver, xpath, timeout = 1):
while timeout > 0:
try:
element = driver.find_element_by_xpath(xpath)
element.click()
return element
except:
time.sleep(0.1)
timeout = timeout - 0.1
return False

how to reload page until a button appears using capybara and ruby

I want to click a button after an action and button does not appear until and unless I reload the page. And some times it takes some time to appear the button and I have to reload page for more than once. I don't want to put static delays. So is there a way to achieve following using capybara and ruby:
do
page.evaluate_script("window.location.reload()")
until a button appears
While Mesut's code should work fine, I would re-write it as:
Timeout.timeout(Capybara.default_max_wait_time) do
loop do
page.evaluate_script("window.location.reload()")
break if page.has_selector?(...)
end
end
This will make sure to fail if it will have to wait more than timeout defined in Capybara settings. It can be useful when for example specs are running on the CI server.
Be aware that it can still lead to unexpected behaviors in some drivers, because it can interrupt while some scripts are evaluating.
reoload until page.has_selector? returns true, check this:
while true
page.evaluate_script 'window.location.reload()'
if page.has_selector?("css_selector")
break
end
end

Sammy loses control of routing when combining back button and history.replaceState

I am having an issue with Sammy routing, and combining use of the back button with history.replaceState. Is there an alternative way to replace the browser's URL without adding to the history stack, and fire a Sammy route other than history.replaceState?
history.replaceState triggers the Sammy route initially, but if I use history.pushState, then click the back button, then history.replaceState, it does not trigger the Sammy route.
It also seems to break if I use location.hash or location.href in combination with history.replaceState
I am wanting to use replaceState to control pagination and sorting of a table, but let the standard history stack work for different search parameters and navigating to different pages and back to a search screen.
Edit: I am looking into ways to set Sammy's internal URL without triggering routing.
Faced with the same problem. Solved with:
history.replaceState(path);
sammy.setLocation(path);
First- update browser history (sammy doesn't trigger location changes)
Second- force sammy to raise events with new path.
Also custom route logic is needed:
Sammy.mapRoutes([{'get': 'myPath', callback}])
Callback should parse new path, and if only params after '?' symbol are changed, then pass these parameters to the viewModel's method.
Then inside this method you could handle changes like this:
public onParamsChanged({page}) {
this.myTable.setPage(page);
}

Play framework flash scope does not clear on ajax

If I have a controller method that sets flash.success("some.i18n.key"); and I render a page that is loaded via ajax that item does not get removed from flash. Even though I've rendered the content to the screen (html loaded into a div in the success handler of my ajax post) the next page I visit still has the success message in flash. Pages that work with a normal form post,non ajax) this issue does not happen. Any idea whats going on?
Further investigation seems like this might be some sort of race condition. When I do a normal post and the FLASH cookie is returned it expires immediately and on the next request it is not sent back to the server. In the case of the AJAX post and then a subsequent request the cookie IS sent back to the server.
flash values are kept for one redirect. If you call render in your controller at the end of your method, you do not issue a redirect, so values will be available for the next request. To avoid this you have the choice :
use renderArgs in your method to pass your value to the view
at the end of your method, do not call render but call another method of the controller, thus you will issue a redirect instead of a direct render.
Since play 2 they changed the flashing a bit, instead of 2 maps (incoming, outgoing) there is just one.
What I end up doing is calling:
#flash.clear()
Just after the flash messages are rendered (in the view). This way, you are sure they are rendered just once, regardless of weather you use direct render, or redirect.

Watir and Ajax requests

In my webapp I have a simple textfield. To this textfield I have a jQuery function which will be always executed on every keyup. With this function there is an Ajax request assigned which loads every time the result of the SQL-Query. My code is equivalent to the code of
RailsCasts. Now I'm testing my webapp with Selenium. With this line of code
browser.text_field(:id => 'textfield').set("Search text")
the text will be written and the content will be changed. After it should click on a link which is placed on the dynamic content with this code
browser.a(:id => "link").click
The problem now is that the click event won't be executed. Has somebody an idea what the problem could be? Or maybe an example with Watir and Ajax?
Without an example site to test against it's hard to be sure but I will throw out a few potential solutions for you
If the client side javascript is looking for onkeyup events, you may need to fire one after setting the contents of the field. You can do that via the .fire_event method
You could just be looking at a timing issue. If the nature of the link is changing as a result of the input, it's possible that Watir is firing off the two comments in rapid succession and the client side code is still in the midst of doing it's thing (especially if there is back and forth between the jquery code and the webserver that as happening as that also induces networking delays. You may need a brief sleep between commands (brute force) or to wait for a particular element to assume an expected state (a little more work but also a bit more robust and not subject to breaking is the delay exceeds your sleep duration)
I'd suggest executing the commands manually via IRB (you could just cut and paste from your script as needed) to be able to watch the state of the browser, and note any delay in updating the screen/DOM after a particular action. If stuff works properly in IRB but not when executed via a script it's often taken as confirmation of a timing issue.
If this is the original Watir/Firewatir I would try getting it to hover over the link before it attempts to click it.
I've had this problem previously with links that appear after typing into an "autocomplete" field (i.e. it attempts to guess at the text you want by filtering down from a huge list of possibilities).
Try this:
browser.wait_until{browser.link(:id => "link").present?}
browser.link(:id => "link").fire_event("onmouseover")
browser.link(:id => "link").click
If it works, try it without the .fire_event("onmouseover"), because it could just be that it's trying to click the link before it's visible and failing to do so.
If this is actually a question regarding "Selenium" (wrongly labelled Watir) then ignore all of the above, because I that is an application I've never used.
you can use capybara method.
click_link("link")
for ajax set :js => true in you test case
http://opinionatedprogrammer.com/2011/02/capybara-and-selenium-with-rspec-and-rails-3/

Resources