我有一个动态列表,其中包含5k到10k的搜索关键字。 我建立的api所收到的关键字, 收到请求后,我将同时启动5到50个浏览器(硒驱动程序)。 所有设备都需要以循环方式在网站中运行此列表,或者以任何很好的方式在每个浏览器之间分隔该列表。 浏览器需要运行而无需再次重复关键字。有人可以帮我吗?我坚持了很长时间 谢谢
编辑: 此方法可以并行启动所有浏览器(浏览器已经在重新使用)
public void testNGBrowser() {
TestNG tng = new TestNG();
tng.setPreserveOrder(true);
tng.setVerbose(1);
XmlSuite suite = new XmlSuite();
suite.setName("Keywork-Search");
suite.setParallel(XmlSuite.ParallelMode.TESTS);
suite.setThreadCount(DRIVERS.size());
suite.setVerbose(DRIVERS.size());
for (int i = 0; i < DRIVERS.size(); i++) {
XmlTest test = new XmlTest(suite);
List<XmlClass> classes = new ArrayList<>();
test.setName("KEYWORD" + i);
test.setGroupByInstances(true);
classes.add(new XmlClass(SMS.class));
test.setXmlClasses(classes);
}
List<XmlSuite> suites = new ArrayList<>();
suites.add(suite);
tng.setParallel(XmlSuite.ParallelMode.TESTS);
tng.setXmlSuites(suites);
tng.run();
}
此测试适用于所有并行设备
@Test
public void VerifyKeyword() {
System.out.println("VerifyKeyword method, ThreadID: " +
Thread.currentThread().getId());
try {
Thread.sleep((long) (Math.random() * 10000));
} catch (InterruptedException e) {
e.printStackTrace();
}
WebDriver driver = getWebDriver();
VerifyPage verifyPage = new VerifyPage(driver);
try {
for (int i = 0; i < KEYWORDS.size(); i++){
keyword= KEYWORDS.get(KEYWORDS.size() - 1);
KEYWORDS.remove(keyword);
verifyPage.verifyKeyword(keyword);
}
}
}
我总是尝试使用最后一个关键字,然后删除已经运行的关键字,但是有时会有两个使用相同关键字的浏览器冲突
答案 0 :(得分:0)
尝试添加为您的任务指定的类线程,并将关键字列表与每个线程的关键字块分开:
@Test
public void verifyKeywordTest() {
// Beginning of code here
List<String> keywordsChunks = getChuncks(KEYWORDS);
keywordsChunks.forEach(chunk -> {
VerifyKeyword verifyKeyword = new VerifyKeyword(getWebDriver(), LOCK, chunk);
verifyKeyword.start();
// Rest of the code here
可运行类:
class VerifyKeyword implements Runnable {
WebDriver driver;
VerifyPage verifyPage;
private Thread thread;
private List<String> keywords;
private final Object LOCK;
private volatile boolean exit = false;
public VerifyKeyword(
WebDriver driver, Logger logger, Object LOCK, List<String> keywords) {
this.driver = driver;
this.keywords = keywords;
this.LOCK = LOCK;
}
@Override
public void run() {
VerifyPage verifyPage = new VerifyPage(driver);
try {
for (int i = 0; i < keywords.size(); i++){
keyword = keywords.get(keywords.size() - 1);
keywords.remove(keyword);
verifyPage.verifyKeyword(keyword);
}
}
synchronized (LOCK) {
LOCK.notify();
}
}
public void start() {
if (thread == null) {
thread = new Thread(this, this.getClass().getName());
thread.start();
}
}
public void stop() {
exit = true;
}
}