【问题标题】:Is there any way to execute the UI tests in headless mode in Selenium version 4 and heigher?有没有办法在 Selenium 版本 4 和更高版本中以无头模式执行 UI 测试?
【发布时间】:2019-05-08 16:21:14
【问题描述】:

最近 Selenium 发布了他们的主要版本 Selenium 4,并宣布从 Selenium4 开始不会进一步支持 PhantomJS。这是否意味着 Selenium 不再支持无头自动化,或者有没有办法在 Selenium 版本 4 中以无头模式执行测试?欣赏代码示例。

【问题讨论】:

  • 请编辑问题以将其限制为具有足够详细信息的特定问题,以确定适当的答案。避免一次问多个不同的问题。请参阅“如何提问”页面以获得澄清此问题的帮助。
  • @cruisepandey 你能告诉我你在找什么细节吗?根据我对英语的理解,这里只有一个问题。如果您知道 Selenium 确实发布了它的下一个版本,您就会理解这个问题。仍然有些人可以理解这个问题,甚至可以回答。

标签: javascript node.js selenium selenium-webdriver google-chrome-headless


【解决方案1】:

在 Selenium 4 中,已移除对 PhantomJS 的原生支持。尽管如此,使用 PhantomJS 在 headless 模式下运行脚本的用户仍然可以在 headless 模式下使用 Chrome 或 Firefox,如下所示。

const chrome = require('../chrome');
const firefox = require('../firefox');
const {Builder, By, Key, until} = require('..');

const width = 640;
const height = 480;

let driver = new Builder()
    .forBrowser('chrome')
    .setChromeOptions(
        new chrome.Options().headless().windowSize({width, height}))
    .setFirefoxOptions(
        new firefox.Options().headless().windowSize({width, height}))
    .build();

driver.get('http://www.google.com/ncr')
    .then(_ =>
        driver.findElement(By.name('q')).sendKeys('webdriver', Key.RETURN))
    .then(_ => driver.wait(until.titleIs('webdriver - Google Search'), 1000))
    .then(
        _ => driver.quit(),
        e => driver.quit().then(() => { throw e; }));

要了解有关 Selenium 4 更改的更多信息,请参阅 Selenium 4 (alpha) is Released: What’s New?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-19
    • 1970-01-01
    • 2013-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多