java web start ie_Java Selenium3 WebDriver启动火狐、Chrome、IE,Edge浏览器的方法(一)

selenium3和selenium2没有太大的区别,就是精简了一些不用的东西,对浏览器的支持更好了,比如对高版本的浏览器(FireFox,Chrome,Edge等)可以完美的支持,不用受限于版本的问题

下面总结一下启动常用浏览器的方法

前提

安装好java的环境配置

新建一个maven工程

在maven的pom文件加入selenium的依赖包(版本可根据需要自行变更)

org.seleniumhq.seleniumselenium-java3.14.0org.testngtestng6.8.21

一、火狐浏览器

把压缩包里的exe文件放到java项目中,这里用0.27.0版本的

火狐浏览器的版本>=65版本

启动火狐浏览器

方式1:直接启动浏览器

public class OpenBrower {

private static String browerPath=OpenBrower.class.getResource("/files").getPath();

@Test

public void openFireFox() throws IOException {

System.out.println("start seleniium firefox");

//设置FirefoxDriver路径

System.setProperty("webdriver.gecko.driver",browerPath+"/geckodriver.exe");

//初始化firefox浏览器实例

WebDriver driver=new FirefoxDriver();

//打开一个站点

driver.get("https://www.baidu.com/");

//设置隐形等待时间

driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);

//窗口最大化

driver.manage().window().maximize();

driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);

//关闭浏览器

driver.close();

}

}

方式2:通过指定profile启动浏览器

这样启动的好处:带着自己配置好的浏览器设置;查看profile的方法如下

WIN+R快捷键

fbc27299909cfd3d046b7b744c4e3e66.png

如果不知道哪个是默认的,点击启动firefox按钮查看即可,也可以新建profile一个

40a1411d5b495792f4f5b5f17d769f01.png

代码如下:

@Test

public void openFireFox_default() throws Exception {

System.setProperty("webdriver.gecko.driver", browerPath + "/geckodriver.exe");

//启动带插件的火狐浏览器

ProfilesIni pi=new ProfilesIni();

//default-release用户配置文件名

FirefoxProfile profile = pi.getProfile("default-release");

FirefoxOptions options = new FirefoxOptions();

options.setProfile(profile);

//初始化firefox浏览器实例

WebDriver driver = new FirefoxDriver(options);

//打开一个站点

driver.get("https://www.jd.com/");

//设置隐形等待时间

Thread.sleep(3000);

//窗口最大化

driver.manage().window().maximize();

Thread.sleep(3000);

//关闭浏览器

driver.close();

}

selenium3后之前2版本的启动方式不一样

二、Chrome浏览器

把压缩包里的exe文件放到java的目录里

启动浏览器

@Test

public void openChrome() throws Exception {

//System.setProperty("webdriver.chrome.driver", browerPath + "/chromedriver.exe");

System.setProperty("webdriver.chrome.driver", browerPath + "/chromedriver.exe");

//初始化Chrome浏览器实例

WebDriver driver = new ChromeDriver();

//打开一个站点

driver.get("https://www.baidu.com/");

// 设置隐形等待时间

Thread.sleep(5000);

//窗口最大化

driver.manage().window().maximize();

Thread.sleep(5000);

//关闭浏览器

driver.close();

}

三、IE浏览器

把压缩包里的exe文件放到java的目录里

启动IE时,需要关闭如下图4个区域的保护模式:

46a46bc4db818dba76eeaab099f002a5.png

启动浏览器

@Test

public void openIE() throws Exception {

System.setProperty("webdriver.ie.driver", browerPath + "/IEDriverServer.exe");

//初始化浏览器实例

WebDriver driver = new InternetExplorerDriver();

//打开一个站点

driver.get("https://www.jd.com/");

// 设置隐形等待时间

Thread.sleep(3000);

//窗口最大化

driver.manage().window().maximize();

Thread.sleep(3000);

//关闭浏览器

driver.close();

}

四、Edge

@Test

