Selenium lấy tiêu đề phản hồi python

Mở rộng các lớp Selenium WebDriver để bao gồm chức năng yêu cầu từ thư viện Yêu cầu, đồng thời thực hiện tất cả các cookie cần thiết và xử lý tiêu đề yêu cầu

Trước khi yêu cầu thực tế được thực hiện, một máy chủ HTTP cục bộ được khởi động để phục vụ một yêu cầu duy nhất do phiên bản webdriver thực hiện để nhận các tiêu đề yêu cầu HTTP "tiêu chuẩn" được gửi bởi webdriver này; . Các cookie do phiên bản webdriver nắm giữ sẽ được thêm vào các tiêu đề yêu cầu và những cookie được trả về trong phản hồi sẽ tự động được đặt cho phiên bản webdriver

Đặc trưng

  • Xác định và gửi các tiêu đề HTTP mặc định (Tác nhân người dùng, v.v. ) cho WebDriver đã chọn
  • Quản lý cookie hai chiều giữa các yêu cầu và Selenium
  • Chuyển sang xử lý cửa sổ hiện có hoặc tạm thời tạo chúng để hoạt động với cookie của trình điều khiển web khi thực hiện yêu cầu
  • Tất cả các hoạt động giữ nguyên trạng thái ban đầu của WebDriver (tay cầm cửa sổ đang hoạt động và tay cầm cửa sổ)
  • Đã thử nghiệm để hoạt động với Selenium (v4. 1. 0) bằng Mozilla Firefox (v97. 0) và Chromium (v98. 0. 4758. 80)

Cách sử dụng

# Import any WebDriver class that you would usually import from
# selenium.webdriver from the seleniumrequests module
from seleniumrequests import Firefox

# Simple usage with built-in WebDrivers:
webdriver = Firefox()
response = webdriver.request('GET', 'https://www.google.com/')
print(response)


# More complex usage, using a WebDriver from another Selenium-related module:
from seleniumrequests.request import RequestsSessionMixin
from someothermodule import CustomWebDriver


class MyCustomWebDriver(RequestsSessionMixin, CustomWebDriver):
    pass


custom_webdriver = MyCustomWebDriver()
response = custom_webdriver.request('GET', 'https://www.google.com/')
print(response)

Cài đặt

# A response interceptor takes two args which
# then allows to tinker with the response
def interceptor(request, response):  
    if request.url == 'https://server.com/some/path':
        response.headers['New-Header'] = 'Some Value'
driver.response_interceptor = interceptor
driver.get(<URL_where_to_modify_the_header)

# Responses from https://server.com/some/path will now contain 
# the New-Header
8

Trình điều khiển web từ xa

Khi sử dụng

# A response interceptor takes two args which
# then allows to tinker with the response
def interceptor(request, response):  
    if request.url == 'https://server.com/some/path':
        response.headers['New-Header'] = 'Some Value'
driver.response_interceptor = interceptor
driver.get(<URL_where_to_modify_the_header)

# Responses from https://server.com/some/path will now contain 
# the New-Header
9, rất có khả năng máy chủ proxy HTTP do
# A response interceptor takes two args which
# then allows to tinker with the response
def interceptor(request, response):  
    if request.url == 'https://server.com/some/path':
        response.headers['New-Header'] = 'Some Value'
driver.response_interceptor = interceptor
driver.get(<URL_where_to_modify_the_header)

# Responses from https://server.com/some/path will now contain 
# the New-Header
0 tạo ra không chạy trên cùng một máy. Theo mặc định, webdriver sẽ cố gắng truy cập máy chủ proxy trong
# A response interceptor takes two args which
# then allows to tinker with the response
def interceptor(request, response):  
    if request.url == 'https://server.com/some/path':
        response.headers['New-Header'] = 'Some Value'
driver.response_interceptor = interceptor
driver.get(<URL_where_to_modify_the_header)

# Responses from https://server.com/some/path will now contain 
# the New-Header
1. Điều này có thể được thay đổi bằng cách chuyển đối số
# A response interceptor takes two args which
# then allows to tinker with the response
def interceptor(request, response):  
    if request.url == 'https://server.com/some/path':
        response.headers['New-Header'] = 'Some Value'
driver.response_interceptor = interceptor
driver.get(<URL_where_to_modify_the_header)

