在硒中捕获客户端验证消息(testng)

Capturing client side validation message in selenium (testng)

我想捕获在文本框中输入无效输入后生成的消息(以文本形式显示)。当我单击其他文本框或仅单击屏幕时,会显示此消息。此文本已分配给它的 ID,但在输入无效输入时会在 运行 次显示,否则不会显示。

这是代码行

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

因为,我在 testng 中使用@dataProvider 传递多个输入,所以我可能同时有正数和负数 results.So 如果结果为负数,我想捕获生成的错误消息 运行时间在客户端,后来将其提取到 report.As 现在我只能提取服务器端错误消息,很难提取客户端错误消息。

我附上了网站查看源的图片,

提前致谢 网站图片

您好,请按照下面的方式操作

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");
        }   
    }
    }

希望对您有所帮助

我认为您可以使用 ExpectedConditions.not.invisibilityOfElementWithText 解决它,因为使用 presenceOf 无论如何都会 return 为真DOM 的可见度:

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);

我还没有尝试过,但我认为它会奏效。如果没有,对代码进行小幅调整应该可以解决它。