2013-08-26 8 views
13

なぜ@FindByWebDriver#findElement()を使用しますか?Selenium @FindBy vs driver.findElement()

@FindByは、すべての変数をクラスレベルに移動させます(ほとんどの場合、メソッドレベルにする必要があります)。それが私に買われる唯一の事は、PageFactory.initElements()と呼ぶことができます。これは私のために怠惰な初期化を処理します。

私には何が欠けていますか?

答えて

31

おおまかに言えば、@FindByは要素を見つける代替方法です(あなたが言ったように「通常の方法」はdriver.findElement()です)。

そのアノテーションの大きな利点は、それ自体ではありません。 PageObject patternをサポートする方が良いでしょう。いくつかの単語で

のPageObjectパターンは、あなたが/テストを使用しようとしているシステムのページごとにクラスを作成に説明します。

ので、代わりに(通常driver.findElement()コード)を有するの:

public class TestClass { 
    public void testSearch() { 
     WebDriver driver = new HtmlUnitDriver(); 
     driver.get("http://www.google.com/"); 
     Element searchBox = driver.findElement(By.name("q")); 
     searchBox.sendKeys("stringToSearch"); 
     searchBox.submit(); 
     // some assertions here 
    } 
} 

あなたは(使用される要素のための@FindByアノテーションで)ページのためのクラスを定義したい:

public class GooglePage { 
    @FindBy(how = How.NAME, using = "q") 
    private WebElement searchBox; 
    public void searchFor(String text) { 
     searchBox.sendKeys(text); 
     searchBox.submit(); 
    } 
} 

そして、それを次のように使用してください:

public class TestClass { 
    public void testSearch() { 
     WebDriver driver = new HtmlUnitDriver(); 
     driver.get("http://www.google.com/"); 
     GooglePage page = PageFactory.initElements(driver, GooglePage.class); 
     page.searchFor("stringToSearch"); 
     // some assertions here 
    } 
} 

ここでは、これはfiで冗長に見えるかもしれません最初にそれをしばらくお待ちいただき、複数のテストケースをそのページに含めることを検討してください。 searchBoxの名前が変更されたらどうなりますか? (idからname"q"から、queryを言うの?)

をどのようなコードでは、それが再び動作させるために多くの変化があるでしょうか?ページオブジェクトを持たないもの(および@FindBy)?ページの構造が大きく変わった場合、どのコードでメンテナンスが容易になるのでしょうか? @CacheLookupは一度だけ起こる要素のためのルックアップを行い

@FindBy(name = "q") 
@CacheLookup 
private WebElement searchBox; 

こと:

は、そのようなのような追加の注釈など他のいくつかの利点があります。その後、変数にキャッシュされ、より高速にアクセスできます。

これが役に立ちます。詳細については、PageFactoryPageObject patternを必ず確認してください。その後、IntelliJのは、もはやその変数が使用されている場合、それは痛みをクリーンアップすることができます検出するので

+0

私は自分がPageFactoryを典型的なインスタンス化よりも好むと言っています。はるかに簡単です。 – sircapsalot

+1

+1そのような詳細な回答.... – Akbar

+0

PageObjectパターンは非常に役立つようです!しかし、私はそのパターンを.findElementと同様に使うことはできませんか? – pyrrhicVictory

-1
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.support.FindBy; 
public class CommonPageForStudent { 

    @FindBy(name="usname") 
    private WebElement Studentusername; 
    @FindBy(name="pass") 
    private WebElement Studentpassword; 
    @FindBy(xpath="//button[@type='submit']") 
    private WebElement StudentLetmein; 
    @FindBy(id="logoutLink") 
    private WebElement StudentlogoutLnk; 
    public void loginToStudent(String username , String password){ 
     Studentusername.sendKeys(username); 
     Studentpassword.sendKeys(password); 
     StudentLetmein.click(); 

     } 

    //when you call this methods from another class write this 2 line code that class 
    //CommonPageForStudent page = PageFactory.initElements(driver,  CommonPageForStudent.class); 
    // page.loginToStudent("",""); 

    public void logOut(){ 
     StudentlogoutLnk.click(); 
    } 
    //page.logOut(); method call 

}` 
+1

このコードにいくつかの説明を追加できますか? – mlkn

0

私は注釈を@FindBy好きません。

関連する問題