我正在并行运行我的测试用例。很多时候,我在从 react 下拉列表中选择值时遇到问题。假设,我必须从 react 下拉列表中选择一个值,我必须执行 2 个操作:
1. 单击下拉按钮。
2. 从下拉列表中选择值。
有时,在并行执行过程中,执行完第一步后,下一个线程正在运行,因此窗口会切换。当它切换回第一个窗口并执行第二步时,下拉列表不再存在,因此测试用例失败。
下拉菜单如下所示:
无论如何,我是否应该让其他线程等待一段时间,直到该线程执行该操作?
我尝试了什么?
尝试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 - 如何让其他测试用例等待一段时间,直到测试用例在 TestNG 并行执行中执行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60573416/