【问题标题】:How to add wait time between threads , If we try to run testNG classes in parallel如何在线程之间添加等待时间,如果我们尝试并行运行 testNG 类
【发布时间】:2019-03-07 13:45:45
【问题描述】:

我有一个 testNG XML 文件,我想在其中并行运行我的测试类,线程数 = 4。但我想在这个线程之间添加一些等待时间,这样 4 个浏览器实例就不会在同时。所有实例之间需要等待时间。有没有办法做到这一点?

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" thread-count="2" parallel="classes">
<test name="Invoice and Expenses">
<classes>
<class name="test one" />
<class name="test two" />
<class name="test three" />
<class name="test four" /> 
</classes>
</test> 

【问题讨论】:

  • 添加add some wait time between this threads 不会破坏并行性的目的吗?

标签: selenium automated-tests testng


【解决方案1】:

如果你不希望 4 个类同时打开浏览器,你可能也不希望它们并行运行,那么你需要依次运行它们。

即使你在每个测试类之前添加了一些随机睡眠,因为它们仍然并行运行,你无法避免它们在启动后同时打开浏览器。

如果您确实希望 4 个类并行运行,我想浏览器仅在您的测试类中的某些时刻需要,而不是在测试中总是需要。在这种情况下,这是您的测试代码中的资源争用问题。您需要管理 4 个类需要打开浏览器的资源(例如 WebDriver)上的同步锁,以便请求浏览器的类需要等待其他测试类释放它(关闭它)才能释放它(关闭它)继续,而其他人也被阻止等待发布。

【讨论】:

    【解决方案2】:

    为什么不直接使用Thread.sleep()

    你可以把它放在你的@BeforeClass 方法中,这样它就会在每节课之前执行。此外,如果您想为不同的课程单独延迟,您可以执行以下操作:

        @BeforeClass(alwaysRun = true)
        public void justDelay() throws InterruptedException 
          {
    
            if(this.getClass().getName().equals("test one"))
             {
               Thread.sleep(delay1);
             }
    
    
            else if(this.getClass().getName().equals("test two"))
             {
                   Thread.sleep(delay2);
             }
    
            else
             {
                   Thread.sleep(defaultdelay)
             }
    
            }
    

    【讨论】:

    • 他打开4个浏览器实例有什么帮助?
    • 对不起,我没有得到你的问题。你的意思是说我应该写所有 4 个类,因为这就是问题的数量?
    • 没有。假设我有 10 个类,我想按照 OP 所述的顺序运行它们。我将不得不用 if 条件声明每个类,这显然是最坏/极端的条件。尝试给出一般性的答案。
    猜你喜欢
    • 1970-01-01
    • 2012-03-27
    • 1970-01-01
    • 2015-02-04
    • 2018-09-29
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多