我们已经从前面几篇的文章中基本了解了自动化测试的原理了,那么开始开发我们的第一条自动化测试脚本吧。开发环境配置好了以后,打开集成开发环境Eclipse,新建一个Maven项目:

新建Maven项目以后,把之前录制的那一段Java类放进来,作为maven项目的第一个自动化测试脚本

导入这个脚本后,编译会有很多错误,主要是因为缺少依赖的第三方jar,这里Maven的优势就出来了,打开项目的pom.xml文件,在文件中加入下面这一段:

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
<version>2.47.1</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.47.1</version>
</dependency>
</dependencies>

然后更新一下项目,就会自动下载依赖的第三方jar了,下载完成后,编译通过。下面是完整的代码:

package learn;
import static org.junit.Assert.fail;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class firstScript {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "https://www.baidu.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void test1() throws Exception {
driver.get(baseUrl + "/");
driver.findElement(By.id("kw")).click();
driver.findElement(By.id("kw")).clear();
driver.findElement(By.id("kw")).sendKeys("test");
driver.findElement(By.id("su")).click();
}
@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
private boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
private String closeAlertAndGetItsText() {
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
if (acceptNextAlert) {
alert.accept();
} else {
alert.dismiss();
}
return alertText;
} finally {
acceptNextAlert = true;
}
}
}

上面这段脚本是IDE录制下来的,有一些不必要的方法可以删掉,isElementPresent是查看元素是否显示,isAlertPresent是判断页面上是否有alert窗口弹出,closeAlertAndGetItsText是关闭alert窗口,并获取alert的文本信息。处理后的脚本实际上只有下面一段是有用的:

private WebDriver driver;
private String baseUrl;
@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "https://www.baidu.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void test1() throws Exception {
driver.get(baseUrl + "/");
driver.findElement(By.id("kw")).click();
driver.findElement(By.id("kw")).clear();
driver.findElement(By.id("kw")).sendKeys("test");
driver.findElement(By.id("su")).click();
}
@After
public void tearDown() throws Exception {
driver.quit();

}

针对这条脚本,下面逐行解释一下

@Before  //这个注解是JUnit测试框架提供的一个方法,表示在运行该测试类之前,不管方法放在测试类什么地方,它是最先执行的。
driver = new FirefoxDriver();  //这里的实例化使driver对象指向FirefoxDriver,启动Firefox浏览器。
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);  //这是一个隐性的等待方法,等待浏览器在30秒内启动。
@Test  //也是JUnit的注解方法,表示这是测试的方法对象
driver.get(baseUrl + "/");          //get方法,打开指定的URL地址
driver.findElement(By.id("kw")).click();  //涉及到三个方法,1.findElement是最常用的查找元素的方法,2.By.id通过元素的id来定位需要操作的 元素,所支持的很多,后面再详细说明,3.click点击元素
driver.findElement(By.id("kw")).clear();  //clear清空元素内的所有文本内容
driver.findElement(By.id("kw")).sendKeys("test");  //sendKeys输入指定文本内容到指定元素
driver.findElement(By.id("su")).click();
@After //这个注解也是JUnit测试框架提供的一个方法,表示在运行该测试类后,不管方法放在测试类什么地方,它是最后执行的。
driver.quit(); 关闭浏览器。
每个测试类中只能有一个before和after注解,但是可以有多给@Test注解。

了解了整个脚本的意义以后,我们就可以开始运行脚本,运行之前确保安装了Firefox浏览器。 右键点击测试类 - Run As - JUnit Test,运行完以后会出现JUnit报告,下面这个报告表示测试通过。

上面是一条完整的脚本运行过程,从测试的角度看,这条自动化脚本的用例只有测试步骤,并不是一条完整的用例,就需要对脚本进行处理,加上前提条件,实际结果跟期望结果对比等逻辑。这就需要用到WebDriver的其它方法,也就是需要熟悉Selenium的API了,我们以后再讲。