# Responses from https://server.com/some/path will now contain 
# the New-Header
2 với IP hoặc tên máy chủ chính xác tới webdriver

Chúng tôi có thể kiểm tra các tiêu đề phản hồi HTTP bằng Selenium webdriver. Để xác minh tiêu đề HTTP, chúng tôi sẽ nhận được phản hồi từ một nguồn. Một số mã phản hồi HTTP được liệt kê bên dưới -

  • 5XX − Thể hiện mối quan tâm về máy chủ

  • 4XX - Đại diện cho các vấn đề trong phát hiện tài nguyên

  • 3XX - Đại diện cho chuyển hướng phản hồi

  • 2XX − Đại diện cho ocde chính xác

Lớp HttpURLConnection được sử dụng để lấy mã phản hồi HTTP. Để có một liên kết đến một URL, phương thức openConnection được sử dụng. Sau đó, chúng ta phải sử dụng phương thức setRequestMethod và chuyển HEAD làm tham số cho nó

Phương thức kết nối được áp dụng trên đối tượng của lớp HttpURLConnection. Cuối cùng, phương thức getResponseCode được sử dụng để lấy mã phản hồi

Nafees là một sinh viên IT ham học hỏi những điều mới. Phương châm của anh ấy là “luôn sẵn sàng học hỏi, quên đi và học lại. ” Tìm hiểu thêm về Nafees ↬

Bản tin email

Email (đập vỡ) của bạn

Mẹo hàng tuần về giao diện người dùng & UX.
Được hơn 200.000 người tin cậy.

  • Selenium lấy tiêu đề phản hồi python
    Giao diện người dùng SmashingConf 2023

  • Selenium lấy tiêu đề phản hồi python
    Thiết kế sản phẩm tốt hơn Masterclass

  • Selenium lấy tiêu đề phản hồi python
    Xem trực tiếp vào tháng 12. thứ 8

  • Selenium lấy tiêu đề phản hồi python
    Xây dựng email HTML hiện đại

  • Selenium lấy tiêu đề phản hồi python
    SmashingConf San Francisco. Tất cả về Frontend. Ngày 23–26 tháng 5 năm 2023

Để có thể sửa đổi các tiêu đề trong môi trường thử nghiệm là một điều tuyệt vời để có. Nó cho phép kiểm soát ứng dụng của bạn vì người ta có thể bỏ qua xác thực, đặt cookie, v.v. Trong bài viết này, Nafees Nehar khám phá một số phương pháp cho phép sửa đổi tiêu đề trong thiết lập thử nghiệm tự động hóa

Có nhiều phương pháp khác nhau để sửa đổi tiêu đề. Bạn có thể sửa đổi tiêu đề bằng cách sử dụng tiện ích mở rộng trình duyệt hoặc ứng dụng proxy (chẳng hạn như Charles và Proxyman) chặn yêu cầu và cho phép bạn sửa đổi phản hồi bằng cách thêm tiêu đề

Nhưng trước tiên, hãy bắt đầu từ đầu

Tiêu đề (các) HTTP là gì?

Tiêu đề HTTP(s) là các cặp khóa-giá trị có thể được sử dụng bởi máy khách hoặc máy chủ để truyền thông tin bổ sung cùng với yêu cầu hoặc phản hồi HTTP(s). Họ giữ thông tin bổ sung về dữ liệu được gửi. Tiêu đề (các) HTTP bao gồm tên không phân biệt chữ hoa chữ thường, theo sau là dấu hai chấm (

# A response interceptor takes two args which
# then allows to tinker with the response
def interceptor(request, response):  
    if request.url == 'https://server.com/some/path':
        response.headers['New-Header'] = 'Some Value'
driver.response_interceptor = interceptor
driver.get(<URL_where_to_modify_the_header)

# Responses from https://server.com/some/path will now contain 
# the New-Header
3), sau đó là giá trị của nó. Tên trường phân biệt chữ hoa chữ thường nhưng giá trị trường phân biệt chữ hoa chữ thường. Một tiêu đề có thể có nhiều giá trị được phân tách bằng dấu phẩy

Sửa đổi tiêu đề. Điều này có thể hữu ích như thế nào?

Tôi đang làm việc trên một ứng dụng mở trang web được cung cấp và sau đó cung cấp cho người dùng tùy chọn để sửa đổi các thành phần, thêm sự kiện, thêm phân tích, v.v. mà không cần bất kỳ mã. Điều này đã được thực hiện bằng cách tải trang trong

