【问题标题】:Is it possible to run multiple instances of one selenium test at once?是否可以一次运行一个硒测试的多个实例?
【发布时间】:2018-04-02 19:24:28
【问题描述】:

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

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

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

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

【问题讨论】:

  • 您是否考虑过设置多个虚拟机来同时运行测试?
  • 是的,查一下multiprocessing,这需要知识和工作,除非你想雇人为你做这项工作,那么只需要.
  • 您需要它们位于不同的标签中吗?或者不同的窗口也可以?我想你真的不需要它们是不同的标签,而是你的盒子上有多个浏览器会话?您需要自己做还是有预算?
  • @AliRad 它也可以是多个窗口。我更愿意自己做,但可以投入一些钱
  • 看看TestNG,你应该能够找到实现这一点的框架。否则请查看butlerthing.io/products#demovideo。给我们留言,我们很乐意与您讨论这个问题

标签: python selenium webdriver


【解决方案1】:

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

多线程示例

让我向您展示一个关于如何使用 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 浏览器(使用 python 2.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

【讨论】:

    【解决方案2】:

    看看 TestNG,你应该能够找到实现这一点的框架。

    我做了一个简短的检查,这里有几个链接可以帮助您入门:

    Parallel Execution & Session Handling in Selenium

    Parallel Execution using Selenium Webdriver and TestNG

    如果您想要一个可靠的 rebost 框架,可以进行并行执行以及大规模负载测试,请查看 TurboSelenium:https://butlerthing.io/products#demovideo。给我们留言,我们很乐意与您讨论这个问题。

    【讨论】:

      【解决方案3】:

      Python 3.2+

      具有自己的 webdriver 实例的线程(不同的窗口)

      线程可以通过良好的性能提升 (some explanation here)在不同的窗口上解决您的问题。线程也比进程轻。

      下面的示例使用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
      

      【讨论】:

        猜你喜欢
        • 2020-05-30
        • 1970-01-01
        • 1970-01-01
        • 2020-05-30
        • 2015-10-26
        • 1970-01-01
        • 2014-02-22
        • 2022-08-17
        • 2013-10-10
        相关资源
        最近更新 更多