Selenium 支持无头浏览器测试吗?

发布于 2024-12-06 17:53:40 字数 160 浏览 2 评论 0原文

我现在正在查看 Selenium Server,我似乎没有注意到支持无头浏览器测试的驱动程序。

除非我弄错了,否则它不支持。如果您使用的是 X,您可以创建一个虚拟帧缓冲区来隐藏浏览器窗口,但这并不是真正的无头浏览器。

谁能启发我吗? Selenium 支持无头浏览器测试吗?

I'm looking at Selenium Server at the moment, and I don't seem to notice a driver that supports headless browser testing.

Unless I'm mistaken, it doesn't support it. If you're on X, you can create a virtual framebuffer to hide the browser window, but that's not really a headless browser.

Can anyone enlighten me? Does Selenium support headless browser testing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

尛丟丟 2024-12-13 17:53:40

您无需使用 PhantomJS 作为 Selenium 的替代品。 Selenium 包含一个 PhantomJS webdriver 类,它运行在 GhostDriver 平台上。只需将 PhantomJS 二进制文件安装到您的计算机上即可。在 python 中,你可以使用:

from selenium import webdriver
dr = webdriver.PhantomJS() 

瞧。

you need not use PhantomJS as an alternative to Selenium. Selenium includes a PhantomJS webdriver class, which rides on the GhostDriver platform. Simply install the PhantomJS binary to your machine. in python, you can then use:

from selenium import webdriver
dr = webdriver.PhantomJS() 

and voila.

甜中书 2024-12-13 17:53:40

WebDriver API 支持 HTMLUnit 作为测试浏览器。 Ruby 开发人员已经使用 Capybara 进行无头硒测试一段时间了,所以它绝对是可行的。

The WebDriver API has support for HTMLUnit as the browser for your testing. Ruby people have been using Capybara for a while for their headless selenium testing so it is definitely doable.

热鲨 2024-12-13 17:53:40

我知道这是一个旧帖子。认为这会对其他正在寻找答案的人有所帮助。

您可以使用 XVFB 在任何 Linux 发行版中安装完整的 Firefox。这可以确保您的测试是在真实的浏览器中执行的。一旦完成无头设置,您就可以使用您选择的 Webdriver 来连接并运行测试。

I know this is a old post. Thought it will help others who are looking for an answer.

You can install a full blown firefox in any linux distribution using XVFB. This makes sure your testing is performed in a real browser. Once you have a headless setup, you can use webdriver of your choice to connect and run testing.

初心 2024-12-13 17:53:40

无头浏览器是个坏主意。它们会为您提供一些测试,但与真实用户所看到的完全不同,并且它们掩盖了只有真实浏览器才会遇到的许多问题。在无头环境(例如、Windows 或带有 XVFB 的 Linux)上使用“有头”浏览器(,除 HTMLUnit 之外的任何浏览器)会带来无限的好处。

Headless browsers are a bad idea. They get you some testing, but nothing like what a real user will see, and they mask lots of problems that only real browsers encounter. You're infinitely better off using a "headed" browser (i.e., anything but HTMLUnit) on a headless environment (e.g., Windows, or Linux with XVFB).

我早已燃尽 2024-12-13 17:53:40

我注意到您说使用 X 帧缓冲区并不是真正的无头解决方案,但是,对于大多数人来说,我认为这是可以接受的。除此之外,如果您对此感兴趣,此服务将帮助您实现这一点一个解决方案。

I notice that you say that using an X framebuffer isn't a true headless solution, however, for most, I think it would be acceptable. In addition to that, this service will help get that going for you if you are interested in that as a solution.

你列表最软的妹 2024-12-13 17:53:40

Selenium 确实以某种方式支持无头浏览器测试。 Docker Selenium 由 SeleniumHQ 维护。这些 docker 容器开箱即用,支持 xvfb。有些工具(例如 PhantomJS)可以与 Selenium 连接。但是,Selenium 本身并未正式支持它。

就像其他人所描述的那样,PhantomJS 并不真正推荐。 Selenium 的全部意义在于自动化浏览器。但为什么要自动化一个没有人使用的浏览器呢?我从来不明白为什么开发人员经常忽视这一点。

Selenium does support headless browser testing in a way. Docker Selenium is maintained by SeleniumHQ. Those docker containers come with xvfb support with them out of the box. There are tools like PhantomJS that you can hook up with Selenium. However, it's not officially supported by Selenium itself.

Much like what others have described, PhantomJS isn't really recommended. The whole point of Selenium is to automate browsers. But why automate a browser no one uses? I never understood how that was overlooked so often by developers..

风铃鹿 2024-12-13 17:53:40

是的。 Selenium 支持无头浏览器测试,对于大量测试用例的执行速度更快、更方便。

ChromeOptions cromeOptions = new ChromeOptions();

//Location of browser binary/.exe file
cromeOptions.setBinary("/usr/bin/google-chrome-stable");

