How Selenium WebDriver actually works?

Saturday, January 25, 2025
Edit this post


Understanding the architecture of Selenium WebDriver helps us gain a better overview of how this framework operates. This is also one of the automation engineer interview questions I encountered but, unfortunately, I didn't have this knowledge at the time. The diagram above shows that WebDriver operates based on a Client-Server model, with an intermediary component called the JSON Wire Protocol, where REST APIs play a critical role. Detailed explanations can be found below.

Functions of the Selenium Client Library (SCL)

The SCL is on the client side, supporting various programming languages such as Java, Python, .NET, Ruby, JavaScript, etc. For instance, to use the SCL for Java, you can download the libraries in the form of jar files here: https://www.selenium.dev/downloads/, then include these files in your project, or more simply, use Maven https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java/4.27.0 to automatically download and manage these libraries. The commands you often use to interact with elements, such as: driver.get("https://www.giaphi.com"), driver.findElement(), .click(), etc., come from these libraries.

The Role of JSON Wire Protocol (JWP)

The JWP acts as an intermediary between the SCL (client) and the driver + browser (server). It is responsible for sending requests from the SCL to the browser driver's HTTP server via REST APIs. Requests from the client are serialized (converted from object data to JSON format) before being sent to the server, and vice versa, when the server returns responses in JSON format, the JWP deserializes these results before passing them back to the client.

For example, when you write the command driver.get("https://www.giaphi.com/") to navigate to the page "https://www.giaphi.com/".
    1) This command is sent to the JWP to serialize the data into a JSON payload, specifically the URL of the website to navigate to.
    2) The JWP then forwards the command to the ChromeDriver via a REST API with the URL https://localhost:{{DEBUGGING_PORT}}/session/:sessionId/url using the POST method. How to get the DEBUGGING_PORT and sessionId will be presented later in the article. The cURL command for this would look like the following:

curl --location 'http://localhost:18633/session/c826aa2828884e046f5de30cf624694a/url' \
--header 'Content-Type: application/json' \
--data '{
    "url": "https://www.giaphi.com"
}'
    3) Finally, the request is sent by the ChromeDriver to the Chrome browser, executing the navigation to the new URL. The result of the navigation is also returned in the reverse direction: from the browser to the driver, from the driver to the JWP, and from the JWP to the client.

👉Every Selenium command has a corresponding REST API, and you can look up the details of how to use each API through the following link: https://www.selenium.dev/documentation/legacy/json_wire_protocol/

What is WebDriver?

In Selenium, WebDriver is the name of a public interface. Any object assigned to this driver must be an instance of a class that implements this interface. For example:

WebDriver driver = new ChromeDriver();

In the above example, "driver" is the object (instance), and ChromeDriver is a class that implements WebDriver. More precisely, ChromeDriver inherits from ChromiumDriver, ChromiumDriver inherits from RemoteWebDriver, and RemoteWebDriver implements WebDriver. You can see more here: https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/chrome/ChromeDriver.html

Since in this example we are working with the Chrome browser, we use ChromeDriver. Additionally, we also have FirefoxDriver, InternetExplorerDriver, and SafariDriver to work with the respective browsers like Firefox, Internet Explorer, and Safari.

Empirical Verification

At this point, we understand how a Selenium command operates. However, how can we prove that everything I've explained is correct? I created the video below to give you a clearer picture. Basically, my approach is as follows:
- During the browser launch step, I add parameters to log all requests/responses into the chromedriver.log file.
- Run any test case in debug mode; we will obtain the Debugging Port information, then call the API /sessions to get the sessionId.
- With these two pieces of information, we will call the API /session/:sessionId/url to navigate to any website.


I hope this article provides you with useful information about how Selenium operates. If you have any questions or suggestions, please leave a comment below. Have a nice day.

.
Xin vui lòng chờ đợi
Dữ liệu bài viết đang được tải về

💻Nhận hướng dẫn online 1 kèm 1 Automation Test từ cơ bản tới nâng cao (From Zero to Hero) 😁😁😁
Lộ trình gồm 3 phần:
1) Kỹ thuật lập trình và tư duy lập trình cơ bản
2) Nhập môn kiểm thử (Manual Test)
3) Kiểm thử tự động (Automation Test) + Chuẩn bị cho phỏng vấn
* Lộ trình tham khảo khoảng 30 buổi: Xem tại đây (thời gian học thực tế có thể ngắn hơn hoặc dài hơn tùy vào mỗi cá nhân người học)

🎓Đối tượng người học:
- Những bạn bị mất gốc căn bản môn lập trình.
- Những bạn muốn theo con đường kiểm thử (testing), đặc biệt là kiểm thử tự động (Automation Test).

🦘Người hướng dẫn:
- Mình sẽ là người trực tiếp hướng dẫn.
- Nếu là các vấn đề ngoài chuyên môn hoặc sở trường, mình sẽ nhờ các anh chị em khác cũng làm trong ngành.

🤓Giới thiệu:
- Mình đã có hơn 10 năm kinh nghiệm làm IT ở cả trong và ngoài nước. Trong đó 3 năm đầu là làm lập trình viên Java, sau đó bén duyên với mảng Automation Test và theo nghề tới tận bây giờ. Mình được đào tạo chính quy về IT từ một trường Đại học danh tiếng ở TP.HCM (hệ kỹ sư 4 năm rưỡi), có chứng chỉ ISTQB, có thể giao tiếp tốt bằng tiếng Anh và có kinh nghiệm làm việc thực tế ở cả 2 mảng Outsource và Product. Title chính thức của mình là QA Automation Engineer, tuy nhiên, mình vẫn làm những dự án cá nhân chuyên về lập trình ứng dụng như Học Tiếng Anh StreamlineSách Nhạc. Mình là người có thái độ làm việc chuyên nghiệp, chăm chỉ và luôn nhiệt tình trong công việc.

💵Chi phí và hình thức thanh toán:
- Các bạn vui lòng liên hệ qua email songtoigianvn@gmail.com để book nội dung và khung giờ học.
- Mức phí: 150.000đ/buổi, mỗi buổi 60 phút. Có thể thanh toán mỗi lần cho 5 buổi.
- Thanh toán qua Momo, chuyển khoản v.v...
BÌNH LUẬN
© Copyright by CUỘC SỐNG TỐI GIẢN
Loading...