Rune
Rune

Reputation: 1

Check a defined WebElement in a Page Object

My first question here. I hope this question is not already answered. I previously searched if it exists, sorry if I'm wrong.

My question is the next. I have this webelement in a PageObject class for automated tests:

//Customer filter
@FindBy(id = "customer_filter")
private WebElement customerFilter;

Later I try to check if it's present or not, like this:

Boolean test = customerFilter.isDisplayed();

But it doesn't work, it says the webelement is not present when actually is not present, and the test ends. I've also tried with isEnabled() and isSelected(). I have to use instead the next code so everything works:

Boolean isPresent = driver.findElements(By.id("customer_filter")).size() > 0;
    if(isPresent){

Is there a way to use webelement directly so I don't have to continuosly use the id locator?

Thanks in advance!!!

Edit: Looking for a little more information, I found this thread about the same problem, but it wasn't resolved: https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/1880

Upvotes: 0

Views: 205

Answers (2)

David Lai
David Lai

Reputation: 822

When you use the "@FindBy" annotation, it returns a WebElement Proxy. The WebElementProxy is a wrapper around webelement, and has a "UnderlyingWebElement" property, which is what you're looking for.

https://github.com/barancev/webdriver-samples/blob/master/src/ru/st/selenium/WebElementProxy.java#L141

How you can leverage this, is you can do some creative typecasting to access some of these methods that are not in the IWebElement interface.

if( ((WebElementProxy)customerFilter).getWrappedElement() != null) { 
  //do something
}

Upvotes: 1

Boris Stoyanov
Boris Stoyanov

Reputation: 96

Please refer to this post:post

Also have in mind that when you're checking if .size()>0, this has nothing to do with either visible or presented element. This collection contains all element in the DOM that are matching the criteria.

My advise is to create a method that you call each time with parameter the element that you're looking for.

Upvotes: 0

Related Questions