ruby - 使用 Selenium 2 设置用户代理

标签 ruby firefox selenium selenium-webdriver webautomation

我正在尝试对一个应用程序进行网络自动化,如果从移动设备访问该应用程序,该应用程序的行为会有所不同。有谁知道使用 Selenium 2 实现这一目标的最佳方法吗?

在理想的世界中,我希望找到一种仅配置用户代理的方法,以便我们可以轻松测试大量排列。

最佳答案

寻找here ,它显示了在 Firefox 中设置用户代理字符串的代码:

FirefoxProfile profile = new FirefoxProfile();
profile.addAdditionalPreference("general.useragent.override", "some UA string");
WebDriver driver = new FirefoxDriver(profile);

转换为 Ruby,它看起来像这样:

require 'selenium-webdriver'

profile = Selenium::WebDriver::Firefox::Profile.new
profile['general.useragent.override'] = 'some UA string'

driver = Selenium::WebDriver.for :firefox, :profile => profile

在其末尾添加一行以导航至 http://whatsmyuseragent.com表明它按预期工作。

但是,Selenium 2 附带了适用于 iPhone 和 Android 应用程序的驱动程序。我还没有尝试过它们,但看起来它们都可以在模拟器或真实设备中运行。这些对你不起作用有什么原因吗?它们可以让您更好地了解设备上的实际情况。

关于ruby - 使用 Selenium 2 设置用户代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4750433/

相关文章:

ruby-on-rails - rails 4 - 遍历查询结果

ruby-on-rails - 如何结合载波有条件地显示存在验证?

html - 鼠标悬停仅在 firefox 中不起作用

ruby - 使用 Selenium Webdriver 查找 Web 元素

selenium - 如何使用 xpath 提取 <br/> 标记之间的文本

ruby-on-rails - Rails elasticsearch 映射被忽略

mysql - gem install mysql2 出错

javascript - firefox 跟踪保护阻止 facebook js sdk 的异步加载

html - 为什么我不能在 chrome 中显示这个跨度,但我可以在 FF 中显示?

selenium - 在实践中使用 Selenium 2.0 WebDriver