# A response interceptor takes two args which
# then allows to tinker with the response
def interceptor(request, response):  
    if request.url == 'https://server.com/some/path':
        response.headers['New-Header'] = 'Some Value'
driver.response_interceptor = interceptor
driver.get(<URL_where_to_modify_the_header)

# Responses from https://server.com/some/path will now contain 
# the New-Header
4 và cung cấp các tùy chọn khác nhau cho người dùng ở đầu trang. Tôi đã thử với vô số trang web để xem cách chúng hoạt động trong
# A response interceptor takes two args which
# then allows to tinker with the response
def interceptor(request, response):  
    if request.url == 'https://server.com/some/path':
        response.headers['New-Header'] = 'Some Value'
driver.response_interceptor = interceptor
driver.get(<URL_where_to_modify_the_header)

# Responses from https://server.com/some/path will now contain 
# the New-Header
4. Tôi quan sát thấy rằng hầu hết các trang web không hoạt động trong
# A response interceptor takes two args which
# then allows to tinker with the response
def interceptor(request, response):  
    if request.url == 'https://server.com/some/path':
        response.headers['New-Header'] = 'Some Value'
driver.response_interceptor = interceptor
driver.get(<URL_where_to_modify_the_header)

# Responses from https://server.com/some/path will now contain 
# the New-Header
4 do tiêu đề
# A response interceptor takes two args which
# then allows to tinker with the response
def interceptor(request, response):  
    if request.url == 'https://server.com/some/path':
        response.headers['New-Header'] = 'Some Value'
driver.response_interceptor = interceptor
driver.get(<URL_where_to_modify_the_header)

# Responses from https://server.com/some/path will now contain 
# the New-Header
7 và
# A response interceptor takes two args which
# then allows to tinker with the response
def interceptor(request, response):  
    if request.url == 'https://server.com/some/path':
        response.headers['New-Header'] = 'Some Value'
driver.response_interceptor = interceptor
driver.get(<URL_where_to_modify_the_header)

# Responses from https://server.com/some/path will now contain 
# the New-Header
0

Hầu hết tất cả các trang web đều có tiêu đề

# A response interceptor takes two args which
# then allows to tinker with the response
def interceptor(request, response):  
    if request.url == 'https://server.com/some/path':
        response.headers['New-Header'] = 'Some Value'
driver.response_interceptor = interceptor
driver.get(<URL_where_to_modify_the_header)

# Responses from https://server.com/some/path will now contain 
# the New-Header
1 được đặt thành
# A response interceptor takes two args which
# then allows to tinker with the response
def interceptor(request, response):  
    if request.url == 'https://server.com/some/path':
        response.headers['New-Header'] = 'Some Value'
driver.response_interceptor = interceptor
driver.get(<URL_where_to_modify_the_header)

# Responses from https://server.com/some/path will now contain 
# the New-Header
2 hoặc
# A response interceptor takes two args which
# then allows to tinker with the response
def interceptor(request, response):  
    if request.url == 'https://server.com/some/path':
        response.headers['New-Header'] = 'Some Value'
driver.response_interceptor = interceptor
driver.get(<URL_where_to_modify_the_header)

# Responses from https://server.com/some/path will now contain 
# the New-Header
3 do đó trình duyệt không cho phép tải trang web trong
# A response interceptor takes two args which
# then allows to tinker with the response
def interceptor(request, response):  
    if request.url == 'https://server.com/some/path':
        response.headers['New-Header'] = 'Some Value'
driver.response_interceptor = interceptor
driver.get(<URL_where_to_modify_the_header)

# Responses from https://server.com/some/path will now contain 
# the New-Header
4 hoặc không cho phép khi bất kỳ yêu cầu nguồn gốc chéo nào cố tải trang web đó trong một
# A response interceptor takes two args which
# then allows to tinker with the response
def interceptor(request, response):  
    if request.url == 'https://server.com/some/path':
        response.headers['New-Header'] = 'Some Value'
driver.response_interceptor = interceptor
driver.get(<URL_where_to_modify_the_header)

