Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
251 views
in Technique[技术] by (71.8m points)

locale - How to change the language of a WebDriver?

I want to execute my Selenium tests in different languages. Is it possible to change the language of an existing WebDriver at runtime or do I have to recreate the browser instance?

Right now I'm only using Firefox, but I want to execute tests in different browsers at some later point.

In Firefox I set the language like this:

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("intl.accept_languages", "de");
WebDriver driver = new FirefoxDriver(profile);

I also implemented a WebDriverPool, which holds a WebDriver instance so it can be shared among tests. If the language can only be set at creation time, I could hold an instance for every locale.

All in all I wonder if I miss something here. Why is it so hard to change the language? shouldn't there be a method like WebDriver.setAcceptLanguages(Locale)?

In a nutshell I have these questions:

  1. Why isn't there WebDriver.setAcceptLanguages(Locale)?
  2. How to change the language for the dirrerent WebDrivers?
  3. Can I change the language at runtime?
  4. How did you guys implement your WebDriverPool or do you recreate them everytime?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I ended up creating a WebDriverPool that creates one instance for every combination of WebDriver type (e.g. FirefoxDriver.class) and Locale (e.g. en_US). Maybe this is usful for someone.

public class WebDriverPool {

  private Map<String, WebDriver> drivers = new HashMap<String, WebDriver>();
  private List<WebDriver> driversInUse = new ArrayList<WebDriver>();

  public WebDriverPool() {
    Runtime.getRuntime().addShutdownHook(new Thread(){
      @Override
      public void run(){
        for (WebDriver driver : drivers.values())
          driver.close();

        if (!driversInUse.isEmpty())
          throw new IllegalStateException("There are still drivers in use, did someone forget to return it? (size: " + driversInUse.size() + ")");
      }
    });
  }

  private WebDriver createFirefoxDriver(Locale locale){
    FirefoxProfile profile = new FirefoxProfile();
    profile.setPreference("intl.accept_languages", formatLocale(locale));
    return new FirefoxDriver(profile);
  }

  private String formatLocale(Locale locale) {
    return locale.getCountry().length() == 0
      ? locale.getLanguage()
      : locale.getLanguage() + "-" + locale.getCountry().toLowerCase();
  }

  /**
   * @param clazz
   * @param locale
   * @return web driver which can be new or recycled
   */
  public synchronized WebDriver getWebDriver(Class<? extends WebDriver> clazz, Locale locale){

    String key = clazz.getName() + "-" + locale;

    if(!drivers.containsKey(key)){

      if(clazz == FirefoxDriver.class){
        drivers.put(key, createFirefoxDriver(locale));
      }

      // TODO create other drivers here ...

      // else if(clazz == ChromeDriver.class){
      //     drivers.put(key, createChromeDriver(locale));
      // }

      else{
        throw new IllegalArgumentException(clazz.getName() + " not supported yet!");
      }
    }

    WebDriver driver = drivers.get(key);

    if(driversInUse.contains(driver))
      throw new IllegalStateException("This driver is already in use. Did someone forgot to return it?");

    driversInUse.add(driver);
    return driver;
  }

  public synchronized void returnWebDriver(WebDriver driver){
    driversInUse.remove(driver);
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...