0

so I am working in a Testing Automation case where i should increment the index of my Xpath every time i run dhe test case. So far i have done this:

wd.addPromiseChainMethod("SelectAcc", () => {
  var XPathIndex = I;
  return driver.waitForElementByXpath(
    "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout[" +
      i +
      "]"
  );

  if (XPathIndex > 5) {
    I = 1;
  } else {
    I = XPathIndex + 1;
  }
  click();
});

and also globaly declared`

Var I =1;

But I am not sure how to update the global variable I, so for example if i have ran the test case 2 times, the third time I run it I will be:

    Var I =3; 
MarioG8
  • 5,122
  • 4
  • 13
  • 29
Art
  • 3
  • 4

1 Answers1

1

You can use two "globals" in that case. (I'd recommend to have them confined in an object along with your test though).

let xPathIndex = 1;
const max = 5;
wd.addPromiseChainMethod('SelectAcc', () => {
    if( ++xPathIndex > max ) xPathIndex = 1;
    return driver. waitForElementByXpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout["+ xPathIndex +"]")
} );

In case you need to store the inital value for new contexts (page reloads) you can use localStorage to persist and update the starting value, hence sg like:

let storedXPathIndex = localStorage.getItem("storedXPathIndex");
let xPathIndex = (storedXPathIndex) ? ++storedXPathIndex : 0;
localStorage.setItem("storedXPathIndex", xPathIndex);
const max = 5;
wd.addPromiseChainMethod('SelectAcc', () => {
    if( ++xPathIndex > max ) xPathIndex = 1;
    return driver. waitForElementByXpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout["+ xPathIndex +"]")
} );

Note: JavaScript is case-sensitive! You have a lot of typos in your code above.
Juergen Riemer
  • 1,393
  • 2
  • 13
  • 29
  • This works if the function is called multiple times during one run of the test case.What I need is that if i stop running the test case the global let xPathIndex updates to value 2. 3, 4 and so on. meaning that if i run the test the second time i will have let xPathIndex= 2 @Juergen Riemer – Art Dec 07 '21 at 11:10
  • @Art just to understand: you call the test (T1) a couple of times then you do what? run other tests (T2, T3, ..) in sequence and start running T1 again and then you want to start with a value +1? Or is the page reloaded hence you always start with the initial value? – Juergen Riemer Dec 07 '21 at 11:23
  • after the t1 has stoped, when i sstart running it again i need it to start with value +1. in .net for example i can achive this declaring the variavbles in a .config file, and updateing this variables when the function is called. @Juergen Riemer – Art Dec 07 '21 at 11:27
  • Is ther any kind of configuration i should make to use localStorage? i tried if (typeof localStorage === "undefined" || localStorage === null) { var LocalStorage = require('node-localstorage').LocalStorage; localStorage = new LocalStorage('./scratch') but i am all new to js and i am not getting this @Juergen Riemer – Art Dec 07 '21 at 14:00
  • @Art above code should be sufficient to test for existence of localStorage and manipulating it. I can recommend w3schools for a quick reference: https://www.w3schools.com/jsref/prop_win_localstorage.asp – Juergen Riemer Dec 07 '21 at 14:43
  • But i am not sure it will work, i get the error "ReferenceError: localStorage is not defined at Object". that's why i asked if i need any kind of configuration @Juergen Riemer – Art Dec 07 '21 at 15:29
  • @Art localStorage is a host object it is always accessible do not prefix any Object. Without code its hard to tell what the problem might be. Either edit question with it or create a new one – Juergen Riemer Dec 08 '21 at 08:05
  • i created a new one, hopefully someone cal help https://stackoverflow.com/questions/70287906/referenceerror-localstorage-is-not-defined-at-object @Juergen Riemer – Art Dec 09 '21 at 09:59