• 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/

    需要注意的是根据你系统版本去下载对应的MicrosoftWebDriver,不然会报错。而且下载有两种一种是MicrosoftWebDriver.exe文件,一种是MicrosoftWebDriver.msi文件,如果你下载到的是.msi文件,
    那就双击运行按正常软件安装便可。如果你下载到的是MicrosoftWebDriver.exe文件,那就直接剪切放进项目的drivers文件夹中。
     
    详情请参考:https://www.jianshu.com/p/305ea89b87e9 
     
     
     
  • 相关阅读:
    Kali 查看系统信息的一些命令及查看已安装软件包的命令
    mysql_对于DQL 的简单举例
    java简单分析LinkedList
    java_简单解析ArrayList_iterable
    java_随机密码
    rsync 服务基础配置讲解
    DNS服务器的基础
    NFS服务器配置
    DHCP服务器配置
    VSFTP 配置详解,附带例子
  • 原文地址:https://www.cnblogs.com/janson071/p/10439078.html
Copyright © 2020-2023  润新知