Close IE before the start

Ask general questions here.
User avatar
giuseppe.lacagnina
Posts: 113
Joined: Fri Sep 18, 2015 10:25 am
Location: Brunn am Gebirge, Vienna, Austria

Close IE before the start

Post by giuseppe.lacagnina » Tue Oct 04, 2016 1:16 pm

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!!!

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7470
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Close IE before the start

Post by odklizec » Tue Oct 04, 2016 1:31 pm

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;
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

User avatar
giuseppe.lacagnina
Posts: 113
Joined: Fri Sep 18, 2015 10:25 am
Location: Brunn am Gebirge, Vienna, Austria

Re: Close IE before the start

Post by giuseppe.lacagnina » Tue Oct 04, 2016 1:35 pm

Thanks! I will try it!!!

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

Re: Close IE before the start

Post by krstcs » Tue Oct 04, 2016 1:43 pm

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']
Shortcuts usually aren't...

User avatar
giuseppe.lacagnina
Posts: 113
Joined: Fri Sep 18, 2015 10:25 am
Location: Brunn am Gebirge, Vienna, Austria

Re: Close IE before the start

Post by giuseppe.lacagnina » Tue Oct 04, 2016 2:06 pm

It works! Thanks everyone :D

Vaughan.Douglas
Posts: 254
Joined: Tue Mar 24, 2015 5:05 pm
Location: Des Moines, Iowa, USA

Re: Close IE before the start

Post by Vaughan.Douglas » Tue Oct 04, 2016 8:47 pm

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.
Doug Vaughan

User avatar
giuseppe.lacagnina
Posts: 113
Joined: Fri Sep 18, 2015 10:25 am
Location: Brunn am Gebirge, Vienna, Austria

Re: Close IE before the start

Post by giuseppe.lacagnina » Wed Oct 05, 2016 7:16 am

Hi!

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

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7470
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Close IE before the start

Post by odklizec » Wed Oct 05, 2016 7:47 am

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 ;)
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

User avatar
giuseppe.lacagnina
Posts: 113
Joined: Fri Sep 18, 2015 10:25 am
Location: Brunn am Gebirge, Vienna, Austria

Re: Close IE before the start

Post by giuseppe.lacagnina » Wed Oct 05, 2016 7:48 am

Thanks :D

Vaughan.Douglas
Posts: 254
Joined: Tue Mar 24, 2015 5:05 pm
Location: Des Moines, Iowa, USA

Re: Close IE before the start

Post by Vaughan.Douglas » Wed Oct 05, 2016 12:51 pm

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.
Doug Vaughan

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

Re: Close IE before the start

Post by krstcs » Wed Oct 05, 2016 2:50 pm

Yeah, I completely forgot about OpenBrowser having the close existing option, and I use it myself...

Nice catch Vaughan!
Shortcuts usually aren't...