Page 1 of 1

How to execute javascript on a webpage

Posted: Thu Aug 04, 2016 2:07 pm
by jojomonsta
Hi

I have scoured the forums and the web to see if I can find a solution to this but to no avail, so I am coming to you guys and gals for help!

We have dynamic fields in our web application form which require keystroke actions to trigger the javascript on the input field to do a postback. I want to avoid using keystroke actions if I can so want to know if I can trigger the script using code.

The code for the postback contained in the page is:

Code: Select all

javascript:setTimeout('__doPostBack(\'ctl00$MainContent$ucResidential1$ucAddress$txttimeatadressmonths\',\'\')', 0)
I have tried a few different things but seem to be unable to trigger this postback without using a keystroke action.

Our developer tried this:

Code: Select all

       public void TriggerPageLoad()
        {
        	repo.OriginationSystem.Self.ExecuteScript("setTimeout(__doPostBack('ctl00$MainContent$ucResidential1$ucAddress$txttimeatadressmonths',''), 0)");
        }
And I tried this:

Code: Select all

        public void TriggerPageLoad()
        {
        	WebDocument webDocument = @"/dom[@domain=$varUATDomain]";
        	webDocument.ExecuteScript("setTimeout(__doPostBack('ctl00$MainContent$ucResidential1$ucAddress$txttimeatadressmonths',''), 0)");
        }
I am not that code-literate (learning as I go along) so apologies if my terminology isn't great!

Running Windows 8 and using Ranorex v6.0.0

Thanks in advance!
J

Re: How to execute javascript on a webpage

Posted: Fri Aug 05, 2016 2:12 pm
by jma
Hi,

Basically, the above-mentioned command looks good. I tried executing a similar java script function at the following web page: http://www.w3schools.com/jsref/tryit.as ... settimeout

In this case, I was able to execute the script as follows.

Code: Select all

repo.Dom.IframeResult.ExecuteScript("setTimeout(function(){ alert('Hello'); }, 3000)");
Unfortunately, I don't have a sample at hand where I could try executing the __doPostBack method. Could you create a small sample web page which allows us to reproduce the issue?

Re: How to execute javascript on a webpage

Posted: Mon Aug 08, 2016 5:05 pm
by jojomonsta
Hijma
Thanks for your reply. I am not a developer so unable to create the example you require, sorry.
This one is an awkward one for me because I want to run this script with the PC locked and, using a keystroke action does not work when this is the case.
I have run the above command alongside a KeySequence action and this works but I was hoping to run it alongside a SetValue>TagValue action, but this doesn't have the same effect.

Re: How to execute javascript on a webpage

Posted: Mon Aug 08, 2016 6:17 pm
by odklizec
I'm sorry to be a bearer of bad news, but Ranorex (any test automation tool) can't work with locked desktop. So if working with locked desktop is your main intention behind this post, you can drop it ;)

Re: How to execute javascript on a webpage

Posted: Wed Aug 10, 2016 3:50 pm
by jojomonsta
8)
Thanks for this (I have had it running locked in the past before they made changes to the UI functionality! :roll: )
I understand these limitations but this is now annoying me.
You can execute javascript via code in Ranorex, right? Or am I getting the wrong idea?
Even if I cannot use it when the system is locked then I would still like to know if and how it can be done. More for my own learning than anything else.

I prefer to use a Tag Value or something similar as it's faster (and cleaner) than a Key Sequence, in my opinion anyway, but this does not trigger the post back on this field.
To explain, this is an application form which asks for time at address. If the time at address < 3 years, further address information is required and these fields are generated. It's when this Months Residing value has been input and, in 'real life' the user moves to another field, that the script is run and a post back takes place. This then checks whether the additional fields are required.
This is what I am trying to trigger in the background rather than using Key Sequence {Tab} to leave the field and trigger the post back.

Re: How to execute javascript on a webpage

Posted: Wed Aug 10, 2016 4:32 pm
by krstcs
In my opinion, you should use SetValue and code injection ONLY when everything else doesn't work, exactly because it doesn't fire events and it makes more work in the long run.

In most situations with Functional UI Test Automation (Ranorex, QTP, TestComplete, etc.) we are supposed to be automating the actions a USER would take against the UI. Using code injection and SetValue actions do not do this. They go behind the scenes and do things that our normal users would not be doing. So the test is now not actually testing what it SHOULD be testing, namely USER interaction. This makes the test invalid and ultimately useless. I know my QA director would definitely not like me wasting resources and still not testing the requirements.

So, my suggestion is to test with the Click and Key Sequence/Shortcut actions unless absolutely otherwise necessary.

Is it slower? Maybe, but it's more reliable and consistent (which is what test automation is really about, speed is just a useful by-product).

As for setting TagValue being cleaner, I don't think it is, especially since it doesn't fire events. How is it cleaner to have to now also write code to actually get the events to fire instead of just using ONE Key Sequence action that will handle it all for you? I don't see that as being anything but more code to maintain for the same final outcome.

But, again, my opinion. Take it for what it cost you... :D

Re: How to execute javascript on a webpage

Posted: Thu Aug 11, 2016 10:47 am
by jojomonsta
Fair comment and point taken :oops: