❅
❅
❆
❅
❆
❅
❆
❅
❆
❆

  • 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 Cookies on website using Selenium

  • Blog
  • Automation Testing
Handle Cookies on website using Selenium
Video

Handle Cookies on website using Selenium

  • Anh Tester
  • Automation Testing
  • 4666
Anh Tester chia sẻ cho các bạn cách sử dụng Cookies trên website với Selenium để dùng cho case Login không cần nhập lại thông tin, chỉ cần add cookies đã từng login vào là được.

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.

private static String cookiesCurrent;

@Test(priority = 1)
public void testGetCookies() {
    driver.get("https://rise.fairsketch.com/signin");
    driver.findElement(By.xpath("//button[normalize-space()='Sign in']")).click();
    sleep(5);
    //Get cookies current by name
    cookiesCurrent = driver.manage().getCookieNamed("ci_session").getValue();
    System.out.println("Current Cookies: " + cookiesCurrent);
}

@Test(priority = 2)
public void testAddCookies() {
    driver.get("https://rise.fairsketch.com/signin");
    // Add the cookie into current browser context (cookiesCurrent)
    System.out.println("Get Current Cookies: " + cookiesCurrent);
    driver.manage().addCookie(new Cookie("ci_session", cookiesCurrent));
    sleep(3);
    driver.navigate().refresh();
    sleep(5);
}​


Handle Cookies on website using Selenium | Anh Tester

Cách sử dụng Cookies trên website với Selenium để sử dụng phiên đăng nhập

- Bước 1: Đầu tiên chúng ta cần Login với thông tin đầy đủ để lấy Cookie đã đăng nhập dùng cho các case sau.

Handle Cookies on website using Selenium | Anh Tester

private static String cookiesCurrent;

@Test(priority = 1)
public void testGetCookies() {
    driver.get("https://rise.fairsketch.com/signin");
    driver.findElement(By.xpath("//button[normalize-space()='Sign in']")).click();
    sleep(5);
    //Get cookies current by name
    cookiesCurrent = driver.manage().getCookieNamed("ci_session").getValue();
    System.out.println("Current Cookies: " + cookiesCurrent);
}​


Hiện tại dùng hàm getCookieNamed nghĩa là lấy giá trị cookies theo tên key.

Các bạn xem thêm các hàm tại đây

- Bước 2: Lấy giá trị cookie theo tên và lưu vào một biến toàn cục nào đó (cookiesCurrent)

Chổ này tạm thời An lưu vào biến toàn cục. Nhưng về lâu dài thì các bạn nên lưu nó vào file excel, file txt hay properties gì đó để nó không bị mất và dễ dùng cho sau này ở những chổ khác.

- Bước 3: Sử dụng lại cookies trên và add vào website sau khi đi đến website đó

@Test(priority = 2)
public void testAddCookies() {
    driver.get("https://rise.fairsketch.com/signin");
    // Add the cookie into current browser context (cookiesCurrent)
    System.out.println("Get Current Cookies: " + cookiesCurrent);
    driver.manage().addCookie(new Cookie("ci_session", cookiesCurrent));
    sleep(3);
    driver.navigate().refresh();
    sleep(5);
}​

Hàm add cookie theo cấu trúc (key, value)

driver.manage().addCookie(new Cookie("ci_session", cookiesCurrent));

Tại Test thứ 2 thì chúng ta chỉ cần Add cookie vào và nhấn F5 hay Refresh lại trang là thông tin đã hiển thị lên đã đăng nhập với chính account trước đó mà mình đã lấy cookie của nó add vào.

Quá tiện chứ hả. Case này dùng cho các trường hợp kiểm tra phiên đăng nhập hoặc cookies sinh ra đúng không và thời hạn cụ thể như nào, có đúng với các quyền hạn hay không.

Handle Cookies on website using Selenium | Anh Tester


Source code on GitHub:
https://github.com/anhtester/NewFeatureSelenium4/blob/main/src/test/java/AddCookies.java
  • Tags:
  • Selenium
  • Cookies
  • 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