2015-05-11 32 views
0

這是我測試Web應用程序的場景,我必須做的第一件事是編寫一個測試,檢查每個頁面上的「報告no.x」的存在。 注意:X的值在頁面之間不同,像在主頁上一樣,在其他頁面上是1,它是3等。 有沒有什麼辦法可以減少我的代碼行數。而不是寫IF否則爲我導航的每一頁都可以有一個解決方案,在那裏我可能必須編寫If If else一次,並且每次打開新頁面時都會執行該塊。檢查每個頁面中是否存在元素

回答

0

您可以通過頁面對象模式和基類之間的組合來完成所需的行爲。

public class BingMainPage 
{ 
    private readonly IWebDriver driver; 
    private readonly string url = @"http://www.bing.com/"; 

    public BingMainPage(IWebDriver browser) 
    { 
     this.driver = browser; 
     PageFactory.InitElements(browser, this); 
    } 

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

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

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

    public void Navigate() 
    { 
     this.driver.Navigate().GoToUrl(this.url);    
    } 

    public void Search(string textToType) 
    { 
     this.SearchBox.Clear(); 
     this.SearchBox.SendKeys(textToType); 
     this.GoButton.Click(); 
    } 

    public void ValidateResultsCount(string expectedCount) 
    { 
     Assert.IsTrue(this.ResultsCountDiv.Text.Contains(expectedCount), "The results DIV doesn't contains the specified text."); 
    } 
} 

如果這是您的一個頁面,您可以創建一個基類來派生自。在那裏你可以有X值屬性和你的等待方法。子類應該從這個類派生出來並傳遞X的確切值。這樣你可以在所有的測試中重用邏輯。

你可以在這裏找到更詳細的例子:http://automatetheplanet.com/page-object-pattern/

+0

當然男人感謝很多是地球上我會嘗試 –

+0

非常感謝安東。爲什麼沒有出現在我的腦海裏:p –

+0

我很高興聽到它適合你。如果這解決了你的問題。你能評價並解決問題,因爲它仍然處於未答覆隊列中嗎? –