0
votes

I'm automating e2e tests with Protractor on an angular app. However, I have an issue when sending keys on input fields. The sendKeys would miss few characters everytime so I found a workaround :

static sendKeys(value, element){
    value.split('').forEach((c) => element.sendKeys(c));
}

This works well but it takes more than 3 times the time the original sendKeys function would.

Well no problem my tests are still functionnal right ? My app now has new fields with scripts behind them. One of them is a datepicker input, you can either choose from the datePicker or type it manually. However, for today's date you would type 09022018 and the slashes are automatically appended at the right place (like so 09/02/2018). If you were to enter a wrong date the field is cleared.

Now back to the problem : it seems that both my implementation of sendKeys and the original one loose focus after each submitted key. This means that I can't enter a valid date in the input field as it's cleared after each simulated keypress.

I could use browser.executeScript to fix it but I wouldn't be able to test the functionnality adding slashes. Also, as you type, the datepicker is still open and refreshes after each keypress, you can select a date from it at any time and that is also a feature I want to test.

Thanks in advance

1
looks like an issue with the script from the date-picker which is not able to handle the pace at which Selenium is sending each key. Try to wait for no pending requests before sending the next key. - Florent B.

1 Answers

0
votes

Use executeScript to set the date in backgrond, then use sendKeys to enter a space or Tab at the end to trigger the Keyborad event which will check the input and format the input with slash

function enterDate(date) {

    var script  = 'arguments[0].value=arguments[1]';
    // input box for date
    var dateBox = element(by.xxx(yyy));

    browser.executeScript(script, dateBox, date);

    dateBox.sendKeys(" ");

    // or try send Tab
    dateBox.sendKeys(protractor.Key.TAB);
}

enterDate('09022018');

You can try this solution on other fields you fixed but take 3 more time.