我希望使用其他浏览器语言运行硒测试。我尝试了两个SO答案,甚至没有获得BrowserStack支持。
我曾经希望这将是一个快速而简单的修复,但是看起来像简单的代码却以我不了解的方式失败了。任何人都可以提供一个有效的代码段,或者一个好地方,这个地方出了错吗?
[此答案] How to set Browser Language using RemoteWebDriver和[此答案] How to set browser language in selenium remote webdriver capabilities均已在以下代码中进行了尝试:
DesiredCapabilities DesCaps = new DesiredCapabilities();
DesCaps.SetCapability("browserstack.user", "your_user");
DesCaps.SetCapability("browserstack.key", "your_key");
FirefoxProfile fp = new FirefoxProfile();
fp.SetPreference("intl.accept_languages", "en-US");
DesCaps.SetCapability(FirefoxDriver.ProfileCapabilityName, fp);
ChromeOptions options = new ChromeOptions();
options.AddArguments("--lang=en-DE");
DesCaps.SetCapability(ChromeOptions.Capability, options);
RemoteWebDriver driver = new RemoteWebDriver(new Uri("http://hub-cloud.browserstack.com/wd/hub/"), DesCaps, TimeSpan.FromSeconds(500));
driver.Navigate().GoToUrl("http://google.com");
Console.WriteLine("Completed!");
Console.ReadKey();
当我尝试启动驱动程序时收到异常:
OpenQA.Selenium.WebDriverException
HResult=0x80131500
Message=The property '#/' contains additional properties ["BinaryLocation", "LeaveBrowserRunning", "Arguments", "Extensions", "DebuggerAddress", "MinidumpPath", "PerformanceLoggingPreferences", "UseSpecCompliantProtocol", "BrowserName", "BrowserVersion", "PlatformName", "AcceptInsecureCertificates", "UnhandledPromptBehavior", "PageLoadStrategy", "Proxy"] outside of the schema when none are allowed in chromeOptions
Source=WebDriver
StackTrace:
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver.StartSession(ICapabilities desiredCapabilities)
at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(ICommandExecutor commandExecutor, ICapabilities desiredCapabilities)
at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(Uri remoteAddress, ICapabilities desiredCapabilities, TimeSpan commandTimeout)
答案 0 :(得分:0)
从代码片段中,我看到您已经在同一脚本中为Chrome和Firefox指定了浏览器选项。您将需要针对Chrome和Firefox使用单独的测试脚本。
-要在Chrome浏览器上创建会话,请包含以下功能
DesiredCapabilities capability;
ChromeOptions options = new ChromeOptions();
// Set browser language to French
options.AddArguments("--lang=fr");
capability = (DesiredCapabilities)options.ToCapabilities();
capability.SetCapability("browser", "chrome");
capability.SetCapability("browser_version", "61");
capability.SetCapability("os", "Windows");
capability.SetCapability("os_version", "7");
capability.SetCapability("browserstack.user", "USERNAME");// add username
capability.SetCapability("browserstack.key", "ACCESS_KEY"); //add automate-key
driver = new ScreenShotRemoteWebDriver(new Uri("http://hub-cloud.browserstack.com/wd/hub/"), capability);
-要在Firefox浏览器上创建会话,请包含以下功能
DesiredCapabilities capability = new DesiredCapabilities();
capability.SetCapability("browser", "Firefox");
capability.SetCapability("browser_version", "63.0");
capability.SetCapability("os", "Windows");
capability.SetCapability("os_version", "10");
capability.SetCapability("browserstack.user", "your_usernmae");
capability.SetCapability("browserstack.key","your_access_key");
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.SetPreference("intl.accept_languages", "fr");
capability.SetCapability(FirefoxDriver.ProfileCapabilityName, firefoxProfile.ToBase64String());
有关使用C#传递浏览器选项的更多信息,请参考-https://www.browserstack.com/automate/c-sharp
答案 1 :(得分:0)
BrowserStack支持小组提供了两个有效的示例:
{
IWebDriver driver;
FirefoxOptions options = new FirefoxOptions();
options.AddAdditionalCapability("browser", "firefox", true);
options.AddAdditionalCapability("os", "Windows", true);
options.AddAdditionalCapability("os_version", "10", true);
options.AddAdditionalCapability("browser_version", "63.0", true);
options.AddAdditionalCapability("build", "BuildName", true);
options.SetPreference("intl.accept_languages", "de");
driver = new RemoteWebDriver(new Uri("http://your_username:your_access_key@hub-cloud.browserstack.com/wd/hub/"),
options.ToCapabilities());
driver.Navigate().GoToUrl("http://www.google.com");
Console.WriteLine(driver.Title);
IWebElement query = driver.FindElement(By.Name("q"));
query.SendKeys("Browserstack");
query.Submit();
Console.WriteLine(driver.Title);
Thread.Sleep(2000);
driver.Navigate().GoToUrl("http://www.amazon.in");
Console.WriteLine(driver.Title);
driver.Quit();
}
}
和在Chrome中:
IWebDriver driver;
ChromeOptions options = new ChromeOptions();
options.AddAdditionalCapability("browser", "chrome", true);
options.AddAdditionalCapability("os", "Windows", true);
options.AddAdditionalCapability("os_version", "10", true);
options.AddAdditionalCapability("browser_version", "69.0", true);
options.AddAdditionalCapability("build", "BuildName", true);
options.AddArgument("--lang=de");
driver = new RemoteWebDriver(new Uri("http://your_username:your_access_key@hub-cloud.browserstack.com/wd/hub/"),
options.ToCapabilities());
driver.Navigate().GoToUrl("http://www.google.com");
Console.WriteLine(driver.Title);
IWebElement query = driver.FindElement(By.Name("q"));
query.SendKeys("Browserstack");
query.Submit();
Console.WriteLine(driver.Title);
Thread.Sleep(2000);
driver.Navigate().GoToUrl("http://www.amazon.in");
Console.WriteLine(driver.Title);
driver.Quit();