我有一個測試,加載一個頁面並搜索一個元素。如果它不存在,請等待一段時間,然後返回並重新搜索。如果元素存在,則輸出文本,然後等待一段時間並重複測試。遞歸循環Selenium Webdriver
我對我的硒腳本的理由是不斷地從顯示當前查看者數量的控制檯上刮取網頁,並控制每個~10分鐘保存的變量。我希望遞歸地執行此操作,因爲一旦運行一次,測試就會完成。我希望這個運行只要我想要它,並隨時停止它。任何幫助將不勝感激
//Search for channel (Change below to a new channel)
driver.findElement(By.id("channelName")).clear();
driver.findElement(By.id("channelName")).sendKeys("ch123");
//Label - updateViewer
updateViewer();
//Verify that viewer count is listed
//Label checkViewer
checkViewer();
//Once viewer is updated, loop back to updateViewer and below
loopInterval();
}
@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
private void updateViewer() throws InterruptedException {
System.out.println("Getting current viewer count");
driver.findElement(By.id("gobutton")).click();
for (int second = 0;; second++) {
if (second >= 60) fail("timeout");
try { if (isElementPresent(By.xpath("//table[@id='datatable1']/tbody/tr/td[4]"))) break; } catch (Exception e) {}
Thread.sleep(1000);
}
}
private void checkViewer() throws InterruptedException {
boolean viewElement = isElementPresent(By.xpath("//table[@id='datatable1']/tbody/tr/td[4]"));
if(!viewElement){
System.out.println("no view element, refreshing page");
updateViewer();
}
else{
String viewers = driver.findElement(By.xpath("//table[@id='datatable1']/tbody/tr/td[4]")).getText();
System.out.println(viewers);
//-- placement to write current timestamp and date + viewers to file --
// -- placement method that calls updateViewer and checkViewer in a loop --
}
}
private void loopInterval() throws InterruptedException {
updateViewer();
checkViewer();
}
嘗試使用[線程](https://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html) – 4castle