Hi Telerik team,
I want to hand all Confirm and Alert dialogs in multiple
browsers (IE/Firefox/Chrome) automatically.
The following code is my implementation.
manager.Settings.UnexpectedDialogAction = UnexpectedDialogAction.DoNotHandle;
public
static
void
AddDialogHandlers(Manager manager, EventHandler dialogDisplayedHandler)
{
manager.DialogMonitor.RemoveDialogs();
//// Add dialog handler for ConfirmDialog.
ConfirmDialog confirmDialog = ConfirmDialog.CreateConfirmDialog(manager.ActiveBrowser, DialogButton.OK);
confirmDialog.HandlerDelegate =
new
DialogHandlerDelegate(MyCustomConfirmHandler);
manager.DialogMonitor.AddDialog(confirmDialog);
//// Add dialog handler for AlertDialog.
AlertDialog alertDialog = AlertDialog.CreateAlertDialog(manager.ActiveBrowser, DialogButton.OK);
alertDialog.HandlerDelegate =
new
DialogHandlerDelegate(MyCustomAlertHandler);
manager.DialogMonitor.AddDialog(alertDialog);
// Add more dialogs here...
manager.DialogMonitor.Start();
}
public
static
void
MyCustomConfirmHandler(IDialog dialog)
{
dialog.HandlerDelegate =
null
;
dialog.Handle();
dialog =
null
;
Manager.Current.DialogMonitor.RemoveDialog(dialog);
ConfirmDialog confirmDialog = ConfirmDialog.CreateConfirmDialog(Manager.Current.ActiveBrowser, DialogButton.OK);
confirmDialog.HandlerDelegate =
new
DialogHandlerDelegate(MyCustomConfirmHandler);
Manager.Current.DialogMonitor.AddDialog(confirmDialog);
}
public
static
void
MyCustomAlertHandler(IDialog dialog)
{
dialog.HandlerDelegate =
null
;
dialog.Handle();
dialog =
null
;
Manager.Current.DialogMonitor.RemoveDialog(dialog);
ConfirmDialog confirmDialog = ConfirmDialog.CreateConfirmDialog(Manager.Current.ActiveBrowser, DialogButton.OK);
confirmDialog.HandlerDelegate =
new
DialogHandlerDelegate(MyCustomAlertHandler);
Manager.Current.DialogMonitor.AddDialog(confirmDialog);
}
The dialogs are handled well in IE and Chrome, but both of them can't be handled correctly in Firefox.
In Firefox, the Confirm and Alert dialog just can be handled for the first time they display, and not handled any more from second time.
From debugging, I found that the MyCustomConfirmHandler and MyCustomAlertHandler methods are not invoked when the browser is Firefox. (They are invoked if the browser is IE or Chrome.)
Seems the Customized Dialog handle method does not work in Firefox.
So, is there anything else needs to do for
Confirm and Alert dialogs in Firefox?
I'm looking forward for your feedback. Thanks.
Regards,
Jeffery
11 Answers, 1 is accepted
Two key things you are missing:
1) At run time Test Studio cannot actually tell the difference between an Alert dialog and a Confirm dialog. They look the same. In your code you can use just the ConfirmDialog for everything.
2) You don't need a handler delegate. I'm surprised it's working at all in IE. Instead you should be resetting the state of the one dialog handler as in this full working example:
using
ArtOfTest.WebAii.Core;
using
ArtOfTest.WebAii.TestTemplates;
using
ArtOfTest.WebAii.Win32.Dialogs;
using
Microsoft.VisualStudio.TestTools.UnitTesting;
namespace
MySampleTests
{
/// <summary>
/// Summary description for ScratchTestUnitTest
/// </summary>
[TestClass]
public
class
ScratchTestUnitTest : BaseTest
{
#region [Setup / TearDown]
private
TestContext testContextInstance =
null
;
/// <summary>
///Gets or sets the VS test context which provides
///information about and functionality for the
///current test run.
///</summary>
public
TestContext TestContext
{
get
{
return
testContextInstance;
}
set
{
testContextInstance = value;
}
}
//Use ClassInitialize to run code before running the first test in the class
[ClassInitialize()]
public
static
void
MyClassInitialize(TestContext testContext)
{
}
private
Pages _pages;
public
Pages Pages
{
get
{
if
(_pages ==
null
)
{
_pages =
new
Pages(Manager.Current);
}
return
_pages;
}
}
// Use TestInitialize to run code before running each test
[TestInitialize()]
public
void
MyTestInitialize()
{
#region WebAii Initialization
// Initializes WebAii manager to be used by the test case.
// If a WebAii configuration section exists, settings will be
// loaded from it. Otherwise, will create a default settings
// object with system defaults.
//
// Note: We are passing in a delegate to the VisualStudio
// testContext.WriteLine() method in addition to the Visual Studio
// TestLogs directory as our log location. This way any logging
// done from WebAii (i.e. Manager.Log.WriteLine()) is
// automatically logged to the VisualStudio test log and
// the WebAii log file is placed in the same location as VS logs.
//
// If you do not care about unifying the log, then you can simply
// initialize the test by calling Initialize() with no parameters;
// that will cause the log location to be picked up from the config
// file if it exists or will use the default system settings (C:\WebAiiLog\)
// You can also use Initialize(LogLocation) to set a specific log
// location for this test.
// Pass in 'true' to recycle the browser between test methods
Initialize(
false
,
this
.TestContext.TestLogsDir,
new
TestContextWriteLine(
this
.TestContext.WriteLine));
// If you need to override any other settings coming from the
// config section you can comment the 'Initialize' line above and instead
// use the following:
/*
// This will get a new Settings object. If a configuration
// section exists, then settings from that section will be
// loaded
Settings settings = GetSettings();
// Override the settings you want. For example:
settings.DefaultBrowser = BrowserType.FireFox;
// Now call Initialize again with your updated settings object
Initialize(settings, new TestContextWriteLine(this.TestContext.WriteLine));
*/
// Set the current test method. This is needed for WebAii to discover
// its custom TestAttributes set on methods and classes.
// This method should always exist in [TestInitialize()] method.
SetTestMethod(
this
, (
string
)TestContext.Properties[
"TestName"
]);
#endregion
//
// Place any additional initialization here
//
}
// Use TestCleanup to run code after each test has run
[TestCleanup()]
public
void
MyTestCleanup()
{
//
// Place any additional cleanup here
//
#region WebAii CleanUp
// Shuts down WebAii manager and closes all browsers currently running
// after each test. This call is ignored if recycleBrowser is set
this
.CleanUp();
#endregion
}
//Use ClassCleanup to run code after all tests in a class have run
[ClassCleanup()]
public
static
void
MyClassCleanup()
{
// This will shut down all browsers if
// recycleBrowser is turned on. Else
// will do nothing.
ShutDown();
}
#endregion
[TestMethod()]
public
void
ScratchTest()
{
// Launch an instance of the browser
Manager.Settings.UnexpectedDialogAction = UnexpectedDialogAction.DoNotHandle;
Manager.LaunchNewBrowser(BrowserType.FireFox);
// Setup dialog monitoring
ConfirmDialog confirmDialog = ConfirmDialog.CreateConfirmDialog(ActiveBrowser, DialogButton.OK);
Manager.DialogMonitor.AddDialog(confirmDialog);
Manager.DialogMonitor.Start();
// Navigate to : 'http://www.w3schools.com/js/tryit.asp?filename=tryjs_alert'
ActiveBrowser.NavigateTo(
"http://www.w3schools.com/js/tryit.asp?filename=tryjs_alert"
);
// Click 'Button'
Pages.TryitEditorV14.FrameView.Button.Click(
false
);
// Wait until dialog handled
confirmDialog.WaitUntilHandled(10000);
confirmDialog.CurrentState = DialogCurrentState.NotActive;
// Click 'Button'
Pages.TryitEditorV14.FrameView.Button.Click(
false
);
// Wait until dialog handled
confirmDialog.WaitUntilHandled(10000);
confirmDialog.CurrentState = DialogCurrentState.NotActive;
// Comment: Will popup an Alert dialog
this
.Log.WriteLine(
"Will popup an Alert dialog"
);
// Navigate to : 'http://www.w3schools.com/js/tryit.asp?filename=tryjs_confirm'
ActiveBrowser.NavigateTo(
"http://www.w3schools.com/js/tryit.asp?filename=tryjs_confirm"
);
// Click 'Button'
Pages.TryitEditorV150.FrameView.Button.Click(
false
);
// Wait until dialog handled
confirmDialog.WaitUntilHandled(10000);
confirmDialog.CurrentState = DialogCurrentState.NotActive;
// Comment: Will popup a Confirm dialog
this
.Log.WriteLine(
"Will popup a Confirm dialog"
);
// Comment: Will popup an Alert dialog after handling the Confirm dialog
this
.Log.WriteLine(
"Will popup an Alert dialog after handling the Confirm dialog"
);
// Wait until dialog handled
confirmDialog.WaitUntilHandled(10000);
confirmDialog.CurrentState = DialogCurrentState.NotActive;
// Wait for '3000' msec.
System.Threading.Thread.Sleep(3000);
}
}
}
Cody
the Telerik team
Test Studio Trainings
Thanks for your feedback.
I agree with your first point. The Confirm and Alert dialogs are same in the run time.
For your second point, it doesn't resolve my issue.
I'm making a test tool based on the Telerik testing framework.
I want use this tool to monitor and handle all the confirm and alert dialogs automatically, not handled one by one.
So, I use the dialog handler delegate.
In my implementation, the Confirm dialog is added to DialogMornitor.
If the confirm dialog is displayed, the dialog handler delegate will be invoked to handle the dialog.
This implementation will handle all the confirm dialogs.
The point in my issue is:The the dialog handler delegate method (MyCustomConfirmHandler
) is not invoked for firefox browser, but working for IE/Chrome.
I don't why, do you have any ideas about it?
Please let me know if you want more information. Thanks.
Regards,
Jeffery
manager.Settings.ClientReadyTimeout = 60000;
// Time to wait for client to be ready the first time it is launched
manager.Elements.ReturnElementProxyWhenNotFound =
true
;
manager.Elements.WaitOnElements =
true
;
manager.Elements.WaitOnElementsTimeout = 10000;
// Maximum wait time
manager.Settings.LogLocation = runnerSettings.LogFilePath;
manager.SetNewBrowserTracking(
true
);
manager.Start();
manager.LaunchNewBrowser(runnerSettings.StartWindowMaximized ? System.Diagnostics.ProcessWindowStyle.Maximized : System.Diagnostics.ProcessWindowStyle.Normal);
if
(runnerSettings.StartWindowMaximized)
{
manager.ActiveBrowser.ToggleFullScreen();
// Breathing room for browser to resize
System.Threading.Thread.Sleep(1000);
}
manager.Settings.UnexpectedDialogAction = UnexpectedDialogAction.DoNotHandle;
I have discovered we do have a bug regarding the HandlerDelegate when running your test with Firefox 4 and above. The HandlerDelegate will not get called under any condition. I have filed a bug report on this here. Hopefully we'll be able to fix this in the near future.
As a result the only option available today is to reset the dialog state as shown in my previous post:
confirmDialog.WaitUntilHandled(10000);
confirmDialog.CurrentState = DialogCurrentState.NotActive;
Cody
the Telerik team
Test Studio Trainings
this is a great news for us....
Ganga S
I will keep this bug in tracking.
It's appreciated to let me know if there is any update.
BTW, there is another question here.
I want to capture the javascript dialog in browser, do you know how to implement it?
For example:
If you want to find a button in a web page, you can use Browser.Find() method to find the button by button id/Attribute/...
But the Javascript dialog cannot be found in this way.
Is there any way to implement this function?
// Navigate to : 'http://www.w3schools.com/js/tryit.asp?filename=tryjs_confirm'
ActiveBrowser.NavigateTo(
"http://www.w3schools.com/js/tryit.asp?filename=tryjs_confirm"
);
// Click 'Button'
Pages.TryitEditorV150.FrameView.Button.Click(
false
);
// How to capture the popup javascript dialog here?
We have a code sample article that demonstrates how to Verify Dialog Text, but be aware that it will only work in Internet Explorer.
Anthony
the Telerik team
Test Studio Trainings
I have went through the sample code,
and found that still cannot resolved our issue.
The code is wondering to get the text from a
javascript alert dialog.
TelerikCustomHandler.HandleClass.HandleDialog(dialog.Window.Handle)
The dialog is already got in the sample code.
Currently, our blocking issue is how to capture the JS dialog after it’s popping up?
In the test framework, you can add the dialog to DialogMonitor and the dialog will be handled automatic.
But in our case, we want to handle the JS dialog manual instead of automatic.
That's why we need to capture the dialog firstly.
Please let me know if you want more information. Thanks~
That article demonstrates the Custom Dialog Handler approach, which is what you should use if you want to perform extra tasks before handling the dialog. In that example the text is extracted and the dialog is dismissed by pressing the Enter key. You can easily change that portion of the code to suit your needs.
If that's not what you intended, what do you mean by "capture" the dialog? It cannot be found and acted upon like a standard DOM element through the Framework.
Anthony
the Telerik team
Test Studio Trainings
Hi Anthony,
Thanks for your time and feedback.
As showing in the article “Custom Dialog Handler”, I do want to use HandlerDelegate to handle dialog. But there is a bug as Cody said above.
“regarding the HandlerDelegate when running your test with Firefox 4 and above. The HandlerDelegate will not get called under any condition. “
I cannot use HandlerDelegate in firefox unless the bug is fixed.
It’s OK. I will use a work round solution to solve my issue now.
Thanks all the same.
Regards,
Jeffery