2016-05-25 4 views
1

Seleniumのページオブジェクトモデルのデザインパターンを考えると、アクションを実行する前にWebElementがページ上に存在するかどうかを確認する必要があります。C#SeleniumのIWebElementからFindsByAttributeを取得する方法は? [Page Object Model]

ベース 'ページ' クラス:

public class Page 
{    
    public Page() 
    { 
     PageFactory.InitElements(SeleniumTests.driver, this); 
    } 
} 

アン継承されたクラス:最後に

class Page_Certificate_ChooseOperator : Page 
    {    
     [FindsBy(How = How.Id, Using = "SearchOperatorName")] 
     public IWebElement txtName { get; set; } 

     [FindsBy(How = How.Id, Using = "SearchOperatorEstablishmentNumber")] 
     public IWebElement txtEstablishmentNumber { get; set; } 

     [FindsBy(How = How.Id, Using = "searchButton")] 
     public IWebElement btnSearch { get; set; } 

     public void SelectOperator(String name, String establishmentNumber) 
     { 
      this.txtName.SetInputField(name); 
      this.txtEstablishmentNumber.SetInputField(establishmentNumber);     
      this.btnSearch.SafeClick(); 
     }   
    } 

拡張メソッドのためのクラス:

public static class SeleniumExtensionMethod 
    { 
     public static void SetInputField(this IWebElement webElement, String value) 
     { 
      webElement.Clear(); 
      webElement.SendKeys(value); 
     }  

     public static void SafeClick(this IWebElement webElement, int timeout_in_seconds) 
     {  
      //This is the difficult part. I need to check if the webElement is visible, but I don't want to write "low-level" code and specify the ID and selector. Is there a way I can find out how the webElement was created by this class and say something like "webElement.How" to find "How.ID" or "webElement.Using" to find "btnSearch"? I need to use something like the below code using the PageObject 
      // WebDriverWait wait = new WebDriverWait(SeleniumTests.driver, TimeSpan.FromSeconds(10)); 
      // IWebElement dynamicElement = wait.Until<IWebElement>(driver => driver.FindElement(By.Id("btnSearch"))); 
      // dynamicElement.Click(); 

      //Now, I just use this, but it crashes on NoSuchElementFound since it goes too fast. I don't want to use ImplicitWait. 
      btnSearch.Click(); 


     } 
    } 

挑戦私が直面していますボタンbtnSearchをクリックする前にExplicitWaityが必要なのですが、それはaa NoSuchElemをスローしますentFoundException。 [FindsBy]属性を使用しているので、webElementの検索/作成方法を確認する方法があるはずです。これどうやってするの?または、きちんとしたコードを持ち、ExplicitWaitをページオブジェクトで使用する方法は?

答えて

1

存在/クリック可能/使用可能にする要素を待つために、あなたはElementToBeClickable条件でウェイターを使用することができます。

[FindsBy(How = How.Id, Using = "searchButton")] 
public IWebElement btnSearch { get; set; } 

public void SelectOperator(String name, String establishmentNumber) 
{ 
    this.txtName.SetInputField(name); 
    this.txtEstablishmentNumber.SetInputField(establishmentNumber);     

    new WebDriverWait(SeleniumTests.driver, TimeSpan.FromSeconds(10)) 
     .Until(ExpectedConditions.ElementToBeClickable(this.btnSearch)) 
     .Click(); 
} 
+0

これは時折動作しますが、また時々に、TargetInvocationExceptionがWebDriver.Support.dllで発生したスロー。あなたはこれを修正する方法を知っていますか?私たちのプロジェクトの成功にとっては非常に重要です。ありがとう! –

+0

内部例外とは何ですか?あなたは新しい質問を作成する必要があります、それは別の問題であるようです。 –

関連する問題