Tim*_*the 20 locale webdriver selenium-webdriver
我想用不同的语言执行我的Selenium测试.是否可以在运行时更改现有WebDriver的语言,还是必须重新创建浏览器实例?
现在我只使用Firefox,但我想稍后在不同的浏览器中执行测试.
在Firefox中我设置了这样的语言:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("intl.accept_languages", "de");
WebDriver driver = new FirefoxDriver(profile);
Run Code Online (Sandbox Code Playgroud)
我还实现了一个WebDriverPool,它包含一个WebDriver实例,因此可以在测试之间共享.如果只能在创建时设置语言,我可以为每个语言环境保存一个实例.
总而言之,我想知道我在这里是否遗漏了什么.为什么改变语言这么难?不应该有这样的方法WebDriver.setAcceptLanguages(Locale)吗?
简而言之,我有以下问题:
WebDriver.setAcceptLanguages(Locale)?Tim*_*the 14
我最终创建了一个WebDriverPool,它为WebDriver类型(例如FirefoxDriver.class)和Locale(例如en_US)的每个组合创建一个实例.也许这对某人来说是有用的.
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);
}
}
Run Code Online (Sandbox Code Playgroud)
你也可以通过about:config在firefox中完成.但是你需要使用Actions来操纵它.
下面是一段java代码
Actions act = new Actions(webDriver);
webDriver.get("about:config");
// warning screen
act.sendKeys(Keys.RETURN).perform();
// Go directly to the list, don't use the search option, it's not fast enough
act.sendKeys(Keys.TAB).perform();
// Go to the intl.accept_languages option
act.sendKeys("intl.accept_languages").sendKeys(Keys.RETURN).perform();
// fill the alert with your parameters
webDriver.switchTo().alert().sendKeys("fr, fr-fr, en-us, en");
webDriver.switchTo().alert().accept();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22342 次 |
| 最近记录: |