public void openEdge() throws Exception {

System.setProperty("webdriver.edge.driver", browerPath + "/msedgedriver.exe");

//初始化Chrome浏览器实例

WebDriver driver = new EdgeDriver();

//打开一个站点

driver.get("https://www.jd.com/");

// 设置隐形等待时间

Thread.sleep(3000);

//窗口最大化

driver.manage().window().maximize();

Thread.sleep(3000);

//关闭浏览器

driver.close();

}

备注

1、selenium1.0还是 seleniumRC的时候,需要启动selenium-server-standalone包,用来做server。selenium RC通过server来给code和broswer建立通道,同时,该jar包包括我们用得所有的方法。

2、在新版的selenium中,即selenium2.0-webdriver以后,不需要这个selenium-server-standalone这个包了。WebDriver api 会直接和浏览器的native交互,现在我们用selenium-java.jar包来替代。

3、如果使用selenium-grid或者要在远程机器上的浏览器上运行用例,那么就需要selenium-server和selenium-java配合

注:其实selenium-java就是client,他可以和本地浏览器直接交互(通过各个浏览器自身提供的driver)。同时,通过selenium-server,这个client也可以在远程机器的浏览器进行交互。

使用配置文件控制浏览器的完整代码如下

新建一个maven项目

1e9c885bed3f951b9e260e97f48897ca.png

pom文件添加依赖

org.seleniumhq.selenium

selenium-java

3.14.0

org.testng

testng

6.8.21

创建工具类

Base类

ff729160541572c0d733a57ef94d7abc.png

代码

package selenium.framework.demo.util;

/**

* 初始化基础类

*/

