getWindowTitle always return Title of first Tab

Hi everyone I have the following problem.

At the beginning of my test I open the browser and navigate to a certain URL (Microsoft in the example)

WebUI.openBrowser('')
WebUI.navigateToUrl('http://www.microsoft.com')

Then I open a second browser tab
js.executeScript ('window.open ();', [])

I move to the newly opened tab
WebUI.switchToWindowIndex (1)

I open a certain url in the newly opened tab (google in the example)
WebUI.navigateToUrl ('http://www.google.com')

and so far everything works perfectly.

When I try to retrieve the title of the second tab I opened
String titleSecondTab = WebUI.getWindowTitle ()

the WebUI.getWindowTitle () statement always returns me the title of the first Tab (Microsoft) and not of the second tab (Google)

What am I doing wrong? I just don’t understand …

Thank you

I found a solution.

I just had to insert
Thread.sleep (4000)
before reading the title of the second tab

Thanks for reporting back and offering up a solution. However, that’s not a robust solution since it relies on “luck”.

Fixed delays are not considered reliable.

It only works when the speed of the network connection is faster than your fixed delay of 4 seconds – which, of course, it should be but it may not always be.

You should use other means to verify the second page is loaded and “ready”. At the very least, wait for the page to load and then try reading the page title: