wanderors
wanderors

Reputation: 2292

how to write multiple return conditions based on conditions in nodejs

For webdriverIo, we are taking xpath for an element as a return value for a function

  get loginButton() {
    return browser.element("//div[@class='login']");
  }

But now we are keeping the same function name for Mobile browser, PC browser as well as mobile app. So my doubts are :

  1. Can we keep just one function and write conditions based return
  2. Can we use annotations as in like testng

like :

get loginButton() {
    android : return browser.element("//div[@class='android-login']");
    ios : return browser.element("//li[@class='ios-login']");
    chrome : return browser.element("//a[@class='pc-login']");
  }

or can we write with Switch or any other methods. Please help

Upvotes: 0

Views: 41

Answers (1)

Tumo Masire
Tumo Masire

Reputation: 432

The way I see it, you could probably use a Switch or Else-If statements (plain vanilla JS) in your function.

 function loginButton(x){
  if (x === "android "){
    return browser.element("//div[@class='android-login']");
  }
  else if (x == "ios"){
    return browser.element("//li[@class='ios-login']");
  }else if (x == "chrome"{
    return browser.element("//a[@class='pc-login']");
  } else{
    return -1;
  };
};

The similar should be true with a Switch statement.

Upvotes: 1

Related Questions