Web自动化测试二:selenium打开和登录浏览器(火狐、IE、chrome)

案例1:打开火狐浏览器,登录OutLook网页版

public class Test1 {

WebDriver driver=null;
String url="https://exchange.grandsoft.com.cn/owa/auth/logon.aspx?replaceCurrent=1&url=https%3a%2f%2fexchange.grandsoft.com.cn%2fowa%2f";
String username="caom";
String password="123";

public void testEmail(){
//System.setProperty("webdriver.firefox.bin", "C:/Program Files (x86)/Mozilla Firefox/firefox.exe");
driver = new FirefoxDriver();
driver.get(url);         
driver.manage().window().maximize();
WebElement inputname=driver.findElement(By.id("username"));
inputname.sendKeys(username);
WebElement inputpd=driver.findElement(By.id("password"));
inputpd.sendKeys(password);
WebElement btn =driver.findElement(By.xpath("//div[@id='lgnDiv']//img[@class='imgLnk']"));
btn.click();
}

public static void main(String[] args) {
Test1 test1=new Test1();
test1.testEmail();
}
}

案例2:打开谷歌浏览器,登录OutLook网页版
public class Test2 {
WebDriver driver=null;
String url="https://exchange.grandsoft.com.cn/owa/auth/logon.aspx?replaceCurrent=1&url=https%3a%2f%2fexchange.grandsoft.com.cn%2fowa%2f";
String username="caom";
String password="123";

public void testEmail(){
System.setProperty("webdriver.chrome.driver","C:/Users/Administrator/AppData/Local/Google/Chrome/Application/chromedriver.exe");
driver = new ChromeDriver();  
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);  
driver.get(url);         
driver.manage().window().maximize();
WebElement inputname=driver.findElement(By.id("username"));
inputname.sendKeys(username);
WebElement inputpd=driver.findElement(By.id("password"));
inputpd.sendKeys(password);
WebElement btn =driver.findElement(By.xpath("//div[@id='lgnDiv']//img[@class='imgLnk']"));
btn.click();


注意:1、selenium 2.0  只兼容firefox支持45及以下的版本。

            2、从selenium3.0版本开始,需要JRE1.8。

            3、IE和谷歌浏览器需要对应驱动并且驱动版本要和浏览器的版本相匹配,否则会报错。

                   http://npm.taobao.org/mirrors/chromedriver/,此网址可查找每个版本及匹配。

                  谷歌不同版本驱动下载:http://chromedriver.storage.googleapis.com/index.html

猜你喜欢

转载自blog.csdn.net/u011485276/article/details/75332797