Home > Net >  "For" loop not picking up the "if" condition when it checks for object using xpa
"For" loop not picking up the "if" condition when it checks for object using xpa

Time:06-23

I am a newbie to JS and am facing an issue for which I cannot find a solution for. I am writing a script to automate a "clean up" process using Karate/Selenium.

Scenario:

  1. Go to a webpage and search for a certain criteria.
  2. On populated results, grab all values in a single column. Then in the same search box, search for each value grabbed from the column.
  3. In the results, click on Delete for the populated result.
  4. Some rows will throw an exception, in which case, we want to go back and try the next item in the column value array until all items are either deleted or the leftover items are all the ones that throw exception.

I have this so far:

driver.input('#searchField', ['Mobile', Key.ENTER], 200); //here I am using Karate/Selenium to enter the search criteria on the UI. 
function() {
  var searchResult = false;
  searchResult = driver.exists('//table//tr');
  var errorPage = driver.exists('#exception');

  do {
    var deleteCriteria = driver.scriptAll('//table//tr//td[3]', '_.innerText'); //Here I am grabbing actual values I need to search by across the entire column and putting it in array form.
    for (i = 0; i < deleteCriteria.length; i  ) {
      driver.input('#searchField', [deleteCriteria[i], Key.ENTER], 200);
      driver.click('#deleteBtn');
      driver.click('#deleteNumber');
      driver.delay(10000);
      if (errorPage) {
        driver.back();
        driver.delay(10000);
        driver.back();
        driver.delay(10000);
      } else {
        continue;
      }
    }
  } while (searchResult);
}

Problem: When the exception page does display, the IF block is not initiated. The script fails trying to find the search box to search again. Please advise.

PS- I understand my "while" is not correct yet. I will fix it once the problem in question is resolved.

CodePudding user response:

The code var errorPage = driver.exists('#exception'); runs once and stores the state at that moment in time.

If you want to know if it exists at the current moment in time, you need to look for it again.

if (driver.exists('#exception')) {
  ...
}
  • Related