Web UI自动化测试实例(登入网易云音乐账户)——(二)Python+unittest+selenium

一、简单版_面向对象/ 面向过程实现登入网易云音乐
二、本文主要结合unittest测试框架,实现代码与测试用例分离。

1. web element and operation

页面代码部分:163_music_web.py

from selenium import  webdriver
from selenium.webdriver.common.by import By

class Login_163_Music():
	_login_locator = (By.CSS_SELECTOR, 'a.link.s-fc3')
	_other_login_mode_locator =(By.CSS_SELECTOR ,'a.u-btn2.other')
	_phone_number_login_locator = (By.CSS_SELECTOR ,'a.u-btn2.u-btn2-2')
	_privacy_policy_checkout_loactor = (By.CSS_SELECTOR ,'input#j-official-terms')
	_input_phone_number_locator = (By.CSS_SELECTOR , 'input.j-phone.txt.u-txt')
	_input_password_locator = (By.CSS_SELECTOR , 'input#pw.j-pwd.u-txt')
	_login_button_locator = (By.CSS_SELECTOR, 'a.j-primary.u-btn2.u-btn2-2')
	_locate_mark_locator = (By.CSS_SELECTOR, 'i.u-icn.u-icn-25')

	def __init__(self,url):
		self.driver = webdriver.Chrome()
		self.driver.implicitly_wait(4)
		self.driver.maximize_window()
		self.driver.get(url)

	def click_login(self):
		self.driver.find_element(*self._login_locator).click()

	def select_other_login_mode(self):
		self.driver.find_element(*self._other_login_mode_locator).click()

	def check_privacy_policy_checkout(self):
		if not self.driver.find_element(*self._privacy_policy_checkout_loactor).is_selected():
			self.driver.find_element(*self._privacy_policy_checkout_loactor).click()

	def phone_number_login(self):	
		self.driver.find_element(*self._phone_number_login_locator).click()

	def input_phone_number_account(self,phone_number):
		self.driver.find_element(*self._input_phone_number_locator).send_keys(phone_number)
	
	def input_password(self,password):
		self.driver.find_element(*self._input_password_locator).send_keys(password)
	
	def click_login_button(self):
		self.driver.find_element(*self._login_button_locator).click()

	def close_window(self):
		self.driver.quit()

	def is_login_scuccessfully(self):
		if self.driver.find_element(*self._locate_mark_locator).is_displayed:
			#print ' login  failed'
			raise 'login  failed'
2. testcase

测试用例部分,通过具体测试步骤调用163_music_web.py中相应的方法,组合起来实现自动化。
本次测试用例:选取的是错误的手机号码+随意的密码,登入失败
test_login.py

import unittest
from wangyi_music_web import Login_163_Music
class TestLogin(unittest.TestCase):

    def test_login_wangyiyun(self):
        login = Login_163_Music('https://music.163.com/')
        login.click_login()
        login.select_other_login_mode()
        login.check_privacy_policy_checkout()
        login.phone_number_login()
        login.input_phone_number_account('165564464654')
        login.input_password('hfied')
        login.click_login_button()
        login.is_login_scuccessfully()
        login.close_window()

if __name__ =='__main__':
    unittest.main()
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是基于 PythonSeleniumunittestUI 自动化测试框架搭建步骤: 1. 安装 PythonSelenium 在开始前,需要确保你的电脑上安装了 PythonSelenium。你可以在 Python 官网下载 Python 并安装,安装完成后,在命令行中输入以下命令安装 Selenium: ``` pip install selenium ``` 2. 创建一个测试项目 在你的工作目录中创建一个文件夹来存放你的测试项目,例如 `MyTestProject`。 3. 创建测试用例 在项目文件夹中创建一个文件来存放测试用例,例如 `test_example.py`,并在该文件中写入以下测试用例: ```python import unittest from selenium import webdriver class ExampleTest(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() def tearDown(self): self.driver.quit() def test_example(self): self.driver.get("https://www.example.com") self.assertIn("Example Domain", self.driver.title) if __name__ == '__main__': unittest.main() ``` 这个测试用例会打开 Example Domain 网站,并检查页面标题是否包含 "Example Domain"。 4. 运行测试用例 在命令行中进入项目文件夹,并执行以下命令来运行测试用例: ``` python test_example.py ``` 如果一切顺利,你将会看到测试用例执行成功的结果。 5. 扩展测试用例 你可以根据需要扩展测试用例,例如添加更多的测试步骤、使用断言来检查页面元素、使用数据驱动来进行多组数据的测试等等。 以上就是一个基于 PythonSeleniumunittestUI 自动化测试框架搭建的基本步骤,你可以在此基础上进行进一步的优化和扩展。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值