Compare commits
No commits in common. "95e8debdda5bde15cc1c98ac8bf831efb8bc59e4" and "c4da04e4de2d4ccd18a1c3fe9750e9147b7bd5bd" have entirely different histories.
95e8debdda
...
c4da04e4de
13
README.md
13
README.md
|
|
@ -1,17 +1,16 @@
|
||||||
# Page Object Model
|
# Page Object Model
|
||||||
|
|
||||||
Examples from [Selenium WebDriver](https://gitea.ramoncaballero.dev/mon/SeleniumWebDriver) adopting Page Object Model.
|
Several examples demonstrating some features of Selenium WebDriver using Java.
|
||||||
|
|
||||||
Each test class has its own main method, so they are meant to be executed individually to showcase different Selenium features.
|
Each class has its own main method, so they are meant to be executed individually to showcase different Selenium features.
|
||||||
|
|
||||||
## Run on Eclipse
|
## Run on Eclipse
|
||||||
|
|
||||||
1. **Package Explorer**
|
1. **Package Explorer**
|
||||||
2. Browse to **src/tests** package.
|
2. Select the `.java` file that you want to run.
|
||||||
3. Select the `.java` file that you want to run.
|
3. Contextual menu > **Run As** > **Java Application**
|
||||||
4. Contextual menu > **Run As** > **Java Application**
|
|
||||||
|
|
||||||
## Notes
|
## Notes
|
||||||
|
|
||||||
- `GettingStartedTests.java` needs to be run with `-ea` or `-enableasserts` as arguments.
|
- `GettingStarted.java` needs to be run with `-ea` or `-enableasserts` as arguments.
|
||||||
- `InputsWithUIDialogsTests.java` needs the name of a valid file in your system.
|
- `InputsWithUIDialogs.java` needs the name of a valid file in your system.
|
||||||
|
|
|
||||||
|
|
@ -1,25 +0,0 @@
|
||||||
package pages;
|
|
||||||
|
|
||||||
import org.openqa.selenium.WebDriver;
|
|
||||||
|
|
||||||
public abstract class BasePage
|
|
||||||
{
|
|
||||||
protected WebDriver driver = null;
|
|
||||||
|
|
||||||
protected BasePage(WebDriver driver)
|
|
||||||
{
|
|
||||||
this.driver = driver;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void sleep(long millis)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
Thread.sleep(millis);
|
|
||||||
}
|
|
||||||
catch (InterruptedException e)
|
|
||||||
{
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,96 +0,0 @@
|
||||||
package pages.waits;
|
|
||||||
|
|
||||||
import java.time.Duration;
|
|
||||||
|
|
||||||
import org.openqa.selenium.By;
|
|
||||||
import org.openqa.selenium.WebDriver;
|
|
||||||
import org.openqa.selenium.WebElement;
|
|
||||||
import org.openqa.selenium.support.FindBy;
|
|
||||||
import org.openqa.selenium.support.PageFactory;
|
|
||||||
import org.openqa.selenium.support.ui.ExpectedConditions;
|
|
||||||
import org.openqa.selenium.support.ui.WebDriverWait;
|
|
||||||
|
|
||||||
import pages.BasePage;
|
|
||||||
|
|
||||||
public class WaitingStrategiesPage extends BasePage
|
|
||||||
{
|
|
||||||
@FindBy(xpath = "//button[contains(text(),'Waiting Strategies')]")
|
|
||||||
private WebElement accordionItem;
|
|
||||||
|
|
||||||
@FindBy(id = "disabled-input-toggle")
|
|
||||||
private WebElement enableOrDisableButton;
|
|
||||||
|
|
||||||
@FindBy(id = "disabled-input")
|
|
||||||
private WebElement enabledOrDisabledInput;
|
|
||||||
|
|
||||||
@FindBy(id = "hidden-input-toggle")
|
|
||||||
private WebElement showOrHideButton;
|
|
||||||
|
|
||||||
@FindBy(id = "hidden-input")
|
|
||||||
private WebElement shownOrHiddenInput;
|
|
||||||
|
|
||||||
@FindBy(id = "add-remove-input-button")
|
|
||||||
private WebElement addOrRemoveButton;
|
|
||||||
|
|
||||||
@FindBy(id = "dynamic-input")
|
|
||||||
private WebElement dynamicInput;
|
|
||||||
|
|
||||||
public WaitingStrategiesPage(WebDriver driver)
|
|
||||||
{
|
|
||||||
super(driver);
|
|
||||||
PageFactory.initElements(this.driver, this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void clickOnAccordionItem()
|
|
||||||
{
|
|
||||||
accordionItem.click();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void clickEnableOrDisableButton()
|
|
||||||
{
|
|
||||||
enableOrDisableButton.click();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setEnabledOrDisabledInput(String text)
|
|
||||||
{
|
|
||||||
enabledOrDisabledInput.sendKeys(text);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void clickShowOrHideButton()
|
|
||||||
{
|
|
||||||
showOrHideButton.click();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setShownOrHiddenInput(String text)
|
|
||||||
{
|
|
||||||
shownOrHiddenInput.sendKeys(text);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void clickAddOrRemoveButton()
|
|
||||||
{
|
|
||||||
addOrRemoveButton.click();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setDynamicInput(String text)
|
|
||||||
{
|
|
||||||
dynamicInput.sendKeys(text);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void waitForEnabledOrDisabledInputToBeClickable(long millis)
|
|
||||||
{
|
|
||||||
WebDriverWait wait = new WebDriverWait(driver, Duration.ofMillis(millis));
|
|
||||||
wait.until(ExpectedConditions.elementToBeClickable(enabledOrDisabledInput));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void waitForShownOrHiddenInputToBeClickable(long millis)
|
|
||||||
{
|
|
||||||
WebDriverWait wait = new WebDriverWait(driver, Duration.ofMillis(millis));
|
|
||||||
wait.until(ExpectedConditions.elementToBeClickable(shownOrHiddenInput));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void waitForDynamicInputToBePresent(long millis)
|
|
||||||
{
|
|
||||||
WebDriverWait wait = new WebDriverWait(driver, Duration.ofMillis(millis));
|
|
||||||
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("dynamic-input")));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,28 +0,0 @@
|
||||||
package pages.webelements;
|
|
||||||
|
|
||||||
import org.openqa.selenium.Alert;
|
|
||||||
import org.openqa.selenium.WebDriver;
|
|
||||||
|
|
||||||
import pages.BasePage;
|
|
||||||
|
|
||||||
public class AlertPanel extends BasePage
|
|
||||||
{
|
|
||||||
private Alert alert = null;
|
|
||||||
|
|
||||||
public AlertPanel(WebDriver driver)
|
|
||||||
{
|
|
||||||
super(driver);
|
|
||||||
alert = driver.switchTo().alert();
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getText()
|
|
||||||
{
|
|
||||||
return alert.getText();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void accept()
|
|
||||||
{
|
|
||||||
alert.accept();
|
|
||||||
driver.switchTo().defaultContent();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -9,9 +9,7 @@ import org.openqa.selenium.WebElement;
|
||||||
import org.openqa.selenium.support.FindBy;
|
import org.openqa.selenium.support.FindBy;
|
||||||
import org.openqa.selenium.support.PageFactory;
|
import org.openqa.selenium.support.PageFactory;
|
||||||
|
|
||||||
import pages.BasePage;
|
public class CheckboxesAndRadioButtonsPage
|
||||||
|
|
||||||
public class CheckboxesAndRadioButtonsPage extends BasePage
|
|
||||||
{
|
{
|
||||||
@FindBy(xpath = "//button[contains(text(),'Checkboxes and Radio Buttons')]")
|
@FindBy(xpath = "//button[contains(text(),'Checkboxes and Radio Buttons')]")
|
||||||
private WebElement accordionItem;
|
private WebElement accordionItem;
|
||||||
|
|
@ -22,9 +20,11 @@ public class CheckboxesAndRadioButtonsPage extends BasePage
|
||||||
@FindBy(xpath = "//div[@id='radiobuttons']//input")
|
@FindBy(xpath = "//div[@id='radiobuttons']//input")
|
||||||
private List<WebElement> radioButtons;
|
private List<WebElement> radioButtons;
|
||||||
|
|
||||||
|
private WebDriver driver = null;
|
||||||
|
|
||||||
public CheckboxesAndRadioButtonsPage(WebDriver driver)
|
public CheckboxesAndRadioButtonsPage(WebDriver driver)
|
||||||
{
|
{
|
||||||
super(driver);
|
this.driver = driver;
|
||||||
PageFactory.initElements(this.driver, this);
|
PageFactory.initElements(this.driver, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,28 +0,0 @@
|
||||||
package pages.webelements;
|
|
||||||
|
|
||||||
import org.openqa.selenium.Alert;
|
|
||||||
import org.openqa.selenium.WebDriver;
|
|
||||||
|
|
||||||
import pages.BasePage;
|
|
||||||
|
|
||||||
public class ConfirmPanel extends BasePage
|
|
||||||
{
|
|
||||||
private Alert confirm = null;
|
|
||||||
|
|
||||||
public ConfirmPanel(WebDriver driver)
|
|
||||||
{
|
|
||||||
super(driver);
|
|
||||||
confirm = driver.switchTo().alert();
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getText()
|
|
||||||
{
|
|
||||||
return confirm.getText();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void dismiss()
|
|
||||||
{
|
|
||||||
confirm.dismiss();
|
|
||||||
driver.switchTo().defaultContent();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,57 +0,0 @@
|
||||||
package pages.webelements;
|
|
||||||
|
|
||||||
import java.text.ParseException;
|
|
||||||
import java.text.SimpleDateFormat;
|
|
||||||
import java.util.Date;
|
|
||||||
|
|
||||||
import org.openqa.selenium.JavascriptExecutor;
|
|
||||||
import org.openqa.selenium.WebDriver;
|
|
||||||
import org.openqa.selenium.WebElement;
|
|
||||||
import org.openqa.selenium.support.FindBy;
|
|
||||||
import org.openqa.selenium.support.PageFactory;
|
|
||||||
|
|
||||||
import pages.BasePage;
|
|
||||||
|
|
||||||
public class DatePickersPage extends BasePage
|
|
||||||
{
|
|
||||||
@FindBy(xpath = "//button[contains(text(),'Date Pickers')]")
|
|
||||||
private WebElement accordionItem;
|
|
||||||
|
|
||||||
@FindBy(id = "html-datepicker")
|
|
||||||
private WebElement htmlDatePicker;
|
|
||||||
|
|
||||||
public DatePickersPage(WebDriver driver)
|
|
||||||
{
|
|
||||||
super(driver);
|
|
||||||
PageFactory.initElements(this.driver, this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void clickOnAccordionItem()
|
|
||||||
{
|
|
||||||
accordionItem.click();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setDateOnHTMLDatePicker(String inputDateAsString)
|
|
||||||
{
|
|
||||||
// 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 input date from format "dd-MM-yyyy" into "yyyy-MM-dd"
|
|
||||||
String inputDateAsStringInJavaScriptFormat = "";
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
JavascriptExecutor js = (JavascriptExecutor) driver;
|
|
||||||
js.executeScript("arguments[0].value='" + inputDateAsStringInJavaScriptFormat + "';", htmlDatePicker);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,31 +0,0 @@
|
||||||
package pages.webelements;
|
|
||||||
|
|
||||||
import org.openqa.selenium.WebDriver;
|
|
||||||
import org.openqa.selenium.WebElement;
|
|
||||||
import org.openqa.selenium.support.FindBy;
|
|
||||||
import org.openqa.selenium.support.PageFactory;
|
|
||||||
|
|
||||||
public class Frame
|
|
||||||
{
|
|
||||||
@FindBy(id = "frame-button")
|
|
||||||
private WebElement button;
|
|
||||||
|
|
||||||
private WebDriver driver = null;
|
|
||||||
|
|
||||||
public Frame(WebDriver driver, WebElement frame)
|
|
||||||
{
|
|
||||||
this.driver = driver;
|
|
||||||
PageFactory.initElements(this.driver, this);
|
|
||||||
driver.switchTo().frame(frame);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void clickButton()
|
|
||||||
{
|
|
||||||
button.click();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void dismiss()
|
|
||||||
{
|
|
||||||
driver.switchTo().defaultContent();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,41 +0,0 @@
|
||||||
package pages.webelements;
|
|
||||||
|
|
||||||
import org.openqa.selenium.WebDriver;
|
|
||||||
import org.openqa.selenium.WebElement;
|
|
||||||
import org.openqa.selenium.support.FindBy;
|
|
||||||
import org.openqa.selenium.support.PageFactory;
|
|
||||||
|
|
||||||
import pages.BasePage;
|
|
||||||
|
|
||||||
public class FramesAndWindowsPage extends BasePage
|
|
||||||
{
|
|
||||||
@FindBy(xpath = "//button[contains(text(),'Frames and Windows')]")
|
|
||||||
private WebElement accordionItem;
|
|
||||||
|
|
||||||
@FindBy(id = "custom-iframe")
|
|
||||||
private WebElement frame;
|
|
||||||
|
|
||||||
@FindBy(id = "new-window-button")
|
|
||||||
private WebElement newWindowButton;
|
|
||||||
|
|
||||||
public FramesAndWindowsPage(WebDriver driver)
|
|
||||||
{
|
|
||||||
super(driver);
|
|
||||||
PageFactory.initElements(this.driver, this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void clickOnAccordionItem()
|
|
||||||
{
|
|
||||||
accordionItem.click();
|
|
||||||
}
|
|
||||||
|
|
||||||
public WebElement frame()
|
|
||||||
{
|
|
||||||
return frame;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void clickNewWindowButton()
|
|
||||||
{
|
|
||||||
newWindowButton.click();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,42 +0,0 @@
|
||||||
package pages.webelements;
|
|
||||||
|
|
||||||
import org.openqa.selenium.JavascriptExecutor;
|
|
||||||
import org.openqa.selenium.WebDriver;
|
|
||||||
import org.openqa.selenium.WebElement;
|
|
||||||
import org.openqa.selenium.interactions.Actions;
|
|
||||||
import org.openqa.selenium.support.FindBy;
|
|
||||||
import org.openqa.selenium.support.PageFactory;
|
|
||||||
|
|
||||||
import pages.BasePage;
|
|
||||||
|
|
||||||
public class InputRangePage extends BasePage
|
|
||||||
{
|
|
||||||
@FindBy(xpath = "//button[contains(text(),'Input Range')]")
|
|
||||||
private WebElement accordionItem;
|
|
||||||
|
|
||||||
@FindBy(id = "range-input")
|
|
||||||
private WebElement rangeInput;
|
|
||||||
|
|
||||||
public InputRangePage(WebDriver driver)
|
|
||||||
{
|
|
||||||
super(driver);
|
|
||||||
PageFactory.initElements(this.driver, this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void clickOnAccordionItem()
|
|
||||||
{
|
|
||||||
accordionItem.click();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setRangeInputValue(int value)
|
|
||||||
{
|
|
||||||
JavascriptExecutor js = (JavascriptExecutor) driver;
|
|
||||||
js.executeScript("arguments[0].value = arguments[1];", rangeInput, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void dragAndDropRangeInputBy(int x, int y)
|
|
||||||
{
|
|
||||||
Actions move = new Actions(driver);
|
|
||||||
move.dragAndDropBy(rangeInput, x, y).build().perform();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -6,9 +6,7 @@ import org.openqa.selenium.WebElement;
|
||||||
import org.openqa.selenium.support.FindBy;
|
import org.openqa.selenium.support.FindBy;
|
||||||
import org.openqa.selenium.support.PageFactory;
|
import org.openqa.selenium.support.PageFactory;
|
||||||
|
|
||||||
import pages.BasePage;
|
public class InputsWithUIDialogsPage
|
||||||
|
|
||||||
public class InputsWithUIDialogsPage extends BasePage
|
|
||||||
{
|
{
|
||||||
@FindBy(xpath = "//button[contains(text(),'Inputs With UI Dialogs')]")
|
@FindBy(xpath = "//button[contains(text(),'Inputs With UI Dialogs')]")
|
||||||
private WebElement accordionItem;
|
private WebElement accordionItem;
|
||||||
|
|
@ -19,9 +17,11 @@ public class InputsWithUIDialogsPage extends BasePage
|
||||||
@FindBy(id = "file-input")
|
@FindBy(id = "file-input")
|
||||||
private WebElement fileInput;
|
private WebElement fileInput;
|
||||||
|
|
||||||
|
private WebDriver driver = null;
|
||||||
|
|
||||||
public InputsWithUIDialogsPage(WebDriver driver)
|
public InputsWithUIDialogsPage(WebDriver driver)
|
||||||
{
|
{
|
||||||
super(driver);
|
this.driver = driver;
|
||||||
PageFactory.initElements(this.driver, this);
|
PageFactory.initElements(this.driver, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,49 +0,0 @@
|
||||||
package pages.webelements;
|
|
||||||
|
|
||||||
import org.openqa.selenium.WebDriver;
|
|
||||||
import org.openqa.selenium.WebElement;
|
|
||||||
import org.openqa.selenium.support.FindBy;
|
|
||||||
import org.openqa.selenium.support.PageFactory;
|
|
||||||
|
|
||||||
import pages.BasePage;
|
|
||||||
|
|
||||||
public class JavaScriptPopupMessagesPage extends BasePage
|
|
||||||
{
|
|
||||||
@FindBy(xpath = "//button[contains(text(),'JavaScript Popup Messages')]")
|
|
||||||
private WebElement accordionItem;
|
|
||||||
|
|
||||||
@FindBy(id = "alert-button")
|
|
||||||
private WebElement alertButton;
|
|
||||||
|
|
||||||
@FindBy(id = "confirm-button")
|
|
||||||
private WebElement confirmButton;
|
|
||||||
|
|
||||||
@FindBy(id = "prompt-button")
|
|
||||||
private WebElement promptButton;
|
|
||||||
|
|
||||||
public JavaScriptPopupMessagesPage(WebDriver driver)
|
|
||||||
{
|
|
||||||
super(driver);
|
|
||||||
PageFactory.initElements(this.driver, this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void clickOnAccordionItem()
|
|
||||||
{
|
|
||||||
accordionItem.click();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void clickAlertButton()
|
|
||||||
{
|
|
||||||
alertButton.click();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void clickConfirmButton()
|
|
||||||
{
|
|
||||||
confirmButton.click();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void clickPromptButton()
|
|
||||||
{
|
|
||||||
promptButton.click();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -6,16 +6,16 @@ import org.openqa.selenium.support.FindBy;
|
||||||
import org.openqa.selenium.support.How;
|
import org.openqa.selenium.support.How;
|
||||||
import org.openqa.selenium.support.PageFactory;
|
import org.openqa.selenium.support.PageFactory;
|
||||||
|
|
||||||
import pages.BasePage;
|
public class ModalDialogPage
|
||||||
|
|
||||||
public class ModalDialogPage extends BasePage
|
|
||||||
{
|
{
|
||||||
@FindBy(how = How.ID, using = "form-interactions-modal-close-button")
|
@FindBy(how = How.ID, using = "form-interactions-modal-close-button")
|
||||||
private WebElement closeButton;
|
private WebElement closeButton;
|
||||||
|
|
||||||
|
private WebDriver driver = null;
|
||||||
|
|
||||||
public ModalDialogPage(WebDriver driver)
|
public ModalDialogPage(WebDriver driver)
|
||||||
{
|
{
|
||||||
super(driver);
|
this.driver = driver;
|
||||||
PageFactory.initElements(this.driver, this);
|
PageFactory.initElements(this.driver, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,7 @@ import org.openqa.selenium.WebElement;
|
||||||
import org.openqa.selenium.support.FindBy;
|
import org.openqa.selenium.support.FindBy;
|
||||||
import org.openqa.selenium.support.PageFactory;
|
import org.openqa.selenium.support.PageFactory;
|
||||||
|
|
||||||
import pages.BasePage;
|
public class MostCommonInputsPage
|
||||||
|
|
||||||
public class MostCommonInputsPage extends BasePage
|
|
||||||
{
|
{
|
||||||
@FindBy(xpath = "//button[contains(text(),'Most Common Inputs')]")
|
@FindBy(xpath = "//button[contains(text(),'Most Common Inputs')]")
|
||||||
private WebElement accordionItem;
|
private WebElement accordionItem;
|
||||||
|
|
@ -33,9 +31,11 @@ public class MostCommonInputsPage extends BasePage
|
||||||
@FindBy(id = "form-interactions-submit-button")
|
@FindBy(id = "form-interactions-submit-button")
|
||||||
private WebElement submitButton;
|
private WebElement submitButton;
|
||||||
|
|
||||||
|
private WebDriver driver = null;
|
||||||
|
|
||||||
public MostCommonInputsPage(WebDriver driver)
|
public MostCommonInputsPage(WebDriver driver)
|
||||||
{
|
{
|
||||||
super(driver);
|
this.driver = driver;
|
||||||
PageFactory.initElements(this.driver, this);
|
PageFactory.initElements(this.driver, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -83,4 +83,16 @@ public class MostCommonInputsPage extends BasePage
|
||||||
{
|
{
|
||||||
return readonlyInput.getAttribute("value");
|
return readonlyInput.getAttribute("value");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void sleep(long millis)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Thread.sleep(millis);
|
||||||
|
} catch (InterruptedException e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,53 +0,0 @@
|
||||||
package pages.webelements;
|
|
||||||
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import org.openqa.selenium.WebDriver;
|
|
||||||
|
|
||||||
import pages.BasePage;
|
|
||||||
|
|
||||||
public class NewWindow extends BasePage
|
|
||||||
{
|
|
||||||
private String originalWindowHandle;
|
|
||||||
|
|
||||||
public NewWindow(WebDriver driver)
|
|
||||||
{
|
|
||||||
super(driver);
|
|
||||||
|
|
||||||
// Get the handle (or identifier) of the original window:
|
|
||||||
originalWindowHandle = driver.getWindowHandle();
|
|
||||||
|
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String title()
|
|
||||||
{
|
|
||||||
return driver.getTitle();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void close()
|
|
||||||
{
|
|
||||||
driver.close();
|
|
||||||
driver.switchTo().window(originalWindowHandle);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,33 +0,0 @@
|
||||||
package pages.webelements;
|
|
||||||
|
|
||||||
import org.openqa.selenium.Alert;
|
|
||||||
import org.openqa.selenium.WebDriver;
|
|
||||||
|
|
||||||
import pages.BasePage;
|
|
||||||
|
|
||||||
public class PromptPanel extends BasePage
|
|
||||||
{
|
|
||||||
private Alert prompt = null;
|
|
||||||
|
|
||||||
public PromptPanel(WebDriver driver)
|
|
||||||
{
|
|
||||||
super(driver);
|
|
||||||
prompt = driver.switchTo().alert();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setText(String text)
|
|
||||||
{
|
|
||||||
prompt.sendKeys(text);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getText()
|
|
||||||
{
|
|
||||||
return prompt.getText();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void dismiss()
|
|
||||||
{
|
|
||||||
prompt.dismiss();
|
|
||||||
driver.switchTo().defaultContent();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,58 +0,0 @@
|
||||||
package pages.webelements;
|
|
||||||
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.openqa.selenium.Keys;
|
|
||||||
import org.openqa.selenium.WebDriver;
|
|
||||||
import org.openqa.selenium.WebElement;
|
|
||||||
import org.openqa.selenium.support.FindBy;
|
|
||||||
import org.openqa.selenium.support.PageFactory;
|
|
||||||
import org.openqa.selenium.support.ui.Select;
|
|
||||||
|
|
||||||
import pages.BasePage;
|
|
||||||
|
|
||||||
public class SelectDatalistAndOptionsPage extends BasePage
|
|
||||||
{
|
|
||||||
@FindBy(xpath = "//button[contains(text(),'Select, Datalist and Options')]")
|
|
||||||
private WebElement accordionItem;
|
|
||||||
|
|
||||||
@FindBy(id = "select")
|
|
||||||
private WebElement selectElement;
|
|
||||||
|
|
||||||
@FindBy(id = "datalist-input")
|
|
||||||
private WebElement datalistElement;
|
|
||||||
|
|
||||||
Select select = null;
|
|
||||||
List<WebElement> selectOptions = null;
|
|
||||||
|
|
||||||
public SelectDatalistAndOptionsPage(WebDriver driver)
|
|
||||||
{
|
|
||||||
super(driver);
|
|
||||||
PageFactory.initElements(this.driver, this);
|
|
||||||
|
|
||||||
select = new Select(selectElement);
|
|
||||||
|
|
||||||
// Take the options and remove the 1st option so it cannot be selected:
|
|
||||||
selectOptions = select.getOptions();
|
|
||||||
selectOptions.remove(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void clickOnAccordionItem()
|
|
||||||
{
|
|
||||||
accordionItem.click();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void selectRandomOption()
|
|
||||||
{
|
|
||||||
// Shuffle the options to get a random order and select the 1st one:
|
|
||||||
Collections.shuffle(selectOptions);
|
|
||||||
select.selectByVisibleText(selectOptions.get(0).getText());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void selectFromDatalist(String text)
|
|
||||||
{
|
|
||||||
datalistElement.sendKeys(text);
|
|
||||||
datalistElement.sendKeys(Keys.RETURN);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,57 +0,0 @@
|
||||||
package pages.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.support.FindBy;
|
|
||||||
import org.openqa.selenium.support.PageFactory;
|
|
||||||
|
|
||||||
import pages.BasePage;
|
|
||||||
|
|
||||||
public class TablesPage extends BasePage
|
|
||||||
{
|
|
||||||
@FindBy(xpath = "//button[contains(text(),'Tables')]")
|
|
||||||
private WebElement accordionItem;
|
|
||||||
|
|
||||||
@FindBy(xpath = ".//table[@id='products-batman']/descendant::*/tr")
|
|
||||||
private List<WebElement> rows;
|
|
||||||
|
|
||||||
List<WebElement> headerRow;
|
|
||||||
|
|
||||||
public TablesPage(WebDriver driver)
|
|
||||||
{
|
|
||||||
super(driver);
|
|
||||||
PageFactory.initElements(this.driver, this);
|
|
||||||
|
|
||||||
// Take the cells of the 1st row (header row):
|
|
||||||
headerRow = rows.remove(0).findElements(By.xpath("th"));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void clickOnAccordionItem()
|
|
||||||
{
|
|
||||||
accordionItem.click();
|
|
||||||
}
|
|
||||||
|
|
||||||
public String batmanProductsTableHeaderAsString()
|
|
||||||
{
|
|
||||||
return headerRow.stream().map(WebElement::getText).collect(Collectors.joining(", "));
|
|
||||||
}
|
|
||||||
|
|
||||||
public String batmanProductsTableRowsAsString()
|
|
||||||
{
|
|
||||||
String rowsAsString = "";
|
|
||||||
|
|
||||||
for (WebElement row : rows)
|
|
||||||
{
|
|
||||||
List<WebElement> cellsInRow = row.findElements(By.xpath("td"));
|
|
||||||
|
|
||||||
String rowAsString = cellsInRow.stream().map(WebElement::getText).collect(Collectors.joining(", "));
|
|
||||||
rowsAsString += rowAsString + '\n';
|
|
||||||
}
|
|
||||||
|
|
||||||
return rowsAsString;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,61 +0,0 @@
|
||||||
package tests.waits;
|
|
||||||
|
|
||||||
import org.openqa.selenium.WebDriver;
|
|
||||||
import org.openqa.selenium.firefox.FirefoxDriver;
|
|
||||||
|
|
||||||
import pages.waits.WaitingStrategiesPage;
|
|
||||||
|
|
||||||
public class ExplicitWaitTests
|
|
||||||
{
|
|
||||||
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/");
|
|
||||||
|
|
||||||
// Instantiate the page model:
|
|
||||||
WaitingStrategiesPage page = new WaitingStrategiesPage(driver);
|
|
||||||
|
|
||||||
// Perform actions on the page:
|
|
||||||
page.clickOnAccordionItem();
|
|
||||||
|
|
||||||
// ======================================================================================== //
|
|
||||||
//
|
|
||||||
// DISABLED INPUT
|
|
||||||
//
|
|
||||||
// ======================================================================================== //
|
|
||||||
|
|
||||||
page.clickEnableOrDisableButton();
|
|
||||||
page.waitForEnabledOrDisabledInputToBeClickable(1000); // Wait for the JavaScript to enable the input.
|
|
||||||
page.setEnabledOrDisabledInput("Explicit Wait with disabled elements");
|
|
||||||
|
|
||||||
// ======================================================================================== //
|
|
||||||
//
|
|
||||||
// HIDDEN INPUT
|
|
||||||
//
|
|
||||||
// ======================================================================================== //
|
|
||||||
|
|
||||||
page.clickShowOrHideButton();
|
|
||||||
page.waitForShownOrHiddenInputToBeClickable(1000); // Wait for the JavaScript to show the input.
|
|
||||||
page.setShownOrHiddenInput("Explicit Wait with hidden elements");
|
|
||||||
|
|
||||||
// ======================================================================================== //
|
|
||||||
//
|
|
||||||
// DYNAMIC INPUT
|
|
||||||
//
|
|
||||||
// ======================================================================================== //
|
|
||||||
|
|
||||||
page.clickAddOrRemoveButton();
|
|
||||||
page.waitForDynamicInputToBePresent(1000);
|
|
||||||
page.setDynamicInput("Explicit Wait with dynamic elements");
|
|
||||||
|
|
||||||
//
|
|
||||||
|
|
||||||
// This is commented out so you can actually see what happened in the web page:
|
|
||||||
// driver.quit();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,42 +0,0 @@
|
||||||
package tests.webelements;
|
|
||||||
|
|
||||||
import java.time.LocalDate;
|
|
||||||
import java.time.format.DateTimeFormatter;
|
|
||||||
|
|
||||||
import org.openqa.selenium.WebDriver;
|
|
||||||
import org.openqa.selenium.firefox.FirefoxDriver;
|
|
||||||
|
|
||||||
import pages.webelements.DatePickersPage;
|
|
||||||
|
|
||||||
public class DatePickersTests
|
|
||||||
{
|
|
||||||
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/");
|
|
||||||
|
|
||||||
// Instantiate the page model:
|
|
||||||
DatePickersPage page = new DatePickersPage(driver);
|
|
||||||
|
|
||||||
// This could be considered our test data:
|
|
||||||
|
|
||||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
|
|
||||||
LocalDate today = LocalDate.now();
|
|
||||||
|
|
||||||
String inputDateAsString = formatter.format(today);
|
|
||||||
|
|
||||||
// Perform actions on the page:
|
|
||||||
page.clickOnAccordionItem();
|
|
||||||
page.setDateOnHTMLDatePicker(inputDateAsString);
|
|
||||||
|
|
||||||
//
|
|
||||||
|
|
||||||
// This is commented out so you can actually see what happened in the web page:
|
|
||||||
// driver.quit();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,42 +0,0 @@
|
||||||
package tests.webelements;
|
|
||||||
|
|
||||||
import org.openqa.selenium.WebDriver;
|
|
||||||
import org.openqa.selenium.firefox.FirefoxDriver;
|
|
||||||
|
|
||||||
import pages.webelements.Frame;
|
|
||||||
import pages.webelements.FramesAndWindowsPage;
|
|
||||||
import pages.webelements.NewWindow;
|
|
||||||
|
|
||||||
public class FramesAndWindowsTests
|
|
||||||
{
|
|
||||||
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/");
|
|
||||||
|
|
||||||
// Instantiate the page model:
|
|
||||||
FramesAndWindowsPage page = new FramesAndWindowsPage(driver);
|
|
||||||
|
|
||||||
// Perform actions on the page:
|
|
||||||
page.clickOnAccordionItem();
|
|
||||||
|
|
||||||
Frame frame = new Frame(driver, page.frame());
|
|
||||||
frame.clickButton();
|
|
||||||
frame.dismiss();
|
|
||||||
|
|
||||||
page.clickNewWindowButton();
|
|
||||||
NewWindow window = new NewWindow(driver);
|
|
||||||
System.out.println("The title of the new window is: " + window.title());
|
|
||||||
window.close();
|
|
||||||
|
|
||||||
//
|
|
||||||
|
|
||||||
// This is commented out so you can actually see what happened in the web page:
|
|
||||||
// driver.quit();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,33 +0,0 @@
|
||||||
package tests.webelements;
|
|
||||||
|
|
||||||
import org.openqa.selenium.WebDriver;
|
|
||||||
import org.openqa.selenium.firefox.FirefoxDriver;
|
|
||||||
|
|
||||||
import pages.webelements.InputRangePage;
|
|
||||||
|
|
||||||
public class InputRangeTests
|
|
||||||
{
|
|
||||||
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/");
|
|
||||||
|
|
||||||
// Instantiate the page model:
|
|
||||||
InputRangePage page = new InputRangePage(driver);
|
|
||||||
|
|
||||||
// Perform actions on the page:
|
|
||||||
page.clickOnAccordionItem();
|
|
||||||
page.setRangeInputValue(0);
|
|
||||||
page.dragAndDropRangeInputBy(100, 0);
|
|
||||||
|
|
||||||
//
|
|
||||||
|
|
||||||
// This is commented out so you can actually see what happened in the web page:
|
|
||||||
// driver.quit();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,49 +0,0 @@
|
||||||
package tests.webelements;
|
|
||||||
|
|
||||||
import org.openqa.selenium.WebDriver;
|
|
||||||
import org.openqa.selenium.firefox.FirefoxDriver;
|
|
||||||
|
|
||||||
import pages.webelements.AlertPanel;
|
|
||||||
import pages.webelements.ConfirmPanel;
|
|
||||||
import pages.webelements.JavaScriptPopupMessagesPage;
|
|
||||||
import pages.webelements.PromptPanel;
|
|
||||||
|
|
||||||
public class JavaScriptPopupMessagesTests
|
|
||||||
{
|
|
||||||
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/");
|
|
||||||
|
|
||||||
// Instantiate the page model:
|
|
||||||
JavaScriptPopupMessagesPage page = new JavaScriptPopupMessagesPage(driver);
|
|
||||||
|
|
||||||
// Perform actions on the page:
|
|
||||||
page.clickOnAccordionItem();
|
|
||||||
|
|
||||||
page.clickAlertButton();
|
|
||||||
AlertPanel alert = new AlertPanel(driver);
|
|
||||||
System.out.println("Alert text: " + alert.getText());
|
|
||||||
alert.accept();
|
|
||||||
|
|
||||||
page.clickConfirmButton();
|
|
||||||
ConfirmPanel confirm = new ConfirmPanel(driver);
|
|
||||||
System.out.println("Confirm text: " + confirm.getText());
|
|
||||||
confirm.dismiss();
|
|
||||||
|
|
||||||
page.clickPromptButton();
|
|
||||||
PromptPanel prompt = new PromptPanel(driver);
|
|
||||||
System.out.println("Prompt text: " + prompt.getText());
|
|
||||||
prompt.dismiss();
|
|
||||||
|
|
||||||
//
|
|
||||||
|
|
||||||
// This is commented out so you can actually see what happened in the web page:
|
|
||||||
// driver.quit();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,34 +0,0 @@
|
||||||
package tests.webelements;
|
|
||||||
|
|
||||||
import org.openqa.selenium.WebDriver;
|
|
||||||
import org.openqa.selenium.firefox.FirefoxDriver;
|
|
||||||
|
|
||||||
import pages.webelements.SelectDatalistAndOptionsPage;
|
|
||||||
|
|
||||||
public class SelectDatalistAndOptionsTests
|
|
||||||
{
|
|
||||||
|
|
||||||
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/");
|
|
||||||
|
|
||||||
// Instantiate the page model:
|
|
||||||
SelectDatalistAndOptionsPage page = new SelectDatalistAndOptionsPage(driver);
|
|
||||||
|
|
||||||
// Perform actions on the page:
|
|
||||||
page.clickOnAccordionItem();
|
|
||||||
page.selectRandomOption();
|
|
||||||
page.selectFromDatalist("Batman");
|
|
||||||
|
|
||||||
//
|
|
||||||
|
|
||||||
// This is commented out so you can actually see what happened in the web page:
|
|
||||||
// driver.quit();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,33 +0,0 @@
|
||||||
package tests.webelements;
|
|
||||||
|
|
||||||
import org.openqa.selenium.WebDriver;
|
|
||||||
import org.openqa.selenium.firefox.FirefoxDriver;
|
|
||||||
|
|
||||||
import pages.webelements.TablesPage;
|
|
||||||
|
|
||||||
public class TablesTests
|
|
||||||
{
|
|
||||||
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/");
|
|
||||||
|
|
||||||
// Instantiate the page model:
|
|
||||||
TablesPage page = new TablesPage(driver);
|
|
||||||
|
|
||||||
// Perform actions on the page:
|
|
||||||
page.clickOnAccordionItem();
|
|
||||||
System.out.println("Batman Products Table Header: " + page.batmanProductsTableHeaderAsString());
|
|
||||||
System.out.println("Batman Products Table Rows: " + page.batmanProductsTableRowsAsString());
|
|
||||||
|
|
||||||
//
|
|
||||||
|
|
||||||
// This is commented out so you can actually see what happened in the web page:
|
|
||||||
// driver.quit();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,13 +1,12 @@
|
||||||
package tests.waits;
|
package waits;
|
||||||
|
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
|
|
||||||
|
import org.openqa.selenium.By;
|
||||||
import org.openqa.selenium.WebDriver;
|
import org.openqa.selenium.WebDriver;
|
||||||
import org.openqa.selenium.firefox.FirefoxDriver;
|
import org.openqa.selenium.firefox.FirefoxDriver;
|
||||||
|
|
||||||
import pages.waits.WaitingStrategiesPage;
|
public class ImplicitWait
|
||||||
|
|
||||||
public class ImplicitWaitTests
|
|
||||||
{
|
{
|
||||||
private static void sleep(long milliseconds)
|
private static void sleep(long milliseconds)
|
||||||
{
|
{
|
||||||
|
|
@ -26,17 +25,14 @@ public class ImplicitWaitTests
|
||||||
// Specify path to WebDriver:
|
// Specify path to WebDriver:
|
||||||
System.setProperty("webdriver.gecko.driver", "/snap/bin/geckodriver");
|
System.setProperty("webdriver.gecko.driver", "/snap/bin/geckodriver");
|
||||||
|
|
||||||
// Launch browser and navigate to test page:
|
// Launch browser, set implicit waiting time, and navigate to test page:
|
||||||
WebDriver driver = new FirefoxDriver();
|
WebDriver driver = new FirefoxDriver();
|
||||||
driver.manage().window().maximize();
|
driver.manage().window().maximize();
|
||||||
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(1000));
|
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(1000));
|
||||||
driver.get("https://ramoncaballero.dev/sdet/selenium-webdriver/playgrounds/");
|
driver.get("https://ramoncaballero.dev/sdet/selenium-webdriver/playgrounds/");
|
||||||
|
|
||||||
// Instantiate the page model:
|
// Click on the Waiting Strategies accordion item:
|
||||||
WaitingStrategiesPage page = new WaitingStrategiesPage(driver);
|
driver.findElement(By.xpath("//button[contains(text(),'Waiting Strategies')]")).click();
|
||||||
|
|
||||||
// Perform actions on the page:
|
|
||||||
page.clickOnAccordionItem();
|
|
||||||
|
|
||||||
// ======================================================================================== //
|
// ======================================================================================== //
|
||||||
//
|
//
|
||||||
|
|
@ -44,14 +40,16 @@ public class ImplicitWaitTests
|
||||||
//
|
//
|
||||||
// ======================================================================================== //
|
// ======================================================================================== //
|
||||||
|
|
||||||
page.clickEnableOrDisableButton();
|
// 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.
|
// 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
|
// In this case, the disabled input is available (i.e. exists in the DOM), so we sleep the execution
|
||||||
// before continuing:
|
// before continuing:
|
||||||
sleep(1000);
|
sleep(1000);
|
||||||
|
|
||||||
page.setEnabledOrDisabledInput("Implicit Wait with disabled elements");
|
// Enter some text in the input:
|
||||||
|
driver.findElement(By.id("disabled-input")).sendKeys("Implicit Wait with disabled elements");
|
||||||
|
|
||||||
// ======================================================================================== //
|
// ======================================================================================== //
|
||||||
//
|
//
|
||||||
|
|
@ -59,14 +57,16 @@ public class ImplicitWaitTests
|
||||||
//
|
//
|
||||||
// ======================================================================================== //
|
// ======================================================================================== //
|
||||||
|
|
||||||
page.clickShowOrHideButton();
|
// 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.
|
// 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
|
// In this case, the hidden input is available (i.e. exists in the DOM), so we sleep the execution
|
||||||
// before continuing:
|
// before continuing:
|
||||||
sleep(1000);
|
sleep(1000);
|
||||||
|
|
||||||
page.setShownOrHiddenInput("Implicit Wait with hidden elements");
|
// Enter some text in the input:
|
||||||
|
driver.findElement(By.id("hidden-input")).sendKeys("Implicit Wait with hidden elements");
|
||||||
|
|
||||||
// ======================================================================================== //
|
// ======================================================================================== //
|
||||||
//
|
//
|
||||||
|
|
@ -74,12 +74,14 @@ public class ImplicitWaitTests
|
||||||
//
|
//
|
||||||
// ======================================================================================== //
|
// ======================================================================================== //
|
||||||
|
|
||||||
page.clickAddOrRemoveButton();
|
// 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
|
// 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.
|
// will keep trying for the specified amount of time until the input is available.
|
||||||
|
|
||||||
page.setDynamicInput("Implicit Wait with dynamic elements");
|
// Enter some text in the input:
|
||||||
|
driver.findElement(By.id("dynamic-input")).sendKeys("Implicit Wait with dynamic elements");
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
||||||
|
|
@ -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,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,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