How to submit a form in a new page with using ruby web-watir phantomjs - ruby

I am using web-watir to drive phantomjs. I am trying to submit a bunch of forms(POST) on the webpage. I have all the forms in a collection. When I click the submit button and browser.back(), I get Selenium::WebDriver::Error::StaleElementReferenceError. I tried using form.submit() but same issue of going back and getting StaleElementReferenceError. I tried to submit the form in another page (browser.execute_script( "window.open(page)" )) but I don't think that will submit my form (if the form was a GET, that might have worked). I tried Net::HTTP.post_form() which is separate from the phantomjs session (did not work, I need to be logged in).
I am out of ideas except find the elements all over each time I navigate back. I guess I could replace phantomjs with Chrome or Firefox. Tell me, is there a way to submit the form in a new page phantomJS?
Here is some code:
forms = browser.forms()
forms.each{ | form |
form.submit()
browser.back()
}

If submitting each form brings you to another page, then it is expected that you get a StaleElementReferenceError for any previously saved elements. This is how Selenium-WebDriver was designed, so the problem will still exist if you switch to Chrome or Firefox.
Given that you are iterating through each form, it would be easy to locate each form by index:
browser.forms.length.times do |i|
browser.form(index: i).submit
browser.back
end
The above code took the same approach of directly submitting the form. However, if possible, the submit button should be clicked. Directly submitting the form may bypass important code that is tied to the action of clicking the submit button.

Related

Ajax is not working on first click on submit in contact form 7

I have a form created in contact form 7. But Ajax submission is not working on the first click of submit. On first click it leads to URL like https://example.com/resolution-table-reservation-management-system/#wpcf7-f651-o1 and when click second time on submit the ajax works. I have checked that my header and footer have wp_head() and wp_footer(); functions. and also checked after deactivating all the other plugins. But still ajax is not working. Is there any solution to this? Please help. I don't know how to solve this.

MVC Ajax form and browser back button

I am currently working on an MVC site which has a search page that sits behind a login page. When the user logs in, they are redirected to the search page.
The search form uses an ajax form that returns search results to the same view.
When we have search results, the search form is no longer shown. This all works fine.
However, once you have fired off the form and have search results - the user will probably try and hit the browser back button to try and get back to the search form. Of course, this instead takes them to the previous page (the login page).
So, my question is this - I would still like to keep the form and results on the same page using ajax. Is there anyway that I can achieve this, in conjunction with when the user clicks back, it takes them to the search form?
I appreciate I am probably asking for too much here, but I thought I'd put it out there anyway!
ps - I already have a 'back to search' link which re-loads the form. I believe the average user will try and click the browser back button. I would also rather not have some sort of function on the login page that redirects them back to the search page.
You need to use the History API. It's supported in modern browsers, and there's a polyfill for IE9 and below.
UPDATE
For what it's worth, I agree with #TravisJ. This is a bad design, but if you insist on going down this path, then my notes above apply.

MVC3 using BeginForm on IE9 Needs to submit twice before contoller gets called

I have a form that uses Html.BeginForm and for most cases this works as you would expect.
But in some situations I display a partialview that does several ajax calls to populate itself. When the partial view has been displayed I need to click the submit button twice to get the form to post. On the first click I can see the form refresh, and then on the second click the form actually posts. This is when using IE9, using Firefox the posts work on the first click.
I would like to know if anyone has seen this behaivor before I spend a lot of time trying figure this out.
Install Fiddler, open it up, and then reproduce the issue in IE. Fiddler will capture all of the requests, so you can see exactly what was sent to the server (and back).
Had a similar issue. The reason was, we had a in view and in addition we were doing $('#form').submit() as well. But the button didn't have "type" attribute. After setting button type=button. It was good.

EditLive! Rich Text Editor - submitting form via ajax

The project I'm working on is using the Java EditLive! rich text editor. I've been trying to make the EditLive form post via ajax, but am having some problems using IE8. Here are the steps we're taking:
Load the main page
The user clicks a link and the EditLive applet is loaded and attached to the page via ajax
The user finishing editing their document and clicks the submit button
The form posts via ajax (we're using jQuery.post())
The EditLive section is reloaded and the EditLive content is correct.
The form immediately posts again
The EditLive content is back to being blank.
Unfortunately (for debugging reasons), this is not happening in FireFox - there is only a single form post and the values are saved correctly.
From what I can tell debugging this in IE8, it looks like the submit event is getting called twice with 2 different forms. My thought is that the applet isn't getting destroyed correctly, though I've tried everything in my power to destroy it.
So I was wondering if anyone has any experience successfully submitting EditLive data via ajax? Or maybe this is just a limitation to the product?
Any help would be greatly appreciated!
I know this is an old issue but you likely want to look at the autoSubmit property of EL:
http://docs.ephox.com/display/EditLive7/AutoSubmit+Property
http://docs.ephox.com/display/EditLive/AutoSubmit+Property
I suspect that by using an AJAXy submit process this is somehow causing you issues with EditLive and its standard behavior. I would try turning off autoSubmit and grabbing the content yourself in your jQuery posting process.

Call interrupted by page load

I am a beginner using ajax and I always thought that it is completely asynchronous. But I discovered that a call can be interrupted by a page reload or a page change (like clicking on a hyperlink). I was under the impression that when an ajax call is started, it is carried out no matter what the browser does afterwards. Is that wrong?
Now to the specific problem I am having: think of an online test where users answer questions (by typing into textboxes). When a textbox loses focus, an ajax call is triggered which persists the value of the textbox to a DB. That works well when changing between textboxes. However, I also have a submit button which triggeres a post action to another page (it is the submit button). When I enter something into a textbox and click on the button afterwards, the call is not carried out. Moreover, when I type into a textbox, click somewhere else (also triggering the call) and swiftly click on the submit button, the call is also not made. Is that expected behaviour?
The reason I am using ajax in the first place is to persist the values so when something unforseeable happens, like a browser crash, the already typed in text is already saved.
Is my way of thinking wrong? How would you go about solving this problem?
Thank you for your time!
AJAX is asynchronous.
When you send an AJAX request the javascript engine sends it off and sets up a handler for the response.
However, if you send an AJAX request to the server and then navigate away from the page before it is received, nothing will happen. Why? Because with each page load the entire Javascript environment is tore down and reinitialized, it has no idea what happened on the last page.
For your problem I would intercept the form submit action and do whatever you need to do with the data, and then submit the form.
Edit: In response to your comment. You are correct. If the ajax request is sent, and you're not depending on it's return value, then it should not matter.
I'd suggest debugging your problem with Firebug to see if the AJAX call is really being sent properly, and to confirm your server is properly processing it.
Unless you do something special with persistent local storage, all javascript and ajax calls are blown away when a new page is loaded over the current page. Also when a submit is done on a form.
To save things intra-page, save the data asap. Eg, perhaps save on key-up, perhaps periodically with a timer, not just on lose-focus.
Re submitting the page: change the on-click behavior to first store, then to go to a new page.
All of the effects that you are seeing are normal.
Also, be sure to test on both slow (ie 6 or 7) and fast browsers (chrome)

Resources