简体   繁体   English

使用IntelliJ Idea的Selenium WebDriver运行JUnit时如何使浏览器窗口在后台运行?

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

I am running automation tests on Selenium WebDriver as JUnit tests from IntellijIdea. 我在Selenium WebDriver上运行自动化测试,作为来自IntellijIdea的JUnit测试。 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. 或者,如果我想要运行整个套件,我不能只是坐着观看30分钟。

I started to have this problem after I switched from Windows OS to Linux Mint. 从Windows操作系统切换到Linux Mint后,我开始遇到此问题。 On Windows browser remained in the background and did not bother me. 在Windows上浏览器仍然在后台,并没有打扰我。 Is there any way to configure such behavior on Linux Mint OS? 有没有办法在Linux Mint OS上配置这样的行为?

I have already tried to run the browser/IntelliJ Idea in a separate LinuxMint's workspace, doesn't help. 我已经尝试在单独的LinuxMint工作区中运行浏览器/ IntelliJ Idea,但没有帮助。 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: 此外,我在LinuxMint的Windows行为设置中设置了此配置: 在此输入图像描述

As of this week (chrome 57 or higher) it is possible to run chrome in headless mode . 截至本周(铬57或更高),可以在无头模式下运行 To do this you simply pass the --headless flag. 要做到这一点,你只需传递--headless标志。

To pass this flag to chrome via Selenium webdriver you can use the following code: 要通过Selenium webdriver将此标志传递给chrome,您可以使用以下代码:

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. 我将继续关注焦点问题并在发现问题时进行更新。

Selenium doesn't have a built in method for minimizing the browser, however you can take out of sight by using: Selenium没有内置的方法来最小化浏览器,但是你可以使用以下方法看不见:

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. 虽然这种'hack'会将浏览器窗口移开视线,但是当驱动程序初始化时,浏览器会在短时间内可见。

Try using RemoteDriver and it will execute code with minimized browser. 尝试使用RemoteDriver,它将使用最小化的浏览器执行代码。 Give your localhost ip as a host address. 将localhost ip作为主机地址。 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();
        }

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 . 寻找xvfb However, I'm not 100% it will work with Chrome. 但是,我不是100%它将与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. 另一个选择是在linux下创建虚拟机(例如使用虚拟机)并使用Selenium Grid连接到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM