Page 1 of 1

Close IE before the start

Posted: Tue Oct 04, 2016 1:16 pm
by giuseppe.lacagnina
Hi.

I have a problem. I am running HTML test cases on IE 11. I am running Ranorex Studio 5.4.4 (cannot update for the time being) and Windows 7.

At the beginning of my test cases, I would like to:

- check if there is already an instance of IE open on the website I need to test and close it
- start again IE on the website I need to test

How can I achieve this? I am not sure what elements of the page might be present at the beginning, so I would just look for the repo.client, but I do not know how to do this.

Thanks!!!

Re: Close IE before the start

Posted: Tue Oct 04, 2016 1:31 pm
by odklizec
Hi,

I'm using something like this in my tests (using code module placed in "Setup" section of each TC):

Code: Select all

try
{
	IList<Ranorex.WebDocument> webList = Host.Local.Find<Ranorex.WebDocument>("/dom[@domain='your.domain.name']");
	Report.Log(ReportLevel.Info, "Application", "Closing all IE instances.");
	foreach (Ranorex.WebDocument webdoc in webList)
	{ 
		webdoc.Close();
	}
}
catch(Exception exBrowserClose)
{
	Report.Warn("An error occured while an attempt to close browser: " + exBrowserClose.ToString());
	// try kill iexplore process           	
	try
	{				
		foreach (var procIE in Process.GetProcessesByName("iexplore"))
		{
			procIE.Kill();
			Report.Warn("Process Kill", "Process " + procIE.ToString() + " found and killed.");
		}
	}
	catch(Exception exProcKill)
	{
		Report.Warn("Process Kill failed", "Process iexplore found but Process Kill failed... " + exProcKill.Message);
	}    
}
And you need to add this at start of code module:

Code: Select all

using System.Diagnostics;

Re: Close IE before the start

Posted: Tue Oct 04, 2016 1:35 pm
by giuseppe.lacagnina
Thanks! I will try it!!!

Re: Close IE before the start

Posted: Tue Oct 04, 2016 1:43 pm
by krstcs
The easiest way is to use your existing DOM object and check if it exists and then close it.

Code: Select all

if (repo.myDom.SelfInfo.Exists(10000)) repo.myDom.Self.Close(); 
You don't need the '10000' if you want to use the repo item's default timeout.


I would add " and state='complete'" to your XPath as well as this will help with timing issues.

Code: Select all

/dom[@domain='www.mydomain.com' and state='complete']

Re: Close IE before the start

Posted: Tue Oct 04, 2016 2:06 pm
by giuseppe.lacagnina
It works! Thanks everyone :D

Re: Close IE before the start

Posted: Tue Oct 04, 2016 8:47 pm
by Vaughan.Douglas
Sorry I'm late to the party, but is there any reason you can't just use the overloads in OpenBrowser?

Code: Select all

Host.Local.OpenBrowser(URL, browser,"", True, True, True, False, False)
The first true is the "KillExisting" parameter.

Re: Close IE before the start

Posted: Wed Oct 05, 2016 7:16 am
by giuseppe.lacagnina
Hi!

How does one specify IE as the browser? but it does look interesting, thanks!

Re: Close IE before the start

Posted: Wed Oct 05, 2016 7:47 am
by odklizec
Hi,

to giuseppe.lacagnina:
You should replace the "browser" string with browser name (IE, chrome, firefox, etc...)

to Vaughan.Douglas:
Thanks for the info! ;) I'm using OpenBrowser action from recording modules and there is no such option available, so it never occurred to me to even try it from code. I will give it a try and see if it works as expected so I can get rid of my slightly over-complicated code ;)

Re: Close IE before the start

Posted: Wed Oct 05, 2016 7:48 am
by giuseppe.lacagnina
Thanks :D

Re: Close IE before the start

Posted: Wed Oct 05, 2016 12:51 pm
by Vaughan.Douglas
giuseppe.lacagnina wrote:Hi!

How does one specify IE as the browser? but it does look interesting, thanks!
"browser" is the argument set in the module UI that would carry the specific browser type. I bind this to a global parameter so I can easily change the browser type.
odklizec wrote:Thanks for the info! I'm using OpenBrowser action from recording modules and there is no such option available, so it never occurred to me to even try it from code. I will give it a try and see if it works as expected so I can get rid of my slightly over-complicated code
Thanks!! I was pretty surprised that the OpenBrowser method contained overload arguments that are not available through the GUI. I strive to create all automation in recorded modules for ease of maintenance by less experienced automation folks, so it is pretty frustrating that I have to convert this method to user code each time someone wants to open a browser.

Also when I do feel the need to break into actual code, I tend to edit the solution in Visual Studio as it gives me more real time information on my code. Although SharpDevelop is coming along.

Re: Close IE before the start

Posted: Wed Oct 05, 2016 2:50 pm
by krstcs
Yeah, I completely forgot about OpenBrowser having the close existing option, and I use it myself...

Nice catch Vaughan!