90 lines
3.3 KiB
Java
90 lines
3.3 KiB
Java
package waits;
|
|
|
|
import java.time.Duration;
|
|
|
|
import org.openqa.selenium.By;
|
|
import org.openqa.selenium.WebDriver;
|
|
import org.openqa.selenium.WebElement;
|
|
import org.openqa.selenium.firefox.FirefoxDriver;
|
|
import org.openqa.selenium.support.ui.ExpectedConditions;
|
|
import org.openqa.selenium.support.ui.WebDriverWait;
|
|
|
|
public class ExplicitWait
|
|
{
|
|
public static void main(String[] args)
|
|
{
|
|
// Specify path to WebDriver:
|
|
System.setProperty("webdriver.gecko.driver", "/snap/bin/geckodriver");
|
|
|
|
// Launch browser and navigate to test page:
|
|
WebDriver driver = new FirefoxDriver();
|
|
driver.manage().window().maximize();
|
|
driver.get("https://ramoncaballero.dev/sdet/selenium-webdriver/playgrounds/");
|
|
|
|
// Click on the Waiting Strategies accordion item:
|
|
driver.findElement(By.xpath("//button[contains(text(),'Waiting Strategies')]")).click();
|
|
|
|
// ======================================================================================== //
|
|
//
|
|
// DISABLED INPUT
|
|
//
|
|
// ======================================================================================== //
|
|
|
|
// Get a reference to the disabled input (we can do that because the input is in the DOM):
|
|
WebElement disabledInput = driver.findElement(By.id("disabled-input"));
|
|
|
|
// Click on the Enable button that will enable the disabled input:
|
|
driver.findElement(By.id("disabled-input-toggle")).click();
|
|
|
|
// Wait for the JavaScript to enable the input:
|
|
WebDriverWait wait = new WebDriverWait(driver, Duration.ofMillis(1000));
|
|
wait.until(ExpectedConditions.elementToBeClickable(disabledInput));
|
|
|
|
// Enter some text in the input:
|
|
disabledInput.sendKeys("Explicit Wait with disabled elements");
|
|
|
|
// ======================================================================================== //
|
|
//
|
|
// HIDDEN INPUT
|
|
//
|
|
// ======================================================================================== //
|
|
|
|
// Get a reference to the hidden input (we can do that because the input is in the DOM):
|
|
WebElement hiddenInput = driver.findElement(By.id("hidden-input"));
|
|
|
|
// Click on the Hide button that will show the hidden input:
|
|
driver.findElement(By.id("hidden-input-toggle")).click();
|
|
|
|
// Wait for the JavaScript to show the input:
|
|
wait = new WebDriverWait(driver, Duration.ofMillis(1000));
|
|
wait.until(ExpectedConditions.visibilityOf(hiddenInput));
|
|
|
|
// Enter some text in the input:
|
|
hiddenInput.sendKeys("Explicit Wait with hidden elements");
|
|
|
|
// ======================================================================================== //
|
|
//
|
|
// DYNAMIC INPUT
|
|
//
|
|
// ======================================================================================== //
|
|
|
|
// We cannot get a reference to the dynamic input because it doesn't exist in the DOM yet!
|
|
// WebElement dynamicInput = driver.findElement(By.id("dynamic-input"));
|
|
|
|
// Click on the Add button that will add a new input:
|
|
driver.findElement(By.id("add-remove-input-button")).click();
|
|
|
|
// Wait for the JavaScript to add the input:
|
|
wait = new WebDriverWait(driver, Duration.ofMillis(1000));
|
|
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("dynamic-input")));
|
|
|
|
// Enter some text in the input:
|
|
driver.findElement(By.id("dynamic-input")).sendKeys("Explicit Wait with dynamic elements");
|
|
|
|
//
|
|
|
|
// This is commented out so you can actually see what happened in the web page:
|
|
// driver.quit();
|
|
}
|
|
}
|