Custom profile for Chrome

Environment: Mac OS X 10.8.3, Ruby 2.0.0p0, selenium-webdriver 2.32.1, ChromeDriver 26.0.1383.0.

I want to change default browser language. I am testing if the site detects the browser language correctly and displays the pages in the language.

I was able to set Firefox language to German:

require "selenium-webdriver"

profile = Selenium::WebDriver::Firefox::Profile.new 
profile["intl.accept_languages"] = "de"

caps = Selenium::WebDriver::Remote::Capabilities.firefox(firefox_profile: profile) 
caps.platform = "Linux" 
caps.version = 20

driver = Selenium::WebDriver.for( 
:remote, 
url: "http://USERNAME:ACCESS-KEY@ondemand.saucelabs.com:80/wd/hub", 
desired_capabilities: caps)

driver.navigate.to "http://sandbox.translatewiki.net/"

I want to do the same using Chrome (and other browsers, if possible).

I have tried several things trying to open the page in German in Chrome, but every time the page is displayed in English, instead of in German.

require "selenium-webdriver"

profile = Selenium::WebDriver::Chrome::Profile.new 
profile["intl.accept_languages"] = "de"

caps = Selenium::WebDriver::Remote::Capabilities.chrome(firefox_profile: profile) 
caps.platform = "Linux" 
caps.version = ""

driver = Selenium::WebDriver.for( 
:remote, 
url: "http://USERNAME:ACCESS-KEY@ondemand.saucelabs.com:80/wd/hub", 
desired_capabilities: caps)

driver.navigate.to "http://sandbox.translatewiki.net/"

If I change firefox_profile: profile to profile: profile or chrome_profile: profile , the page opens in English (instead of in German) every time.

As far as I can see in the API docs, only :firefox_profile is supported.

I was able to do it on a local machine, but not when using Sauce Labs.


这应该工作:

require "selenium-webdriver"

profile = Selenium::WebDriver::Chrome::Profile.new 
profile["intl.accept_languages"] = "de"

caps = Selenium::WebDriver::Remote::Capabilities.chrome(
  platform: "Linux", 
  version: "", 
  'chrome.profile' => profile.as_json['zip']
)

Selenium::WebDriver.for(:remote, 
  url: "http://...@ondemand.saucelabs.com:80/wd/hub", 
  desired_capabilities: caps
)

Wow, the documentation for SauceLabs + Chrome + Selenium + Ruby is very inconsistent and sometimes contradictory. Unfortunately I do not have a SauceLabs account to test so all I can do is give you suggestions.

This documentation says it is a known issue that ChromeDriver does not support a custom profile. This post shows how to set a custom profile for Chrome. Go figure.

Setting a profile or a default language for that matter is NOT part of the standard WebDriver wire protocol so you may be out of luck.

One workaround would be to set your browser to use a proxy and in the proxy add/replace the Accept-Language header in the proxy.

Still, looking through the Selenium Ruby code, it looks like that post might be on to something, so give this a try:

profile = Selenium::WebDriver::Chrome::Profile.new
profile["intl.accept_languages"] = "de"

caps = Selenium::WebDriver::Remote::Capabilities.chrome
caps['chromeOptions'] = { 'profile'    => profile.as_json['zip'] }
driver = Selenium::WebDriver.for( 
:remote, 
url: "http://USERNAME:ACCESS-KEY@ondemand.saucelabs.com:80/wd/hub", 
desired_capabilities: caps)

driver.navigate.to "http://sandbox.translatewiki.net/"

EDIT: It seems like the --lang- switch does not do what you want, so ignore the following. I'm leaving it here for posterity.

This might work (forget about the profile, use command line switches):

caps = Selenium::WebDriver::Remote::Capabilities.chrome
caps['chrome.switches'] = ['--lang-de']

I am seeing the German Translation on my local machine using:

profile = Selenium::WebDriver::Chrome::Profile.new
profile["intl.accept_languages"] = "de"
@driver = Selenium::WebDriver.for :chrome, :profile => profile  
@target = 'http://sandbox.translatewiki.net/'

osx: 10.7.5

ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin11.4.2]

链接地址: http://www.djcxy.com/p/71196.html

上一篇: ECMAscript中的案例性能

下一篇: Chrome的自定义个人资料