I have a test that loads a page and searches for an element. If it is not present, wait a time interval and then go back and search again. If the element is present output the text, and then wait a time interval and repeat the test.
My reason for my selenium script is to continually scrape a webpage from a console that displays amount of current viewers, and console out the variable saved every ~10 minutes. I want to do this recursively since the test will complete once it runs through one time. I want this to run as long as I want it to, and stop it at will. Any help would be greatly appreciated
//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();
}