简体   繁体   English

Selenium - 在继续之前等待Javascript函数执行

[英]Selenium - Wait for Javascript function to execute before continuing

I'm currently creating some test cases with Selenium and I've come across a problem. 我目前正在用Selenium创建一些测试用例,我遇到了一个问题。

In my test case, the website I'm trying to walk through has a small form and a button to search. 在我的测试用例中,我试图浏览的网站有一个小表单和一个搜索按钮。 No problem filling the form and clicking the button. 填写表单并单击按钮没问题。 The problem comes once clicking the problem. 单击问题后会出现问题。

Once clicked the button, this function is called: 点击按钮后,会调用此函数:

function muestraEspera(){
document.getElementById("divCargando").style.display = "";
}

Basically, this makes visible a DIV which contains an "loading" image so the visitor do just see the image and wont be able to actually see the website loading the content until finished (the content is loaded with ajax). 基本上,这使得可见DIV包含“加载”图像,因此访问者只看到图像并且不能实际看到网站加载内容直到完成(内容加载了ajax)。

Once the content is loaded, this function is executed: 加载内容后,执行以下功能:

function escondeEspera(){
document.getElementById("divCargando").style.display = "none";
}

Which basically hides the "loading" DIV so the visitor can see the results. 这基本上隐藏了“加载”DIV,因此访问者可以看到结果。

Now, I can't use SLEEPS because loading can take more or less, and because I need the real execution time of the website. 现在,我不能使用SLEEPS,因为加载可能需要更多或更少,因为我需要网站的实际执行时间。 Is there any way (using java - junit4) to make selenium wait until the second function is executed before continuing with the next steps? 是否有任何方法(使用java-junit4)使selenium等到第二个函数执行后再继续下一步?

EDIT: I do use Selenium RC. 编辑:我确实使用Selenium RC。 To start the driver I use: 要启动我使用的驱动程序:

public void setUp() throws Exception {
        selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "website-url");
        selenium.start();
    }

At the end, the solution that worked perfectly for me, given by Pavel Janicek is: 最后,由Pavel Janicek给出的完美适合我的解决方案是:

boolean doTheLoop = true;
      int i = 0;
      while (doTheLoop){      
      i= i+200;
      Thread.sleep(1000);
      if (i>30000){
        doTheLoop = false;
      }
      if (!selenium.isVisible("id=divCargando")){
         doTheLoop = false;
      }      
      if (selenium.isVisible("id=divCargando")){
             doTheLoop = true;
          }      
     }

You can use waitForCondition 你可以使用waitForCondition
And if you're using WebDriver, you can try WebDriverBackedSelenium or FluentWait. 如果您使用的是WebDriver,则可以尝试WebDriverBackedSelenium或FluentWait。
I think this link will help. 我认为这个链接会有所帮助。

You should try this. 你应该试试这个。 It waits until the specified timeout. 它一直等到指定的超时。 And what it waits for is specified in the FluentWait-object. 它等待的内容在FluentWait对象中指定。 It waits until the Boolean gets true. 它一直等到布尔值变为true。 So if your element is not visible any more, the Boolean gets true and the method stops waiting. 因此,如果您的元素不再可见,则布尔值为true,方法停止等待。 The nice thing about this is that it only asks every 1 second if your element is visible instead of asking as fast as it can which makes no sense. 关于这一点的好处是,它只会每1秒钟询问一下你的元素是否可见,而不是尽可能快地询问,这是没有意义的。

public static void wait(WebDriver driver, int timeout, final By locator){
    FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver)
       .withTimeout(timeout, TimeUnit.SECONDS)
       .pollingEvery(1, TimeUnit.SECONDS)
       .ignoring(NoSuchElementException.class)

    wait.until(new Function<WebDriver, Boolean>() {
        public Boolean apply(WebDriver driver) {
            WebElement element = driver.findElement(locator);
            return !element.isDisplayed();
        }
    });
}

EDIT: As you wrote in your comments and in your edit, it seems that you use Selenium 1. WebDriver is part of Selenium 2. So just get the wrapped driver like this: 编辑:正如您在评论和编辑中所写,似乎您使用Selenium 1. WebDriver是Selenium 2的一部分。所以只需获得包装的驱动程序:

Selenium selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "website-url");
CommandExecutor executor = new SeleneseCommandExecutor(selenium); 
WebDriver driver = new RemoteWebDriver(executor, new DesiredCapabilities());

** EDIT 3** **编辑3 **

So we have: 所以我们有:

DefaultSelenium selenium = new DefaultSelenium("localhost",4444,"*iexplore", "websiteURL");

Still you can use the command isVisible like this: 仍然可以像这样使用命令isVisible

boolean doTheLoop = true;
    int i = 0;
    while (doTheLoop){      
        i = i+200;
        Thread.sleep(200);
        if (i>30000){
            doTheLoop = false;
        }
        if (!selenium.isVisible("id=the ID Of element")){
            doTheLoop = false;
      }      
}

Hope you will not end up in infinite loop. 希望你不会陷入无限循环。

I never worked with DefaultSelenium, so use please the isVisible() function the same way as you are using the click() for example. 我从未使用过DefaultSelenium,所以请使用isVisible()函数,就像使用click()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM