Host.Local.OpenBrowser and Host.Local[processId]

Class library usage, coding and language questions.
JSH_QA
Posts: 56
Joined: Thu Apr 05, 2012 9:03 am

Host.Local.OpenBrowser and Host.Local[processId]

Post by JSH_QA » Tue Jul 01, 2014 12:07 pm

I am having a problem with the process id returned from Host.Local.OpenBrowser.
Running on a 64-bit Windows 7 PC, the following code seems to work reliably for the first time around the loop,
but not for subsequent iterations. On the iterations where it fails, the call to Host.Local[] to get the Form
that relates to the process id, results in
[...] threw exception. Ranorex.ElementNotFoundException: Ranorex.ElementNotFoundException: No element found for path '/form[@title='13492' or @processname='13492' or @processid='13492']'


for (int browserCount = 0; browserCount < numberOfBrowsers; ++browserCount)
{
int processId = Host.Local.OpenBrowser(url, browserInRanorexForm, "", (browserCount == 0), true);
Form formForProcessId = Host.Local[processId.ToString()];
WebDocument domWebDocument = formForProcessId.FindSingle("/dom").As<WebDocument>();

// other code here
}

Whilst most testing is done on Internet Explorer v11, this same issue is encountered with Chrome etc.


The intent of the code is to open multiple browser instances, and to get the DOM for each browser with 100% certainty that the DOMs relate to the newly started browsers and not any other
browsers that are already running on the machine (e.g. I may want to start 3 Internet Explorer instances, but without closing any Chrome or Firefox instances already running on the machine).

Can anybody advise on how to make this code work reliably please? Or alternatively, is there a better way of getting the DOM for each of the new browser instances, with no possibility of gettng the DOM for a different browser by accident.

Many thanks,

John H.

JSH_QA
Posts: 56
Joined: Thu Apr 05, 2012 9:03 am

Re: Host.Local.OpenBrowser and Host.Local[processId]

Post by JSH_QA » Tue Jul 01, 2014 12:46 pm

BTW - I know that I can do the following, but using the processId would be preferable (particularly as this needs to work for multiple browser types) if it can be made to work:

Form formForProcessId = Host.Local.FindSingle(
"/form[@processname='iexplore' and @class='IEFrame' and @instance='" + browserCount.ToString() + "']",
Duration.Parse("10s"));
WebDocument domWebDocument = formForProcessId.FindSingle("/dom").As<WebDocument>();

Thanks again,

John H.

krstcs
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: Host.Local.OpenBrowser and Host.Local[processId]

Post by krstcs » Tue Jul 01, 2014 1:28 pm

The problem is, with modern browser design and tabbed browsing, if the browser is already open Host.Local.OpenBrowser may not return the pid of the most recent window. Some browsers start a management process that controls the spawn of new windows and will return the spawning process's pid instead of the spawned process's pid.

Why are you needing to run multiple browser windows at the same time on the same system?
Shortcuts usually aren't...

JSH_QA
Posts: 56
Joined: Thu Apr 05, 2012 9:03 am

Re: Host.Local.OpenBrowser and Host.Local[processId]

Post by JSH_QA » Tue Jul 01, 2014 3:28 pm

I need multiple browsers under control of one test, as I am testing applications that make use of SignalR etc. When a change is made by a test in one browser instance, the test should see the other browser instances update very shortly thereafter.

If the pid returned from OpenBrowser for browser instances after the first one cannot be relied on, I assume the options open to me then are:

(1) To use different browser types, so making a change on IE and checking for an update on Chrome (and other combinations)
(2) To use Host.Local.Find (or FindSingle) with @instance to identify the browser instances
(3) To use operating system services to try to identify the new browser instance

All of those options either have drawbacks or are just more work, which is why I was hoping the pid from OpenBrowser would be usable. Are there any reliable solutions available in the Ranorex libraries that I could use rather than going for any of the above options? Basically, I want to start multiple browsers and get the DOM for each in a reliable way, running on a machine that may have other types of browser running that I do not want to interfere with.

BTW - could the OpenBrowser API documentation be updated please to warn about the pid not being a reliable thing to use please, in order to prevent other people losing time trying an approach that will not work?

Thanks,

John H.

krstcs
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: Host.Local.OpenBrowser and Host.Local[processId]

Post by krstcs » Tue Jul 01, 2014 3:47 pm

John,

I would suggest that the easiest method would be to use different browsers. You could setup your tests so that it runs for each browser combination to make sure they all work in each role (send/receive).

Also, there isn't much that Ranorex can do about this, it is the design of the browsers, so I don't think there is anything in the Ranorex lib that could help here, but I could be wrong.

You could keep a running list of all browser pids in the Program.cs class that you would update when you start the test, and again when you start new browsers. The issue is still that you would need to manually look at each process to see if it was what you wanted.


1. Again, I would ask why the need other browsers running? If they are not part of the test, they probably shouldn't be running. If this is just because it is on your system, maybe you could look into running a virtual machine for your test runs. You could have it running on your system and it wouldn't interfere with your use of your system when running tests.

2. You could restructure the tests to use different VMs for each role so that you could have the same browser open on each VM. I'm assuming they don't have to be on the same system. The tests could be coordinated through a database or a continuous integration solution (I use Jenkins). You would just need to modularize your tests so that they are independent of each other (this is recommended for re-usability and maintenance reasons anyway).
Shortcuts usually aren't...

JSH_QA
Posts: 56
Joined: Thu Apr 05, 2012 9:03 am

Re: Host.Local.OpenBrowser and Host.Local[processId]

Post by JSH_QA » Tue Jul 01, 2014 4:56 pm

Thanks again for your quick response.

One extra complexity has just been added to the mix (the joy of Agile projects!). I have just come out of a meeting in which it was decided that as well as the customer using a browser for some functionality, and a Windows client for other functionality, we now also need to support a browser embedded within the Windows client (this one will almost certainly be restricted to Internet Explorer). With that configuration, we will need to test with Internet Explorer embedded in the Windows client, with Internet Explorer running separately on the same PC. I haven't looked yet to see what happens in this situation in terms of running processes, but wonder if this would encounter the problem of the pid being returned by OpenBrowser (hopefully the embedded one will be treated as a library rather than an executable, in which case the pid would presumably not be an issue)?

We also need the browser part of our system to run on tablets, which realistically will mean multiple instances of the same browser being used on the same device/emulator (unless we make our tests more complicated and have an individual test running browsers on multiple devices). I'm probably not looking at that for a couple of months, but the requirement is there.

The option of keeping a running list of browser pids is one I am keeping as a very last resort, as diffing and reconciling lists, taking into account differences between browser behaviours is not an attractive option. Hopefully, that one won't be necessary.

In terms of VMs - we have a full test lab set up (TFS, MTM, LabCenter, SCVVM etc), with our TFS build processes deploying and running tests in virtualised environments. Whilst this helps in many ways, it does not provide a solution to the need to test with more than one instance of the same browser type running on the same machine. Whilst most multi-browser tests could be done with instances of different browser types, not every test can.

Regarding the other browser types being running at the same time - yes, this is largely a convenience during test development. We have more than a person-year's worth of tests to implement for just one project (and other projects lined up too). Being able to run the tests on our own PCs (unfortunately, we only have one each) before committing them to version controls will save a significant amount of time over the course of that development. Being able to do that without other browsers interfering (or being interfered with) will allow us to be more productive (our test framework is written with an option to switch between preferred input methods, including using invoking of actions rather than mouse clicks etc, so we can run many tests whilst getting on with other things).

Thanks again,

John H.