Validation 如何使用SeleniumWebDriver在表单字段中使用maxlength=20验证

Validation 如何使用SeleniumWebDriver在表单字段中使用maxlength=20验证,validation,selenium-webdriver,automated-tests,selenium-chromedriver,Validation,Selenium Webdriver,Automated Tests,Selenium Chromedriver,表单有三个字段 1.名字 2.姓 3.电话 我希望名称字段中的验证长度不超过20个字符。在文本框中使用selenium web驱动程序输入超过20个字符,然后使用selenium获取同一文本框的文本,并计算字符串的长度,该长度应为20个字符,且不超过20个字符。验证字段的方法不同 如果用户试图在字段中输入超过最大限制 它阻止用户输入文本,并将字段颜色变为红色 它允许更多的文本到字段,并在尝试提交表单或移动到下一个字段时显示验证消息 解决方案: 使用sendKeys()方法将超过20个字符发送到字

表单有三个字段 1.名字 2.姓 3.电话


我希望名称字段中的验证长度不超过20个字符。

在文本框中使用selenium web驱动程序输入超过20个字符,然后使用selenium获取同一文本框的文本,并计算字符串的长度,该长度应为20个字符,且不超过20个字符。

验证字段的方法不同

如果用户试图在字段中输入超过最大限制

  • 它阻止用户输入文本,并将字段颜色变为红色
  • 它允许更多的文本到字段,并在尝试提交表单或移动到下一个字段时显示验证消息
  • 解决方案:

    使用sendKeys()方法将超过20个字符发送到字段

    案例1: 1.使用getText()方法获取同一字段的文本,然后执行String.length和Assert以确保字符数为20

  • 然后验证字段的CSS属性,并确保使用Assert将颜色变为红色
  • 案例2:执行案例1第一点,
    然后获取验证消息的定位器,从中获取文本,并使用预期的验证消息进行验证。

    要检查任何字段的最大和最小长度,可以使用HTML的
    maxlength
    minlength
    属性,然后使用Selenium的
    getAttribute(“maxlength”)查找长度
    getAttribute(“minlength”)
    函数

    下面给出了示例代码:假设我们讨论的是用户名字段

    //Getting the minimum and maximum value of the username field
    int minLengthDefined = Integer.parseInt(mcp.userName().getAttribute("minlength"));
    int maxLengthDefined = Integer.parseInt(mcp.userName().getAttribute("maxlength"));
    
    //Get the typed value
    String typedValue = mcp.userName().getAttribute("value");
    
    //Get the length of the typed value
    int size = typedValue.length();
    
    if( size < minLengthDefined ){
        Assert.assertEquals(size, minLengthDefined, "Username field has incorrect value.");
        Sysout.out.println("It contains "+size+" characters.");
        Sysout.out.println("Username should lie between "+minLengthDefined+ " and "+maxLengthDefined+ " characters." );
    }
    else if( size > maxLengthDefined) {
        Assert.assertEquals(size, maxLengthDefined, "Username field has incorrect value.");
        Sysout.out.println("It contains "+size+" characters.");
        Sysout.out.println("Username should lie between "+minLengthDefined+ " and "+maxLengthDefined+ " characters." );
    }
    else
        Sysout.out.println("unhandled issue occurred!");
    }
    
    //获取用户名字段的最小值和最大值
    int minLengthDefined=Integer.parseInt(mcp.userName().getAttribute(“minlength”));
    int-maxLengthDefined=Integer.parseInt(mcp.userName().getAttribute(“maxlength”);
    //获取键入的值
    字符串typedValue=mcp.userName().getAttribute(“值”);
    //获取键入值的长度
    int size=typedValue.length();
    如果(尺寸<定义的最小长度){
    Assert.assertEquals(大小、最小长度定义,“用户名字段的值不正确”);
    Sysout.out.println(“它包含“+size+”个字符”);
    Sysout.out.println(“用户名应介于“+minLengthDefined+”和“+maxLengthDefined+”字符之间。”);
    }
    else if(大小>定义的最大长度){
    Assert.assertEquals(大小,maxLengthDefined,“用户名字段的值不正确”);
    Sysout.out.println(“它包含“+size+”个字符”);
    Sysout.out.println(“用户名应介于“+minLengthDefined+”和“+maxLengthDefined+”字符之间。”);
    }
    其他的
    Sysout.out.println(“发生了未处理的问题!”);
    }
    
    Hi,欢迎来到Stack Overflow。如果你能让我们看看你的尝试,我们会更喜欢。另外,Selenium是一个web测试系统。它不做表单验证,尽管它可能会验证它。您是否希望测试或实施此验证?