Apply POM to DatePickers

This commit is contained in:
Ramon Caballero 2024-07-25 18:08:11 +01:00
parent c018aee877
commit cdd8d98acc
2 changed files with 98 additions and 0 deletions

View File

@ -0,0 +1,56 @@
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;
public class DatePickersPage
{
@FindBy(xpath = "//button[contains(text(),'Date Pickers')]")
private WebElement accordionItem;
@FindBy(id = "html-datepicker")
private WebElement htmlDatePicker;
private WebDriver driver = null;
public DatePickersPage(WebDriver driver)
{
this.driver = 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);
}
}

View File

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