Несколько драйверов одновременно
Введение | |
Обход предупреждения о небезопасности |
Введение
Чтобы запустить несколько драйверов браузера одновременно можно воспользоваться Python Multiprocessing
import multiprocessing
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
def main():
# Нужное число драйверов
driver_count = 3
for i in range(0, driver_count):
p = multiprocessing.Process(target=run)
p.start()
processes_list.append(p)
for p in processes_list:
p.join()
def run():
chrome_options = webdriver.ChromeOptions()
driver = webdriver.Chrome(options=chrome_options)
driver.get(http://urn.su)
if __name__ == '__main__':
main()
Обход предупреждения о небезопасности
Ещё один пример, в котором действия драйвера оформлены в виде функций.
Также в этом примере можно увидеть обход предупреждения о посещении
небезопасной страницы.
# Этот скрипт вызывается с
# аргументами командной строки
# python3 multidriver.py
# USERNAME SERVICE_IP SERVICE_PWD HOST_PWD NUMBER_OF_PROCESSES
import multiprocessing
import time
import datetime
import sys
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
print('Number of arguments:', len(sys.argv), 'arguments.')
print('Argument List:', str(sys.argv))
class User:
def __init__(self, name, SERVICE_ip, SERVICE_ui_pwd, remote_host_pwd):
self._name = name
self._SERVICE_ip = SERVICE_ip
self._SERVICE_url = "https://" + SERVICE_ip + "/"
self._SERVICE_ui_pwd = SERVICE_ui_pwd
self._remote_host_pwd = remote_host_pwd
def name(self):
return self._name
def SERVICE_ip(self):
return self._SERVICE_ip
def SERVICE_url(self):
return self._SERVICE_url
def SERVICE_ui_pwd(self):
return self._SERVICE_ui_pwd
def remote_host_pwd(self):
return self._remote_host_pwd
def main():
NUMBER_OF_TASKS = int(sys.argv[5])
print("URN.SU: -------------------------------------------\n NEW TEST STARTED")
with open("performance_data.txt", "a") as pf:
pf.write("URN.SU: -------------------------------------------\nNEW TEST STARTED\n")
processes_list = []
for i in range(0,NUMBER_OF_TASKS):
p = multiprocessing.Process(target=run)
p.start()
processes_list.append(p)
for p in processes_list:
p.join()
print("\n URN.SU: TEST FINISHED\n-------------------------------------------")
def run():
start_time = datetime.datetime.utcnow()
log_prefix = start_time.strftime("%Y%m%d%H%M%S%f")
log_name = log_prefix + "_log.txt"
with open(log_name,"w+") as f:
f.write(f"{start_time}: Test started\r\n")
user = setup_user()
driver = setup_driver()
driver = open_SERVICE(driver, user)
driver = pass_unsafe_warning(driver)
driver = login_to_SERVICE(driver, user)
driver = open_connections_tab(driver)
driver = select_host(driver)
driver = login_to_host(driver, user)
confirm_login(driver)
finish_time = datetime.datetime.utcnow()
tdelta = finish_time - start_time
with open(log_name,"a") as f:
f.write(f"{finish_time}: Test finished \r\n")
f.write(f"Test execution time is {tdelta} \r\n")
with open("performance_data.txt", "a") as pf:
pf.write(f"{log_name} : {tdelta} \r\n")
def setup_user():
current_time = datetime.datetime.utcnow()
USERNAME = sys.argv[1]
SERVICE_IP = sys.argv[2]
SERVICE_UI_PWD = sys.argv[3]
REMOTE_HOST_PWD = sys.argv[4]
user = User(USERNAME, SERVICE_IP, SERVICE_UI_PWD, REMOTE_HOST_PWD)
return user
def setup_driver():
current_time = datetime.datetime.utcnow()
chrome_options = webdriver.ChromeOptions()
driver = webdriver.Chrome(options=chrome_options)
driver.set_window_size(1366, 768)
return driver
def open_SERVICE(driver, user):
current_time = datetime.datetime.utcnow()
driver.get(user.SERVICE_url())
return driver
def pass_unsafe_warning(driver):
try:
driver.implicitly_wait(10)
advanced_button = driver.find_element_by_id("details-button");
advanced_button.click()
driver.implicitly_wait(10)
proceed_to_link = driver.find_element_by_id("proceed-link");
proceed_to_link.click()
except:
print("URN.SU: unable to find unsafe warning")
return driver
return driver
def login_to_SERVICE(driver, user):
driver.implicitly_wait(10)
input_tags = driver.find_elements_by_tag_name("input")
username_input = input_tags[0]
username_input.send_keys(user.name())
password_input = input_tags[1]
password_input.send_keys(user.SERVICE_ui_pwd())
title_class = driver.find_elements_by_class_name("title")
for el in title_class:
if el.text == "LOGIN":
login_button = el
login_button.click()
return driver
def open_connections_tab(driver):
driver.implicitly_wait(10)
tab_link_class = driver.find_elements_by_class_name("tab-link")
connections_button = tab_link_class[1]
connections_button.click()
return driver
def select_host(driver):
driver.implicitly_wait(10)
glyph_icon_right_triangle = driver.find_elements_by_class_name("glyph-icon-right-triangle")
glyph_icon_right_triangle[0].click()
glyph_icon_command_line_interface = driver.find_elements_by_class_name("glyph-icon-command-line-interface")
glyph_icon_command_line_interface[0].click()
return driver
def login_to_host(driver, user):
driver.implicitly_wait(10)
auth_dialog_class = driver.find_elements_by_class_name("AuthenticationDialog")
driver.implicitly_wait(10)
input_tags = driver.find_elements_by_tag_name("input")
for el in input_tags:
attribute = el.get_attribute("type")
if attribute == "password":
password_button = el
password_button.send_keys(user.remote_host_pwd())
driver.implicitly_wait(10)
title_class = driver.find_elements_by_class_name("title")
for el in title_class:
if el.text == "LOGIN" or el.text == "Login" or el.text == "login":
login_button = el
login_button.click()
else:
pass
if not(login_button):
print("URN.SUERROR: LOGIN button not found")
return driver
def confirm_login(driver):
driver.implicitly_wait(10)
menu_class = driver.find_elements_by_class_name("menu")
driver.implicitly_wait(10)
terminals_button = driver.find_elements_by_xpath("//*[contains(text(), 'Terminals')]")
if (terminals_button):
print("URN.SU: Success")
else:
print("URN.SUERROR: Did not find Terminals button")
return driver
if __name__ == '__main__':
main()
Selenium | |
Selenium Python | |
Несколько драйверов одновременно | |
Добавить Selenium в PyCharm | |
Тестирование ПО | |
Учебник по тестированию | |
Тестирование API | |
Тестирование с помощью Python | |
Robot Framework |