In pilot project selection of date of birth is wrong | Selenium Forum
R
Rajan Kumar Kaushik Posted on 09/02/2021

Hi Ashish,

I am following pilot project,In this you have explained selection of DOB in easy way but the desired selection is 12-04-2018 and when you running code DOB selection is 12-04-2020. When I am running code my DOB selection is also wrong.Please provide the solution. I am attaching scrrenshot and pasting my code.

Thanks in advance.

 

package suitea;

import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;

import testbase.TestBase;

public class SakraWorldTest extends TestBase {

@Test
public void appointmentTest() throws InterruptedException {

launchBrowser("Chrome");//prop init
log("Opened Browser Chrome");
driver.get(prop.getProperty("url"));
waitForPageToLoad();
driver.findElement(By.linkText(prop.getProperty("doctor_name"))).click();
//validate the presence and visibility of name field
//************************Explicit Wait(do it later)**************************
Thread.sleep(3000);
if(!isElementPresent(prop.getProperty("name")))
failAndStopTest("Name field is not present/visible");

log("Name field is present");
driver.findElement(By.id(prop.getProperty("name"))).sendKeys(prop.getProperty("first_name"));
driver.findElement(By.cssSelector(prop.getProperty("email"))).sendKeys(prop.getProperty("email_id"));
driver.findElement(By.id(prop.getProperty("mobile_number"))).sendKeys(prop.getProperty("phone_number"));
//*******************************validate the options in gender field (will do it later)********************
WebElement gender=driver.findElement(By.id(prop.getProperty("gender")));
Select s=new Select(gender);
s.selectByVisibleText("Male");
driver.findElement(By.id(prop.getProperty("dob"))).click();
String dob=prop.getProperty("dob_val");

//dynamic -date selection logic
String monthYearDisplayed=driver.findElement(By.cssSelector(prop.getProperty("monthyear"))).getText();
System.out.println("monthYearDisplayed -" +monthYearDisplayed);
SimpleDateFormat sd=new SimpleDateFormat("dd-MM-yyyy");

try {
Date dateToBeSelected=sd.parse(dob);
Date currentDate=new Date();
String day=new SimpleDateFormat("d").format(dateToBeSelected);
System.out.println(day);
String month=new SimpleDateFormat("MMMM").format(dateToBeSelected);
System.out.println(month);
String year=new SimpleDateFormat("yyyy").format(dateToBeSelected);
System.out.println(year);
String monthYearToBeSelected=month+" "+year;
System.out.println("monthYearToBeSelected -"+monthYearToBeSelected);

while(!monthYearToBeSelected.equals(monthYearDisplayed)) {
//click on forward or backward icon
if(dateToBeSelected.compareTo(currentDate)==1) {//
//forward icon
driver.findElement(By.xpath(prop.getProperty("calendar_forward"))).click();
}else if(dateToBeSelected.compareTo(currentDate)==-1){
//back icon
driver.findElement(By.xpath(prop.getProperty("calendar_back"))).click();
}
monthYearDisplayed=driver.findElement(By.cssSelector(prop.getProperty("monthyear"))).getText();
System.out.println("monthYearDisplayed -" +monthYearDisplayed);

//now we have to select day
driver.findElement(By.xpath("//a[text()='"+day+"']")).click();
}


}catch(Exception e) {
e.printStackTrace();
}












}
//true-present and not hidden
//false-not present or hidden
public boolean isElementPresent(String Locator) {
WebElement e=null;
//first check the presence of element
try {
e=driver.findElement(By.id(Locator));
}catch(Exception ex) {
log("Exception while extracting object" +ex.getMessage());
return false;
}
//then check the visibilty of element
log("Element visibility status" +e.isDisplayed());
if(!e.isDisplayed())
return false;
//reach here-element is present and not hidden
return true;
}

public void waitForPageToLoad() throws InterruptedException {
JavascriptExecutor js=(JavascriptExecutor)driver;
int i=0;
while(i!=0) {
String state=(String)js.executeScript("return document.readyState;");
System.out.println(state);

if(state.equals("complete"))
break;
else
wait(2);
i++;
}
//check for jquery status
i=0;
while(i!=0) {

Long d=(Long) js.executeScript("return jQuery.active;");
System.out.println(d);
if(d.longValue()==0)
break;
else
wait(2);
i++;
}
}

public void wait(int time) {
try {
Thread.sleep(time*1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


A
Ashish Thakur Replied on 09/02/2021

Logic is same.

1) First you have to reach the right combination of month and year... no matter how many times you have to go back

2) Then you have to select the day.


Related Posts