0

How can I use the path/location of a @FindBy variable as an argument to a method?

I have the following @FindBy value in my class...

@FindBy(xpath=".//*[@id='HasAnotherSubsidisedQual_container']") 
@CacheLookup WebElement mSubsidisedQual;

I then have a method for checking whether an element exists...

public boolean isElementPresent(By element){
    try {
        mDriver.findElement(element);
        return true;
    }
    catch (org.openqa.selenium.NoSuchElementException e){
        return false;
    }
}

I then use that method in another method which contains an assertion

public void checkSmartAndSkilled () {
    Assert.assertTrue(isElementPresent(By.xpath(".//*[@id='HasAnotherSubsidisedQual_container']")));
}

This all works fine, however instead of specifying By.xpath... etc in the assertion, is there anyway to pass in as an argument the path for my @FindBy WebElement mSubsidisedQual?

Many thanks

Curious
  • 282
  • 4
  • 21
Konzy262
  • 2,747
  • 6
  • 42
  • 71
  • Possible duplicate of [How to supply value to an annotation from a Constant java](http://stackoverflow.com/questions/2065937/how-to-supply-value-to-an-annotation-from-a-constant-java) – SiKing Mar 29 '16 at 18:30

2 Answers2

0

You do not need to provide the xpath again. Once it is initialized by Page Factory, simply pass the element as an argument.

public void checkSmartAndSkilled () {
    Assert.assertTrue(isElementPresent(mSubsidisedQual));
}
Haxor
  • 2,306
  • 4
  • 16
  • 18
  • This doesn't work unfortunately. You can't pass in a WebElement to an argument that is expecting By. – Konzy262 Mar 30 '16 at 16:53
  • Assert.assertTrue(boolean) expects a boolean argument. And, isElementPresent returns a Boolean. It works. – Haxor Apr 19 '16 at 09:16
0

You just created a wrong method. And the way you check element existence is wrong. But if you want to do it this way then what you should do is to overload it so the parameter is your element.

Then call the element (for example click, or use its property like Enable, or Count or Length or whatever is available in Java) and if the element doesn't exist it will catch the same error. If it exists then return true.

Denis Koreyba
  • 3,144
  • 1
  • 31
  • 49