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

How to make other test cases to wait for some time till a test case performs an action in TestNG parallel execution

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

有时,在并行执行期间,执行完第一步后,下一个线程是运行,因此window被切换。当它切换回第一个 window 并执行第二步时,下拉菜单不再存在,因此测试用例失败。

下拉菜单如下所示:

有没有办法让其他线程等待一段时间,直到这个线程执行操作?

我试了什么?
尝试 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



谁能帮我解决这个问题?

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

尽可能保持测试独立。

要运行 并行测试,您可以使用像Selenoid 这样的工具。调整一次,您将有一个良好的环境来启动您的测试。

我想这会对你有所帮助。

@Ilya 感谢您的回答。我不想,但我不得不。我检查了其他选项。终于找到了join

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

@Test
public void test2() throws Throwable {
    System.out.println("test2 - " + printThread(Thread.currentThread()));   
    int j = 0;
    while(true) {
        System.out.println("test2");
        if(j==0)
            Thread.currentThread().join(10000);
        Thread.sleep(2000); 
        j++;
    }
}

@Test
public void test3() throws Throwable {
    System.out.println("test3 - " + printThread(Thread.currentThread()));   
    int j = 0;
    while(true) {
        System.out.println("test3");
        if(j==0)
            Thread.currentThread().join(10000);
        Thread.sleep(2000); 
        j++;
    }
}

输出如下:

test2 - 14 - TestNG-test=test-2 - false
test2
test3 - 15 - TestNG-test=test-3 - false
test3
test1 - 13 - TestNG-test=test-1 - false
test1
test1
test1
test1
test1
test1
test3
test2
test1
test3
test2
test1

我以同样的方式修改了我的项目。我现在可以毫不费力地选择下拉值。