我做了一些研究,共识似乎表明,如果没有大量的知识和工作,这是不可能的。然而:
如果是的话,我该怎么做呢?我正在使用python,并试图一次运行3-5相同的测试。
这不是一个通用的测试,因此我不在乎它是否会中断一个干净的测试环境。
发布于 2018-04-03 07:31:05
我觉得你能做到。但我觉得更好或更容易的方法是使用不同的窗口。尽管如此,我们可以使用multithreading、multiprocessing或subprocess模块来并行地(几乎并行)触发任务。
多线程示例
让我向您展示一个简单的示例,说明如何使用threading模块生成多个测试。
from selenium import webdriver
import threading
import time
def test_logic():
driver = webdriver.Firefox()
url = 'https://www.google.co.in'
driver.get(url)
# Implement your test logic
time.sleep(2)
driver.quit()
N = 5 # Number of browsers to spawn
thread_list = list()
# Start test
for i in range(N):
t = threading.Thread(name='Test {}'.format(i), target=test_logic)
t.start()
time.sleep(1)
print(t.name + ' started!')
thread_list.append(t)
# Wait for all threads to complete
for thread in thread_list:
thread.join()
print('Test completed!')这里我生成了5个浏览器来一次运行测试用例。为了演示,我没有实现测试逻辑,而是将睡眠时间设置为2秒。该代码将启动5个firefox浏览器(用python2.7测试),打开google,等待2秒后退出。
日志:
Test 0 started!
Test 1 started!
Test 2 started!
Test 3 started!
Test 4 started!
Test completed!
Process finished with exit code 0发布于 2021-09-16 13:36:02
Python 3.2+
具有自己的webdriver实例的线程(不同的窗口)
线程可以在不同的windows上使用性能良好的 (这里的一些解释) 来解决您的问题。此外,线程比进程轻。
concurrent.futures.ThreadPoolExecutor,使用它自己的own驱动程序。headless选项。下面的示例使用chrome-webdriver。为了举例说明使用整数作为参数url_test,测试函数selenium_test使用了6次。
from concurrent import futures
from selenium import webdriver
def selenium_test(test_url):
chromeOptions = webdriver.ChromeOptions()
#chromeOptions.add_argument("--headless") # make it not visible
driver = webdriver.Chrome(options=chromeOptions)
print("testing url {:0} started".format(test_url))
driver.get("https://www.google.com") # replace here by driver.get(test_url)
#<actual work that needs to be done be selenium>
driver.quit()
# default number of threads is optimized for cpu cores
# but you can set with `max_workers` like `futures.ThreadPoolExecutor(max_workers=...)`
with futures.ThreadPoolExecutor() as executor:
future_test_results = [ executor.submit(selenium_test, i)
for i in range(6) ] # running same test 6 times, using test number as url
for future_test_result in future_test_results:
try:
test_result = future_test_result.result() # can use `timeout` to wait max seconds for each thread
#... do something with the test_result
except Exception as exc: # can give a exception in some thread, but
print('thread generated an exception: {:0}'.format(exc))输出
testing url 1 started
testing url 5 started
testing url 3 started
testing url 4 started
testing url 0 started
testing url 2 started发布于 2018-04-03 10:19:40
看看TestNG,您应该能够找到实现这一目标的框架。
我做了一个简短的检查,这里有几个链接让你开始:
如果您想要一个可靠的、能在规模上执行并行执行和负载测试的篮板球框架,那么请看TurboSelenium:https://butlerthing.io/products#demovideo。给我们发个口信,很乐意和你讨论这个问题。
https://stackoverflow.com/questions/49617485
复制相似问题