Python+selenium 【第八章】开源项目实战
- Python+selenium 【第八章】开源项目实战
以iwebshop商城为例子。讲述登录流程
test_iweb_cases.py
# -*- coding: utf-8 -*- # @Time : 2021/12/30 14:51 # @Author : Limusen # @File : test_iweb_cases import time import unittest from selenium import webdriver
class TestIwebCases(unittest.TestCase): def setUp(self) -> None: self.driver = webdriver.Chrome() self.driver.maximize_window() def test_login_success(self): self.driver.get("http://shop.aircheng.com/") # 点击登录按钮 self.driver.find_element_by_xpath('/html/body/header/div[1]/div/div[2]/div/a[1]').click() # 输入账号 self.driver.find_element_by_xpath('//input[@alt="填写用户名或邮箱"]').send_keys("nswe") # 输入密码 self.driver.find_element_by_xpath('//input[@alt="填写密码"]').send_keys('111111') # 点击登录 self.driver.find_element_by_xpath('//input[@value="登录"]').click() user_name = self.driver.find_element_by_xpath('/html/body/header/div[1]/div/div[2]/div[1]/a').text # 断言登录之后的用户名是否为nswe self.assertEqual(user_name,"nswe","登录用例执行失败")
if __name__ == '__main__': unittest.main()
- 优化脚本,新增错误用户名,错误密码的用例
# -*- coding: utf-8 -*- # @Time : 2021/12/30 14:51 # @Author : Limusen # @File : test_iweb_cases import time import unittest from selenium import webdriver class TestIwebCases(unittest.TestCase): def setUp(self) -> None: self.driver = webdriver.Chrome() self.driver.maximize_window() self.driver.get("http://shop.aircheng.com/") self.driver.implicitly_wait(10) def test_login_success(self): # 点击登录按钮 self.driver.find_element_by_xpath('/html/body/header/div[1]/div/div[2]/div/a[1]').click() # 输入账号 self.driver.find_element_by_xpath('//input[@alt="填写用户名或邮箱"]').send_keys("nswe") # 输入密码 self.driver.find_element_by_xpath('//input[@alt="填写密码"]').send_keys('111111') # 点击登录 self.driver.find_element_by_xpath('//input[@value="登录"]').click() user_name = self.driver.find_element_by_xpath('/html/body/header/div[1]/div/div[2]/div[1]/a').text # 断言登录之后的用户名是否为nswe self.assertEqual(user_name,"nswe","登录用例执行失败") def test_login_name_fail(self): # 点击登录按钮 self.driver.find_element_by_xpath('/html/body/header/div[1]/div/div[2]/div/a[1]').click() # 输入错误账号 self.driver.find_element_by_xpath('//input[@alt="填写用户名或邮箱"]').send_keys("ajsdhjkahsdkha") # 输入密码 self.driver.find_element_by_xpath('//input[@alt="填写密码"]').send_keys('111111') # 点击登录 self.driver.find_element_by_xpath('//input[@value="登录"]').click() time.sleep(1) # 查看提示 text = self.driver.find_element_by_xpath('/html/body/div[3]/section/section/form/div').text self.assertEqual(text,'账号或密码错误',"登录失败") def test_login_password_fail(self): # 点击登录按钮 self.driver.find_element_by_xpath('/html/body/header/div[1]/div/div[2]/div/a[1]').click() # 输入账号 self.driver.find_element_by_xpath('//input[@alt="填写用户名或邮箱"]').send_keys("nswe") # 输入密码 self.driver.find_element_by_xpath('//input[@alt="填写密码"]').send_keys('asdasd32qw23e1') # 点击登录 self.driver.find_element_by_xpath('//input[@value="登录"]').click() time.sleep(1) # 查看提示 text = self.driver.find_element_by_xpath('/html/body/div[3]/section/section/form/div').text self.assertEqual(text,'账号或密码错误',"登录失败") if __name__ == '__main__': unittest.main()
- 后续持续更新~