/*
Copyright 2007-2009 WebDriver committers
Copyright 2007-2009 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package org.openqa.selenium.internal.seleniumemulation;

import com.google.common.base.Throwables;
import com.thoughtworks.selenium.Wait;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;

import java.util.logging.Logger;

public class WaitForPageToLoad extends SeleneseCommand<Void> {
  Logger log = Logger.getLogger(WaitForPageToLoad.class.getName());
  private int timeToWait = 250;

  /**
   * Overrides the default time to wait (in milliseconds) after a page has finished loading.
   *
   * @param timeToWait the time to wait, in milliseconds, after the page has finished loading
   */
  public void setTimeToWait(int timeToWait) {
    this.timeToWait = timeToWait;
  }

  @Override
  protected Void handleSeleneseCommand(final WebDriver driver, String timeout, String ignored) {
    // Wait until things look like they've been stable for "timeToWait"
    if (!(driver instanceof JavascriptExecutor)) {
      // Assume that we Do The Right Thing
      return null;
    }

    long timeoutInMillis = Long.parseLong(timeout);

    // Micro sleep before we continue in case an async click needs processing.
    pause();

    Object result = ((JavascriptExecutor) driver).executeScript(
        "return !!document['readyState'];");

    log.fine("Does browser support readyState: " + result);

    Wait wait = (result != null && (Boolean) result) ?
        getReadyStateUsingWait(driver) : getLengthCheckingWait(driver);

    wait.wait(String.format("Failed to load page within %s ms", timeout), timeoutInMillis);

    pause();

    return null;
  }

  private void pause() {
    try {
      Thread.sleep(timeToWait);
    } catch (InterruptedException e) {
      throw Throwables.propagate(e);
    }
  }

  private Wait getReadyStateUsingWait(final WebDriver driver) {
    return new Wait() {
      public boolean until() {
        try {
          Object result = ((JavascriptExecutor) driver).executeScript(
              "return 'complete' == document.readyState;");

          if (result != null && result instanceof Boolean && (Boolean) result) {
            return true;
          }
        } catch (Exception e) {
          // Possible page reload. Fine
        }
        return false;
      }
    };
  }

  public Wait getLengthCheckingWait(final WebDriver driver) {
    return new Wait() {
      private int length;
      private long seenAt;

      @Override
      public boolean until() {
        // Page length needs to be stable for a second
        try {
          int currentLength = driver.findElement(By.tagName("body")).getText().length();
          if (seenAt == 0) {
            seenAt = System.currentTimeMillis();
            length = currentLength;
            return false;
          }

          if (currentLength != length) {
            seenAt = System.currentTimeMillis();
            length = currentLength;
            return false;
          }

          return System.currentTimeMillis() - seenAt > 1000;
        } catch (NoSuchElementException ignored) {}
        catch (NullPointerException ignored) {}

        return false;
      }
    };
  }
}
@@ -903,16 +903,63 @@
 bDriver;
+%0Aimport org.openqa.selenium.WebDriverException;
 %0A%0Aimport
@@ -1880,28 +1880,319 @@
     
-pause();%0A%0A    Object
+hesitate(timeToWait);%0A%0A    Object result;%0A    try %7B%0A       result = ((JavascriptExecutor) driver).executeScript(%0A        %22return !!document%5B'readyState'%5D;%22);%0A    %7D catch (WebDriverException e) %7B%0A      // Page might still be loading. Give it a chance to get some content.%0A      hesitate(500);%0A      try %7B%0A       
  res
@@ -2242,32 +2242,34 @@
 Script(%0A        
+  
 %22return !!docume
@@ -2284,24 +2284,192 @@
 yState'%5D;%22);
+%0A      %7D catch (WebDriverException e2) %7B%0A        log.warning(%22Cannot determine whether page supports ready state. Abandoning wait.%22);%0A        return null;%0A      %7D%0A    %7D
 %0A%0A    log.fi
@@ -2744,22 +2744,35 @@
 );%0A%0A    
-pause(
+hesitate(timeToWait
 );%0A%0A    
@@ -2808,14 +2808,30 @@
 oid 
-pause(
+hesitate(long duration
 ) %7B%0A
@@ -2859,26 +2859,24 @@
 d.sleep(
-timeToWait
+duration
 );%0A    %7D

Deal with partially loaded pages not executing JS properly when waiting. Address issue 1972.