public class Base {

//初始化InitProperties配置文件类

static{

new InitProperties();

}

public static void delay(int time) {

try {

Thread.sleep(time*1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

InitProperties类

87bdbd19b1ab8cc446c688b8a6b66e6b.png

代码:

package selenium.framework.demo.util;

import org.testng.annotations.Test;

import java.io.*;

import java.util.HashMap;

import java.util.Map;

import java.util.Properties;

import java.util.Set;

/**

* 初始化配置类

*/

public class InitProperties {

//配置文件的路径

public static final String PFILEPATH = File.separatorChar + "config" + File.separatorChar + "CONFIG.properties";

private InputStreamReader fn = null;

private InputStream in = null;

private Properties config = new Properties();

public static Map mapproperties = new HashMap();

public InitProperties() {

//构造初始配置文件

init();

}

/**

* 初始化Property配置文件,放入系统属性变量中

*/

private void init() {

Logger.Defaultlog("初始化配置文件");

try {

//调用 对象的getClass()方法是获得对象当前的类类型

in = getClass().getClassLoader().getResourceAsStream(PFILEPATH);

config.load(new InputStreamReader(in, "utf-8"));

System.out.println(config.getProperty("WebDriver.Browser.Location"));

if (!config.isEmpty()) {

Set keys = config.keySet();

for (Object key : keys) {

InitProperties.mapproperties.put(key.toString(), config.getProperty(key.toString()));

//系统参数不包含指定的键名且配置文件的键值不为空

if (!System.getProperties().containsKey(key.toString()) && !config.getProperty(key.toString()).isEmpty()) {

//置指定键对值的系统属性

System.setProperty(key.toString(), config.getProperty(key.toString()));

}

}

keys.clear();

}

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

//fn.close();

in.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

// }

/**

* 对外调试使用

*/

public static void showAllSystemProperties() {

Set syskeys = InitProperties.mapproperties.keySet();

for (Object key : syskeys) {

if (System.getProperties().containsKey(key)) {

System.out.println(key.toString() + " " + System.getProperty(key.toString()));

}

}

syskeys.clear();

}

}

WebDriverPlus类

a24681874ae5b554e44e631a57a6d6ce.png

代码:

package selenium.fromework.demo.webdriver;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.support.events.EventFiringWebDriver;

/**

* WebDriver扩展类

*/

public class WebDriverPlus extends EventFiringWebDriver{

public WebDriverPlus(WebDriver driver) {

super(driver);

}

}

Logger类

代码

package selenium.fromework.demo.util;

import org.testng.Reporter;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Date;

/**

* 日志

*/

public class Logger {

//日期格式

private static final DateFormat DATE_FORMAT=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

//日志开关

public static boolean isLog=true;

//框架默认日志开关

public static boolean isDefault=true;

//控制台输出开关

public static boolean isToStandardOut=true;

//日志格式开关

public static boolean isFormat=true;

//命令行信息打印等级(1-5)

public static int verbose=1;

/**

* 日志输出的相关信息

* @param s

* @param level

* @param logToStandardOut

*/

public static void log(String s,int level,boolean logToStandardOut){

if(isLog){

Reporter.log(logPrefix(s),level,logToStandardOut);

}

}

public static void log(String s){

log(s,verbose,isToStandardOut);

}

public static void log(String s,int level){

log(s,verbose,isToStandardOut);

}

public static void log(String s,boolean logToStandardOut){

log(s,verbose,logToStandardOut);

}

/**

* 默认日志

* @param s 日志输出信息

*/

public static void Defaultlog(String s){

if(isLog&&isDefault){

Reporter.log(logPrefix(s),verbose,isToStandardOut);

}

}

/**

* 日志前缀

* @param s

* @return

*/

private static String logPrefix(String s) {

Date logtime = new Date();

if(isFormat) {

return "[" + System.getProperty("Project.Name", "liang") + " "+ DATE_FORMAT.format(logtime) + "] " + s;

}

return s;

}

/**

* 对日志开关的操作

*/

public static void setLog(){

//获得properties 配置文件Logger的值,如果是false

if(System.getProperty("Logger","true").equalsIgnoreCase("false")){

//关闭日志开关

Logger.isLog=false;

}

//Logger.StandardOut的值,如果是true

if (System.getProperty("Logger.StandardOut", "false").equalsIgnoreCase("true")) {

//控制台输出开关打开

Logger.isToStandardOut = true;

}

//如果Logger.FrameWorkOut的值是false

if (System.getProperty("Logger.FrameWorkOut", "true").equalsIgnoreCase("false")) {

//框架默认日志开关关闭

Logger.isDefault = false;

}

//如果Logger.Format的值为false

if (System.getProperty("Logger.Format", "true").equalsIgnoreCase("false")) {

//日志格式开关关闭

Logger.isFormat = false;

}

}

}

DriverBase类

package selenium.framework.demo.webdriver;

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.ie.InternetExplorerDriver;

import selenium.framework.demo.util.Base;

import selenium.framework.demo.util.InitProperties;

import selenium.framework.demo.util.Logger;

import java.util.Set;

import java.util.concurrent.TimeUnit;

/**

* 驱动基础类

*/

public class DriverBase extends Base{

private static WebDriver dr=null;

public static WebDriverPlus driver=null;

//浏览器名称

public static String browser=null;

//设置的等待时间 是针对全局设置

public static int implicitlyWait = 30; //隐式等待时间:秒,针对Driver 每次执行命令的 最长执行时间也可以理解为超时时间

public static int webDriverWait = 30;//显示等待时间:秒,要等到某个元素的出现,等不到就一直等直到超时

public static void launch(){

//获取配置文件中的WebDriver.Browser的值

browser = System.getProperty("WebDriver.Browser");

//获取配置文件中的WebDriver.Browser.Location的值

String browserLocation = System.getProperty("WebDriver.Browser.Location");

/*Set strings = InitProperties.mapproperties.keySet();

for (String string : strings) {

System.out.println("=================="+string+"==================");

}*/

if (browser.equalsIgnoreCase("Firefox")) {

Logger.log("打开Firefox浏览器");

if (browserLocation != null && !browserLocation.isEmpty()) {

//System.setProperty("webdriver.firefox.bin", browserLocation);

System.setProperty("webdriver.gecko.driver", browserLocation);

}

dr = new FirefoxDriver();

} else if (browser.equalsIgnoreCase("Chrome")) {

Logger.log("打开Chrome浏览器");

if (browserLocation != null && !browserLocation.isEmpty()) {

System.setProperty("webdriver.chrome.driver", browserLocation);

}

dr = new ChromeDriver();

}else if(browser.equalsIgnoreCase("Edge")){

Logger.log("打开Edge浏览器");

if (browserLocation != null && !browserLocation.isEmpty()) {

System.setProperty("webdriver.Edge.driver", browserLocation);

}

dr = new InternetExplorerDriver();

} else{

Logger.log("打开IE浏览器");

if (browserLocation != null && !browserLocation.isEmpty()) {

System.setProperty("webdriver.ie.driver", browserLocation);

}

dr = new EdgeDriver();

}

driver = new WebDriverPlus(dr);

//获取配置文件WebDriver.ImplicitlyWait的值并赋值给implicitlyWait

implicitlyWait = Integer.parseInt(System.getProperty("WebDriver.ImplicitlyWait").trim());

//获取配置文件WebDriver.WebDriverWait的值并赋值给webDriverWait

webDriverWait = Integer.parseInt(System.getProperty("WebDriver.WebDriverWait").trim());

Logger.log("设置全局显示等待:" + implicitlyWait);

//设置超时时间

driver.manage().timeouts().implicitlyWait(implicitlyWait, TimeUnit.SECONDS);

//窗口最大化

driver.manage().window().maximize();

}

}

显式等待

显式等待,就是明确的要等到某个元素的出现或者是某个元素的可点击等条件,等不到,就一直等,除非在规定的时间之内都没找到,那么就跳出Exception.抛出异常。这样就>通过回调函数,直接获得了这个WebElement.也就是页面元素.

隐式等待

隐式等待是针对Driver 每次执行命令的 最长执行时间也可以理解为超时时间。WebDriver会进行一个隐式等待,但参数只有时间,这就导致我需要什么元素出现,我不

一定能等到它,得不到某个元素,就延迟一下...

创建测试类

clear工具类并install

21f3ce96a7dc702ca30088e3e4e69c74.png

因为测试类要依赖工具类,所以必须要install才能是否

新建一个maven项目selenium.framework.demo

pom文件

selenium

selenium.framework

1.0-SNAPSHOT

创建配置文件Config.properties文件

############################################################

# WebDriver Configure

############################################################

# (Firefox, Chrome or IE)

WebDriver.Browser=Firefox

WebDriver.Browser.Location=files\\geckodriver.exe

WebDriver.ImplicitlyWait=30

WebDriver.WebDriverWait=30

############################################################

# Log configure

############################################################

#日志开关

Logger = true

#日志格式开关

Logger.Format = true

#框架默认日志开关

Logger.FrameWorkOut = false

#控制台输出开关

Logger.StandardOut = true

浏览器驱动

浏览器驱动

b6afb412668a4c2afccae1c6b547b5ed.png

浏览器工具启动类

package selenium.framework.test.util;

import org.testng.annotations.AfterClass;

import org.testng.annotations.AfterTest;

import org.testng.annotations.BeforeClass;

import org.testng.annotations.BeforeTest;

import selenium.fromework.demo.webdriver.DriverBase;

/**

* 浏览器工具启动类

*/

public class BrowerBase extends DriverBase{

@BeforeTest

public void beforeTestBase() {

launch();

}

@AfterTest(alwaysRun=true)

public void afterTest() {

quit();

}

@BeforeClass

public void beforeBaseClass() {

}

@AfterClass(alwaysRun=true)

public void afterBaseClass() {

}

/**

* quit browser

*/

private void quit() {

if (driver != null) {

driver.quit();

driver = null;

}

}

}

TestOpenBrowser类

package selenium.framework.demo.test;

import org.testng.annotations.Test;

import selenium.framework.demo.util.BrowserBase;

import selenium.framework.demo.util.Logger;

public class TestOpenBrowser extends BrowserBase {

@Test

public void openBrowser(){

Logger.log("打开" + browser + "浏览器");

Logger.log("进入百度页面");

String baseUrl = "https://www.baidu.com/";

driver.get(baseUrl);

delay(2);

Logger.log("关闭" + browser + "浏览器");

}

}

完整的项目结构

63cd6327cb995f42d1b6aacc2e5ce647.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值