# Responses from https://server.com/some/path will now contain 
# the New-Header
4. Ngoài ra, tiêu đề
# A response interceptor takes two args which
# then allows to tinker with the response
def interceptor(request, response):  
    if request.url == 'https://server.com/some/path':
        response.headers['New-Header'] = 'Some Value'
driver.response_interceptor = interceptor
driver.get(<URL_where_to_modify_the_header)

# Responses from https://server.com/some/path will now contain 
# the New-Header
0 có chỉ thị
# A response interceptor takes two args which
# then allows to tinker with the response
def interceptor(request, response):  
    if request.url == 'https://server.com/some/path':
        response.headers['New-Header'] = 'Some Value'
driver.response_interceptor = interceptor
driver.get(<URL_where_to_modify_the_header)

# Responses from https://server.com/some/path will now contain 
# the New-Header
7 ngăn chặn điều này

Việc tải trang trong

# A response interceptor takes two args which
# then allows to tinker with the response
def interceptor(request, response):  
    if request.url == 'https://server.com/some/path':
        response.headers['New-Header'] = 'Some Value'
driver.response_interceptor = interceptor
driver.get(<URL_where_to_modify_the_header)

# Responses from https://server.com/some/path will now contain 
# the New-Header
4 rất quan trọng, tôi đang lang thang trên mạng để tìm cách tải nó. Rõ ràng là tôi cần ghi đè tiêu đề
# A response interceptor takes two args which
# then allows to tinker with the response
def interceptor(request, response):  
    if request.url == 'https://server.com/some/path':
        response.headers['New-Header'] = 'Some Value'
driver.response_interceptor = interceptor
driver.get(<URL_where_to_modify_the_header)

# Responses from https://server.com/some/path will now contain 
# the New-Header
1 thành
require("chromedriver");
const { Builder } = require("selenium-webdriver");
const chrome = require("selenium-webdriver/chrome");
const { getRequestlyExtension, importRequestlySharedList } = require("@requestly/selenium");

const options = new chrome.Options().addExtensions(getRequestlyExtension("chrome"));
const driver = new Builder()
    .forBrowser("chrome")
    .setChromeOptions(options)
    .build();

// Imports Rules in Selenium using Requestly sharedList feature
// importRequestlySharedList(driver, <sharedList_URL>);

importRequestlySharedList(driver, 'https://app.requestly.io/rules/#sharedList/1626984924247-Adding-Headers-Example');
0 hoặc xóa hoàn toàn tiêu đề đó. Đó là khi tôi tình cờ thấy tiện ích mở rộng
require("chromedriver");
const { Builder } = require("selenium-webdriver");
const chrome = require("selenium-webdriver/chrome");
const { getRequestlyExtension, importRequestlySharedList } = require("@requestly/selenium");

const options = new chrome.Options().addExtensions(getRequestlyExtension("chrome"));
const driver = new Builder()
    .forBrowser("chrome")
    .setChromeOptions(options)
    .build();

// Imports Rules in Selenium using Requestly sharedList feature
// importRequestlySharedList(driver, <sharedList_URL>);

importRequestlySharedList(driver, 'https://app.requestly.io/rules/#sharedList/1626984924247-Adding-Headers-Example');
1 cung cấp cho tôi tính năng sửa đổi tiêu đề
# A response interceptor takes two args which
# then allows to tinker with the response
def interceptor(request, response):  
    if request.url == 'https://server.com/some/path':
        response.headers['New-Header'] = 'Some Value'
driver.response_interceptor = interceptor
driver.get(<URL_where_to_modify_the_header)

# Responses from https://server.com/some/path will now contain 
# the New-Header
1 bằng cách khớp với URL của trang và do đó cho phép tôi ghi đè tiêu đề
# A response interceptor takes two args which
# then allows to tinker with the response
def interceptor(request, response):  
    if request.url == 'https://server.com/some/path':
        response.headers['New-Header'] = 'Some Value'
driver.response_interceptor = interceptor
driver.get(<URL_where_to_modify_the_header)

# Responses from https://server.com/some/path will now contain 
# the New-Header
1 khi gỡ lỗi

Đó là lần đầu tiên tôi được chứng kiến ​​sức mạnh của tiêu đề mạng. Họ mang dữ liệu về dữ liệu được truyền. Khả năng sửa đổi các tiêu đề của lưu lượng truy cập đi qua trình duyệt của bạn là một công cụ tuyệt vời để có. Bên cạnh việc ghi đè

