需要Thread.sleep();的替代解决方案; [英] Needs alternative solution to Thread.sleep();

查看:616
本文介绍了需要Thread.sleep();的替代解决方案;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Selenium Web驱动程序,i该体系结构是由Selenium IDE组件设计的,对于其他事件,则实现了核心java方法. 我目前面临的主要问题是,我必须根据Internet的速度来设计脚本,并且大多数情况下,由于超时错误,脚本会崩溃.

I am using Selenium Web-driver,i that architecture is designed by the Selenium IDE component and for other events core java methods are implemented. The main problem that I ma facing currently is,I have to design my Script according to the Speed of the Internet,And most of time script crashes because of time out error.

为解决此问题,我实现了"Thread.Sleep();". java方法. 但是使用此解决方案,系统自动化变慢了,系统必须等到线程完成其计时器完成.

To overcome this problem I am implementing the "Thread.Sleep();" java method. But with this solution,system automation got slow,And System has to wait till the Thread gets its timer completed.

因此,我需要针对此问题的优化解决方案,例如A方法,该方法将保留进一步的操作,直到加载整个页面为止.

So,I need an optimized solution for this problem like A method which will hold the further action till the complete page got loaded.

推荐答案

您还可以尝试这段代码,等待浏览器提供是否加载了页面.

you can also try out this code that waits for the browser to provide whether page loaded or not.

objExecutor = (JavascriptExecutor) objDriver;
if (!objExecutor.executeScript("return document.readyState").toString()
    .equalsIgnoreCase("complete")){
    Thread.sleep(100);
}

您可以简单地将其放在基础页面中,因此您无需在每个pageobjects中写下它.我从未在任何启用AJAX的网站上进行过尝试,但这可能会帮助您,并且您的方案依赖关系也将摆脱.

You can simply put it in your base page so you wont need to write it down in every pageobjects. I have never tried it out with any AJAX enabled sites, but this might help you and your scenario dependency will also get away.

我更喜欢使用Selenium的FluentWait API.通过使用它,您可以额外设置最大等待时间(.withTimeout),也可以忽略某些异常.当您使用FluentWait搜索页面上的元素时,这一点尤其重要:

I prefer to use the FluentWait API of Selenium. By using it you can additionally set a maximum waiting time (.withTimeout) and can also ignore certain exception. This is especially important when you use FluentWait to search for elements on a page:

public void waitUntilDocumentIsReady() {
    Wait<WebDriver> wait = new FluentWait<>(this.webDriver())
            .withTimeout(25, TimeUnit.SECONDS)
            .pollingEvery(1, TimeUnit.SECONDS)
            .ignoring(UnhandledAlertException.class);

    wait.until(new Function<WebDriver, Boolean>() {
        @Override
        public Boolean apply(WebDriver f) {
            return ((JavascriptExecutor) f).executeScript("return document.readyState").equals("complete");
        }
    });

这篇关于需要Thread.sleep();的替代解决方案;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