Check a defined WebElement in a Page Object - selenium

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

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
}

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.

Related

How to get false if element is not present on page in Selenium webdriver

I am working with automation now and again have faced a problem. In general, I have logic in my code where I need to check for the existence of an element and next step will depend on the result of checking. But base instruments of Selenium return only boolean true otherwise NoSuchElementException. But I need "false".
As on my previous project, I use simple wrapper for solving this problem now:
private boolean isDisplayedOnPage(WebElementFacade wef){
try{
return wef.isDisplayed();
} catch (NoSuchElementException nsee){
return false;
}
}
It works perfectly but the use of the exception confuses me. Also, I read about "wait" but it doesn't return false as well, only lets me ignore the exception. Are there built-in instruments for solving this problem in Selenium? Or maybe someone can offer a more elegant way to solve it?
One of the core tenets of the raw WebDriver API is the expectation that the user knows the state of the DOM for the page being automated. This means that, in the logic of the API, calling findElement using a locator of an element that doesn’t exist is an exceptional condition, making the throwing of an exception perfectly legitimate. While one could argue that the expectation built into the API behavior is faulty, that’s beyond the scope of this answer. If you need Boolean logic for whether an element exists, you need a wrapper method, as you’ve already discovered. Within that wrapper method, you have two choices:
Use findElement and catch the NoSuchElementException. Note that using WebDriverWait implicitly catches this exception for you, so is a semantic equivalent.
Use findElements (note the "s"), which returns an empty list without throwing an exception if the element doesn’t exist.
Boolean bool = my_driver.findElements(By.id("my element id")).size()>0;
this will help you.
An unclean workaround could be to check the DOM for a unique String that symbolizes your WebElement and then write into a boolean if it could be found.
This loop will check the DOM for the string around every second for 60 times with refreshing the page if nothing was found:
for (int i= 0; i <60; i++){
String pageSource = browser.getPageSource().toString();
boolean elementThere = pageSource.contains("uniqueStringOfElement");
if (elementThere){
browser.WebElement.click()
break;
}
else {
Thread.sleep(1000);
browser.navigate().refresh();
}
}

WebDriver isDisplayed() returns true even if the element is hidden

The element I am looking for is as below
I want to call isDisplayed() on the highlighted span web element, and as it is hidden it should return exception. But it is returning true and continuing to the next statement.
Your question lacks a lot of information.
maybe you are looking for some of these:
ispresent()
isvisible()
textpresent()
isDisplayable()
don't know how your code looks but it should be
boolean visible=driver.findElement(By.id("yourverylongid")).isDisplayed();
or using xpath
But I am not sure if it will work because I didn't use is Displayed()

Passing parameters in #FindBy Page factory

Is there any way so that we can parameterize the string which we pass to create a page object using a page factory?
ex:
String v = "password";
#FindBy(name=v)
private WebElementFacade password_Field;
I am trying to push the string v into the #FindBy but I am getting an error.
I am getting
The value for annotation attribute FindBy.name must be a constant expression.
Yes. It is possible. Please find the solution below:
In your Page, instead of using #FindBy, get the WebElementFacade using find method of the Page and then use it for operations.
For example: If you need to click on an element, please see the code below:
public void click(String elementId) {
WebElementFacade element = find(ById.id(elementId));
element.click();
}
This works perfectly for me. And the element id is coming all the way from .story examples.
No, Unfortunately, it's not possible to send arguments to annotation (reference). Since Java annotations does not allow dynamic parameterization. The compiler evaluates annotation metadata at compile time. So, it must be present. Though there are work around for this.
Check this- Java Annotations values provided in dynamic manner

When i use Assert.assertFalse(), to check the absence of an element, my scripts are getting failed

Assert.assertFalse(driver.findElement(By.xpath("element's xpath")).isDisplayed(),
"Bug!! The element is appeared");
When i run my code with the above script, my scripts are failing with the statement like "can't find the element, (Which is mentioned int he element's xpath)"
Now what i have to do? My notion is to verify the element is not present in the screen
findElement throws an exception if no matching element is found, so you tests will fail with the selenium exception, not JUnit.
you can use findElements (which doesn't throws exceptions) and check that the returned list length is equals to 0
See details in the Selenium documentation
I see that this has been asked a long while ago, but perhaps someone who is looking for a solution to the similar problem will find it useful now.
I have a certain way to deal with such problems, not sure if it's a good way to do this, but at least it makes sense and does the job.
Firstly, I usually define a separate method for finding such elements:
private static WebElement element = null;
public static WebElement findYourElement(WebDriver driver) {
try {
element = driver.findElement(By.xpath("your_xpath_here"));
}
catch(NoSuchElementException e) {
System.out.println(e.getMessage());
element = null;
return element;
}
return element;
}
You can see here that the possible NoSuchElementException is being caught should it be thrown. In case of exception, the value for "element" is also set to null, since in my program there might be values from previous elements stored here and we don't to have any other non-relevant elements here.
Next comes the assertion:
Assert.assertFalse(findYourElement != null, "Bug!! The element is there!"
Here the assertFalse method fails if the element returned by the findYourElement is not empty, yielding a True result from element != null comparison and making assertFalse fail, as it is expecting to have a false value in order to continue.
You could just as well go:
Assert.assertTrue(findYourElement = null, "Bug!! The element is there!"
I think that such solution helps to keep things well organized, but on the other hand it may be larger in terms of code volume and more complicated too. It also depends how good are you with Java and if you are willing to mess with the exceptions.
I am no Java guru myself, so any contribution towards improving my current solution will be much appreciated!
Do this small change, it definitely works
Assert.assertTrue(driver.findElements(By.xpath("element's xpath")).size()<1;,
"Bug!! The element is appeared");

Rational Functional Tester wait for object existence

I'm currently modifying a Java script in Rational Functional Tester and I'm trying to tell RFT to wait for an object with a specified set of properties to appear. Specifically, I want to wait until a table with X number of rows appear. The only way I have been able to do it so far is to add a verification point that just verifies that the table has X number of rows, but I have not been able to utilize the wait for object type of VP, so this seems a little bit hacky. Is there a better way to do this?
Jeff
No, there is not a built-in waitForProperty() type of method, so you cannot do something simple like tableObject.waitForProperty("rowCount", x);
Your options are to use a verification point as you already are doing (if it ain't broke...) or to roll your own synchronization point using a do/while loop and the find() method.
The find() codesample below assumes that doc is an html document. Adjust this to be your parent java window.
TestObject[] tables = doc.find(atDescendant(".rowCount", x), false);
If you are not familiar with find(), do a search in the RFT API reference in the help menu. find() will be your best friend in RFT scripting.
You can do one thing.... you can try getting the particular property and check that you are getting the desired value of that. If not then iterate in a IF loop.
while (!flag) {
if (obj.getproperty(".text").equals("Desired Text")) {
flag = true
}
}
You can use:
getobject.gettext();