Selenium-RC Unable to handle Confirmation box - c#

I am testing a web app where i delete an item from a list. Upon clicking on delete, the app asks for confirmation. Selenium IDE detects it as a confirmation box. When I run the code thro' the RC (C#), it even catches the confirmation-box, executes the click of delete button on that confirmation box, but, the confirmation box is never visible on the screen. Further, it only clicks on the delete button; the item doesn't get deleted. I tried it manually, works fine.
Please help, I'm new to Selenium and tried to find the answers at multiple forums without any success.
Here's the code:
string confirmation;
for (int second = 0;; second++) {
if (second >= 60) Assert.Fail("timeout");
try
{
confirmation=selenium.GetConfirmation();
if ((confirmation == " Delete confirmation message")) break;
}
catch (Exception e)
{
PrintLog("Error while waiting for confirmation. Error: "+e.Message);
}
Thread.Sleep(1000);
}
try
{
Assert.IsTrue(confirmation == "Delete confirmation message");
}
catch (AssertionException e)
{
PrintLog(e.Message);
}
selenium.FireEvent("//a[#id='btnOkConfirm']","click");
After the last statement, the selected item must get deleted and page should refresh, but, nothing happens. All I can see is "Javascript:;" written in the status bar of the firefox window. I guess its problematic to get javascript hrefs working in selenium-rc.
Thanks,
Vamyip

There are a number of commands to deal with JavaScript confirmations. Selenium will by default choose 'OK' at a confirmation, unless you send the chooseCancelOnNextConfirmation command. In order to consume the confirmation you will need to use the getConfirmation command.
Selenium reference for above commands:
http://release.seleniumhq.org/selenium-core/1.0/reference.html#storeConfirmation
http://release.seleniumhq.org/selenium-core/1.0/reference.html#chooseCancelOnNextConfirmation
http://release.seleniumhq.org/selenium-core/1.0/reference.html#chooseOkOnNextConfirmation
Additionally, if your click command is not showing the JavaScript confirmation you might find that the appropriate event is not being fired. You could try using the mouseDown and mouseUpcommands, or the fireEvent command.

Lately, I've discovered that this behavior is occurring due to the architecture of selenium(more precisely, its javascript based core). when I do the test manually, keeping the selenium IDE open, this behavior is replicated. So, I think there's no immediate solution to this problem right now. Will post here if I find a workaround.
Thanks to Dave for replying.
Update: The development team informed me that a javascript function is not being called with selenium IDE running alongside. This indeed is a problem with Selenium's Javascript core.
Thanks to everybody who took time to respond to this question.
Regards,
Vamyip

Related

How to Click OK on chrome popup using Selenium Webdriver C#

I'm trying to code below script for clicking OK button through script but fail to do it.
IAlert alert = driver.SwitchTo().Alert();
alert.Accept();
What am I doing wrong, Correct me.
I found an excellent nuget package, AutoItX.Dotnet, that will handle popups in chrome.
Link to nuget package - https://www.nuget.org/packages/AutoItX.Dotnet/
Please use the code below as a reference
//This code snippet will fix your specific issue
AutoItX.WinWait("Untitled - Google Chrome", "", 2);
AutoItX.WinActivate("Untitled - Google Chrome");
AutoItX.Send("{Enter}");
//Use code below to switch between buttons/text boxes within the popup
//And send text to text boxes within the popup
AutoItX.Send("{TAB}");
AutoItX.Send("HelloWorld");
You may have to test a bit further to see what keys you need to pass to make this code work seamlessly with each specific popup that you wish to interact with.
JavascriptExecutor should work for you. Just take care that you should execute it before clicking the event which invoke alert.
C# Code
IWebDriver driver; // assume assigned elsewhere
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("window.confirm = function(msg) { return true; }");
Java Code
((JavascriptExecutor) driver).executeScript("window.confirm = function(msg) { return true; }");
Note :- do not use it after clicking on event which invoke alert confirmation box. Above code by default set the confirmation box as true means you are accepting/click on ok on all confirmation box on that page if invoked
Hope it will help you :)

How to implement an if else condition in the code behind for ImageButton OnClick

