【问题标题】:Capturing client side validation message in selenium (testng)在 selenium (testng) 中捕获客户端验证消息
【发布时间】:2020-07-28 14:25:15
【问题描述】:

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

这是代码行

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

因为,我在 testng 中使用 @dataProvider 传递多个输入,所以我可能有正面和负面的结果。所以如果出现负面结果,我想在客户端捕获运行时生成的错误消息,然后再获取它到报告。截至目前,我只能获取服务器端错误消息,难以获取客户端错误消息。

我已经附上了网站视图源的图片,

提前致谢 网站图片

【问题讨论】:

  • 查看源图片在哪里
  • 我刚刚添加了@rajNishKuMar

标签: java selenium selenium-webdriver testng


【解决方案1】:

你好,请按照下面的方式进行

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

希望对你有帮助

【讨论】:

  • 非常感谢,我错过了一次点击事件,这就是为什么它不起作用
  • 它有时会发生,我很高兴它帮助了你
【解决方案2】:

我认为您可以使用 ExpectedConditions.not.invisibilityOfElementWithText 来解决它,因为无论是否在 DOM 上可见,使用 presenceOf 都会返回 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);

我还没有尝试过,但我认为它会起作用。如果没有,对代码稍作调整即可解决。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-09
    • 1970-01-01
    • 2012-01-21
    • 1970-01-01
    • 1970-01-01
    • 2012-08-24
    相关资源
    最近更新 更多