Several examples demonstrating some features of Selenium WebDriver using Java
This commit is contained in:
commit
ae1223a949
|
|
@ -0,0 +1,7 @@
|
||||||
|
# Unwanted Eclipse IDE settings files:
|
||||||
|
/.classpath
|
||||||
|
/.project
|
||||||
|
/.settings/
|
||||||
|
|
||||||
|
# Unwanted build generated folders:
|
||||||
|
/bin/
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
# Page Object Model
|
||||||
|
|
||||||
|
Several examples demonstrating some features of Selenium WebDriver using Java.
|
||||||
|
|
||||||
|
Each class has its own main method, so they are meant to be executed individually to showcase different Selenium features.
|
||||||
|
|
||||||
|
## Run on Eclipse
|
||||||
|
|
||||||
|
1. **Package Explorer**
|
||||||
|
2. Select the `.java` file that you want to run.
|
||||||
|
3. Contextual menu > **Run As** > **Java Application**
|
||||||
|
|
||||||
|
## Notes
|
||||||
|
|
||||||
|
- `GettingStarted.java` needs to be run with `-ea` or `-enableasserts` as arguments.
|
||||||
|
- `InputsWithUIDialogs.java` needs the name of a valid file in your system.
|
||||||
|
|
@ -0,0 +1,89 @@
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,91 @@
|
||||||
|
package waits;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
|
|
||||||
|
import org.openqa.selenium.By;
|
||||||
|
import org.openqa.selenium.WebDriver;
|
||||||
|
import org.openqa.selenium.firefox.FirefoxDriver;
|
||||||
|
|
||||||
|
public class ImplicitWait
|
||||||
|
{
|
||||||
|
private static void sleep(long milliseconds)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Thread.sleep(milliseconds);
|
||||||
|
}
|
||||||
|
catch (InterruptedException e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
|
// Specify path to WebDriver:
|
||||||
|
System.setProperty("webdriver.gecko.driver", "/snap/bin/geckodriver");
|
||||||
|
|
||||||
|
// Launch browser, set implicit waiting time, and navigate to test page:
|
||||||
|
WebDriver driver = new FirefoxDriver();
|
||||||
|
driver.manage().window().maximize();
|
||||||
|
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(1000));
|
||||||
|
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
|
||||||
|
//
|
||||||
|
// ======================================================================================== //
|
||||||
|
|
||||||
|
// Click on the Enable button that will enable the disabled input:
|
||||||
|
driver.findElement(By.id("disabled-input-toggle")).click();
|
||||||
|
|
||||||
|
// implicitlyWait is used when trying to find an element or elements that are not immediately available.
|
||||||
|
// In this case, the disabled input is available (i.e. exists in the DOM), so we sleep the execution
|
||||||
|
// before continuing:
|
||||||
|
sleep(1000);
|
||||||
|
|
||||||
|
// Enter some text in the input:
|
||||||
|
driver.findElement(By.id("disabled-input")).sendKeys("Implicit Wait with disabled elements");
|
||||||
|
|
||||||
|
// ======================================================================================== //
|
||||||
|
//
|
||||||
|
// HIDDEN INPUT
|
||||||
|
//
|
||||||
|
// ======================================================================================== //
|
||||||
|
|
||||||
|
// Click on the Hide button that will show the hidden input:
|
||||||
|
driver.findElement(By.id("hidden-input-toggle")).click();
|
||||||
|
|
||||||
|
// implicitlyWait is used when trying to find an element or elements that are not immediately available.
|
||||||
|
// In this case, the hidden input is available (i.e. exists in the DOM), so we sleep the execution
|
||||||
|
// before continuing:
|
||||||
|
sleep(1000);
|
||||||
|
|
||||||
|
// Enter some text in the input:
|
||||||
|
driver.findElement(By.id("hidden-input")).sendKeys("Implicit Wait with hidden elements");
|
||||||
|
|
||||||
|
// ======================================================================================== //
|
||||||
|
//
|
||||||
|
// DYNAMIC INPUT
|
||||||
|
//
|
||||||
|
// ======================================================================================== //
|
||||||
|
|
||||||
|
// Click on the Add button that will add a new input:
|
||||||
|
driver.findElement(By.id("add-remove-input-button")).click();
|
||||||
|
|
||||||
|
// We don't need to sleep here, as the dynamic input doesn't exist in the DOM and implicitlyWait
|
||||||
|
// will keep trying for the specified amount of time until the input is available.
|
||||||
|
|
||||||
|
// Enter some text in the input:
|
||||||
|
driver.findElement(By.id("dynamic-input")).sendKeys("Implicit Wait with dynamic elements");
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
// This is commented out so you can actually see what happened in the web page:
|
||||||
|
// driver.quit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,32 @@
|
||||||
|
package webdriver;
|
||||||
|
|
||||||
|
import org.openqa.selenium.WebDriver;
|
||||||
|
import org.openqa.selenium.firefox.FirefoxDriver;
|
||||||
|
|
||||||
|
public class GettingStarted
|
||||||
|
{
|
||||||
|
public static void main(String[] args)
|
||||||
|
{
|
||||||
|
// 1. Define the path to the WebDriver for the web browser:
|
||||||
|
System.setProperty("webdriver.gecko.driver", "/snap/bin/geckodriver");
|
||||||
|
|
||||||
|
// 2. Start a session in a maximized web browser:
|
||||||
|
WebDriver driver = new FirefoxDriver();
|
||||||
|
driver.manage().window().maximize();
|
||||||
|
|
||||||
|
// 3. Navigate to a URL:
|
||||||
|
driver.navigate().to("https://ramoncaballero.dev/sdet/selenium-webdriver/playgrounds/");
|
||||||
|
|
||||||
|
// 4. Interact with WebElements.
|
||||||
|
// When testing on a web page we want to make sure that something in it matches an expected value.
|
||||||
|
// For example, this tests that the title of the web page is what we expect:
|
||||||
|
String actual = driver.getTitle();
|
||||||
|
String expected = "ramoncaballero.dev - Selenium Playground";
|
||||||
|
assert actual.equals(expected);
|
||||||
|
|
||||||
|
System.out.print("All tests passed!");
|
||||||
|
|
||||||
|
// 5. End the session:
|
||||||
|
driver.quit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,48 @@
|
||||||
|
package webelements;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.openqa.selenium.By;
|
||||||
|
import org.openqa.selenium.WebDriver;
|
||||||
|
import org.openqa.selenium.WebElement;
|
||||||
|
import org.openqa.selenium.firefox.FirefoxDriver;
|
||||||
|
|
||||||
|
public class CheckboxesAndRadioButtons
|
||||||
|
{
|
||||||
|
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 'Checkboxes and Radio Buttons' accordion item:
|
||||||
|
driver.findElement(By.xpath("//button[contains(text(),'Checkboxes and Radio Buttons')]")).click();
|
||||||
|
|
||||||
|
// Get checkboxes and radio buttons by XPath:
|
||||||
|
List<WebElement> checkboxes = driver.findElements(By.xpath("//div[@id='checkboxes']//input"));
|
||||||
|
List<WebElement> radioButtons = driver.findElements(By.xpath("//div[@id='radiobuttons']//input"));
|
||||||
|
|
||||||
|
// Shuffle the lists to get random order:
|
||||||
|
Collections.shuffle(checkboxes);
|
||||||
|
Collections.shuffle(radioButtons);
|
||||||
|
|
||||||
|
// Click on the first 3 checkboxes:
|
||||||
|
for (int i = 0; i < 3; i++)
|
||||||
|
{
|
||||||
|
checkboxes.get(i).click();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Click on the first radio button:
|
||||||
|
radioButtons.get(0).click();
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
// This is commented out so you can actually see what happened in the web page:
|
||||||
|
// driver.quit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,69 @@
|
||||||
|
package webelements;
|
||||||
|
|
||||||
|
import java.text.ParseException;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
import org.openqa.selenium.By;
|
||||||
|
import org.openqa.selenium.JavascriptExecutor;
|
||||||
|
import org.openqa.selenium.WebDriver;
|
||||||
|
import org.openqa.selenium.WebElement;
|
||||||
|
import org.openqa.selenium.firefox.FirefoxDriver;
|
||||||
|
|
||||||
|
public class DatePickers
|
||||||
|
{
|
||||||
|
static WebDriver driver;
|
||||||
|
|
||||||
|
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 'Date Pickers' accordion item:
|
||||||
|
driver.findElement(By.xpath("//button[contains(text(),'Date Pickers')]")).click();
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
|
||||||
|
LocalDate today = LocalDate.now();
|
||||||
|
|
||||||
|
String inputDateAsString = formatter.format(today);
|
||||||
|
String inputDateAsStringInJavaScriptFormat = "";
|
||||||
|
|
||||||
|
//
|
||||||
|
// HTML Date Picker
|
||||||
|
//
|
||||||
|
|
||||||
|
// JavaScript uses dates in format "yyyy-MM-dd"
|
||||||
|
// We set the value of the HTML Date Picker input using JavascriptExecutor
|
||||||
|
// So we need to convert the date from format "dd-MM-yyyy" into "yyyy-MM-dd"
|
||||||
|
try
|
||||||
|
{
|
||||||
|
SimpleDateFormat inputDateFormat = new SimpleDateFormat("dd-MM-yyyy");
|
||||||
|
Date inputDate = inputDateFormat.parse(inputDateAsString);
|
||||||
|
|
||||||
|
SimpleDateFormat outputDateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
|
inputDateAsStringInJavaScriptFormat = outputDateFormat.format(inputDate);
|
||||||
|
}
|
||||||
|
catch (ParseException e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
WebElement htmlDatePickerInput = driver.findElement(By.id("html-datepicker"));
|
||||||
|
JavascriptExecutor js = (JavascriptExecutor) driver;
|
||||||
|
js.executeScript("arguments[0].value='" + inputDateAsStringInJavaScriptFormat + "';", htmlDatePickerInput);
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
// This is commented out so you can actually see what happened in the web page:
|
||||||
|
// driver.quit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,97 @@
|
||||||
|
package webelements;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.openqa.selenium.By;
|
||||||
|
import org.openqa.selenium.WebDriver;
|
||||||
|
import org.openqa.selenium.WebElement;
|
||||||
|
import org.openqa.selenium.firefox.FirefoxDriver;
|
||||||
|
|
||||||
|
public class FramesAndWindows
|
||||||
|
{
|
||||||
|
private static void sleep(long milliseconds)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Thread.sleep(milliseconds);
|
||||||
|
}
|
||||||
|
catch (InterruptedException e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 'Frames and Windows' accordion item:
|
||||||
|
driver.findElement(By.xpath("//button[contains(text(),'Frames and Windows')]")).click();
|
||||||
|
|
||||||
|
//
|
||||||
|
// FRAMES
|
||||||
|
//
|
||||||
|
|
||||||
|
// Switch to the iframe:
|
||||||
|
WebElement frame = driver.findElement(By.id("custom-iframe"));
|
||||||
|
driver.switchTo().frame(frame);
|
||||||
|
|
||||||
|
// Perform the actions we need to do:
|
||||||
|
driver.findElement(By.id("frame-button")).click();
|
||||||
|
|
||||||
|
// Switch back to the default document:
|
||||||
|
driver.switchTo().defaultContent();
|
||||||
|
|
||||||
|
//
|
||||||
|
// WINDOWS
|
||||||
|
//
|
||||||
|
|
||||||
|
// Get the handle (or identifier) of the original window:
|
||||||
|
String originalWindowHandle = driver.getWindowHandle();
|
||||||
|
|
||||||
|
// Click the button that opens a new window:
|
||||||
|
driver.findElement(By.id("new-window-button")).click();
|
||||||
|
|
||||||
|
// Most likely the new window won't finish loading its content before its title is queried by the
|
||||||
|
// driver, so we sleep the execution a little bit to give it enough time:
|
||||||
|
sleep(500);
|
||||||
|
|
||||||
|
// Get the handles of all the windows (i.e. handle of the original window + handle of the new window):
|
||||||
|
Set<String> windowHandles = driver.getWindowHandles();
|
||||||
|
|
||||||
|
// Figure out the handle of the new window:
|
||||||
|
String newWindowHandle = "";
|
||||||
|
|
||||||
|
for (String windowHandle : windowHandles)
|
||||||
|
{
|
||||||
|
if (!originalWindowHandle.contentEquals(windowHandle))
|
||||||
|
{
|
||||||
|
newWindowHandle = windowHandle;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Switch to the new window:
|
||||||
|
driver.switchTo().window(newWindowHandle);
|
||||||
|
|
||||||
|
// Perform the actions we need to do:
|
||||||
|
System.out.println("The title of the new window is: " + driver.getTitle());
|
||||||
|
|
||||||
|
// Close the window:
|
||||||
|
driver.close();
|
||||||
|
|
||||||
|
// Switch back to the default document:
|
||||||
|
driver.switchTo().window(originalWindowHandle);
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
// This is commented out so you can actually see what happened in the web page:
|
||||||
|
// driver.quit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,39 @@
|
||||||
|
package webelements;
|
||||||
|
|
||||||
|
import org.openqa.selenium.By;
|
||||||
|
import org.openqa.selenium.JavascriptExecutor;
|
||||||
|
import org.openqa.selenium.WebDriver;
|
||||||
|
import org.openqa.selenium.WebElement;
|
||||||
|
import org.openqa.selenium.firefox.FirefoxDriver;
|
||||||
|
import org.openqa.selenium.interactions.Actions;
|
||||||
|
|
||||||
|
public class InputRange
|
||||||
|
{
|
||||||
|
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 'Input Range' accordion item:
|
||||||
|
driver.findElement(By.xpath("//button[contains(text(),'Input Range')]")).click();
|
||||||
|
|
||||||
|
// Using JavascriptExecutor:
|
||||||
|
JavascriptExecutor js = (JavascriptExecutor) driver;
|
||||||
|
js.executeScript("document.getElementById('range-input').value = '0';");
|
||||||
|
|
||||||
|
// Using Actions:
|
||||||
|
WebElement slider = driver.findElement(By.id("range-input"));
|
||||||
|
Actions move = new Actions(driver);
|
||||||
|
move.dragAndDropBy(slider, 100, 0).build().perform();
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
// This is commented out so you can actually see what happened in the web page:
|
||||||
|
// driver.quit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,37 @@
|
||||||
|
package webelements;
|
||||||
|
|
||||||
|
import org.openqa.selenium.By;
|
||||||
|
import org.openqa.selenium.JavascriptExecutor;
|
||||||
|
import org.openqa.selenium.WebDriver;
|
||||||
|
import org.openqa.selenium.WebElement;
|
||||||
|
import org.openqa.selenium.firefox.FirefoxDriver;
|
||||||
|
|
||||||
|
public class InputsWithUIDialogs
|
||||||
|
{
|
||||||
|
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 'Inputs With UI Dialogs' accordion item:
|
||||||
|
driver.findElement(By.xpath("//button[contains(text(),'Inputs With UI Dialogs')]")).click();
|
||||||
|
|
||||||
|
// Color input:
|
||||||
|
JavascriptExecutor js = (JavascriptExecutor) driver;
|
||||||
|
js.executeScript("document.getElementById('color-input').value = '#000000';");
|
||||||
|
|
||||||
|
// File input:
|
||||||
|
WebElement fileInput = driver.findElement(By.id("file-input"));
|
||||||
|
fileInput.sendKeys("/etc/legal");
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
// This is commented out so you can actually see what happened in the web page:
|
||||||
|
// driver.quit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,65 @@
|
||||||
|
package webelements;
|
||||||
|
|
||||||
|
import org.openqa.selenium.Alert;
|
||||||
|
import org.openqa.selenium.By;
|
||||||
|
import org.openqa.selenium.WebDriver;
|
||||||
|
import org.openqa.selenium.firefox.FirefoxDriver;
|
||||||
|
|
||||||
|
public class JavaScriptPopupMessages
|
||||||
|
{
|
||||||
|
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 'JavaScript Popup Messages' accordion item:
|
||||||
|
driver.findElement(By.xpath("//button[contains(text(),'JavaScript Popup Messages')]")).click();
|
||||||
|
|
||||||
|
//
|
||||||
|
// ALERT
|
||||||
|
//
|
||||||
|
|
||||||
|
driver.findElement(By.id("alert-button")).click();
|
||||||
|
|
||||||
|
Alert alert = driver.switchTo().alert();
|
||||||
|
String alertText = alert.getText();
|
||||||
|
alert.accept();
|
||||||
|
|
||||||
|
System.out.println("Alert text: " + alertText);
|
||||||
|
|
||||||
|
//
|
||||||
|
// CONFIRM
|
||||||
|
//
|
||||||
|
|
||||||
|
driver.findElement(By.id("confirm-button")).click();
|
||||||
|
|
||||||
|
Alert confirm = driver.switchTo().alert();
|
||||||
|
String confirmText = confirm.getText();
|
||||||
|
confirm.dismiss();
|
||||||
|
|
||||||
|
System.out.println("Confirm text: " + confirmText);
|
||||||
|
|
||||||
|
//
|
||||||
|
// PROMPT
|
||||||
|
//
|
||||||
|
|
||||||
|
driver.findElement(By.id("prompt-button")).click();
|
||||||
|
|
||||||
|
Alert prompt = driver.switchTo().alert();
|
||||||
|
prompt.sendKeys("QA");
|
||||||
|
String promptText = prompt.getText();
|
||||||
|
prompt.dismiss();
|
||||||
|
|
||||||
|
System.out.println("Prompt text: " + promptText);
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
// This is commented out so you can actually see what happened in the web page:
|
||||||
|
// driver.quit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,79 @@
|
||||||
|
package webelements;
|
||||||
|
|
||||||
|
import org.openqa.selenium.By;
|
||||||
|
import org.openqa.selenium.WebDriver;
|
||||||
|
import org.openqa.selenium.WebElement;
|
||||||
|
import org.openqa.selenium.firefox.FirefoxDriver;
|
||||||
|
|
||||||
|
public class MostCommonInputs
|
||||||
|
{
|
||||||
|
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 'Most Common Inputs' accordion item:
|
||||||
|
driver.findElement(By.xpath("//button[contains(text(),'Most Common Inputs')]")).click();
|
||||||
|
|
||||||
|
// Text input:
|
||||||
|
WebElement textInput = driver.findElement(By.id("text-input"));
|
||||||
|
textInput.sendKeys("Selenium WebDriver");
|
||||||
|
|
||||||
|
// Read-only input:
|
||||||
|
WebElement readonlyInput = driver.findElement(By.id("readonly-input"));
|
||||||
|
String readonlyText = readonlyInput.getAttribute("value");
|
||||||
|
System.out.println("The read-only input text says '" + readonlyText + "'");
|
||||||
|
|
||||||
|
// Email input:
|
||||||
|
WebElement emailInput = driver.findElement(By.id("email-input"));
|
||||||
|
emailInput.sendKeys("hello@ramoncaballero.dev");
|
||||||
|
|
||||||
|
// Password input:
|
||||||
|
WebElement passwordInput = driver.findElement(By.id("password-input"));
|
||||||
|
passwordInput.sendKeys("YouDontNeedToKnowThis");
|
||||||
|
|
||||||
|
// Textarea:
|
||||||
|
WebElement textarea = driver.findElement(By.id("textarea"));
|
||||||
|
textarea.sendKeys("This is a textarea and I can write inside it.");
|
||||||
|
|
||||||
|
// Clear text input and write something else:
|
||||||
|
textInput.clear();
|
||||||
|
textInput.sendKeys("Interacting with WebElements");
|
||||||
|
|
||||||
|
// Click Reset button:
|
||||||
|
driver.findElement(By.id("form-interactions-reset-button")).click();
|
||||||
|
|
||||||
|
// Write again on the fields (no need to find the elements as we already have them!):
|
||||||
|
textInput.sendKeys("Interacting with WebElements with Selenium WebDriver");
|
||||||
|
emailInput.sendKeys("hello@example.com");
|
||||||
|
passwordInput.sendKeys("YouMustNotKnowThis");
|
||||||
|
textarea.sendKeys("Let's try to write something again.");
|
||||||
|
|
||||||
|
// Click Submit button:
|
||||||
|
WebElement submitButton = driver.findElement(By.id("form-interactions-submit-button"));
|
||||||
|
submitButton.click();
|
||||||
|
|
||||||
|
// The modal dialog might take a few milliseconds to appear, so let's wait in a simple way:
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Thread.sleep(500);
|
||||||
|
}
|
||||||
|
catch (InterruptedException e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close the modal dialog:
|
||||||
|
driver.findElement(By.id("form-interactions-modal-close-button")).click();
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
// This is commented out so you can actually see what happened in the web page:
|
||||||
|
// driver.quit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,52 @@
|
||||||
|
package webelements;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.openqa.selenium.By;
|
||||||
|
import org.openqa.selenium.Keys;
|
||||||
|
import org.openqa.selenium.WebDriver;
|
||||||
|
import org.openqa.selenium.WebElement;
|
||||||
|
import org.openqa.selenium.firefox.FirefoxDriver;
|
||||||
|
import org.openqa.selenium.support.ui.Select;
|
||||||
|
|
||||||
|
public class SelectDatalistAndOptions
|
||||||
|
{
|
||||||
|
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 'Select, Datalist and Options' accordion item:
|
||||||
|
driver.findElement(By.xpath("//button[contains(text(),'Select, Datalist and Options')]")).click();
|
||||||
|
|
||||||
|
// Initialize a Select object with the HTML select:
|
||||||
|
WebElement selectElement = driver.findElement(By.id("select"));
|
||||||
|
Select select = new Select(selectElement);
|
||||||
|
|
||||||
|
// Take the options and remove the 1st option
|
||||||
|
// (that option cannot be selected as it disappears when another one is selected):
|
||||||
|
List<WebElement> selectOptions = select.getOptions();
|
||||||
|
selectOptions.remove(0);
|
||||||
|
|
||||||
|
// Shuffle the options to get a random order and select the 1st one:
|
||||||
|
Collections.shuffle(selectOptions);
|
||||||
|
select.selectByVisibleText(selectOptions.get(0).getText());
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
WebElement datalistElement = driver.findElement(By.id("datalist-input"));
|
||||||
|
datalistElement.sendKeys("Batman");
|
||||||
|
datalistElement.sendKeys(Keys.RETURN);
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
// This is commented out so you can actually see what happened in the web page:
|
||||||
|
// driver.quit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,52 @@
|
||||||
|
package webelements;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import org.openqa.selenium.By;
|
||||||
|
import org.openqa.selenium.WebDriver;
|
||||||
|
import org.openqa.selenium.WebElement;
|
||||||
|
import org.openqa.selenium.firefox.FirefoxDriver;
|
||||||
|
|
||||||
|
public class Tables
|
||||||
|
{
|
||||||
|
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 'Tables' accordion item:
|
||||||
|
driver.findElement(By.xpath("//button[contains(text(),'Tables')]")).click();
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
// Obtain the rows in the 'products-batman' table:
|
||||||
|
List<WebElement> rows = driver.findElements(By.xpath(".//table[@id='products-batman']/descendant::*/tr"));
|
||||||
|
|
||||||
|
// Take the cells of the 1st row (header row):
|
||||||
|
List<WebElement> headerRow = rows.remove(0).findElements(By.xpath("th"));
|
||||||
|
|
||||||
|
// Print the table header cells:
|
||||||
|
String headerString = headerRow.stream().map(WebElement::getText).collect(Collectors.joining(", "));
|
||||||
|
System.out.println("Header: " + headerString);
|
||||||
|
|
||||||
|
// Print table body cells:
|
||||||
|
for (WebElement row : rows)
|
||||||
|
{
|
||||||
|
List<WebElement> cellsInRow = row.findElements(By.xpath("td"));
|
||||||
|
|
||||||
|
String rowString = cellsInRow.stream().map(WebElement::getText).collect(Collectors.joining(", "));
|
||||||
|
System.out.println("Row: " + rowString);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
// This is commented out so you can actually see what happened in the web page:
|
||||||
|
// driver.quit();
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue