【问题标题】:Recursive loop Selenium Webdriver递归循环 Selenium Webdriver
【发布时间】:2016-06-23 02:15:03
【问题描述】:

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

我使用 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();
    }

【问题讨论】:

标签: java maven selenium recursion junit


【解决方案1】:

我在这里看不到任何递归,只有预定的重复。如果您的代码成为由任务调度程序管理的任务,例如ScheduledExecutorService,它肯定会简化您的代码。你可以从你的代码中去掉所有的循环和休眠。

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)

【讨论】:

  • 在阅读本文之前,我尝试使用 Timer 和 TimerTask,但似乎在运行脚本时,它永远不会到达 timertask。你还会推荐 ScheduledExecutorService 而不是 timertask 吗?
  • 是的。 Timer/TimerTask 仍然有效,但现在并不理想。请参阅:stackoverflow.com/a/409993/954442stackoverflow.com/q/6111645/954442
猜你喜欢
  • 2014-05-09
  • 2018-12-30
  • 2022-11-29
  • 2018-06-27
  • 2017-01-21
  • 1970-01-01
  • 1970-01-01
  • 2013-08-12
  • 2011-02-09
相关资源
最近更新 更多