笔记
练习用例的代码
# coding = utf-8
from selenium import webdriver
import unittest
import time
from selenium.webdriver.support.ui import Select
#登录
def login_success(self,username,password):
#self.driver = webdriver.Chrome()
#self.driver.get('http://127.0.0.1/mgr/sign.html')
el_username=self.driver.find_element_by_id('username')
el_username.clear()
el_username.send_keys(username)
el_password=self.driver.find_element_by_id('password')
el_password.clear()
el_password.send_keys(password)
self.driver.find_element_by_xpath('/html/body/div/div[2]/div[1]/div[3]/div/button').click()
class SMS_login(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.get('http://127.0.0.1/mgr/sign.html')
self.driver.maximize_window()
self.driver.implicitly_wait(5)
#测试用例
#不输入账号点击登录
def test_login1(self):
login_success(self,'','88888888')
time.sleep(1)
alert = self.driver.switch_to.alert
self.assertEqual(alert.text,"请输入用户名")
#输入账号不输入密码点击登录
def test_login2(self):
login_success(self,'byhy','')
time.sleep(1)
alert = self.driver.switch_to.alert
self.assertAlmostEqual(alert.text, "请输入密码")
#用不存在的账号byh登录
def test_login3(self):
login_success(self,'byh','88888888')
time.sleep(1)
alert = self.driver.switch_to.alert
self.assertAlmostEqual(alert.text, "登录失败 : 用户名或者密码错误")
#用正确的账号错误的密码登录
def test_login4(self):
login_success(self,'byhy','888888888')
time.sleep(1)
alert = self.driver.switch_to.alert
self.assertAlmostEqual(alert.text, "登录失败 : 用户名或者密码错误")
#用正确的账号密码成功登录,前三项菜单分别为客户药品订单
def test_login5(self):
login_success(self,'byhy','88888888')
actual_result = ''
elements = self.driver.find_elements_by_css_selector('.sidebar-menu span')
for element in elements[0:3]:
actual_result += ''.join(element.text)
# 预期结果
expected_result = '客户药品订单'
#print('预期结果为: ', expected_result)
# 通过try抛出异常进行断言判断
try:
assert expected_result == actual_result
#print('UI_0101 PASS,实际结果与预期结果一致!')
except: #Exception as e: #用e来打印详细的信息
return False
#print('UI_0101 FAIL,实际结果与预期结果不一致!', format(e))
#成功添加客户信息
def test_login6(self):
login_success(self,'byhy','88888888')
time.sleep(1)
self.driver.find_element_by_xpath('//*[@id="root"]/div/section[2]/div[1]/button').click()
self.driver.find_element_by_xpath('//*[@id="root"]/div/section[2]/div[1]/div[1]/div[1]/input').send_keys('南京中医院')
self.driver.find_element_by_xpath('//*[@id="root"]/div/section[2]/div[1]/div[1]/div[2]/input').send_keys('2551867853')
self.driver.find_element_by_xpath('//*[@id="root"]/div/section[2]/div[1]/div[1]/div[3]/textarea').send_keys('江苏省-南京市-秦淮区-汉中路-503')
self.driver.find_element_by_xpath('//*[@id="root"]/div/section[2]/div[1]/div[2]/button[1]').click()
self.driver.find_element_by_css_selector('.content>.col-lg-12>.col-lg-12 button:nth-child(2)').click()
actual_result = ''
customs = self.driver.find_elements_by_xpath('//*[@id="root"]/div/section[2]/div[3]')
for a in customs:
actual_result += ''.join(a.text)
expected_result = '南京中医院2551867853江苏省-南京市-秦淮区-汉中路-503'
try:
actual_result == expected_result
except:
return False
#成功修改客户信息
def test_login7(self):
login_success(self, 'byhy', '88888888')
time.sleep(1)
self.driver.find_element_by_xpath('//*[@id="root"]/div/section[2]/div[1]/button').click()
self.driver.find_element_by_xpath('//*[@id="root"]/div/section[2]/div[1]/div[1]/div[1]/input').send_keys(
'南京中医院')
self.driver.find_element_by_xpath('//*[@id="root"]/div/section[2]/div[1]/div[1]/div[2]/input').send_keys(
'2551867853')
self.driver.find_element_by_xpath('//*[@id="root"]/div/section[2]/div[1]/div[1]/div[3]/textarea').send_keys(
'江苏省-南京市-秦淮区-汉中路-503')
self.driver.find_element_by_xpath('//*[@id="root"]/div/section[2]/div[1]/div[2]/button[1]').click()
self.driver.find_element_by_css_selector('.content>.col-lg-12>.col-lg-12 button:nth-child(2)').click()
self.driver.find_element_by_xpath('//*[@id="root"]/div/section[2]/div[3]/div[4]/div/label[1]').click()
self.driver.find_element_by_xpath('//*[@id="root"]/div/section[2]/div[3]/div[1]/div[1]/input').clear()
self.driver.find_element_by_xpath('//*[@id="root"]/div/section[2]/div[3]/div[1]/div[1]/input').send_keys('南京省中医院')
self.driver.find_element_by_xpath('//*[@id="root"]/div/section[2]/div[3]/div[2]/div/label[1]').click()
actual_result = ''
customs = self.driver.find_elements_by_xpath('//*[@id="root"]/div/section[2]/div[3]')
for a in customs:
actual_result += ''.join(a.text)
expected_result = '南京省中医院2551867853江苏省-南京市-秦淮区-汉中路-503'
try:
actual_result == expected_result
except:
return False
#成功添加药品信息
def test_login8(self):
login_success(self, 'byhy', '88888888')
time.sleep(1)
self.driver.find_element_by_css_selector('[href="#/medicines"]').click()
self.driver.find_element_by_css_selector('.content>.col-lg-12>button').click()
self.driver.find_element_by_css_selector('.col-lg-8.col-md-8.col-sm-8 input').send_keys('青霉素盒装9')
self.driver.find_element_by_css_selector('.col-lg-8 div:nth-child(2) input').send_keys('YP-32342345')
self.driver.find_element_by_css_selector('.col-lg-8 div:nth-child(3) textarea').send_keys('青霉素注射液,每支15ml,40支装')
self.driver.find_element_by_css_selector(' div.col-lg-12> .col-lg-12.col-md-12.col-sm-12 > button:nth-child(1)').click()
actual_result = ''
element = self.driver.find_elements_by_css_selector(' section> div:nth-child(3)')
for a in element:
actual_result += ''.join(a.text)
expected_result = '青霉素盒装9YP-32342345青霉素注射液,每支15ml,40支装'
try:
actual_result == expected_result
except:
return False
def test_login8(self):
login_success(self, 'byhy', '88888888')
time.sleep(1)
yuanwindow = self.driver.current_window_handle
self.driver.find_element_by_css_selector('footer > strong> a').click()
for handle in self.driver.window_handles:
self.driver.switch_to.window(handle) #先切到window
if '白月黑羽' in self.driver.title:
break
self.driver.implicitly_wait(10)
element = self.driver.find_elements_by_xpath('//*[@id="navbar-content"]/ul')
actual_result = ''
for a in element:
actual_result = ''.join(a.text)
expected_result = 'Python基础Python进阶图形界面Web开发自动化测试行性能测试常见问题其他'
try:
actual_result == expected_result
except:
return False
self.driver.switch_to.window(yuanwindow)#切回原窗口
self.driver.find_element_by_xpath('//*[@id="root"]/header/nav/div/ul/li[2]/a/span').click()
self.driver.find_element_by_xpath('//*[@id="root"]/header/nav/div/ul/li[2]/ul/li[3]/div[2]/a').click()
time.sleep(2)
aurl = self.driver.current_url
burl = 'http://127.0.0.1/mgr/sign.html'
self.assertEqual(aurl, burl)
def test_login9(self): #UI-0108
login_success(self, 'byhy', '88888888')
time.sleep(1)
# 先找到系统中已经存在的信息,依次删除,然后再重新添加新的药品、客户、订单等信息
def del_info():
els = self.driver.find_elements_by_css_selector('.search-result-item-actionbar label:nth-last-of-type(1)')
if els:
for element in els:
element.click()
self.driver.switch_to.alert.accept()
time.sleep(0.5)
text1= '没有找到相关记录'
# 点击菜单栏订单,进入订单信息页面
self.driver.find_element_by_css_selector('[href="#/orders"]').click()
order = self.driver.find_element_by_xpath('//*[@id="root"]/div/section[2]/div[3]/h4').text
if order != text1:
del_info()
# 点击菜单栏药品,进入药品信息页面
self.driver.find_element_by_css_selector('[href="#/medicines"]').click()
element = self.driver.find_elements_by_xpath('//*[@id="root"]/div/section[2]/div[3]/h4')
medicinal = ''
for a in element:
medicinal += ''.join(a.text)
if medicinal != text1:
del_info()
# 点击菜单栏客户,进入客户信息页面
self.driver.find_element_by_css_selector('[href="#/customers"]').click()
element = self.driver.find_elements_by_xpath('//*[@id="root"]/div/section[2]/div[3]/h4')
customs = ''
for a in element:
customs += ''.join(a.text)
if customs != text1:
del_info()
self.driver.find_element_by_css_selector('[href="#/medicines"]').click()
time.sleep(1)
self.driver.find_element_by_css_selector('.content>.col-lg-12>button').click()#点击添加药品按钮
#依次添加药品
medicines_data = [
['青霉素盒装1', 'YP-32342341', '青霉素注射液,每支15ml,20支装'],
['青霉素盒装2', 'YP-32342342', '青霉素注射液,每支15ml,30支装'],
['青霉素盒装3', 'YP-32342343', '青霉素注射液,每支15ml,40支装']
]
for i in range(len(medicines_data)):
a = self.driver.find_element_by_css_selector('.col-lg-8.col-md-8.col-sm-8 input')
a.send_keys(medicines_data[i][0])
b = self.driver.find_element_by_css_selector('.col-lg-8 div:nth-child(2) input')
b.send_keys(medicines_data[i][1])
c = self.driver.find_element_by_css_selector('.col-lg-8 div:nth-child(3) textarea')
c.send_keys(medicines_data[i][2])
d = self.driver.find_element_by_css_selector(
' div.col-lg-12> .col-lg-12.col-md-12.col-sm-12 > button:nth-child(1)')
d.click()
time.sleep(1)
time.sleep(1)
# 依次添加客户信息
self.driver.find_element_by_xpath('//*[@id="root"]/aside/section/ul/li[2]/a/span').click()
self.driver.find_element_by_xpath('//*[@id="root"]/div/section[2]/div[1]/button').click()
customer_data = [
['南京中医院1', '2551867851', '江苏省-南京市-秦淮区-汉中路-501'],
['南京中医院2', '2551867852', '江苏省-南京市-秦淮区-汉中路-502'],
['南京中医院3', '2551867853', '江苏省-南京市-秦淮区-汉中路-503']
]
for i in range(len(customer_data)):
a = self.driver.find_element_by_css_selector('.col-lg-8 div:nth-child(1) input')
a.send_keys(customer_data[i][0])
b = self.driver.find_element_by_css_selector('.col-lg-8 div:nth-child(2) input')
b.send_keys(customer_data[i][1])
c = self.driver.find_element_by_css_selector('.col-lg-8 div:nth-child(3) textarea')
c.send_keys(customer_data[i][2])
d = self.driver.find_element_by_css_selector('.content>.col-lg-12>.col-lg-12 button:nth-child(1)')
d.click()
time.sleep(1)
time.sleep(1)
def create_order():
# 创建订单
# 点击菜单栏药品,进入订单信息页面
order_menu = self.driver.find_element_by_css_selector('[href="#/orders"]')
order_menu.click()
# 找到添加客户按钮并点击
customer_button = self.driver.find_element_by_css_selector('.content>.col-lg-12>button')
customer_button.click()
# 订单信息
order_info = {'order_name': '佩奇的订单', 'customer': '南京中医院2', 'medicine': '青霉素盒装1', 'number': '100盒'}
# 输入订单名称
order_name = self.driver.find_element_by_css_selector('.col-lg-8 div:nth-child(1) input')
order_name.send_keys(order_info['order_name'])
# 创建select对象,通过select对象选中 南京中医院2
customer = Select(self.driver.find_element_by_css_selector('.col-lg-8 div:nth-child(2) select'))
customer.select_by_visible_text(order_info['customer'])
# 创建select对象,通过select对象选中 青霉素盒装1
medicines = Select(self.driver.find_element_by_css_selector('.col-lg-8 div:nth-child(3) select'))
medicines.select_by_visible_text(order_info['medicine'])
# 选中药品后,输入药品数量
time.sleep(1)
medicines_number = self.driver.find_element_by_css_selector('.col-lg-8 div:nth-child(3) div input')
medicines_number.send_keys(order_info['number'])
# 点击创建按钮
create_button = self.driver.find_element_by_css_selector('.content>.col-lg-12>.col-lg-12 button:nth-child(1)')
create_button.click()
# 点击取消按钮
time.sleep(1)
create_button = self.driver.find_element_by_css_selector('.content>.col-lg-12>.col-lg-12 button:nth-child(2)')
create_button.click()
def check_order():
# 取出列表中新增的订单信息
order_name = self.driver.find_element_by_css_selector(
'.content>div:nth-of-type(3)>div:nth-of-type(1)>span:nth-child(2)')
# 实际结果
actual_result = order_name.text
# 预期结果
expected_result = '佩奇的订单'
try:
assert expected_result == actual_result
except:
return False
def tearDown(self):
#time.sleep(5)
self.driver.quit()
if __name__ == "__main__":
unittest.main()
后续补充生成报告和发生邮件部分。