Jacobvu84 / serenity-pageobject-junit-webdriver

4 stars 1 forks source link

Cách thay đổi cấu hình Chrome Webdriver #47

Open Jacobvu84 opened 5 years ago

Jacobvu84 commented 5 years ago

Hiện tại, trên Selenium javadocs trong mục org.openqa.selenium.chrome.ChromeDriver ta thấy việc sử dụng DesiredCapabilities thay đổi cấu hình ChromeDriver đã bị đánh dấu @Deprecated nghĩa là KHÔNG CÒN DÙNG nữa mà thay vào đó là sẽ sử dụng ChromeOptions. Tham khảo thêm tại Capabilities & ChromeOptions

Đồng thời cũng có rất nhiều sự thay đổi trong bản thân các trình duyệt. Ví dụ: Trước đây việc xử lý popup boxs như là Alert, Confirm hay Prompt mặc định là ingore thì giờ đây nó để mặc định là dismiss and notify nên để handle được các popup này cần phải thay đổi lại giá trị CapabilityType.UNEXPECTED_ALERT_BEHAVIOURignore nếu không cứ có popup hiện lên nó sẽ auto là chọn vào dismiss và đẩy ra thông báo exception là NoAlertPresent....

Mình xin được update thông tin và chia sẻ cho các bạn source code mẫu để khởi tạo ChromeDriver theo cách mới.

Đừng quên theo dõi mình trên fan page hoặc trên youtube channel . Mình sẽ update nhưng thay đổi mới nhất nếu có cho mọi người.

public ChromeDriver getDriver() {

        // Cấu hình Chrome Prefs
    Map<String, Object> chromePrefs = new HashMap<String, Object>();
    chromePrefs.put("download.default_directory", "downLoadDirectory");
    chromePrefs.put("profile.default_content_settings.popups", 0);
    chromePrefs.put("pdfjs.disabled", true);

    final DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    capabilities.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.IGNORE);

    ChromeOptions options = new ChromeOptions();

        // 1. Chrome arguments
    options.addArguments("--start-maximized");
    options.addArguments("--incognito");
    options.addArguments("disable-infobars");

        // 2. Thiết lập Chrome preference
    options.setExperimentalOption("prefs", chromePrefs);

        // 3. Tích hợp sự thay đổi cấu hình WebDriver được cung cấp bởi DesiredCapabilities
    options.merge(capabilities);

    return new ChromeDriver(options);
}
Jacobvu84 commented 5 years ago

Đối với trình duyệt Firefox để thay đổi cấu hình của Gecko Driver

public FirefoxDriver getDriver() {
    // 1 profile
    FirefoxProfile profile = new FirefoxProfile();
    profile.setPreference("browser.startup.homepage", "http://www.google.com");
    //profile.setPreference("browser.privatebrowsing.autostart", true);

    // 2 capaibilities
    DesiredCapabilities capabilities = DesiredCapabilities.firefox();
    capabilities.setCapability(FirefoxDriver.PROFILE, profile);
    capabilities.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.IGNORE);

    // 3. options
    FirefoxOptions options = new FirefoxOptions(capabilities);
    // options.addArguments("--headless");
    // options.addArguments("--width=800");
    // options.addArguments("--height=800");
     options.addArguments("-private");

    // 4. Initialization (constructor ) firefox webdriver 
    eturn new FirefoxDriver(options);
}