How to select date of datepicker in protractor - protractor

I am automating the application through protractor and as part of one testcase need to select the date, the date-picker definition is as below. I tried to select the date using sendkeys which is not working.
element(by.lid("new-transaction-doat-field)).sendKeys("24 Aug 2019");
Please help me in to select the date.
Note: lid is the custom attribute and to select the value by lid, create a custom attribute method in protractor

Two issues in your given code:
1) Should interact with a Input html element
2) sendKeys() can't interact with a readonly Input html element.
// because protractor not support invoke setAttribute api directly,
// need to using executeScript to inject DOM Javascript snippet into browser
// then execute the snippet in browser
browser.executeScript(
"arguments[0].setAttribute('value', arguments[1])",
element(by.css( '[lid="new-transaction-doat-field"] > span > input')).getWebElement(),
"24 Aug 2019"
)
Keep in mind, setAttribute() is not simulating user interact with web page from UI.
Please confirm it's acceptable from your test perspective.

I found the solution to select the date through datepicker where 'sendKeys' won't work as input is disabled. The solution is like how manually we will traverse through the calendar and select the date.
The code is as below:
this.selectDate = function (dateElement, dateValue, yearButton, arrowButton) {
var splitDate = dateValue.split('/');
var year = splitDate[2];
var month = splitDate[1];
var day = splitDate[0];
var monthName = getMonthName(month);
dateElement.click()
.then(() => yearButton.click())
.then(() => yearButton.click())
.then(() => selectYear(year, arrowButton))
.then(() => element(by.xpath("//span[text()='" + year + "']")).click())
.then(() => element(by.xpath("//span[text()='" + monthName + "']")).click())
.then(() => element(by.xpath("//span[text()='" + day + "']")).click());
};
async function selectYear(year, arrowButton) {
for(var i=0; i < 10; i++) {
var present = await element(by.xpath("//span[text()='" + year + "']")).isPresent()
if(present) {
break;
} else {
await arrowButton.click();
}
}
}
function takes inputs as dateElement, dateValue, yearButton - which is center button to traverse to years, arrowButton - previous or next button in the calendar
First script clicks on the year button in the center in the above image it is 'September 2019' , after that '2019' year calendar comes, it clicks on that again.
Then 2001 to 2019 calendar comes as below
if the year displays it clicks on that otherwise it clicks on 'previous' or 'next' button based on the date to select until it got displayed (looping for 10 times to search for the year, if not in 10 clicks throws error).
After year clicked, it clicks on month and day.

have you tried the .sendKeys() on the input?
element(by.model('dateModel')).sendKeys('dd mmm yyyy');

Related

How to scroll down in Rselenium until a specific date appear in facebook

I a working with facebook and I want scroll one page until the first publication of 2020 appears. The problem is that I can't get the html text for each date at the same time that the code is scrolling down. This my code:
tryCatch({
Sys.sleep(2)
suppressMessages({
vermas <- remDr$findElement("css", "body")
fecha_mirar <- remDr$findElement("css",".x4k7w5x.x1h91t0o.x1h9r5lt.x1jfb8zj.xv2umb2.x1beo9mf.xaigb6o.x12ejxvf.x3igimt.xarpa2k.xedcshv.x1lytzrv.x1t2pt76.x7ja8zs.x1qrby5j")
fecha <- as.character(fecha_mirar$getElementText())
while(fecha != "[2020]"){
vermas$sendKeysToElement(list(key = "end"))
vermas$sendKeysToElement(list(key = "enter"))
Sys.sleep(5)
fecha_mirar <-remDr$findElement("css",".x4k7w5x.x1h91t0o.x1h9r5lt.x1jfb8zj.xv2umb2.x1beo9mf.xaigb6o.x12ejxvf.x3igimt.xarpa2k.xedcshv.x1lytzrv.x1t2pt76.x7ja8zs.x1qrby5j")
fecha <- as.character(fecha_mirar$getElementText())
}
})
},
error = function(e) {
NA_character_
}
)
The idea of the code is scroll the facebook page until one publication is from 2020 and the code give me a NA to continue with other code.
The problem is that the only date that I get is from the first publication and I can't figure out how to solve it.
Thanks community!!!
PD: You can check the code in any facebook page.

