Selenium Webdriver Java验证名称字段
我对硒不是很有经验。我想通过执行以下操作来测试我的知识,验证表单中的名称字段是否没有特殊字符。我不能这样做。首先,我尝试将字符放入数组并从数组中读取,但我不断收到警报失败消息。然后我想到了下面的方法,并且总是得到“有效”的输出 导入junit.framework.AssertSelenium Webdriver Java验证名称字段,java,selenium,character,Java,Selenium,Character,我对硒不是很有经验。我想通过执行以下操作来测试我的知识,验证表单中的名称字段是否没有特殊字符。我不能这样做。首先,我尝试将字符放入数组并从数组中读取,但我不断收到警报失败消息。然后我想到了下面的方法,并且总是得到“有效”的输出 导入junit.framework.Assert import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.NoAlertPresentExcep
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class NameField {
public static FirefoxDriver fx= new FirefoxDriver();
public static String doCheck()
{
fx.get("http://www.gogamers.com/#!blank/gs4id");
String regex = "^[A-Z0-9+$";
String str=fx.findElement(By.id("comp-iikjotq8nameField")).getText();
fx.findElement(By.id("comp-iikjotq8nameField")).sendKeys("@john");
if (str.matches("[" + regex + "]+")){
System.out.println("Invalid character in Name field");
}
else{
System.out.println("valid");
}
return str;
我想的是,如果您使用sendkey(例如:John#,@John)提供名称,您将收到无效消息。我在想的另一件事是我应该使用断言吗?请建议一个最好的方法一个小样本代码将是有益的
我今天尝试的新代码仍然有效,而我预期无效。有人能看一下吗?我试了两条火柴,结果都找到了 公共类邮件{
public static void main(String[] args) {
FirefoxDriver fx= new FirefoxDriver();
fx.get("https://login.yahoo.com/account/create?");
String title=fx.getTitle();
Assert.assertTrue(title.contains("Yahoo"));
//First I send a text, then I get the text
fx.findElement(By.id("usernamereg-firstName")).sendKeys("$John");
fx.findElement(By.id("usernamereg-firstName")).getText();
//This is the String I want to find
String firstName="John";
//If there are these symbols associated with the name-show invalid
String patternString = ".*$%^#:.*";
Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(firstName);
if(matcher.find()){
System.out.println("Invalid Name" );
}
else{
System.out.println("Valid Name");
}
}
}您可以修复正则表达式以匹配任何非字母数字字符,并使用
模式
和匹配器
:
Pattern p = Pattern.compile("\\W");
Matcher m = p.matcher(str);
if (m.find()) {
System.out.println("Invalid character in Name field");
}
else {
System.out.println("valid");
}
它现在正在工作,问题是我没有捕获sendKeys值。我应该使用getAttribute
f.get("https://mail.yahoo.com");
f.findElement(By.id("login-username")).sendKeys("jj%jo.com");
//The getAttribute method returns the value of an attribute of an HTML Tag;
//for example if I have an input like this:
WebElement element = f.findElement(By.id("login-username"));
String text = element.getAttribute("value");
System.out.println(text);
if((text).contains("@")){
System.out.println("pass");
}
else{
System.out.println("not pass");
}
enter code here
}为了验证名称字段,我使用了与我们的开发人员在网站中使用的相同的正则表达式。在我的例子中,Name字段只接受字母数字字符。首先,我创建了一个java函数来随机生成带有特殊字符的字母数字,如下所示,然后将自动生成的输入与实际的正则表达式进行比较。由于在我的例子中不允许使用特殊字符,if语句将返回false,而else块将被执行,显示不允许使用特殊字符
//can also be used for complex passwords
public String randomSpecial(int count)
{
String characters = "~`!@#$%^&*()-_=+[{]}\\|;:\'\",<.>/?ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
String generatedString = RandomStringUtils.random(count, characters);
return generatedString;
}
public void TC_05_regExpression_Invalid()
{
String regex = "/^[a-zA-Z0-9- ]*$/";
WebElement element = driver.findElement(By.name("firstName"));
element.sendKeys(randomSpecial(10));
String fieldText = element.getAttribute("value");
if(fieldText.matches("["+ regex + "]+"))
{
logger.info("Valid Input: " + fieldText);
}
else
{
logger.info("InValid Input: " + fieldText + "not allowed");
}
element.clear();
}
//也可用于复杂密码
公共字符串随机特殊(整数计数)
{
字符串字符=“~`!@$%^&*()-\=+[{]}\\\\\\\:\'\”,/?ABCDEFGHIJKLMNOPQRSTUVXYZABCDFGHIJKLMNOPQRSTUVXYZ0123456789”;
String generatedString=RandomStringUtils.random(计数,字符);
返回生成的字符串;
}
公共无效TC_05_regExpression_Invalid()
{
字符串regex=“/^[a-zA-Z0-9-]*$/”;
WebElement=driver.findElement(By.name(“firstName”);
元素。发送键(随机特殊(10));
字符串fieldText=element.getAttribute(“值”);
if(fieldText.matches(“[”+regex+“]+”))
{
logger.info(“有效输入:”+fieldText);
}
其他的
{
logger.info(“无效输入:“+fieldText+”不允许”);
}
元素。clear();
}
通常最好解释一个解决方案,而不是仅仅发布几行匿名代码。您可以阅读,也可以
//can also be used for complex passwords
public String randomSpecial(int count)
{
String characters = "~`!@#$%^&*()-_=+[{]}\\|;:\'\",<.>/?ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
String generatedString = RandomStringUtils.random(count, characters);
return generatedString;
}
public void TC_05_regExpression_Invalid()
{
String regex = "/^[a-zA-Z0-9- ]*$/";
WebElement element = driver.findElement(By.name("firstName"));
element.sendKeys(randomSpecial(10));
String fieldText = element.getAttribute("value");
if(fieldText.matches("["+ regex + "]+"))
{
logger.info("Valid Input: " + fieldText);
}
else
{
logger.info("InValid Input: " + fieldText + "not allowed");
}
element.clear();
}