Java+selenium+Firefox/ IE/ Chrome主流浏览器自动化环境搭建

Posted xiaozhaoboke

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java+selenium+Firefox/ IE/ Chrome主流浏览器自动化环境搭建相关的知识,希望对你有一定的参考价值。

一、java+selenium+firefox

1.环境准备:JDK1.8

2.安装firefox浏览器v59

3.下载驱动:https://github.com/mozilla/geckodriver/releases

技术图片

4.selenium依赖pom.xml导入:https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java

   selenium 历史版本镜像下载地址:https://npm.taobao.org/mirrors/selenium

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.5.1</version>
</dependency>

5.代码实现

package cn.test;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class FirefoxDemo01 

    public static void main(String[] args) 
        WebDriver driver=null;
        try 
            //自定义安装Firefox路径
            System.setProperty("webdriver.firefox.bin", "D:\\\\Firefox59\\\\App\\\\Firefox\\\\firefox.exe");
            //存放Firefox驱动的路径
            System.setProperty("webdriver.gecko.driver", "src/test/resources/geckodriver.exe");
            driver = new FirefoxDriver();
            driver.get("https://www.baidu.com/");
         catch (Exception e) 
            e.printStackTrace();
        finally 
            driver.quit();
            System.out.println("执行结束,关闭浏览器");
        
    

二、java+selenium+IE

1.JDK1.8已安装

2.selenium3.51 依赖包上述已经导入;

3.下载IE驱动地址:https://npm.taobao.org/mirrors/selenium/3.5/

技术图片

4.代码实现

package cn.test;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class IEDemo02 
    public static void main(String[] args) 
        WebDriver driver = null;
        try 
            //指定IEDriver的存放路径
            System.setProperty("webdriver.ie.driver", "src/test/resources/IEDriverServer.exe");
            //创建Capabilities对象
            DesiredCapabilities capabilities = new DesiredCapabilities();
            //浏览器安全模式保持一致(通过代码忽略设置安全模式保持一致)
            capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
            //浏览器缩放比例保持正常(忽略浏览器缩放比例设置)
            capabilities.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);
            driver = new InternetExplorerDriver(capabilities);
            driver.get("https://www.baidu.com");
            Thread.sleep(3000);
         catch (Exception e) 
            e.printStackTrace();
        finally 
            //关闭浏览器
            driver.quit();
            System.out.println("执行结束,关闭IE浏览器");
        
        
    

技术图片

5.常见报错信息

(1) IE驱动路径未指定,导致找不到IE驱动报错如下:

技术图片

解决方法如下:

//指定IEDriver的存放路径
System.setProperty("webdriver.ie.driver", "src/test/resources/IEDriverServer.exe");

2.IE 安全模式设置

org.openqa.selenium.SessionNotCreatedException: Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones. Enable Protected Mode must be set to the same value (enabled or disabled) for all zones.
//百度翻译仅供参考
org.openqa.selenium。启动Internet Explorer SessionNotCreatedException:意想不到的错误。 保护模式设置为所有区域是不一样的。启用保护模式必须设置为相同的值为所有区域(启用或禁用)。

技术图片

 解决方法如下:

//创建Capabilities对象
DesiredCapabilities capabilities = new DesiredCapabilities();
//浏览器安全模式保持一致(通过代码忽略设置安全模式保持一致)
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
WebDriver driver = new InternetExplorerDriver(capabilities);

3.浏览器的缩放比例保持正常

org.openqa.selenium.SessionNotCreatedException: Unexpected error launching Internet Explorer. Browser zoom level was set to 111%. It should be set to 100%
//通过360浏览器翻译,仅供参考
org.openqa.selenium。启动Internet Explorer SessionNotCreatedException:意想不到的错误。浏览器缩放级别设置为111%。它应该被设置为100%

技术图片

解决方法如下:

//浏览器缩放比例保持正常(忽略浏览器缩放比例设置)
capabilities.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);
driver = new InternetExplorerDriver(capabilities);

