6

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();
    }
}
3
  • 1
    You mean focus on child window OR popup? Commented Jan 8, 2016 at 11:18
  • Can you share more of your code around this?
    – Swagin9
    Commented Jan 8, 2016 at 18:53
  • I mean focus on child window.
    – klmlfl
    Commented Jan 8, 2016 at 20:15

7 Answers 7

7

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.

2
  • 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
    Commented Jun 9, 2016 at 18:27
  • Of course this is possible! See the answer at the end (sqa.stackexchange.com/a/49062/52561) Commented Mar 3, 2022 at 10:32
4

To bring the window to front, in C# I use this :

protected void BringWindowToFront()
{
    var window = Driver.Manage ().Window;
    var position = window.Position;
    window.Minimize();
    window.Position = position;
}
2
  • This is a perfekt workaround Commented Mar 3, 2022 at 10:33
  • Great workaround! And this should be the accepted answer.
    – tbaskan
    Commented Aug 25, 2022 at 10:44
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.

0
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)
1
  • 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) Commented Jul 11, 2017 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.

0

Sorry to revive an old question, but I came here when to trying focus the browser window (using direct calls to chrome remote debugger).

In case it helps anyone, I was able to figure it out with this command to chrome "{'method':'Target.activateTarget', 'params':{'targetId':tabId} ,'id':1}"

where tabId is the id of the tab as listed in the localhost:9222/json

I've never used selenium, so I don't know if the option to send a command like that is available or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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