在 Java 中使用 selenium 驱动程序进行测试,而无需打开任何浏览器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11345173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:42:57  来源:igfitidea点击:

Testing with a selenium driver in Java without opening any browser

javaseleniumselenium-chromedriver

提问by Ahmet DAL

I need to test with selenium chrome driver in Java. But chrome window should't be opened. Assume this a product and no window should be opened.

我需要在 Java 中使用 selenium chrome 驱动程序进行测试。但不应打开镀铬窗口。假设这是一个产品,不应打开任何窗口。

I've also looked at this one ; Is it possible to hide the browser in Selenium RC?But no solution for me. The testing should be operating system independent and I've tried HtmlUnitDriver for testing without opening any window but it has some problem. When there is finding components by id, it may not find the component by id. Some servers may send the component id according to browser and I can't know what id I should use to test.

我也看过这个; 是否可以在 Selenium RC 中隐藏浏览器?但对我来说没有解决方案。测试应该是独立于操作系统的,我已经尝试过 HtmlUnitDriver 进行测试而不打开任何窗口,但它有一些问题。当有通过id查找组件时,可能不会通过id查找组件。有些服务器可能会根据浏览器发送组件 id,我不知道应该使用什么 id 来测试。

Because of that I'm trying to use chrome driver.

因此,我正在尝试使用 chrome 驱动程序。

Is there a way to use chromedriver without opening chrome window or another way to test without opening any window with Selenium in Java?

有没有办法在不打开 chrome 窗口的情况下使用 chromedriver 或另一种在 Java 中使用 Selenium 在不打开任何窗口的情况下进行测试的方法?

Thank!

感谢!

采纳答案by Russell Bradley

Go with PhantomJSbut if running them in chromedriver is required and you have the resources, this blog has a good recipe on running headless selenium with chrome. Requiring you to download the following...

使用PhantomJS,但如果需要在 chromedriver 中运行它们并且你有资源,这个博客有一个关于使用 chrome 运行 headless selenium的好方法。要求您下载以下...

  • VirtualBox
  • Vagrant
  • NodeJS
  • 虚拟盒子
  • 流浪汉
  • 节点

If you plan to implement Jenkinsor any other CI in the future, I strongly suggest going with PhantomJS though.

如果你打算在未来实现Jenkins或任何其他 CI,我强烈建议你使用 PhantomJS。

回答by NuOne

In selenium web driver there is headless mode. so in headless mode you can do the automation without opening the web browser. and also you can deploy your application in none gui system

在 selenium web 驱动程序中有无头模式。所以在无头模式下,您可以在不打开网络浏览器的情况下进行自动化。并且您也可以在无 gui 系统中部署您的应用程序

    ChromeOptions options = new ChromeOptions();
    // setting headless mode to true.. so there isn't any ui
    options.setHeadless(true);

    // Create a new instance of the Chrome driver
    WebDriver driver = new ChromeDriver(options);

回答by Michael Herrmann

GhostDriver and PhantomJSshould let you do what you want.

GhostDriver 和 PhantomJS应该让你做你想做的。

回答by Betlista

I like this article.

我喜欢这篇文章

Basically you need to add PhantomJS dependency in pom (I like maven for dependency management):

基本上你需要在 pom 中添加 PhantomJS 依赖(我喜欢 maven 进行依赖管理):

<dependency>
    <groupId>com.github.detro.ghostdriver</groupId>
    <artifactId>phantomjsdriver</artifactId>
    <version>1.1.0</version>
</dependency>

And run code

并运行代码

    System.setProperty( "phantomjs.binary.path", "c:\path\to\phantomjs-1.9.8-windows\phantomjs.exe" );
    WebDriver driver = new PhantomJSDriver();
    driver.get("http://www.google.com");
    driver.quit();

It worked for me with versions:

它对我有用的版本:

  • PhantomJS 1.9.8
  • PhantomJS driver 1.1.0
  • Selenium 2.44.0
  • PhantomJS 1.9.8
  • PhantomJS 驱动程序 1.1.0
  • 硒 2.44.0