I am working on my website design and have already implemented a simple slideshow consisting of image buttons that would redirect users to different pages when they click on them. However the issue that I've noticed is that all of the images redirect users to the same page and I realized it was due to all the Images in the slideshow inheriting the method Response.Redirect("url")...then I tried to use an if else condition for example
if(ImageButton3.ImageUrl=="/Images/Homepage_Button.PNG")
{
Response.Redirect("Homepage.aspx");
}
However I have noticed that this method does not work. Do you guys have any suggestions, huge thanks in advance!!!
I would suggest you use a data-url attribute on your image buttons. When you post back you can get the url off the ((ImageButton)sender).Attributes("data-url") and redirect to that url.
Another suggestion, if you aren't doing anything else in the event handler, you could simply use links around images.
Did you try to debug this code? To see if it reaches the URL you specified?
You can do that by clicking on "Start with Debugging" in Visual Studio.
Another good bug ugly debugging method is to add an alert (MessageBox) inside the if sequence and see if it gets there. For example:
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", ("The URL is " + ImageButton3.ImageUrl));
if(ImageButton3.ImageUrl=="/Images/Homepage_Button.PNG")
{
Response.Redirect("Homepage.aspx");
}
Edit: After seeing your comment, I suggest accessing this image like that (notice the #):
if(ImageButton3.ImageUrl=="#Images\Homepage_Button.PNG")
{
Response.Redirect("Homepage.aspx");
}

C# Selenium WebDriver - Is it possible to check if a certain webpage is already opened in the browser?

I was wondering if it is possible to check with C# and selenium web driver if a certain webpage is opened in the default browser?
My idea is to link certain ticketing system's time tracker with toggl.
For instance - on click of the "Time Track" button in the ticketing system, the program to click the toggl start button programmatically, at the same time.
Yes, it's possible.
You can devise a solution that checks the default window's URL or title.
if (driver.Url == 'http://some_url') { /* you are there */ }
or
if (driver.Title == 'Some Title') { /* the window is open and currently there */ }
Now, if you are running a browser manually using your own browser, then expect Selenium to detect that, then i'm sorry, but that is not possible.
In addition to #sircapsalot answer:
This won't be enough since the goal is to
on click of the "Time Track" button in the ticketing system
First you should be sure that the page has been loaded and the IWebElement is clickable. Without going in some advanced usage (like JS validation of the page state), this should do the work just fine:
var wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(15));
var myElement = wait.Until(x => x.FindElement(By.Id("timeTrackBtnId")));
if(myElement.Displayed)
myElement.Click();
Then go for the
program to click the toggl start button programmatically, at the same time
I'm not sure how you'll sync and how page's JS events will handle this simultaneous actions, but you can try with System.Threading. If the page is created by you maybe this second part (click button, click togl) is better to be handled in the JS code.

Message box in asp.net website

using (DataServer server = new DataServer())
{
server.ExecuteNonQuery(CommandType.Text, updateuser, param);
}
MessageBox.Show("User succesfully updated");
I have a update button when The fields are updated I show a message box, but the problem is the message box is behind the browser. I have to minimize the browser to close the message box, at the same time I am able to navigate in the website without closing the message box, I want to blur out the remaing area in the website when the message box is displayed. How can I do this in my website.
In this case you would either want to throw up an alert with JavaScript, or use a dialog of some sort.
To throw up an alert, you can do this:
Page.ClientScript.RegisterStartupScript(GetType(), "UserDialogScript", "alert(\"User successfully updated\");", true);
As for using a dialog, I would suggest the ModalPopupExtender or the jQuery UI Dialog.
ModalPopupExtender:
http://www.asp.net/ajaxlibrary/AjaxControlToolkitSampleSite/modalpopup/modalpopup.aspx
jQuery UI Dialog:
http://jqueryui.com/demos/dialog/
Don't do it this way. Instead, emit client side JavaScript to pop up an alert dialog, or add a div that a user can click to make it go away. You can also use a more proper UX convention of adding a message div that simply states "record updated" or similar, in a very specific color, so the user knows what to look for.
The idea of popping up a message box sounds tempting, but stick this out on a server and hit it from a browser on a separate box and you will quickly find out why this is an extremely bad idea.

How to click ok button in alert dialog box using watin?

I am trying to automate one website, in my project after clicking on 'submit' button, one alert message with ok button will come. I want to click that ok button.
I tried with these two codes seperatly but these are not working
AlertDialogHandler AlertDialog = new AlertDialogHandler();
ie.AddDialogHandler(AlertDialog);
ie.Button(Find.ByValue("Submit")).ClickNoWait();
AlertDialog.WaitUntilExists();
AlertDialog.OKButton.Click();
ie.WaitForComplete();
ie.RemoveDialogHandler(AlertDialog);
var AlertDialogHandler = new AlertDialogHandler();
using (new UseDialogOnce(ie.DialogWatcher, AlertDialogHandler))
{
ie.Button(Find.ByValue("Submit")).ClickNoWait();
AlertDialogHandler.WaitUntilExists(50);
var message = AlertDialogHandler.Message;
AlertDialogHandler.OKButton.Click();
ie.WaitForComplete();
}
While using these two codes, I got the same exception 'dialog not available within 30 seconds'.
Any help will be deeply appreciated. Thank You :)
This has happened with me a couple of times when I had multiple browser windows open and I was trying it out.
The solution at that time was to close all instances of IE, close NUnit / VS and start over again, and it worked like a charm. However, I was using a ConfirmDialogHandler and not an AlertDialog Handler.
If that does not help you might want to try adding the following before you fire up your browser instance.
Settings.AutoStartDialogWatcher = true;
Settings.AutoCloseDialogs = true;
I'm not familiar with WatiN, but here's another SO post that might help:
WatiN seems to not find JavaScript alert
There is a way to perform this without watin:
See my post:
Need a way to perform auto confirm for firefox or any other browser popup's
this is slightly unrelated but AlertDialogHandler's don't work for Firefox but there is a workaround...
http://pastebin.com/ZapXr9Yf

Categories