# A response interceptor takes two args which
# then allows to tinker with the response
def interceptor(request, response):  
    if request.url == 'https://server.com/some/path':
        response.headers['New-Header'] = 'Some Value'
driver.response_interceptor = interceptor
driver.get(<URL_where_to_modify_the_header)

# Responses from https://server.com/some/path will now contain 
# the New-Header
1, bạn có thể xóa tiêu đề để giảm thiểu theo dõi trực tuyến, ghi đè tiêu đề
# A response interceptor takes two args which
# then allows to tinker with the response
def interceptor(request, response):  
    if request.url == 'https://server.com/some/path':
        response.headers['New-Header'] = 'Some Value'
driver.response_interceptor = interceptor
driver.get(<URL_where_to_modify_the_header)

# Responses from https://server.com/some/path will now contain 
# the New-Header
0, trang web thử nghiệm trong sản xuất, v.v.

Trong khi thử nghiệm các ứng dụng web, sửa đổi các tiêu đề cung cấp một bản hack tuyệt vời

  • để kiểm tra chế độ khách của một ứng dụng;
  • có thể đặt cookie bằng tiêu đề;
  • để kiểm tra một số phần nhất định của ứng dụng bị tắt theo mặc định và có thể được bật bằng cách chuyển tiêu đề yêu cầu tùy chỉnh;
  • để kiểm tra các trường hợp thử nghiệm khác nhau được liên kết với các tiêu đề;
  • để bỏ qua luồng xác thực trong ứng dụng của bạn bằng cách chuyển tiêu đề ủy quyền

Khi tôi biết về thử nghiệm tự động ứng dụng web, tôi nhận ra rằng việc sửa đổi tiêu đề nên là một tính năng ở đó do khả năng ứng dụng rộng rãi của nó trong thử nghiệm ứng dụng web. Vì vậy, tôi quyết định viết phần này để làm sáng tỏ các cách sửa đổi tiêu đề trong thử nghiệm tự động

Thêm sau khi nhảy. Tiếp tục đọc bên dưới ↓

Gặp gỡ “TypeScript trong 50 bài học”, hướng dẫn mới tuyệt vời của chúng tôi về TypeScript. Với các hướng dẫn chi tiết về mã, các ví dụ thực hành và các vấn đề phổ biến — tất cả được chia thành các bài học ngắn, dễ quản lý. Đối với những nhà phát triển biết đủ JavaScript để trở nên nguy hiểm

Chuyển đến mục lục ↬

Selenium lấy tiêu đề phản hồi python

selen

Selenium được sử dụng rộng rãi như một khung tự động hóa thử nghiệm để thử nghiệm từ đầu đến cuối các ứng dụng web. Nó được phát triển vào năm 2004. Ban đầu, Selenium IDE được sử dụng nhưng nó chỉ hỗ trợ firefox sau đó Selenium RC được phát triển để cho phép thử nghiệm trên nhiều trình duyệt. Hiện tại, Selenium WebDriver đang được sử dụng vì nó cung cấp hỗ trợ cho trải nghiệm di động và các trang web động. Nó bắt chước một người dùng thực sự tương tác với trang web

Ưu điểm của việc sử dụng Selenium WebDriver

  • Selenium WebDriver là mã nguồn mở;
  • Nó cung cấp các ràng buộc cho mọi ngôn ngữ lập trình chính;
  • Hoạt động trên nhiều hệ điều hành. Bài kiểm tra được viết bằng Windows sẽ dễ dàng hoạt động trên Mac;
  • Hỗ trợ mô phỏng bàn phím và con trỏ;
  • Tiện ích bổ sung có thể được cài đặt

Hạn chế của Selenium WebDriver

  • Không hỗ trợ sửa đổi tiêu đề;
  • Không hỗ trợ thêm tham số yêu cầu;
  • Không thể chặn yêu cầu

Như đã thảo luận, khả năng sửa đổi các tiêu đề giúp ích rất nhiều trong việc thử nghiệm các ứng dụng nhưng Selenium WebDriver không hỗ trợ nó và gần đây họ không có kế hoạch đưa nó vào

Bài viết này tập trung vào các cách tiếp cận khác nhau để sửa đổi tiêu đề trong thiết lập tự động hóa Selenium

Cách tiếp cận 1. Dây Selenium (Trăn)

