2016-04-21 57 views
0

site image我想捕获在文本框中输入无效输入后生成的消息(以文本形式显示)。当我单击其他文本框或仅在屏幕上时,将显示此消息。该文本具有分配给它的标识,但在输入无效输入时在运行时显示,否则不显示。在硒中捕获客户端验证消息(testng)

这是代码

driver.findElement(By.id("ctl00_CenterCPH_txtCityName")).sendKeys(c.cityname); 

如,我传递使用TestNG中@dataProvider多个输入行,所以我可以具有负结果的情况下的正面和负面results.So欲捕获客户端生成的运行时错误消息,然后将其提取到报告。截至目前,我只能够获取服务器端错误消息,难以获取客户端错误消息。

我附上该网站的查看源代码的图片,

提前感谢 现场图像view source of the site

+0

这里是观看源图像 –

+0

我已经加入刚才@rajNishKuMar –

回答

0

喜请做到像下面

public class CapturingErrorMessage { 
    public static void main(String[] args) { 

     WebDriver driver = new FirefoxDriver(); 
     driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 

     driver.get("ur url"); 

     // suppose u entered wrong i.e more then 128 character string in the input box 

     driver.findElement(By.id("ctl00_CenterCPH_txtCityName")).sendKeys("more then 128 character"); 

     // talking under consideration that error message is displayed when you click on other textbox or simply on the screen. 

     // suppose below line is to click on other option or some where on the screen 
     driver.findElement(By.id("ctl00_CenterCPH_txtCityName")).click(); 

     // now error message comes so before capturing the error message firstly try to 
     // identify if the span id with error message is present or not 

     // simply in the if i am checking that if the size of the span tag with error is more then 0 or not if more then zero 
     // i.e error is present on the page 
     if(driver.findElements(By.id("ct100_CenterCPH_revAlphaNum1Len")).size() > 0){ 
      String error = driver.findElements(By.id("ct100_CenterCPH_revAlphaNum1Len")).get(0).getText(); 
      System.out.println(error); 
     }else{ 
      System.out.println("Element not present : no error message"); 
     } 
    } 
    } 

希望这将有助于你

+0

这是行不通的 –

+0

什么错误る越来越 –

+0

非常感谢,我已经错过了一个点击事件,这就是为什么它不是工作 –

0

我想你可以用这样的方法解决它ExpectedConditi ons.not.invisibilityOfElementWithText因为使用presenceOf将在DOM或不可见的返回true不管:

public static final By INVALID_CITY_NAME = 
    By.xpath(".//span[ends-with(@id,'CenterCPH_txtCityName"); 
    ... 
public static boolean waitForTextToAppear(String textToAppear, By locator, 
    int timeoutSeconds) 
{ 
    WebDriverWait wait = new WebDriverWait(driver,timeoutSeconds); 
    return wait.until(
     ExpectedConditions.not.invisibilityOfElementWithText(locator, textToAppear) 
    ); 
} 

然后调用它像这样:

boolean visible = waitForTextToAppear(INVALID_CITY_NAME, "Invalid City name.", 10); 

我还没有试过,但我认为它会起作用。如果没有,对代码的小调整应该修复它。