Apply POM to DatePickers
This commit is contained in:
parent
c018aee877
commit
cdd8d98acc
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue