2016-04-21 56 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); 

我還沒有試過,但我認爲它會起作用。如果沒有,對代碼的小調整應該修復它。