1
Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.
Tôi muốn chạy tập lệnh của mình trên nhiều trình duyệt bằng selen. Đến bây giờ tôi có thể thực hiện thao tác bằng cách mở một trình duyệt tại một thời điểm. Ví dụ:- Đăng ký vào Amazon. Tôi muốn có thể đăng ký hai người dùng đến Amazon cùng một lúc.
Đây là mã tôi có như bây giờ.
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.select import Select
driver.get["//www.amazon.com/ap/register?openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.amazon.com%2F%3Fref_%3Dnav_signin&prevRID=VBHFJ50CPKFJ3PGG7RDY&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.assoc_handle=usflex&openid.mode=checkid_setup&openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&prepopulatedLoginId=&failedSignInCount=0&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&pageId=usflex&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0"]
driver.find_element_by_xpath["""//*[@id="s2id_ID_form4a8055de_guest_register_sponsor_lookup"]/a/span[2]/b"""].click[]
driver.find_element_by_xpath["""//*[@id="s2id_autogen1_search"]"""].send_keys[v1]
Bằng cách sử dụng điều này, tôi có thể chạy nó cho một người dùng cùng một lúc. Nhưng tôi muốn có thể đăng ký nhiều hơn hai người dùng lên đến N cùng một lúc. Do đó, nhiều câu hỏi của Windows.
Hỏi ngày 26 tháng 4 năm 2017 lúc 6:11Apr 26, 2017 at 6:11
1
Bạn có thể tạo nhiều trường hợp của webDriver. Sau đó, bạn có thể thao tác từng cá nhân. Ví dụ,
from selenium import webdriver
driver1 = webdriver.Chrome[]
driver2 = webdriver.Chrome[]
driver1.get["//google.com"]
driver2.get["//yahoo.com"]
Đã trả lời ngày 26 tháng 4 năm 2017 lúc 6:24Apr 26, 2017 at 6:24
WindmillwindmillWindmill
6.1072 Huy hiệu vàng17 Huy hiệu bạc23 Huy hiệu Đồng2 gold badges17 silver badges23 bronze badges
3
Hướng dẫn này là sự tiếp nối của hướng dẫn khác của tôi có tên là Selen Selenium WebDriver với Python, vui lòng xem nó trước khi bắt đầu cái này. Hướng dẫn này dựa trên khóa học web selenium với khóa học Python, do Andrew Knight quản lý. Ông cũng có một blog, nhấp vào liên kết bên dưới để theo dõi tin tức: //automationpanda.com/. Như đã nói trước đây, vì dự án này tiếp tục là selenium webdriver với hướng dẫn của Python, bạn sẽ phải sao chép repo của nó để tiếp tục công việc của bạn. Trước khi bạn bắt đầu, hãy kiểm tra xem bạn có đang nhân bản đúng nhánh không: Hướng dẫn/WebDriver-with-python. Git Repo: //github.com/luizgustavor/intro-selenium-py/tree/tutorial/webdriver-with-python
Mục lục
2.1 Clone Project
2.2 Run Test
3.1 Selenium Webdriver
3.2 Headless Browsers
3.3 Reading Inputs
6.1 Setup GeckoDriver
6.2 Implement
7.1 Repository
1. Giới thiệu
Please take a look at it before starting this one.
He also has a blog, click the link below to follow the news:
//automationpanda.com/.
2. Thiết lập ban đầu
2.1 Dự án nhân bản
“tutorial/webdriver-with-python”.
//github.com/LuizGustavoR/intro-selenium-py/tree/tutorial/webdriver-with-python
Sau khi nhân bản nó, hãy kiểm tra VSCODE nếu bạn nhân bản đúng nhánh:
2.2 Kiểm tra chạy
Để đảm bảo dự án nhân bản đang thực sự hoạt động, hãy mở lệnh nhắc lệnh của bên trong thư mục dự án.
open the “Command Prompt” inside the project folder.
Chạy lệnh bên dưới:
pipenv run python -m pytest
Bạn sẽ thấy tin nhắn này:
3. quy ước
3.1 Selenium webdriver
Như đã thảo luận trong hướng dẫn trước, Selenium WebDriver hỗ trợ nhiều trình duyệt:
Dự án của chúng tôi được mã hóa cứng để chỉ chạy các bài kiểm tra trong Chrome, hãy để thay đổi điều đó.
Let’s change that.
3.2 Trình duyệt không đầu
Các trình duyệt không đầu chạy các trang web nhưng không trực quan hiển thị chúng trên màn hình, làm cho các thử nghiệm chạy hiệu quả hơn .Both Firefox và Chrome có các chế độ không đầu. Bạn có thể chạy chế độ không đầu khi phát triển và ở chế độ không đầu khi chạy trên CI.
Both Firefox and Chrome have headless modes.
You can run non headless mode when developing and on headless
mode when running on a CI.
3.3 Đọc đầu vào
Ví dụ về đầu vào là URL, tên người dùng, mật khẩu, giá trị thời gian chờ mặc định. Được chuyển vào tự động hóa khi bộ thử nghiệm bắt đầu chạy. Chúng không nên được mã hóa thành mã tự động hóa, đặc biệt nếu chúng là các giá trị nhạy cảm như mật khẩu.
Need to be passed into the automation when a test suite starts to run.
They should not be hardcoded into the automation code, especially if they are sensitive values like passwords.
4. Tệp cấu hình JSON
Trong hướng dẫn này, chúng tôi sẽ sử dụng tệp json config.python có thể phân tích tệp.
Python can parse a “.json” file into a dictionary in just one line.
Mở dự án thông qua VSCODE và tạo tệp config.json của Infig.json trong root dự án:
Điền vào tệp config.json của config.json với mã bên dưới:
{
"browser": "Headless Chrome",
"implicit_wait": 10
}
5. Lịch thi đấu pytest
Các giá trị tệp Config.json của config.json sẽ được đọc bên trong một vật kiện pytest.
Open the
“tests/conftest.py” file:
tests/conftest.py
"""
This module contains shared fixture.
"""
import json
import pytest
import selenium.webdriver
# scope='session' makes
# this fixture run only one time before the entire test suite
@pytest.fixture
def config[scope='session']:
# Read the file
with open['config.json'] as config_file:
config = json.load[config_file]
# Accept values are acceptable
assert config['browser'] in ['Firefox', 'Chrome', 'Headless Chrome']
assert isinstance[config['implicit_wait'], int]
assert config['implicit wait'] > 0
# Return config so it can be used
return config
# This fixture will run once for each test case
@pytest.fixture
def browser[config]:
# Initialize the WebDriver instance
if config['browser'] == 'Firefox':
b = selenium.webdriver.Firefox[]
elif config['browser'] == 'Chrome':
b = selenium.webdriver.Chrome[]
elif config['browser'] == 'Headless Chrome':
opts = selenium.webdriver.ChromeOptions[]
opts.add_argument['headless']
opts.add_argument['--log-level=1']
b = selenium.webdriver.Chrome[options=opts]
else:
raise Exception[f'Browser "{config["browser"]}" is not supported']
# Make its calls wait for elements to appear
b.implicitly_wait[config['implicit_wait']]
# Return the WebDriver instance for the setup
yield b
# Quit the WebDriver instance for the instance
B.quit
Tái bút: Vào tùy chọn Chrome không đầu, đối số thứ hai được thêm vào thiết lập của Chrom Chromeoptions đã được thêm vào để ẩn tin nhắn được hiển thị bởi selenium webdriver khi trang web được kiểm tra theo dõi khối. Into the “Headless Chrome” option, the second argument added into the “ChromeOptions” setup was added to hide the message shown by the selenium webdriver when the site being tested blocks FLoC tracking.
Floc là phương pháp theo dõi mới của Google.
Floc xấu cho quyền riêng tư: Nó đưa bạn vào một nhóm dựa trên lịch sử duyệt web của bạn và bất kỳ trang web nào cũng có thể khiến ID Floc nhóm đó nhắm mục tiêu và lấy dấu vân tay của bạn.
Để biết thêm thông tin, hãy đọc liên kết dưới đây: Sử dụng tiện ích mở rộng Duckduckgo để chặn Floc, phương thức theo dõi mới của Google trong Chrome.
Use the DuckDuckGo Extension to Block FLoC, Google’s New Tracking Method in Chrome.
6. Firefox [Geckodriver]
6.1 Cài đặt Geckodriver
Để chạy các thử nghiệm trong Firefox, hãy tải xuống bản phát hành ổn định mới nhất của trình điều khiển Firefox [Geckodriver] và trích xuất thực thi trong thư mục [C: \ WebDrivers \].
Để tải xuống Geckodriver, hãy truy cập trang bên dưới: //github.com/mozilla/geckodriver/tags
//github.com/mozilla/geckodriver/tags
Chọn phiên bản ổn định mới nhất và nhấp vào Tải xuống trực tuyến:
Sau đó, đi đến cuối trang trên các tài sản trên mạng và chọn phiên bản phù hợp nhất cho Phiên bản HĐH của bạn [Windows, Linux, MacOS] và Firefox [32 hoặc 64 bit]:
Don Tiết quên trích xuất trình điều khiển vào thư mục web của Web Web.
Tái bút: Don Tiết biết cách đặt các webDribers này vào môi trường biến của bạn, hãy đi đến hướng dẫn khác của tôi và tìm hiểu cách: Cài đặt cromedriver trên windows. Don’t know how to set these
webdrivers into your “Variables Environment” ?
Go to my other tutorial and learn how to: “Installing ChromeDriver on Windows”.
6.2 Thực hiện
Don Tiết quên trích xuất trình điều khiển vào thư mục web của Web Web.
tests/test_search.py
"""
These tests cover DuckDuckGo searches.
"""
from pages.search import DuckDuckGoSearchPage
from pages.result import DuckDuckGoResultPage
def test_basic_duckduckgo_search[browser]:
search_page = DuckDuckGoSearchPage[browser]
result_page = DuckDuckGoResultPage[browser]
PHRASE = "panda"
# Given the DuckDuckGo home page is displayed
search_page.load[]
# When the user searches for "panda"
search_page.search[PHRASE]
# And the search result query is "panda"
assert PHRASE == result_page.search_input_value[]
# And the search result links pertain to "panda"
# for title in result_page.result_link_titles[]:
# assert PHRASE.lower[] in title.lower[]
# Then the search result title contains "panda"
assert PHRASE in result_page.title[]
# raise Exception["Incomplete Test"]
Tái bút: Luôn khẳng định các phần tử web trên mạng trước khi các thuộc tính trang của trang. sẽ ném một lỗi. Always assert “web elements” before “page attributes”.
Web
Elements have the implicit wait setup for them, page attributes don’t, so if the selenium webdriver reads a page attribute before a slower browser sets the new attribute value the test will throw an error.
7. Kết luận
Thay đổi tham số Giá trị Trình duyệt của Trình duyệt bên trong tệp config.json của Google thành tên trình duyệt bạn muốn chạy thử nghiệm.
{
"browser": "Chrome",
"implicit_wait": 10
}
Sau đó chạy lệnh bên dưới trong lời nhắc.
pipenv run python -m pytest
Chrome không đầu
Trình duyệt Chrome
Thành công.CongRatulation, bài kiểm tra chạy như mong đợi.
Congratulations, the test ran as expected.
7.1 Kho lưu trữ
Tôi đã tạo một nhánh mới có tên là Hướng dẫn/Nhiều trình duyệt, cho hướng dẫn này, nhấp vào liên kết bên dưới để xem: //github.com/luizgustavor/intro-selenium-py/tree/tutorial/multiple-Browsers
//github.com/LuizGustavoR/intro-selenium-py/tree/tutorial/multiple-browsers
Kết thúc.
8. Tài liệu tham khảo
- //testautomationu.applitools.com/selenium-webdriver-python-tutorial/chapter7.html
- //www.testgrid.io/blog/selenium-what-is-it/
- //stackoverflow.com/questions/66997942/error-with-permissions-policy-header-when-using-chromedriver-to-a-headless-br
- //spreadprivacy.com/block-floc-with-duckduckgo/
- //github.com/mozilla/geckodriver/tags