Created
August 19, 2012 17:00
-
-
Save djangofan/3396257 to your computer and use it in GitHub Desktop.
Wait for page to load with WebDriver and JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package wd.test.javascript; | |
import java.util.List; | |
import java.util.concurrent.TimeUnit; | |
import org.openqa.selenium.By; | |
import org.openqa.selenium.Dimension; | |
import org.openqa.selenium.JavascriptExecutor; | |
import org.openqa.selenium.Keys; | |
import org.openqa.selenium.Point; | |
import org.openqa.selenium.WebElement; | |
import org.openqa.selenium.firefox.FirefoxDriver; | |
public class JSTest { | |
static FirefoxDriver driver = new FirefoxDriver(); | |
private static JavascriptExecutor js; | |
static String pageLoadStatus = null; | |
public static void main ( String[] args ) { | |
driver.manage().timeouts().implicitlyWait(10000, TimeUnit.MILLISECONDS ); | |
driver.manage().window().setPosition(new Point(200, 10)); | |
driver.manage().window().setSize(new Dimension(1200, 800)); | |
driver.get("http://www.google.com"); | |
selectInGoogleDropdown( "iphone app", "development" ); | |
waitForPageToLoad(); | |
waitSeconds(5); //make sure waitForPageToLoad has finished and demonstrated | |
clickElementWithJSE( "gbql" ); //click Google logo | |
} | |
public static void clickElementWithJSE( String id ) { | |
//Create the object of JavaScript Executor | |
//click command through Javascript | |
JavascriptExecutor js = (JavascriptExecutor)driver; | |
WebElement element= driver.findElement( By.id( id ) ); | |
//Use any locator type using to identify the element | |
js.executeScript( "arguments[0].click();", element ); | |
js = null; | |
} | |
public static void waitForPageToLoad() { | |
do { | |
js = (JavascriptExecutor) driver; | |
pageLoadStatus = (String)js.executeScript("return document.readyState"); | |
System.out.print("."); | |
} while ( !pageLoadStatus.equals("complete") ); | |
System.out.println(); | |
System.out.println("Page Loaded."); | |
} | |
public static void selectInGoogleDropdown( String input, String match ) | |
{ | |
WebElement dd = driver.findElement( By.xpath( "//input[@id='gbqfq']" ) ); | |
dd.click(); | |
dd.sendKeys( input ); | |
waitSeconds(2); | |
long end = System.currentTimeMillis() + 5000; | |
while ( System.currentTimeMillis() < end ) { | |
WebElement resultsLi = driver.findElement( By.xpath( "//div[@class='gsq_a']" ) ); | |
if ( resultsLi.isDisplayed() ) break; | |
} | |
int matchedPosition = 0; | |
int optpos = 0; | |
List<WebElement> allSuggestions = driver.findElements( By.xpath( "//div[@class='gsq_a']/table/tbody/tr/td/span" ) ); | |
for ( WebElement suggestion : allSuggestions ) { | |
System.out.println( optpos + ":" + suggestion.getText() ); | |
if ( suggestion.getText().contains( match ) ) { | |
matchedPosition = optpos; | |
System.out.println("Matched option " + optpos ); | |
} else { | |
System.out.print( "." ); | |
} | |
optpos++; | |
} | |
for ( int i= 0; i < matchedPosition ; i++ ) { | |
dd.sendKeys( Keys.ARROW_DOWN ); | |
waitSeconds(1); // to slow down the arrow key so you can see it | |
} | |
WebElement targetItem = allSuggestions.get( matchedPosition ); | |
System.out.println( "Selecting Google dropdown value: " + targetItem.getText() ); | |
targetItem.click(); | |
} | |
public static void waitSeconds(int secons) { | |
System.out.print("Pausing for " + secons + " seconds: "); | |
try { | |
Thread.currentThread(); | |
int x = 1; | |
while(x <= secons) { | |
Thread.sleep(1000); | |
System.out.print(" " + x ); | |
x = x + 1; | |
} | |
System.out.print('\n'); | |
} catch (InterruptedException ex) { | |
ex.printStackTrace(); | |
} | |
} | |
} |
The description seems wrong, don't see any JavaScript code here
It's JS injection to the browser:
pageLoadStatus = (String)js.executeScript("return document.readyState");
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The description seems wrong, don't see any JavaScript code here 😄