0

I create a function to verify element on android using try and catch and when the element is not present my expectation it will print the variable name, I've tried this syntax but until now it still prints the value of the variable.

public void verifyEl(String element) {
    try {
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.findElementByXPath(element);
    }catch (NoSuchElementException e) {
        Assert.fail("Element "+element+" is not found");
    }
    Assert.assertTrue((driver.findElementByXPath(element)).isDisplayed());
}

String Email_form = "//android.view.View/android.widget.EditText[1]";

public void verifyLoginPage() {     
        mainFunc.verifyEl(Email_form);
     }

Actual result:

Element //android.view.View/android.widget.EditText[1] is not found

Expected result:

Element Email_form  is not found
Rama
  • 39
  • 6

1 Answers1

0

The problem is that you log the value of element which is in this case "//android.view.View/android.widget.EditText[1]". To get the variable name you must use Reflection, but I'm also not sure about how to do that. Possibly you could declare another parameter where you would give the argument "Email_form". That would look like this:

public void verifyEl(String element, String elementName) {
    try {
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.findElementByXPath(element);
    }catch (NoSuchElementException e) {
        Assert.fail("Element "+elementName+" is not found");
    }
    Assert.assertTrue((driver.findElementByXPath(element)).isDisplayed());
}

String Email_form = "//android.view.View/android.widget.EditText[1]";

public void verifyLoginPage() {     
    mainFunc.verifyEl(Email_form, "Email_form");
}

But I'm not sure if this is what you wanted

Ayk Borstelmann
  • 316
  • 2
  • 8