【问题标题】:Selenium python do test every 10 secondsSelenium python 每 10 秒进行一次测试
【发布时间】:2012-01-06 05:10:18
【问题描述】:

我正在使用 selenium (python) 测试,我需要每 10 秒自动测试一次我的应用程序。

我该怎么做?

【问题讨论】:

  • 你真正需要测试什么?

标签: python testing selenium automation


【解决方案1】:

你可以使用threading.Timer:

import threading
import logging

def print_timer(count):
    if count:
        t = threading.Timer(10.0, print_timer,args=[count-1])
        t.start()
    logger.info("Begin print_timer".format(c=count))
    time.sleep(15)
    logger.info("End print_timer".format(c=count))

def using_timer():
    t = threading.Timer(0.0, print_timer,args=[3])
    t.start()

if __name__=='__main__':
    logging.basicConfig(level=logging.DEBUG,
                        format='%(threadName)s: %(asctime)s: %(message)s',
                        datefmt='%H:%M:%S')    
    using_timer()

产量

Thread-1: 06:46:18: Begin print_timer  --
                                         | 10 seconds
Thread-2: 06:46:28: Begin print_timer  --
Thread-1: 06:46:33: End print_timer      | 10 seconds
Thread-3: 06:46:38: Begin print_timer  --
Thread-2: 06:46:43: End print_timer      | 10 seconds
Thread-4: 06:46:48: Begin print_timer  --
Thread-3: 06:46:53: End print_timer
Thread-4: 06:47:03: End print_timer

请注意,这将每 10 秒生成一个新线程。确保在线程数变得无法容忍之前提供一些方法让线程生成停止。

【讨论】:

    【解决方案2】:

    这取决于你有什么资源。 要自动运行一些脚本,你应该看看 cron 程序

    您能做的最好的事情是使用 Jenkins CI,它是一种自动构建工具

    我将它用于自动测试 - 我构建应用程序并运行测试,它提供了许多额外的工具,如图表、检测回归等。

    编辑:如果你想每 10 秒测试一次,那么我想你的应用程序很小,所以不需要 Jenkins,我会看看 cron

    【讨论】:

    • 我同意 - cron 似乎是这里的正确工具。启动浏览器和运行测试会有一些开销,所以也许 10 秒是不可行的,除非测试非常快。
    【解决方案3】:

    我知道这可能不是最好的解决方案,但如果您想简单快速地做一些事情:

    import time
    
    def my_function():#my function
        do_something
        .......
    
    try:
        while True:
            my_function()#call my function
            time.sleep(10)#wait 10 second
    except KeyboardInterrupt:#execute the while loop until you don't press CRTL+C, when you press it the execution is going to stop after 10 sec
        pass
    

    【讨论】:

      【解决方案4】:

      如果您想在您的 Windows 机器上每 10 秒自动测试一次您的应用程序并自动提醒您某事,甚至自动发送电子邮件?使用 Windows 附带的“Task Scheduler”——它的界面可能有点吓人,但很容易使用。

      任务计划程序有多种用途 - 您希望计算机自动执行的任何操作,都可以在此处进行配置。例如,您可以使用任务调度程序在特定时间自动唤醒您的计算机。

      【讨论】:

        猜你喜欢
        • 2012-05-09
        • 2021-12-15
        • 2016-05-10
        • 1970-01-01
        • 1970-01-01
        • 2013-07-24
        • 2022-01-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多