How to test a Angular js date picker from Protract

2019-01-09 17:02发布

问题:

I'm new to Protractor and here I'm trying to test an angularjs date picker from Protractor.

I tried to find a way to do this and this article was the only thing I found and It is not very clear to use

If someone know how to test please help.

What I need is to select today's date.

Thanks in advance :)

  • edit -

alecxe, here is the screen shot of my date picker. Unfortunately cannot provide the link of the page. :(

<input 
       class="form-control ng-pristine ng-valid ng-not-empty ng-touched" 
       ng-model="invoice.fromdate" 
       data-date-format="yyyy-MM-dd" 
       data-date-type="string" 
       data-max-="" data-autoclose="1" 
       bs-datepicker="" 
       ng-change="dateRange()" 
       type="text">

回答1:

I think you can avoid manipulating the datepicker manually and instead set the date either by just sending the keys with a today's date value:

var picker = element(by.model("invoice.fromdate"));

// get today's date
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();

if(dd<10) {
    dd='0'+dd
} 

if(mm<10) {
    mm='0'+mm
} 

today = mm+'/'+dd+'/'+yyyy;

picker.clear();
picker.sendKeys(today);

Or, by setting the associated model's value directly:

picker.evaluate("invoice.fromdate= '" + today + "'");


回答2:

var data_picker = element(by.model("invoice.fromdate"));

// select current date with date function

var current_date = new Date();

var day = today.getDate();

var month = today.getMonth()+1; //By default January count as 0

var year = today.getFullYear();

if(day<10) {
    day='0'+day
} 

if(month<10) {
    month='0'+month
} 

current_date = month+'/'+day+'/'+year;

data_picker.clear();  // Note if you are facing error message related to clear. Comment this line

data_picker.sendKeys(today);

Hope this will work