1

I have 3 dates field in a row, and I'm trying to include randomly date on it, but, protractor is too fast and set something like:

  • first data: 01/08/1990 (correct)
  • second data: 01/09/0009 (invalid)
  • last data : 01/10/0007(invalid)

So, I used browser.sleep(200) and it's works, but, there is another way to do? Is this the correct way?

var dataPublicacao = element(by.xpath("//label[. = 'Data de Publicação*']/following-sibling::input"));
        dataPublicacao.sendKeys(RetornaDataAleatoria());
        browser.sleep(200);
        var dataInicio = element(by.xpath("//label[. = 'Inicio Vigência*']/following-sibling::input"));
        dataInicio.sendKeys(RetornaDataAleatoria());
        browser.sleep(200);
        var fimVigencia = element(by.xpath("//label[. = 'Fim Vigência']/following-sibling::input"));
        fimVigencia.sendKeys(RetornaDataAleatoria());
paulotarcio
  • 461
  • 1
  • 5
  • 20
  • Are you sure the `RetornaDataAleatoria` function produces valid dates? – alecxe Jul 04 '16 at 14:21
  • Yeap. function RetornaDataAleatoria(){ return (Math.floor(Math.random() * 31) +1) + '/' + (Math.floor(Math.random() * 12) +1 ) + '/' + (Math.floor(Math.random() * (2016-1990) + 1990)); } – paulotarcio Jul 04 '16 at 14:23

1 Answers1

0

I would try splitting the date string into components and sending keys in "batches". This is the implementation in a quite broad form:

var action = browser.actions().mouseMove(dataInicio).click();
var randomDate = RetornaDataAleatoria();

var day = randomDate.slice(0, 2),
    month = randomDate.slice(3, 5),
    year = randomDate.slice(6, 10);

action.sendKeys(day).sendKeys("/").sendKeys(month).sendKeys("/").sendKeys(year).perform();

If this would be proven to work, you should probably change the RetornaDataAleatoria function and return an array of parts of a date with delimiters so that you can simplify the sending keys step without needing to slice the string into pieces:

var randomDateParts = RetornaDataAleatoria();
for (var i = 0; i < randomDateParts; i++) {
    action = action.sendKeys(randomDateParts[i]);
}
action.perform();

You can also add a custom sleep() action between every sendKeys() to deliberately slow it down, having a small delay between the next batch of the keys, see:

And, you can just slow Protractor down entirely (probably a good idea for debugging only):

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195