Triggering onchange on SELECT elements

Class library usage, coding and language questions.
chrisjsmith
Posts: 5
Joined: Mon Mar 26, 2012 11:41 am

Triggering onchange on SELECT elements

Post by chrisjsmith » Mon Mar 26, 2012 11:48 am

Hi,

I'm stuck on triggering onchange JavaScript events programmatically. Basically no event is firing even though the value of the select is being set correctly. This is on Windows 7 x64, IE9 (32-bit) with Ranorex add-on installed.

I've used the following code to make the selection in a <select> element:
public static bool SelectComboItem(string value, SelectTag selectTag) {
    var tags = selectTag.FindDescendants<OptionTag>();
    foreach(OptionTag tag in tags) {
        if (string.Compare(value, tag.Value, StringComparison.InvariantCultureIgnoreCase) != 0)
            continue;
        tag.EnsureVisible();
        tag.Selected = true;
        tag.PerformClick();
        return true;
    }
    return false;
}
This is being tested against a simple HTML page which writes a message to a div when the select onchange is triggered. No event is being triggered at all but the value is set correctly and can be validated after it is set. This works perfectly fine if I do it manually but it is resisting any form of automation.

Any help appreciated.

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Triggering onchange on SELECT elements

Post by Support Team » Tue Mar 27, 2012 12:47 pm

Hi,

try to perform a .Click action on the specific tag instead of the .PerformClick action.
Doing so should trigger the onClick event.

Regards,
Tobias
Ranorex Team

chrisjsmith
Posts: 5
Joined: Mon Mar 26, 2012 11:41 am

Re: Triggering onchange on SELECT elements

Post by chrisjsmith » Tue Mar 27, 2012 2:54 pm

I've tried that. It still doesn't trigger the event.

Add to that, we can't use the .Click() method as we need to run this from an integration service (Team City) and according to our test lead, this doesn't work at all. .PerformClick() does but again doesn't trigger the onchange event.

Alternatively, do you have any information on how to configure Ranorex to run initiated from a service which allows desktop interaction, then we can use standard mouse clicks as recorded.

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Triggering onchange on SELECT elements

Post by Support Team » Wed Mar 28, 2012 12:26 pm

Hi,

You can try to manually execute the specific Javascript method with the following code:
yourWebDocument.ExecuteScript("YourMethod('yourValue');");
Alternatively, do you have any information on how to configure Ranorex to run initiated from a service which allows desktop interaction, then we can use standard mouse clicks as recorded.
We haven't tested it yet so there is not a specific Ranorex configuration, but if you want to create a web service you have to create a "Windows service" project with VS, as an exe which should run as a web service have to handle start, stop, and pause messages from the Service Control Manager.

Regards,
Markus
Ranorex Support Team

chrisjsmith
Posts: 5
Joined: Mon Mar 26, 2012 11:41 am

Re: Triggering onchange on SELECT elements

Post by chrisjsmith » Wed Mar 28, 2012 1:42 pm

I'd ideally like to avoid calling JavaScript directly that as it couples our testing environment heavily to our framework then. Are there any other solutions you are aware of?

If not, we will accept this and workaround the issue.

Thanks for your assistance.

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Triggering onchange on SELECT elements

Post by Support Team » Thu Mar 29, 2012 1:48 pm

Hi,
Are there any other solutions you are aware of?
Unfortunately not, if the used method doesn't trigger the JavaScript method you have to directly call it.

Regards,
Markus
Ranorex Support Team

chrisjsmith
Posts: 5
Joined: Mon Mar 26, 2012 11:41 am

Re: Triggering onchange on SELECT elements

Post by chrisjsmith » Fri Mar 30, 2012 11:58 am

Ok I will work around this issue.

Thanks for your help.

omayer
Posts: 458
Joined: Thu Oct 28, 2010 6:14 pm

Re: Triggering onchange on SELECT elements

Post by omayer » Fri Mar 30, 2012 8:43 pm

Markus , would you plz elaborate/describe more details on this line - yourWebDocument.ExecuteScript("YourMethod('yourValue');");
Thanks,
Beginner
Tipu

chrisjsmith
Posts: 5
Joined: Mon Mar 26, 2012 11:41 am

Re: Triggering onchange on SELECT elements

Post by chrisjsmith » Mon Apr 02, 2012 9:05 am

In our case, we've done the following. We added this script to the page (which has jQuery loaded):

function callEvent(id, eventName) {
$('#' + id).trigger(eventName);
}

Then called it after the selection was made via the PerformClick method with the code I provided above as:

document.ExecuteScript(string.Format("callEvent('{0}', 'change');", selectTag));

Hope this helps.