Check if browser is already opened

I have some test cases which i want to execute individually or in a test suite. I don’t want to close and open my explorer in every testCase of my testSuite.
So i came up with this:

try {

String s = WebUI.getUrl()

WebUI.navigateToUrl(GlobalVariable.miUrl)

} catch(BrowserNotOpenedException){

WebUI.openBrowser(GlobalVariable.miUrl)

}

But this takes too long, almost 40 seconds on average, do someone know a faster way to check if a browser is already opened?

4 Likes

Ivan Méndez said:

I have some test cases which i want to execute individually or in a test suite. I don’t want to close and open my explorer in every testCase of my testSuite.
So i came up with this:

try {

String s = WebUI.getUrl()

WebUI.navigateToUrl(GlobalVariable.miUrl)

} catch(BrowserNotOpenedException){

WebUI.openBrowser(GlobalVariable.miUrl)

}

But this takes too long, almost 40 seconds on average, do someone know a faster way to check if a browser is already opened?

You can remove GetUrl() line as it’s not required. something like below :

try {

 WebUI.navigateToUrl(GlobalVariable.miUrl)

} catch(BrowserNotOpenedException){

 WebUI.openBrowser(GlobalVariable.miUrl)

}

again, Why it’s taking 40 second? it should not as i have same script and it takes only time same as time taken to load url. it must be executing additional command. Can you post your whole module?

Whenever getUrl() or navigateToUrl() is called and the browser is actually closed, a StepFailedException is thrown and you cant catch it because Katalon engine will catch it first and throw that exception - and that is exactly the kind of annoyance I rather not have.
This is what worked best for me:

try {
def x = DriverFactory.getCurrentWindowIndex()
if ( ( x != null ) && ( x >= 0 ) )
{
prinln( “Browser is open…” )
}
catch( Exception e )
{
println( “Browser is NOT open…” )
}