在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时,我会得到预期的纯文本响应。所以我已经浏览过火狐选项,我发现我需要禁用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在这两种情况下,结果是相同的。知道为什么吗?谢谢!
使用:
发布于 2018-10-15 21:32:29
您在这里看到的问题是,当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)https://stackoverflow.com/questions/52813680
复制相似问题