cromeOptions.addArguments("--headless");
cromeOptions.addArguments("--no-sandbox");
cromeOptions.addArguments("--disable-gpu");
cromeOptions.addArguments("--window-size=1920,1080");

WebDriver webDriver = new ChromeDriver(cromeOptions);

Yes. Selenium support headless browser testing and it's more faster as well as convient for big amount of test-cases execution.

ChromeOptions cromeOptions = new ChromeOptions();

//Location of browser binary/.exe file
cromeOptions.setBinary("/usr/bin/google-chrome-stable");

cromeOptions.addArguments("--headless");
cromeOptions.addArguments("--no-sandbox");
cromeOptions.addArguments("--disable-gpu");
cromeOptions.addArguments("--window-size=1920,1080");

WebDriver webDriver = new ChromeDriver(cromeOptions);
分开我的手 2024-12-13 17:53:40

是的,selenium 支持无头浏览器测试...但我发现 HTMLUnit 大多数时候都失败...我正在寻找替代方案...PhantomJs 真的很好。你绝对可以尝试一下,与其他浏览器相比,它非常快...这对于冒烟测试非常有用...

http://phantomjs.org/

Yes ,selenium supports headless browser testing...but i found HTMLUnit failing most times...I was searching for an alternative...PhantomJs was really good.you can definitely give it a try it was very fast when compared to other browsers...It is really good for smoke testing...

http://phantomjs.org/

怀念你的温柔 2024-12-13 17:53:40

对于 ruby​​ 和 macOS:brew install phantomjs 然后:

driver = Selenium::WebDriver.for :phantomjs

With ruby and macOS: brew install phantomjs then:

driver = Selenium::WebDriver.for :phantomjs
冰雪之触 2024-12-13 17:53:40

是的,Selenium 支持无头浏览器测试。无头浏览器比实时浏览器更快。

Yes Selenium supports headless browser testing.Headless browsers are faster than real time browsers.

有木有妳兜一样 2024-12-13 17:53:40

在 Linux 服务器上安装 chromeDrivergoogle-chrome-stable 版本,其中将触发测试并在代码中添加相同的二进制文件。

代码片段:

private static String driverPath = "/usr/bin/chromedriver";
static
{
    System.setProperty("webdriver.chrome.driver", driverPath);
    options = new ChromeOptions();
    options.setBinary("/usr/bin/google-chrome-stable");
    options.addArguments("headless");
    driver = new ChromeDriver(options);
}

Install chromeDriver and google-chrome-stable version on the linux server, where the tests will be triggered and add the same binaries in your code.

code snippet:

private static String driverPath = "/usr/bin/chromedriver";
static
{
    System.setProperty("webdriver.chrome.driver", driverPath);
    options = new ChromeOptions();
    options.setBinary("/usr/bin/google-chrome-stable");
    options.addArguments("headless");
    driver = new ChromeDriver(options);
}
变身佩奇 2024-12-13 17:53:40

这是关于如何在运行 Django/Python 的 Ubuntu Linux 环境中将 Selenium 与 xvfb 和 Firefox 驱动程序一起使用的“现代答案”:

# install xvfb and Firefox driver
sudo su
apt-get install -y xvfb firefox
wget https://github.com/mozilla/geckodriver/releases/download/v0.19.1/geckodriver-v0.19.1-linux64.tar.gz
tar -x geckodriver -zf geckodriver-v0.19.1-linux64.tar.gz -O > 
/usr/bin/geckodriver
chmod +x /usr/bin/geckodriver

# install pip modules
pip install selenium
pip install PyVirtualDisplay

然后您可以按照 Django LiveServerTestCase 说明

要使用刚刚安装的驱动程序,请执行以下操作:

from pyvirtualdisplay import Display
from selenium.webdriver.firefox.webdriver import WebDriver

driver = WebDriver(executable_path='/usr/bin/geckodriver')
display = Display(visible=0, size=(800, 600)).start()

# add your testing classes here...

driver.quit()
display.stop()

Here's a "modern answer" on how to use Selenium with xvfb and Firefox driver in an Ubuntu Linux environment running Django/Python:

# install xvfb and Firefox driver
sudo su
apt-get install -y xvfb firefox
wget https://github.com/mozilla/geckodriver/releases/download/v0.19.1/geckodriver-v0.19.1-linux64.tar.gz
tar -x geckodriver -zf geckodriver-v0.19.1-linux64.tar.gz -O > 
/usr/bin/geckodriver
chmod +x /usr/bin/geckodriver

# install pip modules
pip install selenium
pip install PyVirtualDisplay

You can then follow the Django LiveServerTestCase instructions.

To use the driver you just installed, do something like this:

from pyvirtualdisplay import Display
from selenium.webdriver.firefox.webdriver import WebDriver

driver = WebDriver(executable_path='/usr/bin/geckodriver')
display = Display(visible=0, size=(800, 600)).start()

# add your testing classes here...

driver.quit()
display.stop()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文