2016-07-25 55 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。