三、java+selenium+chrome

1.JDK1.8已安装

2.selenium 3.5.1 依赖已经导入

3.下载chrome驱动:https://npm.taobao.org/mirrors/chromedriver

    我的chrome是v53. 所以我下载 v2.25驱动

     技术图片

技术图片

  注意:Chrome浏览器版本与ChromeDriver版本映射表查看链接:https://www.cnblogs.com/xiaozhaoboke/p/10107349.html

4.代码实现:

package cn.test;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class ChromeDemo03 
    public static void main(String[] args) throws InterruptedException 
        //自定义安装chrome路径,默认安装目录不需要配置此项
        //System.setProperty("webdriver.chrome.bin", "xxx");
        //指定chromeDriver存放路径
        System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.baidu.com");
        Thread.sleep(3000);
        driver.quit();
        System.out.println("关闭浏览器,执行结束!");
        
    

常见报错:

技术图片

Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://chromedriver.storage.googleapis.com/index.html
//解决办法
System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");

四、引入testNG整合三大浏览器代码示例:

 1.testng导入依赖

<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.9.10</version>
    <scope>test</scope>
</dependency> 

2.新建一个父类BaseCase来实现浏览器的前期准备工作

package cn.test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class BaseCase 
    public static WebDriver driver =null;
    /**根据提供的浏览器类型初始化驱动
     * @param browserType
     */
    @Parameters(value= "browserType")
    @BeforeSuite
    public void init(String browserType) 
        //firefox
        if("firefox".equalsIgnoreCase(browserType)) 
            System.setProperty("webdriver.firefox.bin", "D:\\\\Firefox59\\\\App\\\\Firefox\\\\firefox.exe");
            System.setProperty("webdriver.gecko.driver", "src/test/resources/geckodriver.exe");
            driver = new FirefoxDriver();
        else if("chrome".equalsIgnoreCase(browserType)) 
            //chrome
            System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");
            driver = new ChromeDriver();
        else if("ie".equalsIgnoreCase(browserType)) 
            //ie
            System.setProperty("webdriver.ie.driver", "src/test/resources/IEDriverServer.exe");
            DesiredCapabilities capabilities = new DesiredCapabilities();
            capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
            capabilities.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);
            driver = new InternetExplorerDriver(capabilities);
        
    
    @AfterSuite
    public void over() 
        driver.quit();
        System.out.println("执行结束,关闭浏览器!");
    

3.testng.xml来配置参数,实现浏览器的选择性执行

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
  <test name="Test">
    <classes>
      <class name="cn.test.AutoDemo01"/>
    </classes>
  </test> <!-- Test -->
  <!-- 在这里来配置使用浏览器的类型,如下 -->
  <parameter name="browserType" value="firefox"></parameter>
</suite> <!-- Suite -->

4.执行用例AutoDemo01,直接继承父类来实现浏览器驱动的初始化

package cn.test;
import org.openqa.selenium.By;
import org.testng.annotations.Test;
import cn.test.BaseCase;
public class AutoDemo01 extends BaseCase @Test public void test() throws InterruptedException driver.get("https://www.baidu.com"); driver.findElement(By.id("kw")).sendKeys("开始编写自动化脚本来执行测试用例"); Thread.sleep(3000);

5.执行TestNG Suite

技术图片

6.学习后总结,不足之处后续补充修正!

 

以上是关于Java+selenium+Firefox/ IE/ Chrome主流浏览器自动化环境搭建的主要内容,如果未能解决你的问题,请参考以下文章

selenium webdriver 启动三大浏览器Firefox,Chrome,IE

Selenium多浏览器处理 (Chrome/Firefox/IE)

等待在 Firefox 浏览器中不起作用(Webdriver selenium 2.0 +Java)

Selenium2+Java自动化-启动浏览器

Python+Selenium自动化测试框架-打开和关闭浏览器(Firefox/IE/Chrome)

Selenium 2.0b3 IE WebDriver,点击不触发