• 【Java+selenium3】 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.学习后总结,不足之处后续补充修正!

  • 相关阅读:
    Python--面向对象编程
    Python--私有
    Python--格式化cookie为字典类型
    Python--异常处理
    Python--加密小练习
    bzoj 1774: [Usaco2009 Dec]Toll 过路费
    lougu T7983 大芳的逆行板载
    bzoj 1083(&vijos 1190): [SCOI2005]繁忙的都市 && bzoj 1601: [Usaco2008 Oct]灌水
    vijos 1083 小白逛公园
    51nod 1766 树上的最远点对
  • 原文地址:https://www.cnblogs.com/xiaozhaoboke/p/11074562.html
Copyright © 2020-2023  润新知