❅
❅
❆
❅
❆
❅
❆
❅
❆
❆

  • 0939206009
  • thaian.it15@gmail.com
  • Facebook
  • Youtube
  • Zalo
Anh Tester Logo
  • Khoá học
    • All Courses
    • Website Testing
    • API Testing
    • Desktop Testing
    • Mobile Testing
    • Programming Language
    • CI/CD for Tester
    • Performance Testing
  • 💥Khai giảng
  • tools
    • TestGenAI - AI Test Cases Generator
    • Mobile Apps Demo
    • Automation Framework Selenium TestNG
    • Automation Framework Cucumber TestNG
    • Gherkin Convert Functions in Katalon
    • Convert object from Selenium Java to Playwright Python
    • Website Demo CRM
    • Website Demo HRM
    • Website Demo HRM GO
    • Website Demo POS
    • Website Demo eCommerce CMS
  • blog
    • Selenium C#
    • Selenium Java
    • Katalon Tools
    • Jenkins CI/CD
    • SQL cho Tester
    • Manual Testing
    • Tài liệu cho Tester
    • Automation Testing
    • akaAT Tools
    • Cucumber TestNG
    • API Testing with Postman
    • Apache Maven
    • AI in Software Testing
    • Lịch khai giảng
  • Liên hệ
  • Log in
    Sign up

Handle Authentication with Selenium 4

  • Blog
  • Automation Testing
Handle Authentication with Selenium 4
Video

Handle Authentication with Selenium 4

  • Anh Tester
  • Automation Testing
  • 3208
Anh Tester chia sẻ cho các bạn cách xử lý Authentication trên website với Selenium 4

Anh Tester sử dụng code Selenium với Java. Các bạn dùng ngôn ngữ khác chỉnh lại cho phù hợp nhen.

Handle Authentication with Selenium 4 | Anh Tester

Ở Selenium 4 thì cách xử lý Authentication chỉ cần copy từ docs của nó là xong. Có hàm sẵn hết. An viết lại truyền parameter cho dễ nhìn.

import com.google.common.util.concurrent.Uninterruptibles;
import org.openqa.selenium.By;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.HasDevTools;
import org.openqa.selenium.devtools.v110.network.Network;
import org.openqa.selenium.devtools.v110.network.model.Headers;
import org.testng.annotations.Test;

import java.time.Duration;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public class HandleAuthentication extends BaseTest {
    @Test
    public void handleAuthentication() {
        // Authentication username & password
        String url = "https://the-internet.herokuapp.com/basic_auth";
        String username = "admin";
        String password = "admin";

        // Get the devtools from the running driver and create a session
        DevTools devTools = ((HasDevTools) driver).getDevTools();
        devTools.createSession();

        // Enable the Network domain of devtools
        devTools.send(Network.enable(
                Optional.of(100000),
                Optional.of(100000),
                Optional.of(100000))
        );
        String auth = username + ":" + password;

        // Encoding the username and password using Base64 (java.util)
        String encodeToString = Base64.getEncoder().encodeToString(auth.getBytes());

        // Pass the network header -> Authorization : Basic <encoded String>
        Map<String, Object> headers = new HashMap<>();
        headers.put("Authorization", "Basic " + encodeToString);
        devTools.send(Network.setExtraHTTPHeaders(new Headers(headers)));

        // Load the application url
        driver.get(url);
        Uninterruptibles.sleepUninterruptibly(Duration.ofSeconds(3));

        String successFullyLoggedInText = driver.findElement(By.xpath("//p")).getText();
        System.out.println(successFullyLoggedInText);
    }
}


Handle Authentication with Selenium 4 | Anh Tester


Source code on GitHub:
https://github.com/anhtester/NewFeatureSelenium4/blob/main/src/test/java/HandleAuthentication.java

  • Tags:
  • Selenium
  • Selenium 4
  • Authentication
  • Tips and Tricks

Chia sẻ bài viết

Facebook Linkedin Telegram Pinterest Share with Zalo Zalo

Cộng đồng Automation Testing Việt Nam

🌱 Facebook Fanpage: Anh Tester
🌱 Telegram
Automation Testing:   Cộng đồng Automation Testing
🌱 
Facebook Group Automation: Cộng đồng Automation Testing Việt Nam
🌱 Telegram
Manual Testing:   Cộng đồng Manual Testing
🌱 
Facebook Group Manual: Cộng đồng Manual Testing Việt Nam

  • Anh Tester

    Anh Tester

    Đường dẫu khó chân vẫn cần bước đi
    Đời dẫu khổ tâm vẫn cần nghĩ thấu

    • Facebook
    • Youtube
    • Zalo

Search Blogs

Related Blogs

🤖 So sánh Playwright JS/TS và Playwright Python

🤖 So sánh Playwright JS/TS và Playwright Python

Aug-29-2025 by Anh Tester
So sánh Playwright và Selenium trong Test Automation

So sánh Playwright và Selenium trong Test Automation

Aug-23-2025 by Anh Tester
🚀 Lộ trình học Automation Tester tại Anh Tester

🚀 Lộ trình học Automation Tester tại Anh Tester

Aug-23-2025 by Anh Tester
Những kỹ năng cần có cho level Senior Automation Tester

Những kỹ năng cần có cho level Senior Automation Tester

Dec-17-2024 by Anh Tester
Những kỹ năng cần có cho level Middle Automation Tester

Những kỹ năng cần có cho level Middle Automation Tester

Dec-17-2024 by Anh Tester
Những kỹ năng cần có cho level Junior Automation Tester

Những kỹ năng cần có cho level Junior Automation Tester

Dec-17-2024 by Anh Tester
Những kỹ năng cần có cho level Fresher Automation Tester

Những kỹ năng cần có cho level Fresher Automation Tester

Dec-17-2024 by Anh Tester
Cách xử lý các Exceptions trong Selenium WebDriver

Cách xử lý các Exceptions trong Selenium WebDriver

May-31-2024 by Anh Tester
Tại sao chúng ta cần Kiểm thử Tự động

Tại sao chúng ta cần Kiểm thử Tự động

May-30-2024 by Anh Tester
How to get HTML5 validation message with Selenium

How to get HTML5 validation message with Selenium

Mar-22-2024 by Anh Tester
view all

Blog Tags

  • Selenium
  • Xpath
  • Locator
  • Jenkins
  • Testing
  • Tester
  • Thuật ngữ
  • Lộ trình
  • Khóa học
  • Mindset
  • QA
  • QC
  • Checklist
  • Website
  • Mobile
  • Question
  • Answer
  • Phỏng vấn
  • Extension
  • Cucumber
  • Gherkin
  • Agile
  • Scrum
  • Document
  • Testing Level
  • Automation Test
  • Test Cases
  • Trường hợp
  • Katalon
  • JMeter
  • Postman
  • API
  • Manual Test
  • Developer

Anh Tester

Anh Tester profile
Đường dẫu khó chân vẫn cần bước đi
Đời dẫu khổ tâm vẫn cần nghĩ thấu

Connect me on

  • Facebook
  • Youtube
  • Zalo


Liên hệ

  • 0939206009
  • thaian.it15@gmail.com
  • Anh Tester
  • Donate for Anh Tester
QR Facebook Group
QR Discord Group

Copyright © 2021-2025 Anh Tester Automation Testing