0

actually i want to get an element with the @FindBy that is use into the Page Objects pattern.

I've 2 classes, the 1st one is my page objects named TestPage and the 2nd one is named PageSaveTest (where my tests happen and call the TestPage).

I've also tried to use the @FindBy with xpath and id.

>> This is my TestPage

import java.util.List;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

public class TestPage {

  // get autocomplete input 
  @FindBy(css = "input[id*='supplierOps_input']")
  private WebElement autocompleteSupplierOps;

  // getter
  public WebElement getAutocompleteSupplierOps() {
    return autocompleteSupplierOps;
  }

}

>> This is my PageSaveTest

// How i "inject" my TestPage
@Page
TestPage testpage;

[...]
// My test
WebElement autocomplete = testpage.getAutocompleteSupplierOps();

String keys = "OP";
autocomplete.sendKeys(keys); // >>>>>>> Error throwed here !                
List<WebElement> listSugg = testpage.getSuggestionsSupplierOps();

Error message :

org.openqa.selenium.NoSuchElementException : Returned node was not an HTML element.

My thoughts :

I think the trouble comes from the @FindBy. But i use this example to build my TestPage and my test and this one too.

Question : Can someone explain to me how @FindBy works and be used in my example ? The documentation is really poor about Graphene.


EDIT :

I've modify my getter in TestPage (above), i've tried a simple print of the id attribute value like

public WebElement getAutocompleteSupplierOps() {
  System.out.println(">>>> "+autocompleteSupplierOps.getAttribute("id"));
  return autocompleteSupplierOps;
}

But still the same error, the @FindBy is f*cked up.

Another @FindBy spec to add in this issue.


Update :

I've fixed my selector but actually there is a probleme with the driver session like :

             page2.getAutocompleteSupplierOps();
   PAGE 1   ---------------------------------->   PAGE 2
driver id:1 ----------------------------------> driver id:2
                                                driver.showPageSource() is empty
return no element found <---------------------- driver.findElement() -> not found    

I've used 3 different ways, the @FindBy, the @Drone WebDriver and finally what @Lukas Fryc suggested to me.

e1che
  • 1,241
  • 1
  • 17
  • 34

2 Answers2

2

Instead of injection of WebElement using @FindBy, you can try using driver directly:

WebDriver driver = GrapheneContext.getProxy(); // this will be removed in Alpha5 version, use `@Drone WebDriver` instead
WebElement autocompleteSupplierOps = 
    driver.findElement(By.css("input[id*='supplierOps_input']"));

But it should give you the same result as @FindBy do - however it will check that the issue isn't caused by injection, but some other issue is appearing.

You might have wrong CSS selector - support of CSS selectors depends on the used browser and its version.

The node you are trying to find doesn't have to be yet in the page, you might need to wait before it will appear using Waiting API or request guards.

The best practice is usage of remote reusable session and real browser in a development - it can reveal the cause quickly.

Lukas Fryc
  • 131
  • 4
0

I think that instead of using @FindBy(css ="...") you could try @FindBy(xpath="...") I find it a lot more reliable.

Sharon
  • 146
  • 1
  • 8
  • Thank you for your contribution, but i don't work on this project anymore and i don't have the code to try it out. I still belive that css selector is an awesome tool, better than xpath. At least easier to use and less confuse. – e1che Aug 17 '17 at 06:23