如何使其他测试用例等待一段时间,直到一个测试用例在TestNG并行执行中执行操作为止

问题描述 投票:0回答:1

我正在并行运行测试用例。很多时候,我在从反应下拉列表中选择值时遇到问题。假设我必须从react下拉列表中选择一个值,我必须执行2个操作:1.单击下拉按钮。2.从下拉列表中选择值。

有时,在并行执行期间,执行第一步后,下一个线程正在运行,因此切换了窗口。当它切换回第一个窗口并执行第二步时,不再存在下拉列表,因此测试用例失败。

下拉菜单如下:

enter image description here

无论如何,我是否会让其他线程等待一段时间,直到该线程执行操作?

我尝试了什么?尝试1:他们说过在很多地方,我们不能暂停另一个线程的线程。但是,我还是尝试了这一点。所有TestNG线程似乎都具有包含“ TestNG”的名称,因此我用它来标识它。然后再添加一个条件,如果该线程的ID与我的线程的ID不相同,我可以让它等待。

public void stopThisThread(long threadId) throws Throwable{
        synchronized(this) {
            System.out.println("Stop other threads called...");
            Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
            try {           
                for(Thread t : threadSet) {
                    System.out.println("Inside loop : " + printThread(t));
                    if(t.getName().contains("TestNG") && t.getId()!=threadId) {
                        t.wait();
                        System.out.println("Wait is called on " + t.getId());
                    }
                }
            }catch(Throwable t) {
                t.printStackTrace();
            }
        }
    }

按预期,它没有用。它扔了java.lang.IllegalMonitorStateException尝试2我想,为什么我们不从同一线程调用wait,然后再让它恢复操作,而不是从另一个线程调用wait。因此,这里test2将要从下拉列表中选择值。因此,我使test1线程等待。一旦test2完成了这项工作,我想调用'notify'来使test1恢复其操作。它也没有用。

@Test
public synchronized void test1() throws Throwable {
    System.out.println("test1 - " + printThread(Thread.currentThread()));       
    int i = 0;
    while(true) {           
        System.out.println("test1");
        if(i == 0)
            Thread.currentThread().wait();
        Thread.sleep(2000);         
    }
}

@Test
public synchronized void test2() throws Exception{
    System.out.println("test2 - " + printThread(Thread.currentThread()));
    int i = 0;
    while(true) {
        System.out.println("test2");
        Thread.sleep(2000);
        if(++i == 5)
            Thread.currentThread().notifyAll();
    }
}

这在两个测试用例上都抛出java.lang.IllegalMonitorStateException

有人可以帮我吗?

java multithreading selenium selenium-webdriver testng
1个回答
0
投票

支持线程管理太困难了,您会遇到很多问题。

保持测试尽可能独立。

要并行运行测试,您可以使用Selenoid之类的工具。对其进行一次调整,您将有一个良好的环境来启动测试。

我认为它将为您提供帮助。

© www.soinside.com 2019 - 2024. All rights reserved.