2013-04-29 63 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