java - 递归循环 Selenium Webdriver

标签 java maven selenium recursion junit

我有一个加载页面并搜索元素的测试。如果不存在,则等待一段时间,然后返回并再次搜索。如果该元素存在,则输出文本,然后等待一段时间并重复测试。

我使用 selenium 脚本的原因是不断从显示当前查看者数量的控制台中抓取网页,并控制台输出每约 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();
    }

最佳答案

我在这里没有看到任何递归,只有计划的重复。如果您的代码成为由任务调度程序管理的任务(例如 ScheduledExecutorService),那么它肯定会简化您的代码。 。您可以从代码中删除所有循环和 hibernate 。

ScheduledExecutorService sched = Executors.newScheduledThreadPool(1);  // single-threaded OK

final ScheduledFuture<?> future = sched.scheduleAtFixedRate( new Runnable() {
    public void run() {
        if (canDoStuffNow) {
            doStuffNow();
        }
        else {
            // Skip, wait another second
        }
    }
}, /* Delay for */ 0, /* Every */ 1, TimeUnit.SECONDS);

只要您需要,它就会以固定的速率(每秒)可靠地运行。如果需要取消,只需调用 future.cancel(true) 即可。

如果您需要不同程度的延迟,例如每1秒运行一次,但在某些情况下延迟10秒,你当然可以在任务主体中添加一个Thread.sleep(longer)

关于java - 递归循环 Selenium Webdriver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35879156/

相关文章:

java - 将 XML 解析为 Java 对象

java - java 8中 pretty-print XML

python - Selenium:检索向下滚动时加载的数据

java - 通过将数组中的值与现有键相加来构造 HashMap,而不覆盖键

java - 每次单击 JButton 时都会打开新的 JFrame

java - mvn编译错误LifecycleExecutionException

eclipse - 当我尝试在 Eclipse/STS 中部署到 Tomcat/tc 服务器时出现 ClassNotFoundException

eclipse - 自动 Maven 项目使用 Eclipse 更改分辨率

c# - Selenium ChromeDriver 的 C# 实现中反复出现 ERR_CONNECTION_RESET

python - 使用python操作第三方网站的html源代码