是否可以一次运行一个硒测试的多个实例? [英] Is it possible to run multiple instances of one selenium test at once?

查看:25
本文介绍了是否可以一次运行一个硒测试的多个实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一些研究,共识似乎表明,如果没有大量的知识和工作,这是不可能的.但是:

I have done some research and the consensus appears to state that this is impossible without a lot of knowledge and work. However:

  • 是否可以同时在不同的选项卡中运行相同的测试?

如果是这样,我会怎么做?我正在使用 python 并尝试一次运行 3-5 个相同的测试.

If so, how would I go about that? I'm using python and attempting to run 3-5 of the same test at once.

这不是通用测试,因此我不在乎它是否会中断干净的测试环境.

This is not a generic test, hence I do not care if it interrupts a clean testing environment.

推荐答案

我认为你可以做到.但我觉得更好或更简单的方法是使用不同的窗口.话虽如此,我们可以使用 multithreadingmultiprocessingsubprocess 模块来并行(接近并行)触发任务.

I think you can do that. But I feel the better or easier way to do that is using different windows. Having said that we can use either multithreading or multiprocessing or subprocess module to trigger the task in parallel (near parallel).

多线程示例

让我向您展示一个关于如何使用 threading 模块生成多个测试的简单示例.

Let me show you a simple example as to how to spawn multiple tests using threading module.

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 浏览器(使用 python 2.7 测试),打开 google 并等待 2 秒然后退出.

Here I am spawning 5 browsers to run test cases at one time. Instead of implementing the test logic I have put sleep time of 2 seconds for the purpose of demonstration. The code will fire up 5 firefox browsers (tested with python 2.7), open google and wait for 2 seconds before quitting.

日志:

Test 0 started!
Test 1 started!
Test 2 started!
Test 3 started!
Test 4 started!
Test completed!

Process finished with exit code 0

这篇关于是否可以一次运行一个硒测试的多个实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