I'm doing some modifications on a page in my web application, this results in a popup message displayed "This page is asking you to confirm that you want to leave - data you have entered may not be saved" with two buttons "Leave Page" and "Stay on Page".
I can close my web application if using FF16 or Chrome with below code:
#driver.quit
However, I cannot close if I'm opening my web application on the browser IE9 (it means that I can open another IE9 browser session, but the browser session with popup message displayed is NOT closed).
I'm using Selenium Ruby Webdriver. Please help guide me a solution to resolve this problem. Thanks much.
I found out the expected answer for my question at: https://stackoverflow.com/a/7034926/1385187 (Thanks so much for this great answer).
( ( JavascriptExecutor ) _driver )
.executeScript( "window.onbeforeunload = function(e){};" );
Related
I am working in a website like https://jira.atlassian.com/ using firefox, after clicking the "log in" on the top right corner, i am taken to the next login page, where on the right of "log in" button, there is this "stay logged in" checkbox, already checked.
I tested it manually. It seems that after i log in successfully, if i do not exit the page by log out, but simply close the page, the next time i enter https://jira.atlassian.com/, i am already logged in. Ok, make sense.
However, if testing such using selenium webdriver, log in, close page, open page again, the above does not happen.
My question is what is the mechanism for this "stay logged in", is it the same as "remember me"? and how do i make it happen when using selenium webdriver?
Up till now my understanding is:
this happens because of cookie
cookie is stored in firefox profile
selenium start with a default profile that does not include cookie, and can not store cookie during the test
Is there a relatively standard solution for this problem, in java preferably?
Thanks,
As far as I am aware, each instance of loading a FF with Selenium opens a stripped browser with no history/cookies/extensions etc. It's as if you have just download FF for the first time and its a fresh copy. I am not sure how to solve this issue. Maybe if you keep the session open by not closing the browser down but log out manually and not closing down the browser. Or open a new tab and opening the same address.
you can try to store the cookies and apply them to your new firefox instance
get the cookies using:
Set<Cookie> cookies = driver.manage().getCookies();
and than apply them to your new webdriver instance:
for(Cookie cook : cookies) {
driver.manage().addCookie(cook);
}
I have JavaScript in my application that submits a form when the user hits Ctrl-S or Cmd-S. I want to write an automated test for this using RSpec, Capybara, and Capybara Webkit. I don't think I can just have Capybara execute JavaScript to trigger Ctrl-S/Cmd-S because that's not normally allowed with JavaScript in Chrome as a security concern. I see with Selenium there are page.driver.browser.action.key_down/key_up methods available. Is there anything similar with Capybara Webkit? If not, how can I send Ctrl-S and Cmd-S to the browser in my test?
Edit: I also can't get this to work using the regular Selenium driver with Firefox:
describe 'edit a template and hit Ctrl-S', js: true do
render_views
it 'saves the template' do
visit my_path
page.execute_script("$('#hidden_textarea').val('Fabulous new content')")
builder = page.driver.browser.action
builder.key_down(:control).send_keys('s').key_up(:control).perform
expect(page).to have_text('Record was saved.')
expect(page).to have_text('Fabulous new content')
end
end
It looks like the builder.key_down(:control).send_keys('s').key_up(:control).perform isn't doing anything--the page loads in Firefox but just sits there. This is with Firefox 19 on OS X with selenium-webdriver 2.35.1.
Any suggestions on how to get this to work in either Firefox or Chrome, with either Selenium or Capybara Webkit?
I'm trying to do a similar thing in Chrome using Capybara and SitePrism. This actually works for me in Firefox though:
page.element.native.send_keys :command, 'a'
so I suggest trying this
builder.native.send_keys :control, 's'
I want to click on browser "stop loading this page" icon, when timeout error occurs,what should i do for it? Some thing like this:
browser.stop # this is wrong.
I searched forever and the best I could come up with was:
b = Watir::Browser.new
b.driver.manage.timeouts.implicit_wait = 3
This worked great for me with firefox.
The below link may help you :
Stop loading page watir-webdriver
Is there have method to stop page loading in watir-webdriver
Just try this logic when you want to stop the loading,
#browser.send_keys :escape
It is not the best way of doing what you want but still the above approach worked out for me.
If you use Watir-Webdriver then you can try this for IE
#browser.execute_script "document.execCommand('Stop')"
on my web application, after clicking a download button, a popup message with content "Do you want to open or save "abc.txt" from this site?" with 3 buttons ("Open", "Save" & "Cancel") will be displayed at the bottom of page.
I'm trying to close/quit this browser session with below codes:
#driver.execute_script "window.onbeforeunload = function(e){};"
#driver.quit
However, the browser is NOT (but should be) closed. I'm working with Selenium Ruby Webdriver. Please guide me a way to resolve this problem. Thanks so much.
Note that with the above codes, I'm able to close IE9 browser that has the popup message "This page is asking you to confirm that you want to leave - data you have entered many not be saved" with "Leave Page" and "Stay on Page" buttons successfully. But, codes do NOT work in case the popup message with content "Do you want to open or save "abc.txt" from this site?" with 3 buttons ("Open", "Save" & "Cancel") displayed.
WebDriver has no control over these types of "Save file" dialog prompts.
Please peruse this article on this subject (note, his examples are in Java, but they can all be ported easily and with less code in Ruby). http://ardesco.lazerycode.com/index.php/2012/07/how-to-download-files-with-selenium-and-why-you-shouldnt/
Thus, I would recommend not even clicking the link in question and trying to deal with the dialog. Instead, grab the href value and initiate a Net::HTTP request to it like this SO response shows you how to do: https://stackoverflow.com/a/4581116/1221475 . You can then check the file for correct contents and such using standard Ruby and file parsers.
This is a hack by Dave Haefner. It is written for Java/Selenium combination, but you can easily convert it to Ruby syntax.
If you don't care if a file was downloaded or not and you want to confirm only that a file can be downloaded, you can use an HTTP request. Instead of downloading the file you'll receive the header information for the file which contains things like the content type and length. With this information, you can confirm the file is you expect.
String link = driver.findElement(By.cssSelector("download-link-element")).getAttribute("href");
HttpClient httpClient = HttpClientBuilder.create().build();
HttpHead request = new HttpHead(link);
HttpResponse response = httpClient.execute(request);
String contentType = response.getFirstHeader("Content-Type").getValue();
int contentLength = Integer.parseInt(response.getFirstHeader("Content-Length").getValue());
assertThat(contentType, is("application/octet-stream"));
assertThat(contentLength, is(not(0)));
I'd like to extend my WatiN automated tests to drive a page that guards against the user accidentally leaving the page without saving changes.
The page uses the "beforeunload" technique to seek confirmation from the user:
$(window).bind('beforeunload', function (event) {
if (confirmationRequired) {
return "Sure??";
}
});
My WatIn test is driving the page using IE. I cannot find a way to get WatIn to attach to the popup dialog so I can control it from my test.
All the following have failed (where the hard-coded strings refer to strings that I can see on the popup):
Browser.AttachTo<IE>(Find.ByTitle("Windows Internet Explorer");
browser.HtmlDialog(Find.FindByTitle("Windows Internet Explorer));
browser.HtmlDialog(Find.FindByTitle("Are you sure you want to leave this page?));
browser.HtmlDialog(Find.FindFirst());
Thanks!
You'll need to create and add the dialog handler.
Example Go to example site, click link, click leave page on confirmation dialog:
IE browser = new IE();
browser.GoTo("http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/onbeforeunload.htm");
WatiN.Core.DialogHandlers.ReturnDialogHandlerIe9 myHandler = new WatiN.Core.DialogHandlers.ReturnDialogHandlerIe9();
browser.AddDialogHandler(myHandler);
browser.Link(Find.ByUrl("http://www.microsoft.com")).ClickNoWait();
myHandler.WaitUntilExists();
myHandler.OKButton.Click();
browser.RemoveDialogHandler(myHandler);
The above is working on WatiN2.1, IE9, Win7. If using IE8 or before, you will likely need to use the ReturnDialogHandler object instead of the Ie9 specific handler