java+selenium+maven+IntelliJ IDEA 搭建簡單的UI自動化測試環境

1. 用IntelliJ IDEA新建一個maven工程html

2. 在pom.xml中添加依賴:java

        <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. 編寫自動化測試腳本(模擬打開瀏覽器並訪問百度首頁)git

注:必須先安裝相應瀏覽器版本的驅動github

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. 附加說明web

(1)IE瀏覽器的驅動---------IEDriverServerchrome

咱們能夠從 http://selenium-release.storage.googleapis.com/index.html 下載,若是該地址打不開,能夠用淘寶的鏡像地址:https://npm.taobao.org/mirrors/selenium/npm

IEDriverServer下載時得注意,你用的是什麼版本的Selenium 就在對應版本里面找IEDriverServer。api

(2)Chrome 瀏覽器驅動---------ChromeDriver瀏覽器

chromedriver下載時也須要下載到匹配的版本,特別是chrome瀏覽器和chromedriver的版本須要匹配。maven

能夠到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 
相關文章
相關標籤/搜索