Search code examples
javamacosselenium-webdriversafari

Selenium Java in Safari - Switch to new tab is not working


I have the next situation:

I'm running Automation in MacOS with Selenium in Safari. I want to take control over a new tab opened (Switch to a new tab in Safari)

I'm able to do it with Chrome and Firefox, but in Safari does not work.

The next code is what I'm using for that:

    //Store the parent window
    String parentWindow = driver.getWindowHandle();

    //Open a new Windows(Mailtrap)
    String a = "window.open('https://mailtrap.io/signin','_blank');";
    ((JavascriptExecutor)driver).executeScript(a);


    //Take control over new browser
    for(String handle: driver.getWindowHandles()){
        driver.switchTo().window(handle);
    }

    //Make the same actions over new window
    driver.manage().window().maximize();

    driver.switchTo().defaultContent();

After that...

//Close window and back to the parent window

    driver.close();
    driver.switchTo().window(parentWindow);
    driver.switchTo().defaultContent();

As I told before: It works in Firefox, Chrome, IE11, but in Safari 12.0.3 this is the message when I do a manual click over a tab.

"This Safari window is remotely controlled by an automated test."

To interact with this Safari window, you need to stop the current automated test session. You should not use this window for normal web browsing.

"Turn OFF All Automation" , "Stop Session", "Continue Session"

Could anybody help me with this?

PS: Develop --> Allow remote automation is selected.


Solution

  • As the automation speed is very fast on Safari, the only solution for this was the "old friend forever":

    Thread.sleep
    

    That was the only solution, because the Implicit and Explicit waits were not working.