【问题标题】:How can I validate required input fields using Selenium如何使用 Selenium 验证所需的输入字段
【发布时间】:2019-11-18 21:30:24
【问题描述】:

我是自动化测试的新手,我的任务是使用给定的注册表实现自动化注册学生。

这个场景让我对如何验证注册表单中的输入字段有点困惑。

我的意思是,如果我没有填写必填字段,浏览器会提示我必须填写该字段。

另一件事是我为 2 名学生使用 for loop,我希望第一次将输入字段留空。提交后,我必须看到它已经过验证,然后填写空输入字段以再次提交(现在在字段中插入数据)。

我正在分享我的代码,希望能提供任何帮助。

public class Cartus {

    public static  void fill() throws InterruptedException {

        System.setProperty("webdriver.chrome.driver", "E:\\workspace\\chromeDriver\\chromeDriver.exe");

        ChromeDriver driver = new ChromeDriver();

        for(int i=0; i<2; i++) {

            driver.get("http://qa-5.ls.vu/v2/landing-page/cartus/en");
            driver.findElement(By.xpath("/html/body/div[4]/div/form/div[1]/div")).click();
            driver.findElement(By.xpath("/html/body/div[4]/div/form/div[1]/div/select/option[2]")).click();
            driver.findElement(By.xpath("/html/body/div[4]/div/form/div[2]/div[1]/input")).sendKeys(/*"sami"*/);
            driver.findElement(By.xpath("/html/body/div[4]/div/form/div[2]/div[2]/input")).sendKeys(/*"Mohaname"*/);

            WebElement ele=driver.findElement(By.xpath("/html/body/div[4]/div/form/div[3]/div[1]/input"));/*.sendKeys("0221-1234567");*/

            String date = FastDateFormat.getInstance("yyyyMMddHHmmss").format(System.currentTimeMillis());
            Thread.sleep(1000);
            driver.findElement(By.xpath("/html/body/div[4]/div/form/div[3]/div[2]/input"))
                    .sendKeys(/*"aali_"+date+"@outlook.com"*/);
            driver.findElement(By.id("submitButton")).click();

            if(ele.getAttribute("value").isEmpty()){
                driver.findElement(By.xpath("/html/body/div[4]/div/form/div[2]/div[1]/input")).sendKeys("abcds");
            }
            if(ele.getAttribute("value").isEmpty()){
                driver.findElement(By.xpath("/html/body/div[4]/div/form/div[2]/div[2]/input")).sendKeys("abcds");
            }
            if(ele.getAttribute("value").isEmpty()){
                driver.findElement(By.xpath("/html/body/div[4]/div/form/div[3]/div[1]/input")).sendKeys("abcds");
            }
            if(ele.getAttribute("value").isEmpty()){
                driver.findElement(By.xpath("/html/body/div[4]/div/form/div[3]/div[2]/input")).sendKeys("aali_"+date+"@outlook.com");
            }       
            System.out.println("Test case succesfully run");
        }
    }
}

谢谢。

【问题讨论】:

    标签: java selenium selenium-webdriver automation


    【解决方案1】:

    查看您的代码并了解您是自动化测试的新手,我可以告诉您的第一件事是尽量避免使用XPath,并尽可能使用cssSelector。 如果您有一个可以识别您的元素的 ID 或 CSS 类,它会更安全、更短。

    Here's 一个很好的阅读和理解来源。

    在那之后,我相信你的问题有两个问题:

    1. 如何测试留空字段的验证

    当您不填写信息时,页面有几种验证字段的方法(例如,在输入失去焦点后、单击按钮提交表单后等)。

    无论如何,您必须检查隐藏然后显示的 HTML(带有警告消息 - 例如“需要字段名称”)并使用 Selenium 检查此元素现在是否存在。

    // findElements (using the plural) will not result in "Element not found exception"
    WebElement nameMessage = driver.findElements(By.id("nameErrorMessage"));
    if (nameMessage.size > 0) {
        nameMessage.get(0).getText(); // do your validation to check if the message is the one expected
    } else {
        throw new Exception("Warning message was not displayed properly");
    }
    
    1. 为不同的数据创建一个循环

    在这种情况下,您可以有一个对象来保存表单中使用的信息,将其放入列表中并使用它。

    这里有一个例子:

    class Student {
        private String name;
        private Date birthday;
        private String className;
    
        public Student(String name, Date birthday, String class) {
            this.name = name;
            this.birthday = birthday;
            this.className = class;
        }
    
        // all getters and setters
    }
    
    class StudentTest {
        Student emptyStudent = new Student("", null, "");
        Student student = new Student("John", new Date(), "7th grade");
        List<Student> students = Arrays.asList(emptyStudent, student);
    
        // Set up your selenium
    
        // Loop your data to be tested
        students.forEach(student -> {
            // Fill the information with the student info
            driver.findElement(By.id("name")).sendKeys(student.getName());
            driver.findElement(By.id("birthday")).sendKeys(student.getBirthday());
            driver.findElement(By.id("className")).sendKeys(student.getClassName());
    
            // Submit the form
            driver.findElement(By.id("save")).click();
    
            // Check if warning message is shown (code the logic properly to work with and without it)
        });
    }
    

    希望它能帮助您解决问题。

    【讨论】:

    • 非常感谢您的帮助,它确实有效,如果向输入字段发送无效数据并且如果生成警报,我将清除该输入字段并发送有效数据,这是我的代码试图做 String[] invalidChars = {"#$%^&*1213", " "}; for (String invalid : invalidChars) { driver.findElement(By.xpath("/html/body/div[4]/div/form/div[2]/div[1]/input")).sendKeys("sami "+无效);
    • String alertMessage =driver.switchTo().alert().getText(); if (alertMessage.equals("请仅使用字符作为输入。")) { Thread.sleep(1000); driver.findElement(By.xpath("/html/body/div[4]/div/form/div[2]/div[1]/input")).clear(); driver.findElement(By.xpath("/html/body/div[4]/div/form/div[2]/div[1]/input")).sendKeys("sami"); }
    • 需要您的支持,它是一串无效字符,然后是带有无效字符的 sendkeys,然后 driver.getalert 和 gettext 获取文本将清除该字段并发送有效数据
    【解决方案2】:

    如果您的文本在 HTML 的 VALUE 范围内,则以下内容将起作用。比如——

    value="YourText"

    var UniqueName = YourElement.NameGoesHere.GetAttribute("value");

    当调试时在上面设置一个断点并将鼠标悬停在 UniqueName 上,你会看到你的文本被返回并且你没有得到一个 NULL,因此可以验证这个工作比运行整个测试更容易看到它死了.

    【讨论】:

      猜你喜欢
      • 2016-11-16
      • 1970-01-01
      • 2021-12-11
      • 2017-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-27
      相关资源
      最近更新 更多