Selenium Wire mở rộng các liên kết Selenium Python để cung cấp cho bạn quyền truy cập vào các yêu cầu cơ bản do trình duyệt thực hiện. Bạn viết mã của mình giống như cách bạn làm với Selenium, nhưng bạn nhận được các API bổ sung để kiểm tra các yêu cầu và phản hồi cũng như thực hiện các thay đổi đối với chúng một cách nhanh chóng

Nó cho phép sửa đổi các yêu cầu và phản hồi khi đang di chuyển bằng cách sử dụng các thiết bị chặn. Nó cũng có thể chặn yêu cầu, phản hồi giả, thêm tham số yêu cầu trong URL

Cách sử dụng

Thêm tiêu đề yêu cầu

# A response interceptor takes two args which
# then allows to tinker with the response
def interceptor(request, response):  
    if request.url == 'https://server.com/some/path':
        response.headers['New-Header'] = 'Some Value'
driver.response_interceptor = interceptor
driver.get(<URL_where_to_modify_the_header)

# Responses from https://server.com/some/path will now contain 
# the New-Header
4

Tên tiêu đề trùng lặp được cho phép trong yêu cầu HTTP, vì vậy trước khi đặt tiêu đề thay thế, trước tiên bạn phải xóa tiêu đề hiện có bằng cách sử dụng

require("chromedriver");
const { Builder } = require("selenium-webdriver");
const chrome = require("selenium-webdriver/chrome");
const { getRequestlyExtension, importRequestlySharedList } = require("@requestly/selenium");

const options = new chrome.Options().addExtensions(getRequestlyExtension("chrome"));
const driver = new Builder()
    .forBrowser("chrome")
    .setChromeOptions(options)
    .build();

// Imports Rules in Selenium using Requestly sharedList feature
// importRequestlySharedList(driver, <sharedList_URL>);

importRequestlySharedList(driver, 'https://app.requestly.io/rules/#sharedList/1626984924247-Adding-Headers-Example');
6 nếu không sẽ tồn tại hai tiêu đề cùng tên

Thêm tiêu đề phản hồi

# A response interceptor takes two args which
# then allows to tinker with the response
def interceptor(request, response):  
    if request.url == 'https://server.com/some/path':
        response.headers['New-Header'] = 'Some Value'
driver.response_interceptor = interceptor
driver.get(<URL_where_to_modify_the_header)

# Responses from https://server.com/some/path will now contain 
# the New-Header

Một bộ chặn phản hồi phải chấp nhận hai đối số, một cho yêu cầu ban đầu và một cho phản hồi

Hạn chế

  • Điều này chỉ khả dụng dưới dạng mô-đun Python
  • Nó không hỗ trợ các ngôn ngữ khác

Cách tiếp cận 2. Đang tải tiện ích mở rộng trình duyệt trong Selenium có thể sửa đổi tiêu đề

Có các công cụ như Requestly là công cụ một cửa để gỡ lỗi và sửa đổi các yêu cầu mạng. Yêu cầu cho phép người dùng Sửa đổi tiêu đề, URL chuyển hướng, Chuyển đổi máy chủ, Phản hồi API mô phỏng, Trì hoãn yêu cầu mạng, Chèn tập lệnh tùy chỉnh, v.v. Nó cung cấp gói npm sẵn sàng hoạt động, là một trình bao bọc xung quanh tiện ích mở rộng cho phép sử dụng tiện ích mở rộng trong Selenium. Nó hỗ trợ Chrome, Firefox và Edge

Sử dụng Requestly trong Selenium

Requestly cho phép bạn làm nhiều việc khác hơn là chỉ sửa đổi các tiêu đề. Tất cả những thứ này có thể được thiết lập trong giao diện web và danh sách chia sẻ có thể được tải vào ứng dụng, cho phép người dùng linh hoạt chỉnh sửa các quy tắc một cách dễ dàng trong ứng dụng web

Để cài đặt.

require("chromedriver");
const { Builder } = require("selenium-webdriver");
const chrome = require("selenium-webdriver/chrome");
const { getRequestlyExtension, importRequestlySharedList } = require("@requestly/selenium");

const options = new chrome.Options().addExtensions(getRequestlyExtension("chrome"));
const driver = new Builder()
    .forBrowser("chrome")
    .setChromeOptions(options)
    .build();

