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

本文介绍使用Selenium和unittest框架实现网易云音乐网站的自动化登录测试流程。通过定义页面操作类并结合测试用例,演示了如何进行错误手机号码与密码的登录尝试及验证登录失败的过程。

一、简单版_面向对象/ 面向过程实现登入网易云音乐
二、本文主要结合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()
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值