我正在努力确保在“文本框”中我只能输入特定字符,或者只输入数字。 我想用硒来做这个,但我似乎无法达到脚本扔硒。 我能够检查文本框的属性,但不能检查脚本本身,因为这个不会出现在屏幕上。
这是我要检查的代码。 如何通过selenium测试脚本?
<html>
<head>
<title>Simple form</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<meta name="mobile-web-app-capable" content="yes">
<meta charset="utf-8">
</head>
<body>
<div>
<form onsubmit="return Validted()">
New user registration:<br><br>
<table>
<tr>
<td>
First name: *
</td>
<td>
<input type="text" id="f_name" name="fname" maxlength="128" value="" class="firstn" placeholder="first name" style="border-color: gray"><br>
</td>
</tr>
<tr>
<td>
Last name: *
</td>
<td>
<input type="text" id="l_name" name="lname" maxlength="128" value="" class="lastn" placeholder="last name" style="border-color: gray"><br>
</td>
</tr>
<tr>
<td>
Password: *
</td>
<td>
<input type="password" id="pass" name="pass" maxlength="128" value="" class="pass" placeholder="password" style="border-color: gray"><br>
</td>
</tr>
<tr>
<td>
Phone: *
</td>
<td>
<input type="tel" id="tel" name="tel" maxlength="15" value="" class="phone" placeholder="phone number" style="border-color: gray"><br>
</td>
</tr>
<tr>
<td>
E-mail: *
</td>
<td>
<input type="email" id="user_email" name="email" maxlength="128" value="" class="eml" placeholder="Enter email" style="border-color: gray"><br>
</td>
</tr>
</table>
<input type="submit" value="Register">
</form>
</div>
<br>password rules: <br>
only 0-9a-zA-Z <br>
at least 8 characters <br>
at least 1 number <br>
at least 1 lowercase character (a-z) <br>
at least 1 uppercase character (A-Z) <br>
<script>
var validEmail = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
var validPhone = /\(?([0-9]{3})\)?([ .-]?)([0-9]{3})\2([0-9]{4})/;
var validPass = /^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])([a-zA-Z0-9]{8})$/;
function testThis(what, reg, fld) {
if (reg.test(what)) {
$('.' + fld).css('color', 'green');
console.log(fld);
return true;
}
else {
$('.' + fld).css('color', 'red');
return false;
}
}
function Validted() {
$('.pass').css('border-color', 'gray');
$('.phone').css('border-color', 'gray');
$('.eml').css('border-color', 'gray');
if (!testThis($('#user_email').val(), validEmail, 'eml')) {
$('.eml').css('border-color', 'red');
return false;
}
if (!testThis($('#pass').val(), validPass, 'pass')) {
$('.pass').css('border-color', 'red');
return false;
}
if (!testThis($('#tel').val(), validPhone, 'phon')) {
$('.phone').css('border-color', 'red');
return false;
}
return true;
}
function validatePass() {
$('#pass').keyup(function () {
return testThis(this.value, validPass, 'pass');
});
}
}