How do I set attributes on elements? Specifically, I need to uncheck a radio button if I find that it is checked. I have javascript that responds to a Delete key keypress that will then uncheck the radio button, but if I send a Delete key keyevent, I get
an error. I tried the following code to send a Delete key event, but it doesn't work. A better solution is to simply set the checked attribute to false, but I don't see how to do that.
Dim
ke AsNewHtmlKeyEvent("keypress") ke.KeyCode = 46
elemRb.DispatchEvent(ke)
Where elemRb is the first ChildElement of my radiobuttonlist control on the web page.
Thanks for replying so quickly. I should have elaborated more on my problem. My javascript can uncheck the radio button, but it doesn't even get invoked. When this line runs:
elemRb.DispatchEvent(ke)
I get this exception from LTAF in the Execute method of the Testcase class:
Exception was thrown by the client engine: "[TestExecutorException]: Unhandled JS exception while executing command handler function DispatchKeyEvent. (Details: {\"message\":\"Object doesn't support property or method 'initKeyEvent'\",\"description\":\"Object
doesn't support property or method 'initKeyEvent'\",\"number\":-2146827850,\"name\":\"TypeError\"})" when running command: DispatchEvent:keypress on the target: < id=ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder3_Gender_1, index: 0>.
Is there a better way to uncheck a radio button using LTAF's object model than using DispatchEvent to try to trigger a call to a javascript function?
jmarmet
Member
7 Points
8 Posts
Setting attributes on html elements
Jul 13, 2011 09:11 PM|LINK
How do I set attributes on elements? Specifically, I need to uncheck a radio button if I find that it is checked. I have javascript that responds to a Delete key keypress that will then uncheck the radio button, but if I send a Delete key keyevent, I get an error. I tried the following code to send a Delete key event, but it doesn't work. A better solution is to simply set the checked attribute to false, but I don't see how to do that.
Dim ke As New HtmlKeyEvent("keypress")
ke.KeyCode = 46
elemRb.DispatchEvent(ke)
Where elemRb is the first ChildElement of my radiobuttonlist control on the web page.
RichardY
Star
8376 Points
1573 Posts
Re: Setting attributes on html elements
Jul 13, 2011 09:43 PM|LINK
If you can use jquery, then inside the javascript function where you want to uncheck the checkbox:
$('#checkboxId').attr('checked',false);
jmarmet
Member
7 Points
8 Posts
Re: Setting attributes on html elements
Jul 14, 2011 01:24 PM|LINK
Thanks for replying so quickly. I should have elaborated more on my problem. My javascript can uncheck the radio button, but it doesn't even get invoked. When this line runs:
elemRb.DispatchEvent(ke)
I get this exception from LTAF in the Execute method of the Testcase class:
Exception was thrown by the client engine: "[TestExecutorException]: Unhandled JS exception while executing command handler function DispatchKeyEvent. (Details: {\"message\":\"Object doesn't support property or method 'initKeyEvent'\",\"description\":\"Object doesn't support property or method 'initKeyEvent'\",\"number\":-2146827850,\"name\":\"TypeError\"})" when running command: DispatchEvent:keypress on the target: < id=ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder3_Gender_1, index: 0>.
Is there a better way to uncheck a radio button using LTAF's object model than using DispatchEvent to try to trigger a call to a javascript function?
Anton.Piskun...
Member
16 Points
3 Posts
Re: Setting attributes on html elements
Jul 26, 2011 06:58 PM|LINK
Try something like this:
HtmlPage page = new HtmlPage("Login.aspx"); page.Elements.Find("ctl00_PaperContentPlaceHolder_RadioButton1").Click(); string expression = @" function foo() { document.getElementById('ctl00_PaperContentPlaceHolder_RadioButton1').checked = false; return true; } foo();"; page.WaitForScript(expression, 10); Assert.AreEqual("false", page.Elements.Find("ctl00_PaperContentPlaceHolder_RadioButton1").GetAttributes()["checked"]);javascript: LTAF
jmarmet
Member
7 Points
8 Posts
Re: Setting attributes on html elements
Aug 08, 2011 02:13 PM|LINK
I've been on vacation the last two weeks and just checked on this.
Wow, that's great, Anton! That worked beautifully! I'm going to mark this as answered.
What a refief to get this resolved...
Thank you so much, Anton!