How to bring active webdriver to front?

2,870 views
Skip to first unread message

Nguyen Thai Hoang

unread,
Sep 14, 2016, 3:39:50 AM9/14/16
to webdriver
Hi all,

I'm facing a problem while execute test case that requires 3 Chrome Driver or 3 Chrome Browser.



My expectation is while chromedriver1 running, its browser will be activated (bring to front), not running in background  (when testcase is failed, it will make a screenshot for easy to debug).
I tried with Javascript but still can fix it.

((JavascriptExecutor)this.driver1).executeScript("alert('Test')");
this.driver.switchTo().alert().accept();

SikuliX, AutoIT or any else are welcome.

Thanks all,

Andreas Tolfsen

unread,
Sep 14, 2016, 8:11:55 AM9/14/16
to webd...@googlegroups.com, Nguyen Thai Hoang
Nguyen Thai Hoang <thaih...@gmail.com> writes:

> I'm facing a problem while execute test case that requires 3 Chrome
> Driver or 3 Chrome Browser.

[…]

> My expectation is while chromedriver1 running, its browser will be
> activated (bring to front), not running in background (when testcase
> is failed, it will make a screenshot for easy to debug).
> I tried with Javascript but still can fix it.

There is no assumption that the WebDriver session should run the
browser in focus. In fact, the WebDriver specification specifically
calls out that focus does not matter.

There’s no WebDriver API to bring a window into focus, although some
drivers might implicitly do this i.e. when switching windows.

You shouldn’t _have_ to bring the window into focus for any WebDriver
API to function correctly.

darrell grainger

unread,
Sep 14, 2016, 9:18:49 AM9/14/16
to webdriver
If you are using the built in WebDriver feature for taking a screen capture you shouldn't need to bring the browser to the front. If I set up two drivers then execute:

        driver1.get("http://www.google.com");
        driver2.get("http://www.thoughtworks.com");
        File scrFile1 = ((TakesScreenshot)driver1).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(scrFile1, new File("google.png"));
        File scrFile2 = ((TakesScreenshot)driver2).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(scrFile2, new File("thoughtworks.png"));

The screen captures reflect the correct information.

Nguyen Thai Hoang

unread,
Sep 26, 2016, 12:14:27 AM9/26/16
to webdriver
Hi all,
Firstly, much appreciated for your help, I've found the solution for my case by using AutoIT (it works on Chrome Driver, for other webdriver, just change a little bit)

1. While create new Chrome Driver object 

Chrome1 = new ChromeDriver(caps_Chrome1);

It will open a browser like below


2. At this step, I use AutoIT to get Windows Handle for this window, and store it into a temp.xml file


AutoItX autoIT_Chrome1 = new AutoItX();
autoIT_Chrome1.winGetHandle("data:, - Google Chrome")



3. For other Chrome Driver (chrome2, chrome3), repeat step 1, 2


The result of temp.xml as below




4. If i want to activate any Chrome Browser, I just need to call an AutoIT script as below

Runtime.getRuntime().exec(Utils.ConvertToAbsolutePath("Files\\AutoITScripts\\Activate.exe") + " " + sWindowHandle);

with sWindowHandle is the Windows Handle of ChromeDriver get from temp.xml, and Activate.exe is compiled by external AutoIT script as below



Many thanks all and sorry for late response.


Reply all
Reply to author
Forward
0 new messages