Selenium2 入门[2] —— 切换FireFox浏览器语言 , 以及浏览器语言验证

关键代码:
   
   
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("intl.accept_languages", locale);
profile.setEnableNativeEvents(true);
WebDriver driver = new FirefoxDriver(profile);
完整代码:
   
   
package com.framework.webdriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
 
public class DriverFactory {
/**
* Create a Firefox driver instance, started with the specified
* preferred language "locale".
*
* @param locale Preferred language of the Firefox instance
* @return Return a Firefox driver instance
*
*/
public static WebDriver getFireFoxDriver(String locale)
{
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("intl.accept_languages", locale);
profile.setEnableNativeEvents(true);
WebDriver driver = new FirefoxDriver(profile);
driver.manage().window().maximize();
return driver;
}
}

测试代码:
   
   
package demo.test;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.framework.webdriver.DriverFactory;
import com.google.common.base.Function;
 
public class testPara {
public WebDriver ffdirver;
public String url;
public String searchText;
@BeforeClass
public void BeforeClass(){
url = "http://www.baidu.com";
searchText = "试一试百度搜索";
//获取Firefox 驱动
ffdirver = DriverFactory.getFireFoxDriver("zh-tw");
}
@Test
public void GoToLink(){
//跳转到url
ffdirver.get(url);
}
}
验证浏览器语言是否切换:   运行case,待浏览器启动后终止运行(或在代码中加入强制等待时间),在浏览器页面打开开发者工具,在Console输入JS代码 print(navigator.language)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值