基於Java+Selenium的WebUI自動化測試框架(六)---瀏覽器初始化


  本篇我們來討論,如何寫一個瀏覽器初始化的類。在寫之前,先思考一下,我們需要一個什么樣的初始化?

  先來看看使用原生的Java + selenium是怎么做的。(以firefox為例)

        System.setProperty("webdriver.gecko.driver", "c:\\geckodriver.exe");
        driver = new FirefoxDriver();

  上面是個典型的例子,System.setProperty("webdriver驅動名",“webdriver的路徑”),然后去new一個新的driver對象。這里引出一個問題,即webdriver的版本問題。

      先貼兩張圖,后續可以繼續更新維護。

ChromeDriver的

下載地址(國內淘寶鏡像):https://npm.taobao.org/mirrors/chromedriver

firefox-geckodriver的

下載路徑:https://github.com/mozilla/geckodriver/releases

再放一個參考網址:http://selenium-release.storage.googleapis.com/index.html

       總之,我們在構建Selenium+WebDriver這套環境的時候,需要注意Selenium,WebDriver以及瀏覽器版本之間的對應關系。筆者自己使用的組合:

selenium-server-standalone-3.9.1

chrome瀏覽器 版本 76.0.3809.132(正式版本)  ------->不小心升級了。。。。chromedriver版本 76.0.3809.126

firefox瀏覽器 63.0.1  ------->geckodriver 版本 0.24.0

PS:Chorme瀏覽器在70版本之后,所使用的chomedriver與瀏覽器版本盡量保持一致。

      好了,說完瀏覽器與WebDriver時間的版本對應,我們就要來着手開始寫瀏覽器初始化的代碼了。

      在黑盒手工測試中,我們經常說“啟動XX瀏覽器輸入XXX網址並打開”,那么瀏覽器初始化,我們就基本定義2個參數。一個是XX瀏覽器,一個XXX是網址。

package webui.xUtils;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Reporter;

public class browserUtil {
    static WebDriver driver;
    static logUtil logs = new logUtil(browserUtil.class);
    @SuppressWarnings("deprecation")
    public static WebDriver setDriver(String browserName,String url) {
        logs.info("讀取執行xml配置的"+browserName+"瀏覽器初始化\n");
        Reporter.log("讀取執行xml配置的"+browserName+"瀏覽器初始化\n");
        switch (browserName) {
        case "firefox":
            //此處設置firefox的webdriver地址
            System.setProperty("webdriver.gecko.driver", ".\\libs\\webdriver\\geckodriver.exe");
            FirefoxProfile profile = new FirefoxProfile();
            //設置成 0 代表下載到瀏覽器默認下載路徑, 設置成 2 則可以保存到指定目錄。
            profile.setPreference("browser.download.folderList", 2);
            profile.setPreference("browser.download.dir", ".\\firefox-download");
            //browser.helperApps.neverAsk.saveToDisk
            //指定要下載頁面的 Content-type 值, “binary/octet-stream” 為文件的類型。
            //下載的文件不同,這里的類型也會有所不一樣。如果不清楚你下載的文件什么類型,請用Fiddler抓包。
            profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.ms-excel");
            profile.setPreference("plugin.state.flash", 2);
            FirefoxOptions options = new FirefoxOptions();
            options.setProfile(profile);
            driver = new FirefoxDriver(options);
            driver.manage().window().maximize();
            //隱式等待
//            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
            logs.info("打開瀏覽器,訪問"+url+"網址!");
            Reporter.log("打開瀏覽器,訪問"+url+"網址!");
            driver.get(url);
            break;
        case "chrome":
            System.setProperty("webdriver.chrome.driver", ".\\libs\\webdriver\\chromedriver.exe");
            driver = new ChromeDriver();
            driver.manage().window().maximize();
            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
            logs.info("打開瀏覽器,訪問"+url+"網址!");
            Reporter.log("打開瀏覽器,訪問"+url+"網址!");
            driver.get(url);
            break;
        case "IE":
            System.setProperty("webdriver.ie.driver", ".\\libs\\webdriver\\IEDriverServer32.exe");
            DesiredCapabilities dc = DesiredCapabilities.internetExplorer();
            dc.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
            dc.setCapability("ignoreProtectedModeSettings", true);
            driver=new InternetExplorerDriver(dc);
            driver.manage().window().maximize();
            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
            logs.info("打開瀏覽器,訪問"+url+"網址!");
            Reporter.log("打開瀏覽器,訪問"+url+"網址!");
            driver.get(url);
            break;
        default:
            break;
        }
        return driver;
    }
    
    public static void quit() {
        driver.quit();
    }
}

     在這里說兩個問題。一個是有關於文件的下載,另外一個是關於IE瀏覽器的設置問題。

     當我們在使用Selenium運行自動化測試時,偶爾需要用到下載功能。但瀏覽器的下載可能會彈出下載窗口,或者下載路徑不是我們想要保存的位置,所以在通過Selenium啟動瀏覽器時需要做相關的設置,將使這些設置在啟動的瀏覽器中生效果。
     

 

   針對Firefox瀏覽器,我們可以參考一下的思路來進行設置。(請參考前面的紅色部分代碼)

   先 new 一個FirefoxProfile()類,通過setPreference 設置瀏覽器下載類型、路徑等。

參數:
     browser.download.folderList
     設置成 0 代表下載到瀏覽器默認下載路徑, 設置成 2 則可以保存到指定目錄。
     browser.download.dir
     用於指定所下載文件的目錄。
     browser.helperApps.neverAsk.saveToDisk
     指定要下載頁面的 Content-type 值, “binary/octet-stream” 為文件的類型。下載的文件不同,這里的類型也會有所不一樣。如果不清楚下載的文件什么類型,請使用Fiddler抓包查看。

   針對chrome瀏覽器,我們可以采用類似的思路。可以參考以下的代碼段:

        String downloadFilepath = "D:\\java";
        HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
        chromePrefs.put("profile.default_content_settings.popups", 0);
        chromePrefs.put("download.default_directory", downloadFilepath);
        ChromeOptions options = new ChromeOptions();
        HashMap<String, Object> chromeOptionsMap = new HashMap<String, Object>();
        options.setExperimentalOption("prefs",chromePrefs);
        options.addArguments("--test-type");
        DesiredCapabilities cap = DesiredCapabilities.chrome();
        cap.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap);
        cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
        cap.setCapability(ChromeOptions.CAPABILITY, options);
        
        WebDriver driver = new ChromeDriver(cap);

        driver.get(url);

參數說明:

       相比較Firefox來說,Chrome的下載默認不會彈出下載窗口的,我們主要是想修改默認的默認下載路徑。
       Chrome的設置看上去要比Firefox復雜一次,不過,你需要關注兩個設置:
            profile.default_content_settings.popups 0 設置為禁止彈出下載窗口
            download.default_directory 設置為文件下載路徑

下一篇我們來繼續關注IE瀏覽器的設置。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM