- from表单是我们经常测试的用例,绝大多数web程序都有,例如:用户登录、注册
- form表单的流程是这样的:
1、定位表单元素 2、输入测试值 3、判断表单元素属性 4、获取表单元素属性 5、提交表单进行验证
比如这里有个简单的表单
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="javascript:alert('hello')">
username: <input type="text" name="username" id="username"><br>
password: <input type="password" name="pwd" id="pwd"><br>
<input type="submit" value="submit" id="submit">
</form>
</body>
</html>
以下是定位元素,获取表单元素,输入测试值等一系列操作
from selenium import webdriver
import os
from time import sleep
class TestCase(object):
def __init__(self):
self.driver = webdriver.Chrome()
self.driver.maximize_window()
path = os.path.dirname(os.path.abspath(__file__))
file_path = 'file:///'+path+'/forms.html'
self.driver.get(file_path)
def test_login(self):
username = self.driver.find_element_by_id('username')
username.send_keys('admin')
pwd = self.driver.find_element_by_id('pwd')
pwd.send_keys('123')
print(username.get_attribute('value'))
print(pwd.get_attribute('value'))
sleep(2)
self.driver.find_element_by_id('submit').click()
sleep(2)
self.driver.switch_to.alert.accept()
username.clear()
pwd.clear()
if __name__ == '__main__':
case = TestCase()
case.test_login()
|