Created
January 27, 2013 20:38
-
-
Save Ardesco/4650349 to your computer and use it in GitHub Desktop.
Solution for bensullivan
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
import org.openqa.selenium.By | |
import org.openqa.selenium.StaleElementReferenceException | |
import org.openqa.selenium.WebDriver | |
import org.openqa.selenium.WebElement | |
import org.openqa.selenium.firefox.FirefoxDriver | |
import org.openqa.selenium.support.ui.ExpectedCondition | |
import org.openqa.selenium.support.ui.WebDriverWait | |
import static org.openqa.selenium.support.ui.ExpectedConditions.elementToBeClickable | |
import static org.openqa.selenium.support.ui.ExpectedConditions.findElement | |
def browser = new FirefoxDriver() | |
browser.manage().window().maximize() | |
browser.get('http://translate.google.com/') | |
def wait = new WebDriverWait(browser, 10) | |
def languageButton = browser.findElementById('gt-sl-gms') | |
languageButton.click() | |
def norwegianLanguageLinkLocator = By.xpath("//div[text()='Norwegian']") | |
wait.until(elementToBeClickable(norwegianLanguageLinkLocator)).click() | |
println languageButton.text | |
browser.findElementById('source').sendKeys('ost') | |
def resultTextBoxLocator = By.cssSelector('#result_box span') | |
def result_box = wait.until(textToBePopulatedInElement(resultTextBoxLocator)) | |
println "Result text: \"${result_box.text}\"" | |
browser.quit() | |
public static ExpectedCondition<WebElement> textToBePopulatedInElement( | |
final By locator) { | |
return new ExpectedCondition<WebElement>() { | |
@Override | |
public WebElement apply(WebDriver driver) { | |
try { | |
WebElement element = findElement(locator, driver) | |
if (!element.getText().empty) { | |
return element | |
} | |
} catch (StaleElementReferenceException ignored) { | |
return null; | |
} | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment