How to make browser window to run in background when running JUnit with Selenium WebDriver from IntelliJ Idea?

拈花ヽ惹草 提交于 2020-01-04 03:28:10

问题


I am running automation tests on Selenium WebDriver as JUnit tests from IntellijIdea. When a browser opens and some actions are being done in it then it always brings itself to the front, even if I have tab-ed to other window. This is not very convenient. Some tests run for few minutes and I would like to be able to work on some other tasks while they are running. Or in case if I want to run the whole suite I can't just sit and watch for 30 minutes.

I started to have this problem after I switched from Windows OS to Linux Mint. On Windows browser remained in the background and did not bother me. Is there any way to configure such behavior on Linux Mint OS?

I have already tried to run the browser/IntelliJ Idea in a separate LinuxMint's workspace, doesn't help. The browser window pops up in the other workspace as soon as some activity is being done there. Also I've set this config in LinuxMint's windows behavior settings:


回答1:


As of this week (chrome 57 or higher) it is possible to run chrome in headless mode. To do this you simply pass the --headless flag.

To pass this flag to chrome via Selenium webdriver you can use the following code:

ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");

ChromeDriver chromeDriver = new ChromeDriver(options);

I'm not sure if this solves the focus issue completely though.. In my case there is still a chrome window stealing focus, but I think it is a big improvement as the window doesn't pop up in front of what I'm doing.

I think it's definitely worth a shot. I will continue to look at the focus issue and update if I find something.




回答2:


Selenium doesn't have a built in method for minimizing the browser, however you can take out of sight by using:

driver.manage().window().setPosition(new Point(-2000, 0));

While this 'hack' will move the browser window out of sight, when the driver is initialized the browser will be visible for a short moment.




回答3:


Try using RemoteDriver and it will execute code with minimized browser. Give your localhost ip as a host address. Code would look like this.

  try {


    capabilities = DesiredCapabilities.chrome();
        capabilities.setCapability("chrome.switches", Arrays.asList(
                "--start-maximized", "--disable-popup-blocking"));
        System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/BrowserDrivers/chromedriver.exe");
        // Code to faster execution on Chrome 
        ChromeOptions chromOpt = new ChromeOptions();
        chromOpt.addArguments("Proxy","null");
        capabilities.setCapability(ChromeOptions.CAPABILITY,chromOpt );
        driver = new RemoteWebDriver(new URL("http://127.0.0.1:5555/wd/hub"), capabilities);*/
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



回答4:


It seems that solutions you're looking for is called virtual display, you need to create virtual display and execute your tests in there. Look for xvfb. However, I'm not 100% it will work with Chrome. Another option that you have is to create virtual machine(using virtualbox, for example) under linux and use Selenium Grid to connect to it.



来源:https://stackoverflow.com/questions/39078220/how-to-make-browser-window-to-run-in-background-when-running-junit-with-selenium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!