如何使用Java在Selenium WebDriver中检查必填字段是否为空?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18460687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:49:59  来源:igfitidea点击:

How to check mandatory field whether it is empty or not in Selenium WebDriver using Java?

javaeclipseselenium-webdriverjunit4assertion

提问by Nasrin Naher

I am working on "add new user form" . In this form, there are several mandatory fields. Form will not be submitted when any mandatory field is empty and it will show a validation message like : "Value is required and can't be empty". How can I check/automate the form whether field is empty or not?

我正在处理“添加新用户表单”。在此表单中,有几个必填字段。当任何必填字段为空时,将不会提交表单,它将显示一条验证消息,例如:“值是必需的,不能为空”。如何检查/自动化表单字段是否为空?

采纳答案by Ripon Al Wasim

Way 1: First, write the following method:

方式一:首先编写如下方法:

private boolean isTextPresent(String text){
        try{
            boolean b = driver.getPageSource().contains(text);
            return b;
        }
        catch(Exception e){
            return false;
        }
  }

Now do assertion whether the expected message is present or not on the page by calling the above method:

现在通过调用上述方法断言页面上是否存在预期的消息:

assertTrue(isTextPresent("Value is required and can't be empty"), "Msg is absent/wrong/misspelled");

Way 2: Another way is as follow:

方式二:另一种方式如下:

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;

private StringBuffer verificationErrors = new StringBuffer();

try {
      assertTrue(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\s\S]*Value is required and can't be empty[\s\S]*$"));
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
  }

回答by Sankumarsingh

You can use assertfor checking the mandatory field alert is appearing or not, with try-catch

您可以使用asserttry-catch 检查必填字段警报是否出现

try {
  assertEquals("Value is required and can't be empty", driver.findElement(By.xpath("XPATH_OF_LABLE")).getText());
} catch (Error e) {
 //code for the case when the texts are not same
  verificationErrors.append(e.toString());
}

the example is using xpath, but you can use any other locator method for this.

该示例使用的是 xpath,但您可以为此使用任何其他定位器方法。

回答by Thangalakshmi

Use SoftAssertto validate all of the mandatory fields in a form by a single @Testannotation. Even if any of the assertions fail, it will execute the next line and gives us the all exception caught in @Testwhen we do assertAll()at the end.

用于SoftAssert通过单个@Test注释验证表单中的所有必填字段。即使任何断言失败,将执行下一行,使我们陷入了所有的异常@Test,当我们做assertAll()结尾。

driver.findElement(by.id("submit")).click();
SoftAssert softAssert = new SoftAssert();
softAssert.assertEquals(firstname.getText(), "First Name is required");
softAssert.assertEquals(lastname.getText(), "Last Name is required");
softAssert.assertEquals(email.getText(), "Email Address is required");
softAssert.assertAll();