selenium2讀書筆記(二)啓動Chrome

前一篇文章裏面寫的是用driver打開本地的Chrome, 在一個項目中,大家本地的Chrome可能版本不一定相同,所以最好用diver打開同一個Chrome,這個Chrome可以放在工程裏面。

下面是具體的代碼:

public class TestCaseBase {

    private void setUpChrome() throws Exception{

        File fileForDownload = new File(PROPERTIES_RESOURCES.getProperty("DownloadDir"));
        String desiredFilePathForDownload = fileForDownload.getAbsolutePath();  //Chrome的下載文件的存放路徑 


        DesiredCapabilities capability = DesiredCapabilities.chrome();
        capability.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.ACCEPT);

        File chromeDriver = new File("./lib/chromedriver.exe");
        System.setProperty("webdriver.chrome.driver", chromeDriver.getAbsolutePath()); //設置ChromeDriver的property

        HashMap<String, Object> contentPrefs = new HashMap<String, Object>();
        contentPrefs.put("multiple-automati" + "c-downloads", 1);

        HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
        chromePrefs.put("download.default_directory", desiredFilePathForDownload);
        chromePrefs.put("download.prompt_for_download", false);
        chromePrefs.put("profile.default_content_settings", contentPrefs);

        ChromeOptions options = new ChromeOptions();

        File chromeBinary = new File("lib/Google Chrome/chrome.exe");
        options.setBinary(chromeBinary.getAbsolutePath());
        options.setExperimentalOption("prefs", chromePrefs);
        options.addArguments("--test-type");

        capability.setCapability(CapabilityType.ForSeleniumServer.ONLY_PROXYING_SELENIUM_TRAFFIC, true);
        capability.setCapability(ChromeOptions.CAPABILITY, options);
    }


}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章