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);
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值