Làm việc với Notifications popup sử dụng Selenium Webdriver

Chắc hẳn các bạn đã bắt gặp rất nhiều trang web bật hộp thoại thông báo (notification pop-up) khi bạn truy cập vào nó. Những thông báo đó đến từ website và được gọi là “Web push notification”. Chúng ta cùng tìm hiểu về Web push notification và làm cách nào chúng ta có thể automate nó trong nhé!

“Web push notification” là gì?

Là một công nghệ cho phép một trang web giao tiếp nhanh chóng với khách hàng của mình. Tính năng chính của nó là thông báo bật lên trên màn hình của khách hàng ngay cả khi họ đã đóng trang web.

Sau khi họ truy cập trang web đăng ký nhận thông báo (push notifications), họ sẽ nhận được thông báo từ trang web có kích thước vừa phải. Đăng ký xảy ra khi người dùng cho phép trang web gửi thông báo bằng cách nhấp chuột vào button [Allow].

Web push notification hoàn toàn là một hệ thống liên lạc dựa trên quyền người dùng – Vì đăng ký được quản lý bởi người dùng nên có rất ít khả năng xảy ra thư rác.

Dưới đây là ảnh chụp màn hình notification pop-up từ trình duyệt chrome.

  • Sau khi truy cập vào trang web DevOPsify.co, thông báo hiển thị với nội dung “Bạn có muốn nhận cập nhật và mới nhất về & ?”, bạn nhấp chọn button [Cho phép]. Notification pop-up sẽ hiển thị như hình sau

Bạn có thể xem của notification pop-up trong trình duyệt chrome như sau:

Settings -> Privacy and Secutity -> Site Settings -> [Permission] Notifications

Cách xử lý push Notifications popup sử dụng Selenium WebDriver

Để xử lý push notification popup này trong Selenium WebDriver, chúng ta sẽ sử dụng class ChromeOptions.

Đối với Chrome Version từ 50 về trước

Sử dụng phương thức addArguments(“–disable-notifications–“) của class ChromeOptions

Các bước thực hiện:

  1. Khởi tạo một đối tương (object) thuộc ChromeOptions
  2. Sử dụng phương thức addArguments(“–disable-notifications–“);

Code example

package SeleniumWebDriverTutorial;

import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class NotificationPopup {

    public static void main(String[] args) throws InterruptedException {

        //Chrome Browser
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--disable-notifications");	//To disable the notifications based permission popup

        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver(options);
        driver.manage().window().maximize();

        driver.get("https://devopsify.co/tao-selenium-automation-test-voi-maven-intellij/");
        Thread.sleep(3000);

        driver.quit();

    }
}

Đối với Chrome Version từ 50 về sau

Các bước thực hiện:

  1. Khởi tạo một đối tương (object) thuộc ChromeOptions có tên là options
    • Syntax ChromeOptions options = new ChromeOptions();
  2. Khởi tạo một đối tượng (object) thuộc Map với tên prefs ,trong đó các trường key value chấp nhận loại dữ liệu String object và truyền nó tới HashMap.
  3. Cấu hình cho notification cho đối tượng (object) prefs
    • Syntax : prefs.put(“profile.default_content_setting_values.notifications”, 1);
    • Có 3 value
      • 0: mặc định (default) -> Notification popup vẫn sẽ hiển thị
      • 1: Cho phép (Allow) -> Notification popup không còn xuất hiển thị, kiểm tra trong “Setting -> Notifications” của trình duyệt, “Sites can ask to send notifications” được chọn (checked)
      • 2: Không cho phép (Block) -> Notification popup không còn xuất hiển thị, kiểm tra trong “Setting -> Notifications” của trình duyệt, “Don't allow sites to send notifications” được chọn (checked)
  4. Sử dụng phương thức setExperimentalOption để thiết lập các tùy chọn với đối đượng options
    • Syntax: options.setExperimentalOption(“prefs”, prefs);
  5. Sử dụng đối tượng options của class ChromeOptions dểd khởi tạo WebDriver và Web Client
    • Syntax : WebDriver driver = new ChromeDriver(options);

Code example

Sau khi chạy đoạn chương trình trên, cả hai cách trên, bạn sẽ không còn thấy Notification pop-up hiển thị nữa.

Note: Bạn có thể xem thêm các pref-name tại đây.

  • “profile.default_content_setting_values.cookies”;
  • “profile.default_content_setting_values.images”;
  • “profile.default_content_setting_values.javascript”;
  • “profile.default_content_setting_values.”;
  • “profile.default_content_setting_values.popups”;
  • “profile.default_content_setting_values.geolocation”;
  • “profile.default_content_setting_values.notifications”;
  • “profile.default_content_setting_values.auto_select_certificate”;
  • “profile.default_content_setting_values.fullscreen”;
  • “profile.default_content_setting_values.mouselock”;
  • “profile.default_content_setting_values.mixed_script”;
  • “profile.default_content_setting_values.media_stream”;
  • “profile.default_content_setting_values.media_stream_mic”;
  • “profile.default_content_setting_values.media_stream_camera”;
  • “profile.default_content_setting_values.protocol_handlers”;
  • “profile.default_content_setting_values.ppapi_broker”;
  • “profile.default_content_setting_values.automatic_downloads”;
  • “profile.default_content_setting_values.midi_sysex”;
  • “profile.default_content_setting_values.push_messaging”;
  • “profile.default_content_setting_values.ssl_cert_decisions”;
  • “profile.default_content_setting_values.metro_switch_to_desktop”;
  • “profile.default_content_setting_values.protected_media_identifier”;
  • “profile.default_content_setting_values.app_banner”;
  • “profile.default_content_setting_values.site_engagement”;
  • “profile.default_content_setting_values.durable_storage”;

Cheers!

Exit mobile version