4

When executing a Selenium test with ChromeDriver, I would like to bring the test browser into focus to see the actions taking place. The way I am able to do that now is by manually clicking the instance of Chrome -- otherwise the tests do run, I just don't see them taking place. I'd love for the focus part to happen automatically.

My testing environment looks like this:

  • Operating System: OS X(El Capitan)
  • IDE: IntelliJ 15 - community edition
  • Selenium version 2.48
  • Java JDK: 1.8
  • JUnit: 4.11 ChromeDriver: 2.2

FYI, I have tried this with no luck (a common answer on StackOverflow):

  String currentWindow = driver.getWindowHandle();
  driver.switchTo().window(currentWindow);

Current code:

public class TestLogin {

    private WebDriver driver;

    @Before
    public void setUp() {
        System.setProperty("webdriver.chrome.driver", "/Users/*******/Downloads/chromedriver");
        driver = new ChromeDriver();
    }

    @Test
    public void succeeded() {
        driver.get("http://the-internet.herokuapp.com/login");
        String currentWindow = driver.getWindowHandle();
        driver.switchTo().window(currentWindow);
        driver.findElement(By.id("username")).sendKeys("tomsmith");
        driver.findElement(By.id("password")).sendKeys("SuperSecretPassword!");
        driver.findElement(By.cssSelector("button")).click();
    }

    @After
    public void tearDown() {
        driver.quit();
    }
}
  • 1
    You mean focus on child window OR popup? – Helping Hands Jan 8 '16 at 11:18
  • Can you share more of your code around this? – Swagin9 Jan 8 '16 at 18:53
  • I mean focus on child window. – klmlfl Jan 8 '16 at 20:15
5

If you're talking about bringing the browser's window within the OS to the foreground. Selenium cannot do that. You need to introduce a library that is able to automate actions on an OS level. Selenium is restricted by javascript's same origin policy. Looks like you're using Java, so you could take a look at awt.Robot. But be aware that any solution you come up with using Robot will likely be finicky...

The driver.switchTo().window(handle) code is not meant to switch the active window on an OS level. It just changes which browser window selenium sends it's actions to. Selenium is able to communicate with browser windows that are hidden behind other windows on your screen.

  • This is the right answer -- thank you. For other users out there looking for a solution, you can find it below (see Sooraj's answer). – klmlfl Jun 9 '16 at 18:27
1

I used sikuli to detect whether the browser is out of focus since it can see outside the browser window (usually on mac you can see the name of the application on top bar) and then used robot class to do a command+tab. I think you can also use java script executor to pop up an alert so that browser comes to focus before each test suite.

1

We have used Capybara with Chrome driver and we had the same issue. When we launch the url, Chrome window was sent to background. To overcome this issue, we have used the following code and it works like magic!!

@window = @session.current_window
@session.visit url
@session.switch_to_window(@window)
  • Note that @session here is frequently called page, it's usually set to whatever is returned from Capybara.current_session. You can also combine lines 1 and 3 with @session.switch_to_window(@session.current_window) – Joshua Cheek Jul 11 '17 at 19:40
0

Found the solution worked for our project. Before driver.get(url) we execute the following command:

osascript -e tell application "${browserName}"
   activate
   delay 1
   end tell

browserName could be safari/chrome/firefox.

0

I have a similar problem - when the window is minimized Selenium cannnot focus and sendKeys does not not work. My solution is to use JavascriptExecutor (my code is all c#):

Instead of:

driver.findElement(By.id("password")).sendKeys("SuperSecretPassword!");
driver.findElement(By.cssSelector("button")).click();
// this does not work when the window is minimized

I use:

((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].value = 'SuperSecretPassword!'", driver.findElement(By.id("password")));
((IJavaScriptExecutor)driver).ExecuteScript("document.getElementById('button').fireEvent('onclick')", driver.FindElement(By.Id("button")));

I know this is not a nice way but it is working for me. I hope this is helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.