22

我想用不同的代理(iPhone、iPad、Android)测试一个用 .NET 编写的 Web 应用程序,我使用 NUnit 和 Selenium 进行测试。

任何人都有使用 c# 或 VB 更改 Selenium 中的代理(例如 iPad 或 iPhone)的示例?

4

2 回答 2

40

示例 UA(您可能需要自己谷歌才能找到适合您目的的):

Mozilla/5.0 (iPad; CPU OS 6_0 像 Mac OS X) AppleWebKit/536.26 (KHTML, 像 Gecko) 版本/6.0 Mobile/10A5355d Safari/8536.25

C#火狐:

FirefoxProfile profile = new FirefoxProfile();
profile.SetPreference("general.useragent.override", "Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25");
IWebDriver driver = new FirefoxDriver(profile);

C# 铬:

ChromeOptions options = new ChromeOptions();
options.AddArgument("--user-agent=Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25");
IWebDriver driver = new ChromeDriver(options);

C# PhantomJS(未经测试的代码):

PhantomJSOptions options = new PhantomJSOptions();
options.AddAdditionalCapability("phantomjs.page.settings.userAgent", "Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25");
IWebDriver driver = new PhantomJSDriver(options);

C# IE:抱歉,我认为这在 Selenium 中是不可能的。

进一步阅读:使用 Selenium WebDriver C# 和 Ruby 设置用户代理

于 2013-08-01T21:11:17.647 回答
2

在 C# 中:

public static FirefoxProfile myFireProfile = new FirefoxProfile();
myFireProfile.SetPreference("general.useragent.override", "Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10");
public IWebDriver driver= new FirefoxDriver(myFireProfile);
于 2013-08-01T15:58:23.030 回答