How do get the handle of the javascript alert window?
I tried with the following code in LTAF but I'm not successful here. I'm not sure how to access the alert window and see whether it is showing the message that I want to show.
Hello Murali, handling of alerts currently is only supported in response to a Click action. You can see an example on the sample website that comes with the code plex download. I copy the test below that shows the API.
[WebTestMethod]
public void HandleConfirmPopUp()
{
HtmlPage page = new HtmlPage("ScriptSamples.aspx");
// click on the button and cancel the confirm
page.Elements.Find("ButtonWithConfirm").Click(new CommandParameters(WaitFor.None,PopupAction.ConfirmCancel));
Assert.AreEqual("", page.Elements.Find("Label1").GetInnerText());
// click on the button and accept the confirm
page.Elements.Find("ButtonWithConfirm").Click(new CommandParameters(WaitFor.Postback, PopupAction.ConfirmOK));
Assert.AreEqual("The Button was clicked.", page.Elements.Find("Label1").GetInnerText());
}
Thanks Federico. I had a look at the sample site. It looks like the Assert is based on what the server returns.
In my case, the validation happens at the client. For e.g. I need to check whether mandatory fields have been provided and I provide an alert message (which differs based on what fields have been populated). Is it not possible for me to grab that alert and
Assert on the message?
I see what you are saying, this is a little know feature, but when you issue a Click command using the CommandParameters overload, the text of the alert will be captured and available through the PopupText property. Here is a sample:
tnmurali
0 Points
2 Posts
Cannot hook to javascript alert message
Mar 18, 2010 09:53 AM|LINK
How do get the handle of the javascript alert window?
I tried with the following code in LTAF but I'm not successful here. I'm not sure how to access the alert window and see whether it is showing the message that I want to show.
HtmlPage popup = currentPage.GetPopupPage(1,20);
Thanks
Murali
Vijaya.A
Contributor
2408 Points
629 Posts
Re: Cannot hook to javascript alert message
Mar 18, 2010 10:30 AM|LINK
Hi check this
http://www.willmaster.com/library/features/alert-box-alternative.php
http://www.anyexample.com/webdev/javascript/ie7_javascript_prompt()_alternative.xml
http://www.webdeveloper.com/forum/showthread.php?t=174223
h.zahed@live...
Contributor
3171 Points
514 Posts
Re: Cannot hook to javascript alert message
Mar 18, 2010 10:58 AM|LINK
Hi,
Here is an example:
JsAlert.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="JsAlert.aspx.cs" Inherits="JsAlert" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> </div> </form> </body> </html>JsAlert.aspx.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class JsAlert : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { ShowMessage(Page, "Hi"); } public static void ShowMessage(Page aPage, string aMessage) { aPage.RegisterStartupScript("AlertMsg", "<script language='javascript'> alert('" + aMessage + "'); </script>"); } }wish it helps
MCP-MCTS-MCPD
Mark the post as Answer, if it helped you in solving your question.
farmas
Participant
1164 Points
259 Posts
Microsoft
Re: Cannot hook to javascript alert message
Mar 21, 2010 01:33 AM|LINK
Hello Murali, handling of alerts currently is only supported in response to a Click action. You can see an example on the sample website that comes with the code plex download. I copy the test below that shows the API.
[WebTestMethod]
public void HandleConfirmPopUp()
{
HtmlPage page = new HtmlPage("ScriptSamples.aspx");
// click on the button and cancel the confirm
page.Elements.Find("ButtonWithConfirm").Click(new CommandParameters(WaitFor.None,PopupAction.ConfirmCancel));
Assert.AreEqual("", page.Elements.Find("Label1").GetInnerText());
// click on the button and accept the confirm
page.Elements.Find("ButtonWithConfirm").Click(new CommandParameters(WaitFor.Postback, PopupAction.ConfirmOK));
Assert.AreEqual("The Button was clicked.", page.Elements.Find("Label1").GetInnerText());
}
- Federico
tnmurali
0 Points
2 Posts
Re: Cannot hook to javascript alert message
Mar 22, 2010 05:40 AM|LINK
Thanks Federico. I had a look at the sample site. It looks like the Assert is based on what the server returns.
In my case, the validation happens at the client. For e.g. I need to check whether mandatory fields have been provided and I provide an alert message (which differs based on what fields have been populated). Is it not possible for me to grab that alert and Assert on the message?
Thanks
Murali
farmas
Participant
1164 Points
259 Posts
Microsoft
Re: Cannot hook to javascript alert message
Apr 04, 2010 03:00 PM|LINK
I see what you are saying, this is a little know feature, but when you issue a Click command using the CommandParameters overload, the text of the alert will be captured and available through the PopupText property. Here is a sample:
<div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> // click on the button, cancel the confirm and verify alert text</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> CommandParameters commandParams = new CommandParameters(WaitFor.None, PopupAction.ConfirmCancel);</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> page.Elements.Find("ButtonWithConfirm").Click(commandParams);</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> Assert.AreEqual("string from the alert", commandParams.PopupText);</div>// click on the button, cancel the confirm and verify alert text
CommandParameters commandParams = new CommandParameters(WaitFor.None, PopupAction.ConfirmCancel);
page.Elements.Find("ButtonWithConfirm").Click(commandParams);
Assert.AreEqual("string from the alert", commandParams.PopupText);
Hope this helps,
Federico