Apply POM to SelectDatalistAndOptions

This commit is contained in:
Ramon Caballero 2024-07-25 15:56:51 +01:00
parent 9f8210bf75
commit 702348b16a
3 changed files with 92 additions and 52 deletions

View File

@ -0,0 +1,58 @@
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;
public class SelectDatalistAndOptionsPage
{
@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;
private WebDriver driver;
public SelectDatalistAndOptionsPage(WebDriver driver)
{
this.driver = 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);
}
}

View File

@ -0,0 +1,34 @@
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();
}
}

View File

@ -1,52 +0,0 @@
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();
}
}