2016-07-25 54 views
0

測試的錯誤消息我有多個字段的網頁。在那裏我有幾個必填字段,如果我輸入更新按鈕而不輸入必填字段,它必須顯示錯誤。所以現在我想測試一下特定的錯誤信息是否到來,然後我還要測試剩餘的字段。無法在我的網頁

這裏是我的代碼:

public class TestProfileCompanyDetailsPage 
{ 
    WebDriver driver; 
    @Test 
    public void verifyProfileCompanyDetailsPage() throws Exception 
    { 
     driver = DriverInIt.getInstance().getDriver(); 

     driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 

     LoginIntoVendors login=PageFactory.initElements(driver, LoginIntoVendors.class); 

     login.verifyLoginVendors(); 

     VendorsHomePageApp vhpapp=PageFactory.initElements(driver, VendorsHomePageApp.class); 

     vhpapp.clickOnProfileTab(); 

     ProfileCompanyDetailsPage proCompDetPage=PageFactory.initElements(driver, ProfileCompanyDetailsPage.class); 

     proCompDetPage.clickOnCompDetEdit(); 

     proCompDetPage.clickAndEnterCompName("IBM"); 

     proCompDetPage.clickOnUpdateButton(); 

     } 

    public boolean IsTestElementPresent(WebDriver driver) 
    { 
     try 
     { 
      driver.findElement(By.xpath("html/body/section/div/div/div[1]/div[3]/p[2]/span")); 
      return true; 
     } 
     catch(NoSuchElementException e) 
     { 
      return false; 
     } 
    } 



} 

這裏是頁:

enter image description here

HTML代碼:

<p class="mB10px" style="padding-left: 20%;"> 
<button class="btn btn-success" style="width: 120px;" ng-click="saveVendor(1)" ng-class="{"cbc-btn-spin":persist==1}">Update</button> 
<span class="btn-link mL5px vBot pointer" ng-click="cancelVendor(1)">Cancel</span> 
</p> 
<p class="mB10px show" ng-class="{"show":uiVendorError1.length>0}" style="display: none; padding-left: 20%;"> 
<span class="error ng-binding" ng-bind="uiVendorError1">Please enter Company Description</span> 
</p> 
<p class="mB10px" ng-class="{"show":uiVendorSuccess1.length>0}" style="display: none; padding-left: 20%;"> 
<span class="greenC ng-binding" ng-bind="uiVendorSuccess1"/> 
</p> 
+0

那麼,什麼是這個問題?有什麼異常嗎?並且需要分享錯誤消息部分的HTML代碼以及 –

+0

沒有例外,我有公司名稱和說明字段,我希望在單擊更新按鈕後測試這兩個字段的錯誤消息。上面的代碼只有一次我應該停止這個測試用例,但我需要繼續測試。 –

+0

有沒有其他的方法來測試這些部分。 –

回答

1

在這種情況下,你應該嘗試使用WebDriverWait等到你的元素有一些文字與如下定製ExpectedConditions: -

public boolean IsTestElementPresent(WebDriver driver) 
{ 
    try 
    { 
     WebDriverWait wait = new WebDriverWait(driver, 10); 

     return wait.until(new ExpectedCondition<Boolean>() { 
        public Boolean apply(WebDriver d) { 
         WebElement el = d.findElement(By.cssSelector("span.error.ng-binding")); 
         return (el.getText().length() > 0); 
        } 
       }); 
    }catch(TimeoutExceptione e) 
    { 
     return false; 
    } 
} 

編輯: - 如果你想與包含錯誤meassage獲得元素,嘗試如下圖所示: -

public boolean IsTestElementPresent(WebDriver driver) 
{ 
    try 
    { 
     WebDriverWait wait = new WebDriverWait(driver, 10); 

     wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[contains(., 'Please enter Company Description')]"))); 

     return true; 

    }catch(TimeoutExceptione e) 
    { 
     return false; 
    } 
} 

希望它能幫助.. :)

+0

可以說這個是針對「公司名稱」字段的,我需要繼續測試意思來測試「描述」字段。希望你能理解。 –

2
// do stuff to cause the error message to show up 
Assert.assertTrue(driver.findElement(By.xpath("//*[text()='Please enter Company Description']")).isDisplayed()); 

在T他上面如果錯誤信息不在那裏會引發異常。然而,這並不是很好,因爲錯誤可能需要一些時間才能在驗證後彈出,並且您的測試可能已經過測試,看看它是否存在。更好的解決方案是使用網絡驅動程序等待,並檢查它是否在預期的時間內可見。此外,請注意,如果錯誤消息有一個ID,那麼選擇它比使用匹配XPath的文本好得多。更好的例子:

// do stuff to cause the error message to show up 
new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[text()='Please enter Company Description']"))); 

這會等到錯誤消息是可見的(元素返回true爲它的isDisplayed方法)。如果它不10秒內變得可見然後TimeoutException將被拋出。請注意,這只是如何檢查元素的一個示例,在測試中通常會採用最佳做法來捕獲該異常,並通過更好,更具描述性的錯誤消息來斷言失敗。

要檢查多個字段,只是使你與你所期望的匹配,並且使用一個變量,而不是硬編碼應匹配的文本字符串配套調用一個輔助方法。如果文本中有任何引號(單或雙),請注意,否則可能會導致無效的XPath。