如何用webdriver打开一个浏览器,我们常用的浏览器有firefox和IE两种,firefox是selenium支持得比较成熟的浏览器,很多新的特性都会在firefox中体现。但是做页面的测试,启动速度比较慢,启动以后运行速度还是可以接受的。

启动firefox浏览器

新建一个firefoxDriver
如果火狐浏览器没有默认安装在C盘,需要制定其路径

System.setProperty("webdriver.firefox.bin", "D:/Program Files/Mozilla firefox/firefox.exe"); 
WebDriver driver = newFirefoxDriver(); 

启动IE浏览器

//Create a newinstance of the Internet Explorer driver
WebDriver driver = newInternetExplorerDriver ();

启动HtmlUnit浏览器

//Createa new instance of the HtmlUnit driver
WebDriverdriver = new HtmlUnitDriver();

启动Chrome浏览器

 
下载ChromeDriver.exe
//Createa new instance of the Chromedriver

System.setProperty(“webdriver.chrome.driver”, bsPath);
WebDriverdriver = new ChromeDriver();

对web页面进行测试,首先要打开被测试页面的地址(如:http://www.baidu.com),web driver 提供的get方法可以打开一个页面:
// And now use thedriver to visit Google
driver.get(“http://www.baidu.com“);
//也可以调用以下方法
driver.navigate().to(“http://www.baidu.com“);

测试脚本如下
package com.test.ui.demo;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class TestWebDriver {
private WebDriver driver = null;
private String url = “http://www.baidu.com“;
//每个用例执行前会执行该方法
@BeforeMethod
public void startUp(){
//如果firefox没有安装在c盘需要执行下面这句,否则请注释掉
System.setProperty(“webdriver.firefox.bin”, “D:/Program Files/Mozilla firefox/firefox.exe”);
driver = new FirefoxDriver();
}
//每个用例执行后会执行该方法
@AfterMethod
public void tearDown(){
//退出操作
driver.quit();
driver = null;
}
@Test
public void startTest(){
//打开新窗口
driver.get(url);
}
}

 

各种浏览器比较

Webdirver对浏览器的支持HtmlUnit Driver优点:HtmlUnit Driver不会实际打开浏览器,运行速度很快。对于用FireFox等浏览器来做测试的自动化测试用例,运行速度通常很慢,HtmlUnit Driver无疑是可以很好地解决这个问题。
缺点:它对JavaScript的支持不够好,当页面上有复杂JavaScript时,经常会捕获不到页面元素。
使用:
WebDriver driver = new HtmlUnitDriver();

FireFox Driver优点:FireFox Dirver对页面的自动化测试支持得比较好,很直观地模拟页面的操作,对JavaScript的支持也非常完善,基本上页面上做的所有操作FireFox Driver都可以模拟。
缺点:启动很慢,运行也比较慢,不过,启动之后Webdriver的操作速度虽然不快但还是可以接受的,建议不要频繁启停FireFox Driver。
使用:
WebDriver driver = new FirefoxDriver();
Firefox profile的属性值是可以改变的,比如我们平时可能需要通过代理上网,可以这样修改:
FirefoxProfile profile = new FirefoxProfile();

//使用profile

ProfilesIni allProfiles = new ProfilesIni();
    FirefoxProfile profile = allProfiles.getProfile("default");
    driver = new FirefoxDriver(profile);

// 使用代理

profile.setPreference(“network.proxy.type”, 1);

// http协议代理配置

profile.setPreference(“network.proxy.http”, proxyIp);
profile.setPreference(“network.proxy.http_port”, proxyPort);

// 所有协议公用一种代理配置,如果单独配置,这项设置为false,再类似于http的配置

profile.setPreference(“network.proxy.share_proxy_settings”, true);

// 对于localhost的不用代理,这里必须要配置,否则无法和webdriver通讯

profile.setPreference(“network.proxy.no_proxies_on”, “localhost”);

// 以代理方式启动firefox

FirefoxDriver ff  = new FirefoxDriver(profile);

InternetExplorer Driver优点:直观地模拟用户的实际操作,对JavaScript提供完善的支持。
缺点:是所有浏览器中运行速度最慢的,并且只能在Windows下运行,对CSS以及XPATH的支持也不够好。
使用:
WebDriver driver = new InternetExplorerDriver();

 

元素操作

查找元素

使用操作如何找到页面元素Webdriver的findElement方法可以用来找到页面的某个元素,最常用的方法是用id和name查找。下面介绍几种比较常用的方法。
By ID假设页面写成这样:
<input type=”text” name=”userName”  id=”user” />
那么可以这样找到页面的元素:
通过id查找:
WebElement element = driver.findElement(By.id(“user”));
By Name或通过name查找:
WebElement element = driver.findElement(By.name(“userName”));
By XPATH或通过xpath查找:
WebElement element =driver.findElement(By.xpath(“//input[@id='user']“));
By Class Name假设页面写成这样:

<div class=”top”><span>Head</span></div><divclass=”top”><span>HeadName</span></div>
可以通过这样查找页面元素:
List<WebElement>top= driver.findElements(By.className(“top”));

By Link Text假设页面元素写成这样:
<a href=”http://www.baidu.com”>baidu</a>>
那么可以通过这样查找:
WebElement baidu=driver.findElement(By.linkText(“baidu”));

 

输入框传值

输入框(text field or textarea)   找到输入框元素:
WebElement element = driver.findElement(By.id(“passwd-id”));
在输入框中输入内容:
element.sendKeys(“test”);
将输入框清空:
element.clear();
获取输入框的文本内容:
element.getText();

下拉菜单

下拉选择框(Select)找到下拉选择框的元素:
Select select = new Select(driver.findElement(By.id(“select”)));
选择对应的选择项:select.selectByVisibleText(“testName”);

select.selectByValue(“name”);
不选择对应的选择项:
select.deselectAll();
select.deselectByValue(“name”);
select.deselectByVisibleText(“姓名”);
或者获取选择项的值:
select.getAllSelectedOptions();
select.getFirstSelectedOption();

单选框

单选项(Radio Button)找到单选框元素:
WebElement sex=driver.findElement(By.id(“sex”));

选择某个单选项:

sex.click();
清空某个单选项:
sex.clear();

判断某个单选项是否已经被选择:

sex.isSelected();

复选框

多选项(checkbox)多选项的操作和单选的差不多:
WebElement area =driver.findElement(By.id(“area .”));
area .click();
area .clear();
area .isSelected();
area .isEnabled();

按钮

按钮(button)找到按钮元素:
WebElement saveButton = driver.findElement(By.id(“save”));

点击按钮:

saveButton.click();

判断按钮是否enable:

saveButton.isEnabled ();

左右选择框也就是左边是可供选择项,选择后移动到右边的框中,反之亦然。例如:

Select name= new Select(driver.findElement(By.id(“name”)));
name.selectByVisibleText(“hellen”);
WebElement addName=driver.findElement(By.id(“addButton”));
addName.click();

弹出框

弹出对话框(Popup dialogs)Alert alert = driver.switchTo().alert();
alert.accept();
alert.dismiss();
alert.getText();

表单提交

表单(Form)Form中的元素的操作和其它的元素操作一样,对元素操作完成后对表单的提交可以:
WebElement sub= driver.findElement(By.id(“sub”));
sub.click();

sub.submit();//只适合于表单的提交

上传附件

上传文件 (Upload File)上传文件的元素操作:
WebElement picFile = driver.findElement(By.id(“picFile ”));
String filePath = “d:\\report\\600x600x0.jpg”;
picFile .sendKeys(filePath);

多窗口切换

Windows 或 Frames之间的切换

首先切换到默认的frame
driver.switchTo().defaultContent();
切换到某个frame:
driver.switchTo().frame(“leftFrame”);
从一个frame切换到另一个frame:
driver.switchTo().frame(“mainFrame”);
切换到某个window:
driver.switchTo().window(“windowName”);

 

导航

导航 (Navigationand History)打开一个新的页面:
driver.navigate().to(“http://www.baidu.com”);

通过历史导航返回原页面:
driver.navigate().forward();
driver.navigate().back();

Selenium 2(Webdriver)的更多相关文章

  1. Selenium(Webdriver)自动化测试常问问题

    http://blog.sina.com.cn/s/blog_c189e2590102w3bv.html Selenium(Webdriver)自动化测试常问问题 (1)selenium中如何保证操作 ...

  2. Selenium+Java(四)Selenium Xpath元素定位

    前言 关于Selenium元素定位,这是最后一篇博客. Xpath定位可以实现的功能 Selenium+Java(三)Selenium元素定位中讲的定位方式也可以实现,具体要用那种定位方式要根据自己的 ...

  3. 一个简单的selenium实例(一)

    1.selenium RC+Eclipse下载安装 java环境 官网下载地址http://www.oracle.com/technetwork/java/javase/downloads/index ...

  4. Python+selenium常用方法(Webdriver API)

    小编整理了目前学习的Python+selenium常用的一些方法函数,以后有新增再随时更新. 加载浏览器驱动: webdriver.Firefox() 打开页面:get() 关闭浏览器:quit() ...

  5. Python+Selenium笔记(七):WebDriver和WebElement

    (一)  WebDriver WebDriver提供许多用来与浏览器交互的功能和设置,通过WebDriver的功能和一些方法,来实现与浏览器窗口.警告.框架和弹出窗口的交互,它也提供了自动化操作浏览器 ...

  6. Selenium(Webdriver)自动化测试常问到的问题解答(转自:潜龙0318)

    今天朋友问我了几个关于Selenium自动化测试的问题,我看了一下感觉还比较典型.结合我以往自动化测试的经验,给出了一些儿粗浅的答案,希望能帮大家,如果大家有什么好的看法,希望相互交流,相互学习! ( ...

  7. selenium2(WebDriver)环境搭建

    1.安装jdk并配置环境变量: jdk安装jdk下载地址: http://www.oracle.com/technetwork/java/javase/downloads/index.html环境变量 ...

  8. selenium python (四)键盘事件

    #!/usr/bin/python# -*- coding: utf-8 -*-__author__ = 'zuoanvip' #在实际测试过程中,有时候我们需要使用tab键将焦点转移到下一个需要操作 ...

  9. selenium 基础(一)

    selenium安装 pip install selenium selenium操作浏览器原理 早期selenium 1.0 用的selenium RC, 后来selenum2集合了selenium1 ...

随机推荐

  1. “The subscription does not exist” when a distributor primary replica fails over to a replica that does not use the same agent profile

    Symptoms Consider the following scenario: In Microsoft SQL Server 2017, you have a distribution agen ...

  2. Excel公式与函数——每天学一个

    说明(2018-5-29 20:35:53): 1. 根据刘伟的视频讲解进行总结,网上讲Excel公式与函数的貌似就他讲的还不错.在他的微博里看到现在的照片胖了不少,不过还挺帅的,不再是以前那个小屌丝 ...

  3. vue实例-转载

    http://blog.csdn.net/yuanyuanispeak/article/details/73526795

  4. HTTP缓存及其合理使用

    以前以为HTTP缓存是个简单的事,项目中遇到后才发觉关于缓存实践有挺深的学问. from几篇文章详见: 使用 HTTP 缓存:Etag, Last-Modified 与 Cache-Control 合 ...

  5. 【RPC】手撸一个简单的RPC框架实现

      涉及技术   序列化.Socket通信.Java动态代理技术,反射机制   角色   1.服务提供者:运行在服务端,是真实的服务实现类   2.服务发布监听者:运行在RPC服务端,1将服务端提供的 ...

  6. [JVM] IDEA集成VisualVM

    VisualVM是集成命令行JDK工具和轻量级分析功能的可视化工具. 参考: https://blog.csdn.net/qq_22741461/article/details/80451675 ht ...

  7. windows环境下命令打到服务中

    1.正常redis在本地命令行中启动,现在直接在服务中启动(tomcat同理) cmd下命令如下: sc create redis binPath= D:\redis\redis-server.exe ...

  8. JS数组常用方法总结

    JavaScript中创建数组有两种方式 (一)使用 Array 构造函数: var arr1 = new Array(); //创建一个空数组 var arr2 = new Array(20); / ...

  9. adb命令集锦

    adb 是什么? adb工具即Android Debug Bridge(安卓调试桥) tools.它就是一个命令行窗口,用于电脑端与模拟器或者真实设备交互. 常用操作: 把文件发送到android设备 ...

  10. 第一篇:你不一定了解的"推荐系统"

    前言 [推荐系统 - 基础教程]可能是穆晨的所有博文里,最有趣最好玩的一个系列了^ ^. 作为该系列的[入门篇],本文将轻松愉快地向读者介绍推荐系统这项大数据领域中的热门技术. 为什么要有推荐系统? ...