一、用脚本实现“我爱我家”网站的登录操作
1.分析
(1)登录有两个参数,用户名和密码,查找到这两个元素输入后点击登录操作
(2)断言:判断是否登录成功可以用登录后的用户名来检验
二、selenium+unittest脚本实现
1.代码部分
#coding:utf-8
from selenium import webdriver
import unittest
class Login5i5j(unittest.TestCase):
def setUp(self):
print("开始执行测试...")
self.name = "182xxxxxxxx"
self.pwd = "xxxxxxx"
self.browser = webdriver.Chrome()
self.browser.get("https://passport.5i5j.com/passport/login?service=https%3A%2F%2Fwx.5i5j.com%2Freglogin%2Findex%3FpreUrl%3Dhttps%253A%252F%252Fwx.5i5j.com%252F%253Fpmf_group%253Dbaidu%2526pmf_medium%253Dppzq%2526pmf_plan%253D%2525E5%2525B7%2525A6%2525E4%2525BE%2525A7%2525E6%2525A0%252587%2525E9%2525A2%252598%2526pmf_unit%253D%2525E6%2525A0%252587%2525E9%2525A2%252598%2526pmf_keyword%253D%2525E6%2525A0%252587%2525E9%2525A2%252598%2526pmf_account%253D199&status=1&city=wx")
self.browser.maximize_window()
def test_login(self):
self.browser.find_element_by_id("username").send_keys(self.name)
self.browser.find_element_by_id("password").send_keys(self.pwd)
self.browser.find_element_by_id("login").submit()
self.browser.implicitly_wait(3)
# 查找到登录后的用户名,校验结果
verifyLoginSucceed = self.browser.find_element_by_class_name("log").text
self.assertEqual("karry_sun",verifyLoginSucceed)
def tearDown(self):
self.browser.close()
print("测试结束...")
if __name__ == '__main__':
unittest.main()
2.结果