java+selenium+maven+IntelliJ IDEA 搭建简单的UI自动化测试环境
1. 用IntelliJ IDEA新建一个maven工程
2. 在pom.xml中添加依赖:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.14.0</version>
</dependency> <!-- 与 selenium-java 版本要一致 -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-api</artifactId>
<version>3.14.0</version>
</dependency>
3. 编写自动化测试脚本(模拟打开浏览器并访问百度首页)
注:必须先安装相应浏览器版本的驱动
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.ie.InternetExplorerDriver; public class LoginTest1 {
public static void main(String[] args) { openEdge();
} public static void openFirefox() {
/**
* 打开firefox
*/
FirefoxOptions options = new FirefoxOptions();
options.setBinary("D:\\softwareInstallMenu\\Firefox\\firefox.exe");
System.setProperty("webdriver.gecko.driver", ".\\drivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver(options);
driver.get("http://www.baidu.com");
} /**
* 打开chrome
*/
public static void openChrome() {
System.setProperty("webdriver.chrome.driver", ".\\drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.baidu.com"); } /**
* 打开IE
*/
public static void openIE() {
System.setProperty("webdriver.ie.driver", ".\\drivers\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
driver.get("http://www.baidu.com");
} /**
* 打开Edge
*/
public static void openEdge() {
// 指定MicrosoftWebDriver路径
System.setProperty("webdriver.edge.driver", ".\\drivers\\MicrosoftWebDriver.exe");
//启动 Edge浏览器
WebDriver driver = new EdgeDriver();
driver.get("http://www.baidu.com");
}
}
4. 附加说明
(1)IE浏览器的驱动---------IEDriverServer
我们可以从 http://selenium-release.storage.googleapis.com/index.html 下载,如果该地址打不开,可以用淘宝的镜像地址:https://npm.taobao.org/mirrors/selenium/。
IEDriverServer下载时得注意,你用的是什么版本的Selenium 就在对应版本里面找IEDriverServer。
(2)Chrome 浏览器驱动---------ChromeDriver
chromedriver下载时也需要下载到匹配的版本,特别是chrome浏览器和chromedriver的版本需要匹配。
可以到http://chromedriver.storage.googleapis.com/index.html,国内用户也可以到淘宝npm镜像(http://npm.taobao.org/mirrors/chromedriver)去下载对应版本的chromedriver版本。
(3)Firefox 浏览器驱动----------geckodriver
当火狐的版本<=47时,我们不需要额外的设置。但是如果安装时没有使用默认安装路径,那么和使用默认安装路径在代码处理上会有点不同:
默认安装路径,我们可以直接实例化一个FirefoxDriver,便可:
public static void main(String args[]) {
openFirefoxDef();
}
private static void openFirefoxDef(){
// 实例化 FirefoxDriver, 启动Firefox
WebDriver driver = new FirefoxDriver();
}
如果火狐不是默认安装路径,你需要指定火狐安装路径:
public static void main(String args[]) {
openFireFoxTest();
}
public void openFireFoxTest(){
// 指定firefox 安装路径
System.setProperty("webdriver.firefox.bin","C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
// 启动firefox浏览器
WebDriver driver = new FirefoxDriver();
}
当火狐版本V48+时,那么想启动火狐浏览器,我们得去下载火狐对应的geckodriver。下载地址:https://github.com/mozilla/geckodriver/releases,基本下载最新版便可。那么我看下这时我们如何启动Firefox:
#注意这里只是支持firefox默认安装C盘的情况
public static void main(String args[]) {
openFirefoxByGeck();
}
private static void openFirefoxByGeck() {
// 设置系统变量,并设置 geckodriver 的路径为系统属性值
System.setProperty("webdriver.gecko.driver", ".\\drivers\\geckodriver.exe");
// 实例化 FirefoxDriver
WebDriver driver = new FirefoxDriver();
} #要启动firefox自定义安装位置会报PATH没有firefox二进制文件,处理如下:
FirefoxOptions options = new FirefoxOptions();
options.setBinary("D:\\Firefox\\firefox.exe"); //导入firefox安装路径
System.setProperty("webdriver.gecko.driver","./driver/geckodriver.exe");
driver = new FirefoxDriver(options);
(4)Edge浏览器----------MicrosoftWebDriver
下载地址:https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
java+selenium+maven+IntelliJ IDEA 搭建简单的UI自动化测试环境的更多相关文章
- 搭建基于IDEA+Selenium+Java+TestNG+Maven+Jenkins+SVN的Web端UI自动化测试环境
第一步:工具下载安装配置 JDK安装与配置 IDEA安装与配置 Maven安装与配置 Tomcat部署与配置 Jenkins部署与配置 Svn安装与配置 各浏览器驱动下载与配置 第二步:集成各个工具到 ...
- IDEA+Java:Selenium+Maven+TestNG基本WebUI自动化测试环境搭建
IDEA+java:Selenium+Maven+TestNG 本文介绍的测试环境,应该是最基本的测试环境了,也是很多文章都有写,这里做一个完整的图文配置整理,方便阅读理解! 使用maven的好处,由 ...
- JAVA基础-----Maven项目的搭建
Maven项目的搭建 一.前言 maven官网:http://maven.apache.org/, 文章简介:本文章从三个模块来了解Maven,分别是 Maven的基本概念~, Maven项目的安装和 ...
- 转:windows下使用gvim搭建简单的IDE编译环境(支持C/C++/Python等)
原文来自于:http://www.cnblogs.com/zhuyp1015/archive/2012/06/16/2552269.html 使用gvim在windows环境下搭建简单的IDE环境可以 ...
- 简单Web UI 自动化测试框架 pyse
WebUI automation testing framework based on Selenium and unittest. 基于 selenium 和 unittest 的 Web UI自动 ...
- 【自动化基础】手把手教零基础小白搭建APP的UI自动化环境
前言 帮助零基础小白一步步搭建UI自动化环境,完成Python+Appium+模拟器/真机的UI自动化环境搭建. 环境准备: jdk1.8.0 sdk Node.js appium python Ap ...
- 基于selenium+Python3.7+yaml+Robot Framework的UI自动化测试框架
前端自动化测试框架 项目说明 本框架是一套基于selenium+Python3.7+yaml+Robot Framework而设计的数据驱动UI自动化测试框架,Robot Framework 作为执行 ...
- [java,maven] 使用 maven 来搭建简单的 netty 开发环境
大致过程是: 首先, 使用 mvn 命令在指定路径下面创建一套简单的 java 文件包. 然后, 使用 JIdea 导入 maven 项目的方式将创建好的文件包加载到 IDE 环境中.‘ 接下来, ...
- java+selenium+maven+testng框架(一)安装搭建
1.安装jdk(注意:需配置环境变量,可自行百度方法); 2.安装eclipse; 3.安装maven(注意:需配置环境变量,可自行百度方法); 4.在eclipse中新建maven项目 新建成功 注 ...
随机推荐
- “玲珑杯”线上赛 Round #17 河南专场 B:震惊,99%+的中国人都会算错的问题(容斥计算)
传送门 题意 略 分析 是一道稍微变形的容斥题目,容斥一般的公式 \[ans=\sum_iAi-\sum_{i<j}{Ai∩Aj}+\sum_{i<j<k}{Ai∩Aj∩Ak}+.. ...
- vector刘汝佳算法入门学习笔记
//*****-*-----vector***/////// 常用操作封装,a.size();可以读取大小 a.resize();可以改变大小: ...
- mui中一级联动
<!doctype html><html> <head> <meta charset="utf-8"> <title>& ...
- GraphicsLab Project学习项目
作者:i_dovelemon 日期:2016 / 05 / 30 主题:3D,Graphics 引言 进公司以来,主要在学习的就是如何保证代码的质量,以前热爱的图形学也放置了.但是,作为游戏程序员,特 ...
- try/except/finally
Python也不例外,跟其他高级语言一样,内置了一套try...except...finally...的错误处理机制 当认为某些代码可能会出错时,就可以用try来运行这段代码 使用try时,要么exc ...
- AtCoder Grand Contest 003 E - Sequential operations on Sequence
题目传送门:https://agc003.contest.atcoder.jp/tasks/agc003_e 题目大意 一串数,初始为\(1\sim N\),现有\(Q\)个操作,每次操作会把数组长度 ...
- Bank Hacking CodeForces - 796C
题目 题意: 一条笨狗要去黑银行,银行有n个,它们之间用n-1条边连接.可以选择任意一个银行开始黑,但是后面每一次黑的银行都要求与已经黑过的银行直接相连.每个银行初始有一个防御值,每一个银行被黑后,与 ...
- 模拟+位运算 HDOJ 5491 The Next
题目传送门 题意:意思很简单,找一个最接近D且比D大的数,满足它的二进制表示下的1的个数在[S1, S2]之间 分析:从D + 1开始,若个数小于S1,那么从低位向高位把0替换成1直到S1就是最小值, ...
- html5 input 标签
<!DOCTYPE HTML> <html> <head> <meta http-equiv="content-type" content ...
- JVM补充一
一.为什么废弃永久代(PermGen) 2.1 官方说明 参照JEP122:http://openjdk.java.net/jeps/122,原文截取: Motivation This is part ...