// Imports Rules in Selenium using Requestly sharedList feature
// importRequestlySharedList(driver, <sharedList_URL>);

importRequestlySharedList(driver, 'https://app.requestly.io/rules/#sharedList/1626984924247-Adding-Headers-Example');
7

Cách sử dụng

Quy tắc sửa đổi tiêu đề có thể được tạo tại ứng dụng. theo yêu cầu. io/rules sau khi cài đặt tiện ích mở rộng

Selenium lấy tiêu đề phản hồi python
Thêm Tiêu đề cho tất cả các yêu cầu. (Xem trước lớn)

Sau khi tạo URL, nhấp vào nút

require("chromedriver");
const { Builder } = require("selenium-webdriver");
const chrome = require("selenium-webdriver/chrome");
const { getRequestlyExtension, importRequestlySharedList } = require("@requestly/selenium");

const options = new chrome.Options().addExtensions(getRequestlyExtension("chrome"));
const driver = new Builder()
    .forBrowser("chrome")
    .setChromeOptions(options)
    .build();

// Imports Rules in Selenium using Requestly sharedList feature
// importRequestlySharedList(driver, <sharedList_URL>);

importRequestlySharedList(driver, 'https://app.requestly.io/rules/#sharedList/1626984924247-Adding-Headers-Example');
8 để tạo liên kết cho URL

URL cho

require("chromedriver");
const { Builder } = require("selenium-webdriver");
const chrome = require("selenium-webdriver/chrome");
const { getRequestlyExtension, importRequestlySharedList } = require("@requestly/selenium");

const options = new chrome.Options().addExtensions(getRequestlyExtension("chrome"));
const driver = new Builder()
    .forBrowser("chrome")
    .setChromeOptions(options)
    .build();

// Imports Rules in Selenium using Requestly sharedList feature
// importRequestlySharedList(driver, <sharedList_URL>);

importRequestlySharedList(driver, 'https://app.requestly.io/rules/#sharedList/1626984924247-Adding-Headers-Example');
9 đã tạo ở trên có tại đây

URL này là quy tắc Yêu cầu bổ sung tiêu đề

# A response interceptor takes two args which
# then allows to tinker with the response
def interceptor(request, response):  
    if request.url == 'https://server.com/some/path':
        response.headers['New-Header'] = 'Some Value'
driver.response_interceptor = interceptor
driver.get(<URL_where_to_modify_the_header)

# Responses from https://server.com/some/path will now contain 
# the New-Header
00 cho tất cả các yêu cầu

Quy tắc này có thể được sử dụng trong Selenium WebDriver bằng cách sử dụng URL

require("chromedriver");
const { Builder } = require("selenium-webdriver");
const chrome = require("selenium-webdriver/chrome");
const { getRequestlyExtension, importRequestlySharedList } = require("@requestly/selenium");

const options = new chrome.Options().addExtensions(getRequestlyExtension("chrome"));
const driver = new Builder()
    .forBrowser("chrome")
    .setChromeOptions(options)
    .build();

// Imports Rules in Selenium using Requestly sharedList feature
// importRequestlySharedList(driver, <sharedList_URL>);

importRequestlySharedList(driver, 'https://app.requestly.io/rules/#sharedList/1626984924247-Adding-Headers-Example');
9 được mô tả bên dưới

________số 8

Tìm thêm thông tin tại đây

Hạn chế

  • Nó cung cấp gói npm chỉ giới hạn mô-đun thành JavaScript
  • Danh sách được chia sẻ phải được tạo thủ công để sử dụng các quy tắc trong Selenium, do đó không thể kiểm soát các quy tắc thông qua mã được viết cho thử nghiệm tự động hóa Selenium

Cách tiếp cận 3. Sử dụng nghệ sĩ múa rối

Puppeteer là thư viện Node do Google phát triển, cung cấp API cấp cao để kiểm soát Headless Chrome hoặc Chromium trên Giao thức DevTools. Nó cũng có thể được định cấu hình để sử dụng Chrome hoặc Chromium đầy đủ (không đầu)

Khi nói về thiết lập tự động hóa trình duyệt, Selenium sẽ tự động xuất hiện nhưng kể từ khi Puppeteer ra đời, nó được sử dụng rộng rãi để quét web. Nó cung cấp nhiều quyền kiểm soát hơn đối với chrome so với Selenium có lẽ do sự hỗ trợ của google đối với nó. Cũng vì lý do tương tự, nó loại trừ sự cần thiết của trình điều khiển bên ngoài để chạy trình duyệt

