2013-04-29 64 views
0

实际上,我想获得一个用于页面对象模式的@FindBy元素。@FindBy与Arquillian石墨烯

我有2个类,第一个是我的页面对象名为TestPage和第二个名为PageSaveTest(我的测试发生在那里,并调用TestPage)。

我也尝试使用@FindByxpathid

>>这是我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; 
    } 

} 

>>这是我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(); 

错误消息:

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

我的想法:

我觉得麻烦来自@FindBy。但我使用this example来建立我的TestPage和我的测试和this one too

问题:有人可以向我解释@FindBy如何工作,并在我的例子中使用?关于石墨烯的文档很差。


编辑:

我已经修改我的吸气剂TestPage(上图),我试过id属性值的简单打印像

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

但仍是同样的错误,@FindBy被打开。

Another @FindBy spec在此问题中添加。


更新:

我已经固定我的选择,但确实存在具有像驾驶员会话probleme:

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

我用三种不同的方式,在@FindBy中, @Drone WebDriver最后是什么@Lukas Fryc建议给我。

回答

2

而不是使用@FindByWebElement注射的,你可以尝试使用直接驱动程序:

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']")); 

但它应该给你同样的结果@FindBy做 - 但它会检查,这个问题不是由造成注射,但其他一些问题正在出现。

您可能有错误的CSS选择器 - 对CSS选择器的支持取决于使用的浏览器及其版本。

您试图找到的节点不必在页面中,您可能需要等待,才能使用Waiting API或请求守卫进行显示。

最佳实践是在开发中使用远程可重用会话和真实浏览器 - 它可以快速显示原因。

+0

我已经更新了我的OP。请看看。 – e1che 2013-05-03 09:47:33

0

我认为,而不是使用@FindBy(css ="...")你可以尝试@FindBy(xpath="...")我觉得它更可靠。

+0

谢谢你的贡献,但我不再在这个项目上工作,我没有代码来试用它。我仍然相信CSS选择器是一个很棒的工具,比xpath更好。至少使用起来更容易,而且更少混淆。 – e1che 2017-08-17 06:23:18