Eclipse中开发测试脚本相关推荐

  1. c++工程 eclipse导入项目_在Eclipse中开发C/C++项目

    摘要:通过本文你将获得如何在Eclipse平台上开发C/C++项目的总体认识.虽然Eclipse主要被用来开发Java项目,但它的框架使得它很容易实现对其他开发语言的支持.在这篇文章里,你将学会如何使 ...

  2. Openfire3.9.3源代码导入eclipse中开发配置指南(转载)

    看到这篇文章的的网友应该已经安装了jdk,eclipse,我就不在安装这些开发工具上赘述了,附载一下openfire的下载地址:http://www.igniterealtime.org/downlo ...

  3. 使用Eclipse来开发python脚本

    一.安装python 1.访问https://www.python.org/downloads/,可以看到如下图所示界面 2.点击上图的"Download",可以看到如下图所示的界 ...

  4. Openfire3.9.3源代码导入eclipse中开发配置指南

    软件版本: Eclipse:eclipse-jee-indigo-SR2-win32-x86_64 JDK: 1.7 Openfire: 3.9.3 本文将图文介绍如何把openfire(以3.9.3 ...

  5. eclipse中开发python

    2019独角兽企业重金招聘Python工程师标准>>> 步骤 1 安装Python 下载地址 https://www.python.org/downloads/ 2 python f ...

  6. Eclipse中Junit测试中@Before不执行

    场景 在使用Junit进行单元测试时,一部分获取JPA的entityManager的代码将其放在了 @Before标注的方法中,这样每次执行@TEST标注的方法时会首先执行@Before标注的方法. ...

  7. Eclipse中执行Ant脚本出现Could not find the main class的问题及解

    试过了:https://blog.csdn.net/bookroader/article/details/2300337 但是不管用,偶然看到这篇没有直接关系的 https://blog.csdn.n ...

  8. 在eclipse中开发servlet流程

    1.新建一个web工程--Dynamic Web Project ,配置相关设置 2.功能当中编写程序.如; public class HelloServlet extends GenericServ ...

  9. 在Eclipse中搭建Python开发环境

    在Eclipse中搭建Python开发环境 来自: http://hi.baidu.com/hqwfreefly/blog/item/2543181d0afd9604314e150e.html 前言 ...

  10. 研发团队中最合适的开发测试比是多少?

    早在2010年,淘宝网和阿里巴巴B2B联合主办.InfoQ独家社区支持的第二届互联网测试交流大会上,来自Google.Baidu.网易.腾讯.淘宝.阿里巴巴.FreeWheel等公司的测试经理分别分享 ...

最新文章

  1. 三、Flask_会话控制与请求钩子
  2. 如何使用 Python 进行时间序列预测?
  3. C++静态成员变量和成员函数
  4. Java并发—锁的使用及原理
  5. (四) shiro权限与角色
  6. linux清除log日志,linux清除log日志
  7. 超键、候选键、主键、外键区别?
  8. Java 搭建srs流媒体服务器,并使用ffmpeg推流
  9. Ceph OSD简介
  10. 史上最全:Mac搭建Airtest IDE +IOS 测试环境
  11. Android keyevent值中文表
  12. 金仓数据库在 TPCE(dbt5,tpsE)测试框架方面的实践和突破
  13. 注册企业腾讯邮箱需要对GoDaddy设置域名解析
  14. python中__init_subclass__方法用法详解
  15. 操作系统王道考研复习——第一章(计算机系统概述)
  16. Soul网关发布里程碑的2.3.0版本抢先看
  17. 00005在java结果输出_浅谈Java反序列化漏洞原理(案例未完善后续补充)
  18. centos 安装maven
  19. C++信号量实现线程间同步,windows使用SetEvent,linux使用sem_t,QT测试
  20. linux 配置tomcat环境变量

热门文章

  1. Iframe背景透明
  2. matlab中李亚普诺夫方程,李亚普诺夫函数.ppt
  3. 计算机辅助制造期末试题答案,西工大《计算机辅助制造》期末试题2006-2007A答案.doc...
  4. Linux常用软件包
  5. 应用程序无法启动,因为应用程序的并行配置不正确
  6. PDF文件如何转CAD格式?教你几个简单有效的方法
  7. Linux指纹识别程序,指纹识别系统的安装
  8. 计算机设备故障,计算机常见硬件故障及其原因
  9. 当前网络上迅雷各版本实际效果研究报告
  10. php 拉丁文转中文,拉丁文在线翻译_拉丁语在线翻译