Cách sử dụng

# A response interceptor takes two args which
# then allows to tinker with the response
def interceptor(request, response):  
    if request.url == 'https://server.com/some/path':
        response.headers['New-Header'] = 'Some Value'
driver.response_interceptor = interceptor
driver.get(<URL_where_to_modify_the_header)

# Responses from https://server.com/some/path will now contain 
# the New-Header
0

Sửa đổi tiêu đề

Phương pháp

# A response interceptor takes two args which
# then allows to tinker with the response
def interceptor(request, response):  
    if request.url == 'https://server.com/some/path':
        response.headers['New-Header'] = 'Some Value'
driver.response_interceptor = interceptor
driver.get(<URL_where_to_modify_the_header)

# Responses from https://server.com/some/path will now contain 
# the New-Header
02 có thể được sử dụng để đặt tiêu đề

Nó cũng có thể sửa đổi và loại bỏ các tiêu đề hiện có. Khi các yêu cầu bị chặn bằng cách sử dụng kết hợp

# A response interceptor takes two args which
# then allows to tinker with the response
def interceptor(request, response):  
    if request.url == 'https://server.com/some/path':
        response.headers['New-Header'] = 'Some Value'
driver.response_interceptor = interceptor
driver.get(<URL_where_to_modify_the_header)

# Responses from https://server.com/some/path will now contain 
# the New-Header
03 và
# A response interceptor takes two args which
# then allows to tinker with the response
def interceptor(request, response):  
    if request.url == 'https://server.com/some/path':
        response.headers['New-Header'] = 'Some Value'
driver.response_interceptor = interceptor
driver.get(<URL_where_to_modify_the_header)

# Responses from https://server.com/some/path will now contain 
# the New-Header
04

# A response interceptor takes two args which
# then allows to tinker with the response
def interceptor(request, response):  
    if request.url == 'https://server.com/some/path':
        response.headers['New-Header'] = 'Some Value'
driver.response_interceptor = interceptor
driver.get(<URL_where_to_modify_the_header)

# Responses from https://server.com/some/path will now contain 
# the New-Header
7

nguồn mã

Hạn chế

  • Puppeteer hiện chỉ giới hạn ở chrome. Nó không thể được sử dụng để kiểm tra trình duyệt chéo
  • Nó có một cộng đồng nhỏ hơn so với Selenium. Dường như có nhiều hỗ trợ hơn cho Selenium trong cộng đồng
  • Nó chỉ hỗ trợ JavaScript

Sự kết luận

Sửa đổi tiêu đề mạng là một công cụ rất mạnh trong môi trường thử nghiệm. Có rất nhiều điều về nó hơn những gì có thể được đề cập trong bài viết này. Tôi đã cố gắng đề cập đến một số phương pháp đơn giản nhất để sửa đổi tiêu đề trong Thử nghiệm tự động hóa giao diện người dùng

Mọi phương pháp đều có một số ưu điểm dựa trên trường hợp sử dụng và ngôn ngữ bạn sử dụng. Đây là một số sở thích

  • Nếu bạn muốn sử dụng Selenium trong Python, hãy chọn Selenium-Wire vì nó cung cấp rất nhiều tính năng trên Selenium-Webdriver
  • Nếu bạn chỉ muốn thử nghiệm trên chrome, hãy ưu tiên trình điều khiển rối hơn do hỗ trợ của Google và hỗ trợ vượt trội cho tất cả các tính năng bao gồm sửa đổi tiêu đề
  • Nếu bạn làm việc với Selenium và muốn thực hiện thử nghiệm trên nhiều trình duyệt, hãy thêm tiện ích mở rộng để mô phỏng thử nghiệm trong Selenium-Webdriver. Nếu trường hợp sử dụng của bạn yêu cầu bạn chèn tập lệnh hoặc chuyển hướng tài nguyên mạng trong thời gian chạy (ngoài việc sửa đổi tiêu đề), thì Requestly là một lựa chọn lý tưởng

Hy vọng bài viết này cung cấp cho bạn cái nhìn sâu sắc về việc sửa đổi tiêu đề trong thiết lập thử nghiệm ứng dụng web tự động