首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在geckodriver中禁用plain_text.wrap_long_lines选项?

如何在geckodriver中禁用plain_text.wrap_long_lines选项?
EN

Stack Overflow用户
提问于 2018-10-15 09:31:53
回答 1查看 251关注 0票数 0

在rspec中使用selenium-webdriver和capybara,在一个特性规范中,我试图获得纯文本请求的HTTP响应,即/robots.txt

但是,我没有获得纯文本响应,而是将文本响应包装在HTML中:

代码语言:javascript
复制
   expected: "User-agent: *\nDisallow:\n\nSitemap: https://prj.org/sitemap.xml\n"
        got: "<html><head><link rel=\"alternate stylesheet\" type=\"text/css\" href=\"resource://content-accessible/plaintext.css\" title=\"Wrap Long Lines\"></head><body><pre>User-agent: *\nDisallow:\n\nSitemap: https://prj.org/sitemap.xml\n</pre></body></html>"

当使用curl获取/robots.txt时,我会得到预期的纯文本响应。所以我已经浏览过火狐选项,我发现我需要禁用plain_text.wrap_long_lines选项。

我也不能成功地把这个选项交给geckodriver。

我首先尝试将它传递给Options对象,如下所示:

代码语言:javascript
复制
Capybara.register_driver :firefox_headless do |app|
  options = ::Selenium::WebDriver::Firefox::Options.new
  options.headless!
  options.add_preference 'plain_text.wrap_long_lines', false

  Capybara::Selenium::Driver.new app, browser: :firefox, options: options
end

然后我尝试将它传递给一个Profile对象。

代码语言:javascript
复制
Capybara.register_driver :firefox_headless do |app|
  options = ::Selenium::WebDriver::Firefox::Options.new
  options.headless!

  profile = Selenium::WebDriver::Firefox::Profile.new
  profile['plain_text.wrap_long_lines'] = false

  Capybara::Selenium::Driver.new app, browser: :firefox, options: options, profile: profile
end

在这两种情况下,结果是相同的。知道为什么吗?谢谢!

使用:

  • selenium-WebDriver3.14.1
  • 水豚3.7.2
  • geckodriver 0.22.0
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-15 21:32:29

您在这里看到的问题是,当Firefox打开一个文本文件时,它会自动将其包装在一些样板html中,以便浏览器能够显示它。您没有显示您正在使用的测试代码,但是无论您在做什么,都应该归结为

代码语言:javascript
复制
# If using minitest
visit('/robots.txt')
find('body > pre').assert_text("User-agent: *\nDisallow:\n\nSitemap: https://prj.org/sitemap.xml\n", exact: true)

# If using RSpec
visit('/robots.txt')
expect(find('body > pr')).to have_text("User-agent: *\nDisallow:\n\nSitemap: https://prj.org/sitemap.xml\n", exact: true)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52813680

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档