Cucumber 测试 - 浏览器检测机器人用户代理
Cucumber tests - browser detects bot user agent
我需要使用 "bot" 和 "user" 用户代理测试 cucumber 上的某些行为。
在某些控制器中,我有一个机器人保护条款
if browser.bot?
redirect_to(canonical_path, status: :moved_permanently)
else
do_some_tracking
redirect_somewhere
end
我有一个黄瓜测试,其功能会根据机器人或用户是否访问页面而有所不同
Feature: Visit the special page
Scenario: A bot visits the special page
When I visit the special page
I should see the home page
Scenario: A user visits the special page not signed in
Given I am not logged-in
When I visit the special page
I should see "Who are you ?"
Scenario: A user visits the special page signed in
Given I am logged-in as "michael"
When I visit the special page
I should see "Achievement earned : explorer"
有没有办法在每个场景的基础上修改用户代理?
我虽然在 env.rb
中使用标签和 Before
/After
块,但我不确定如何实际更改当前驱动程序的用户代理字符串(和之后恢复正常)
否则,默认的 Poltergeist 驱动程序的用户代理是 "PhantomJS",它始终被识别为 browser
gem[= 的机器人16=]
假设您正在使用 Poltergeist(因为您的问题中提到了它)您可以通过在基于 Before 的标记中设置 'User-Agent' header 来覆盖当前测试的 user-agent块(它将在下一次测试时自动重置)。
page.driver.add_header("User-Agent", "whatever you want")
这在 Poltergeist 自述文件中有记录 - https://github.com/teampoltergeist/poltergeist#manipulating-request-headers
如果您不使用 Poltergeist,其他驱动程序也有设置 headers 的方法,并且有一个 capybara-user_agent
gem 提供统一的 API用于设置 user-agent。 gem 已经有一段时间没有更新了,所以我不知道它是否仍然有效。
我需要使用 "bot" 和 "user" 用户代理测试 cucumber 上的某些行为。
在某些控制器中,我有一个机器人保护条款
if browser.bot?
redirect_to(canonical_path, status: :moved_permanently)
else
do_some_tracking
redirect_somewhere
end
我有一个黄瓜测试,其功能会根据机器人或用户是否访问页面而有所不同
Feature: Visit the special page
Scenario: A bot visits the special page
When I visit the special page
I should see the home page
Scenario: A user visits the special page not signed in
Given I am not logged-in
When I visit the special page
I should see "Who are you ?"
Scenario: A user visits the special page signed in
Given I am logged-in as "michael"
When I visit the special page
I should see "Achievement earned : explorer"
有没有办法在每个场景的基础上修改用户代理?
我虽然在 env.rb
中使用标签和 Before
/After
块,但我不确定如何实际更改当前驱动程序的用户代理字符串(和之后恢复正常)
否则,默认的 Poltergeist 驱动程序的用户代理是 "PhantomJS",它始终被识别为 browser
gem[= 的机器人16=]
假设您正在使用 Poltergeist(因为您的问题中提到了它)您可以通过在基于 Before 的标记中设置 'User-Agent' header 来覆盖当前测试的 user-agent块(它将在下一次测试时自动重置)。
page.driver.add_header("User-Agent", "whatever you want")
这在 Poltergeist 自述文件中有记录 - https://github.com/teampoltergeist/poltergeist#manipulating-request-headers
如果您不使用 Poltergeist,其他驱动程序也有设置 headers 的方法,并且有一个 capybara-user_agent
gem 提供统一的 API用于设置 user-agent。 gem 已经有一段时间没有更新了,所以我不知道它是否仍然有效。