Flutter DropdownMenuItem delete

I'm developing a combobox using Flutter. combobox needs to be this month and next months. should write month name in combobox and return the number of the selected month. How can I do this in the shortest way?
I developed 12 months as DropdownMenuItem. I gave the current month as the default value. So far everything is fine. but how can I make the items of the past months invisible?
I used this code for a similar problem.
This solution includes intl package but you can change the code in list generation for your purpose.
import 'package:intl/intl.dart';
void main(){
var today = DateTime.now();
var length = 12 - today.month + 1; // I added +1 because I want to include current month too
List validMonths = List.generate(length,(index){
return DateFormat('MMMM').format(DateTime(0, (today.month + index)));
});
print(validMonths);
}

Disabling selection of dates in a date field

I'm using a date field in which I want to limit the date selection to a maximum of 90 days from the current date. How can i achieve this?
I tried dateField.setMaxvalue(maxDate), but I'm not able to limit the selection
I don't think the GWT datepicker has support for this.
You could use a library like GWT-Bootstrap3 which does have all this (https://gwtbootstrap3.github.io/gwtbootstrap3-demo/#dateTimePicker).
Or you could listen for events and rollback changes made by the user if selection was outside the valid range, and display a message to the user.
I second to the suggestion given by #Knarf. You could use GWT DatePicker class and listen for a ValueChangeEvent and in ValueChangeHandler, you could put your logic to check if the selected date is within your range - if not, you could show a message on your UI for the User to reselect a date within the date range (as per your requirement).
DatePicker datePicker = new DatePicker();
final Label text = new Label();
// Listen for a ValueChangeEvent and implement a ValueChangeHandler on your datePicker element
datePicker.addValueChangeHandler(new ValueChangeHandler<Date>() {
public void onValueChange(ValueChangeEvent<Date> valueChangeEvent) {
Date inputDate = valueChangeEvent.getValue();
// Put your logic to test whether the selected date is within your range
String dateString = DateTimeFormat.getMediumDateFormat().format(inputDate);
text.setText(dateString);
}
});
Hope that this helps!
You can add a ShowRangeHandler to your datePicker. This is an example to restrict the datePicker to dates in the past only, you can adapt it to limit to 90 days:
datePicker.addShowRangeHandler(new ShowRangeHandler<Date>() {
#Override
public void onShowRange(ShowRangeEvent<Date> event) {
Date today = new Date();
Date date = new Date(event.getStart().getTime());
while (date.before(event.getEnd())) {
if (date.after(today) && datePicker.isDateVisible(date)) {
datePicker.setTransientEnabledOnDates(false, date);
}
CalendarUtil.addDaysToDate(date, 1);
}
}
});
The above mentioned solutions worked exactly the way i expected..Thank you.I used the bootstrap datepicker in the link (https://gwtbootstrap3.github.io/gwtbootstrap3-demo/#dateTimePicker).

How can I make a cell update with the current date, using a dropdown next to the cell?

I need to make Data Validation on a range of cells. I want a dropdown list, where the only option is the current date. To get the current date, I can use =TODAY(). The problem is, that the dates don't remain static. When the sheet recalculates, so will all the dates. I need the dates to remain the same.
How can I work around this?
I found a blog where the answer might be, but I can't see how the author has made his spreadsheet.
I will have a dropdown item which says "dags dato". Next i use an eventlistener that checks if the text in a cell is changed to "dags dato".
if so, it puts the current date in the cell, like this:
function onEdit(event)
{
var ss = event.source.getActiveSheet();
var r = event.source.getActiveRange();
var currentValue = r.getValue();
if(currentValue == "dags dato")
{
var dd = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd");
r.setValue(dd);
}
}

JQuery UI Datepicker Current Day

My page uses a JQuery UI Datepicker and loads with the current day selected and weekends and selected dates restricted.
I would like the current selected day to become unavailable at 2pm and the day move forward.
Can anyone help.
I would just use a variable for the the minDate
var dt = new Date();
if (dt.getHours() > 14) {
dt = dt.setDate(dt + 1); // go one day in the future
}
$(selector).datepicker({minDate: dt});
You'd just need to use the real javascript Date class methods - which I haven't used in a little while.