Friday, 26 May 2017

Selenium routine works the first time, but subsequent runs fail

I have this routine, using selenium-webdriver for NPM - require('selenium-webdriver'):

       function run(){
          driver.get(cdthost + '/#!/home');
          driver.navigate().refresh();
          let acqId = acq._id;
          assert(acqId, 'acqId is not defined.');
          // on the next line we wait to ensure that the page is loaded
          driver.wait(until.elementLocated(By.id(`open-editing-acqId-${acqId}`)), 3000).then(function () {
            console.log(`open-editing-acqId-${acqId} has been located.`);
          });
          driver.findElement(By.id(`open-editing-acqId-${acqId}`)).click();
          let file = path.resolve($root + '/csv-data/IT-DE-Jasper.csv');
          console.log(' => file to be loaded => ', file);
          let el = driver.findElement(By.id(`import-file-acqId-${acqId}`));
          driver.wait(until.elementIsVisible(el), 2000).then(function () {
            console.log(`import-file-acqId-${acqId} is now visible`);
          });
          el.sendKeys(file);
          return driver.findElement(By.id(`submit-file-acqId-${acqId}`)).click().then(function () {
            return Promise.delay(2000)
          })
          .catch(function (err) {
            console.error('\n\n', ' => Suman caught promise rejection => ', err.stack || err);
            return Promise.reject(err);
          });
       }

I call it once, and it works perfectly. I call it again, and it cannot find any elements on the page, even if I call refresh().

Any idea why this would work the first time, but not after that?



via Alexander Mills

No comments:

Post a Comment