如何禁用 geckodriver 中的 plain_text.wrap_long_lines 选项?
How to disable plain_text.wrap_long_lines option in geckodriver?
在 rspec 中使用 selenium-webdriver 和 capybara,在功能规范中,我试图获取纯文本请求的 HTTP 响应,即 /robots.txt
但是我得到的不是纯文本响应,而是包含在 HTML:
中的文本响应
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 时,我得到了预期的纯文本响应。所以我浏览了 Firefox 选项,发现我需要禁用 plain_text.wrap_long_lines 选项。
而且我无法成功将选项传递给 geckodriver。
我首先尝试将它传递给 Options
对象,像这样:
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
对象。
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-webdriver 3.14.1
- 水豚 3.7.2
- 壁虎驱动程序 0.22.0
您在这里看到的问题是,当 Firefox 打开文本文件时,它会自动将其包装在一些样板文件中 html 以便浏览器能够显示它。你没有显示你正在使用的测试代码 - 但无论你在做什么都应该归结为
# 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)
在 rspec 中使用 selenium-webdriver 和 capybara,在功能规范中,我试图获取纯文本请求的 HTTP 响应,即 /robots.txt
但是我得到的不是纯文本响应,而是包含在 HTML:
中的文本响应 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 时,我得到了预期的纯文本响应。所以我浏览了 Firefox 选项,发现我需要禁用 plain_text.wrap_long_lines 选项。
而且我无法成功将选项传递给 geckodriver。
我首先尝试将它传递给 Options
对象,像这样:
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
对象。
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-webdriver 3.14.1
- 水豚 3.7.2
- 壁虎驱动程序 0.22.0
您在这里看到的问题是,当 Firefox 打开文本文件时,它会自动将其包装在一些样板文件中 html 以便浏览器能够显示它。你没有显示你正在使用的测试代码 - 但无论你在做什么都应该归结为
# 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)