白月黑羽课程连接:白月黑羽
最近自学了白月黑羽老师的自动化测试课程,并非课后作业的准确答案,只是为了个人学习记录,如有不足欢迎留言指点,谢谢大家,共同进步!
白月黑羽selenium课后练习答案代码整理好放在百度网盘了,需要自取:
链接:https://pan.baidu.com/s/1O3dE0L5TAMQJ8eJh64ur2A
提取码:1949
目录结构
byhy_homework_selenium/
│──byhy_sms/
│ ├── __init__.py
│ ├── cases/
│ │ ├── __init__.py
│ │ ├── UI-0101.py #用例UI-0101
│ │ ├── UI-0102.py #用例UI-0102
│ │ ├── UI-0103.py #用例UI-0103
│ │ ├── UI-0105.py #用例UI-0105
│ │ ├── UI-0106.py #用例UI-0106
│ │ ├── UI-0107.py #用例UI-0107
│ │ └── UI-0108.py #用例UI-0108
│ └── lib/
│ ├── __init__.py
│ ├── byhysms_del.py #删除系统的所有订单、客户、药品
│ ├── customer_add.py #添加客户
│ ├── login_success.py #成功登录
│ ├── medical_add.py #添加药品
│ └── order_add.py #添加订单
└──
1、用例UI-0101
这里将成功登录SMS系统写成公共方法login_success放在lib目录中
#login_success.py
from selenium.webdriver.common.by import By
from time import sleep
def login_success(driver, username, password):
# 找到用户名输入正确用户
element_username = driver.find_element(By.ID,'username')
element_username.clear()
element_username.send_keys(username)
# 找到用户名输入正确用户
element_password = driver.find_element(By.ID,'password')
element_password.clear()
element_password.send_keys(password)
# 找到登录按钮并点击
button_submit = driver.find_element(By.TAG_NAME,'button')
button_submit.click()
sleep(1)
用例UI-0101的代码:
#UI-0101.py
from selenium import webdriver
from selenium.webdriver.common.by import By
from byhySMS.lib.login_success import login_success
wd = webdriver.Chrome()
# 隐式等待、全局等待,如果找不到元素,每隔半秒钟再去界面上查看一次,直到找到该元素,或者过了10秒最大时长。
wd.implicitly_wait(10)
wd.get('http://127.0.0.1')
# 成功登录
login_success(wd, 'byhy', '88888888')
# 实际结果
actual_result = ''
# 定位左侧菜单栏
elements = wd.find_elements(By.CSS_SELECTOR, '.sidebar-menu span')
for element in elements[0:3]:
actual_result += ''.join(element.text)
print('页面前三项菜单名称分别为:', actual_result)
# 预期结果
expected_result = '客户药品订单'
print('预期结果为:', expected_result)
# 通过try抛出异常进行断言判断
try:
assert expected_result == actual_result
print('UI_0101 PASS,实际结果与预期结果一致!')
except Exception as e:
print('UI_0101 FAIL,实际结果与预期结果不一致!', format(e))
wd.quit()
2、用例UI-0102
这里将在SMS系统中添加用户写成公共方法customer_add放在lib目录中
#customer_add.py
from selenium.webdriver.common.by import By
from time import sleep
def customer_add(driver,name,mobile,address):
# 创建客户
#进入添加客户的页面
customer_menu = driver.find_element(By.CSS_SELECTOR, "a[href='#/customers']")
customer_menu.click()
#等待进入界面
sleep(1)
# 找到添加客户按钮并点击
customer_button = driver.find_element(By.CSS_SELECTOR, '.content>.col-lg-12>button')
customer_button.click()
# 输入客户信息,客户名,联系电话,地址
customer_name = driver.find_element(By.CSS_SELECTOR, '.col-lg-8 div:nth-child(1) input')
customer_name.send_keys(name)
customer_mobile = driver.find_element(By.CSS_SELECTOR, '.col-lg-8 div:nth-child(2) input')
customer_mobile.send_keys(mobile)
customer_address = driver.find_element(By.CSS_SELECTOR, '.col-lg-8 div:nth-child(3) textarea')
customer_address.send_keys(address)
# 点击创建按钮
create_button = driver.find_element(By.CSS_SELECTOR,'.content>.col-lg-12>.col-lg-12 button:nth-child(1)')
create_button.click()
sleep(1)
用例UI-0102的代码:
#UI-0102.py
from selenium import webdriver
from selenium.webdriver.common.by import By
from byhySMS.lib.login_success import login_success
from byhySMS.lib.customer_add import customer_add
wd = webdriver.Chrome()
# 隐式等待、全局等待,如果找不到元素,每隔半秒钟再去界面上查看一次,直到找到该元素,或者过了10秒最大时长。
wd.implicitly_wait(10)
wd.get('http://127.0.0.1')
# 成功登录
login_success(wd, 'byhy', '88888888')
#添加客户
customer_add(wd,'南京中医院','13370082190','南京中医院110号')
# 实际结果
actual_result = ''
# 取出列表中新增的客户信息
customer_info = wd.find_elements(By.CSS_SELECTOR,
'.content>div:nth-of-type(3) .search-result-item-field>span:nth-child(2)')
for element in customer_info:
actual_result += ''.join(element.text)
print('列表中显示的客户信息为为:', actual_result)
# 预期结果
expected_result = '南京中医院13370082190南京中医院110号'
print('预期结果为:', expected_result)
# 通过try抛出异常进行断言判断
try:
assert expected_result == actual_result
print('UI_0102 PASS,实际结果与预期结果一致!')
except Exception as e:
print('UI_0102 FAIL,实际结果与预期结果不一致!', format(e))
wd.quit()
3、用例UI-0103
用例UI-0103的代码:
#UI-0103.py
from selenium import webdriver
from selenium.webdriver.common.by import By
from byhySMS.lib.login_success import login_success
from byhySMS.lib.customer_add import customer_add
from time import sleep
wd = webdriver.Chrome()
# 隐式等待、全局等待,如果找不到元素,每隔半秒钟再去界面上查看一次,直到找到该元素,或者过了10秒最大时长。
wd.implicitly_wait(10)
wd.get('http://127.0.0.1')
# 成功登录
login_success(wd, 'byhy', '88888888')
# 创建客户
customer_add(wd, '南京中医院', '13370082190', '南京中医院110号')
# 编辑
customer_edit = wd.find_element(By.CSS_SELECTOR,
'.content>div:nth-of-type(3) .search-result-item-actionbar div label:nth-child(1)')
customer_edit.click()
customer_name_edit = wd.find_element(By.CSS_SELECTOR,
'.content>div:nth-of-type(3)>div:nth-of-type(1)>div:nth-of-type(1) input')
customer_name_edit.clear()
customer_name_edit.send_keys('南京省中医院')
# 编辑后点击确定
create_button_edit = wd.find_element(By.CSS_SELECTOR,
'.content>div:nth-of-type(3)>div:nth-of-type(2)>div label:nth-of-type(1)')
create_button_edit.click()
# 实际结果
actual_result = ''
# 等待编辑成功
sleep(1)
# 取出列表中新增的客户信息
customer_info = wd.find_elements(By.CSS_SELECTOR,
'.content>div:nth-of-type(3) .search-result-item-field>span:nth-child(2)')
for element in customer_info:
actual_result += ''.join(element.text)
print('列表中显示的客户信息为:', actual_result)
# 预期结果
expected_result = '南京省中医院13370082190南京中医院110号'
print('预期结果为:', expected_result)
# 通过try抛出异常进行断言判断
try:
assert expected_result == actual_result
print('UI_0103 PASS,实际结果与预期结果一致!')
except Exception as e:
print('UI_0103 FAIL,实际结果与预期结果不一致!', format(e))
wd.quit()
4、用例UI-0105
这里将在SMS系统中添加药品写成公共方法medical_add放在lib目录中
#medical_add.py
from selenium.webdriver.common.by import By
from time import sleep
def medical_add(driver,name,num,des):
#点击进入药品管理界面
medical_menu = driver.find_element(By.CSS_SELECTOR, "a[href='#/medicines']")
medical_menu.click()
sleep(1)
#点击药品管理
medical_manage = driver.find_element(By.CSS_SELECTOR, "div>button[type='button']")
medical_manage.click()
#输入药品的药品名称,编号,描述
medical_name = driver.find_element(By.CSS_SELECTOR, "div[class*='col-lg-8']>:nth-of-type(1) input")
medical_name.send_keys(name)
medical_num = driver.find_element(By.CSS_SELECTOR, "div[class*='col-lg-8']>:nth-of-type(2) input")
medical_num.send_keys(num)
medical_description = driver.find_element(By.CSS_SELECTOR, "div[class*='col-lg-8']>:nth-of-type(3) textarea")
medical_description.send_keys(des)
#点击创建
medical_create = driver.find_element(By.CSS_SELECTOR, "div[class*='area']>div:nth-of-type(2)>button:nth-of-type(1)")
medical_create.click()
sleep(1)
用例UI-0105的代码:
#UI-0105.py
from selenium import webdriver
from selenium.webdriver.common.by import By
from byhySMS.lib.login_success import login_success
from byhySMS.lib.medical_add import medical_add
wd = webdriver.Chrome()
# 隐式等待、全局等待,如果找不到元素,每隔半秒钟再去界面上查看一次,直到找到该元素,或者过了10秒最大时长。
wd.implicitly_wait(10)
wd.get('http://127.0.0.1')
# 成功登录
login_success(wd, 'byhy', '88888888')
# 添加药品
medical_add(wd, '可乐', '666', '服下会有快乐的爽感')
# 实际结果
actual_result = ''
# 取出列表中新增的药品信息
customer_info = wd.find_elements(By.CSS_SELECTOR,
'.content>div:nth-of-type(3)>div>span:nth-of-type(2)')
for element in customer_info:
actual_result += ''.join(element.text)
print('列表中显示的药品信息为:', actual_result)
# 预期结果
expected_result = '可乐666服下会有快乐的爽感'
print('预期结果为:', expected_result)
# 通过try抛出异常进行断言判断
try:
assert expected_result == actual_result
print('UI_0105 PASS,实际结果与预期结果一致!')
except Exception as e:
print('UI_0105 FAIL,实际结果与预期结果不一致!', format(e))
wd.quit()
5、用例UI-0106
用例UI-0106的代码:
#UI-0106.py
from selenium import webdriver
from selenium.webdriver.common.by import By
from byhySMS.lib.login_success import login_success
from time import sleep
wd = webdriver.Chrome()
# 隐式等待、全局等待,如果找不到元素,每隔半秒钟再去界面上查看一次,直到找到该元素,或者过了10秒最大时长。
wd.implicitly_wait(10)
wd.get('http://127.0.0.1')
# 成功登录
login_success(wd, 'byhy', '88888888')
# 点击链接
byhy_net = wd.find_element(By.CSS_SELECTOR, "div[class='pull-right hidden-xs'] a")
byhy_net.click()
# mainWindow变量保存当前窗口的句柄
mainWindow = wd.current_window_handle
for handle in wd.window_handles:
# 先切换到该窗口
wd.switch_to.window(handle)
# 得到该窗口的标题栏字符串,判断是不是我们要操作的那个窗口
if '白月黑羽' in wd.title:
# 如果是,那么这时候WebDriver对象就是对应的该该窗口,正好,跳出循环,
break
sleep(1)
wd.maximize_window()
# 获取导航栏的内容
# 实际导航栏结果
actual_result = ''
navigation = wd.find_elements(By.CSS_SELECTOR, 'ul a')
for element in navigation:
actual_result += ''.join(element.text)
print('白月黑羽网站的导航栏为:', actual_result)
# 预期结果
expected_result = 'Python基础Python进阶Qt图形界面Django自动化测试性能测试JS语言JS前端'
print('预期结果为:', expected_result)
# 通过try抛出异常进行断言判断
try:
assert expected_result == actual_result
print('UI_0106 PASS,实际结果与预期结果一致!')
except Exception as e:
print('UI_0106 FAIL,实际结果与预期结果不一致!', format(e))
# 通过前面保存的老窗口的句柄,自己切换到老窗口
wd.switch_to.window(mainWindow)
# 点击管理员
manage = wd.find_element(By.CSS_SELECTOR, "span[class='hidden-xs']")
manage.click()
# 点击退出登录
sms_quit = wd.find_element(By.CSS_SELECTOR, ".pull-right>a[class*='btn-flat']")
sms_quit.click()
# 等待界面刷新
sleep(1)
print(wd.current_url)
6、用例UI-0107
这里将在SMS系统中添加订单写成公共方法order_add放在lib目录中
#order_add.py
from selenium.webdriver.common.by import By
from time import sleep
def order_add(driver,o_name,c_name,m_name,m_num):
#点击进入订单管理界面
order_menu = driver.find_element(By.CSS_SELECTOR, "a[href='#/orders']")
order_menu.click()
sleep(1)
#点击订单管理
order_manage = driver.find_element(By.CSS_SELECTOR, "div>button[type='button']")
order_manage.click()
#输入订单的订单名称,客户,药品数量
order_name = driver.find_element(By.CSS_SELECTOR, "div[class*='col-lg-8']>:nth-of-type(1) input")
order_name.send_keys(o_name)
# 导入Select类
from selenium.webdriver.support.ui import Select
# 创建Select对象
select1 = Select(driver.find_element(By.CSS_SELECTOR, "div[class*='col-lg-8']>:nth-of-type(2) select"))
# 通过 Select 对象选中客户
select1.select_by_visible_text(c_name)
# 创建Select对象
select2 = Select(driver.find_element(By.CSS_SELECTOR, "div[class*='col-lg-8']>:nth-of-type(3) select"))
# 通过 Select 对象选中药品
select2.select_by_visible_text(m_name)
# 药品数量
medical_num = driver.find_element(By.CSS_SELECTOR, "div[class*='col-lg-8']>:nth-of-type(3) div input")
medical_num.send_keys(m_num)
#点击创建
order_create = driver.find_element(By.CSS_SELECTOR, "div[class*='area']>div:nth-of-type(2)>button:nth-of-type(1)")
order_create.click()
sleep(1)
用例UI-0107的代码:
#UI-0107.py
from selenium import webdriver
from selenium.webdriver.common.by import By
from byhySMS.lib.login_success import login_success
from byhySMS.lib.medical_add import medical_add
from byhySMS.lib.customer_add import customer_add
from byhySMS.lib.order_add import order_add
wd = webdriver.Chrome()
# 隐式等待、全局等待,如果找不到元素,每隔半秒钟再去界面上查看一次,直到找到该元素,或者过了10秒最大时长。
wd.implicitly_wait(10)
wd.get('http://127.0.0.1')
# 成功登录
login_success(wd, 'byhy', '88888888')
# 在系统中添加3种药品
medical_add(wd, '青霉素盒装1', 'YP-32342341', '青霉素注射液,每支15ml,20支装')
medical_add(wd, '青霉素盒装2', 'YP-32342342', '青霉素注射液,每支15ml,30支装')
medical_add(wd, '青霉素盒装3', 'YP-32342343', '青霉素注射液,每支15ml,40支装')
# 在系统中添加3个客户
customer_add(wd, '南京中医院1', '2551867851', '江苏省-南京市-秦淮区-汉中路-501')
customer_add(wd, '南京中医院2', '2551867852', '江苏省-南京市-秦淮区-汉中路-502')
customer_add(wd, '南京中医院3', '2551867853', '江苏省-南京市-秦淮区-汉中路-503')
# 在系统中添加1个订单
order_add(wd, '订单1', '南京中医院2', '青霉素盒装1', '100')
def check_order():
# 取出列表中新增的订单信息
order_name = wd.find_element(By.CSS_SELECTOR, "div[class='search-result-item']>div:nth-of-type(1)>:nth-child(2)")
# 实际结果
actual_result = order_name.text
print('列表中显示的订单名称为:', actual_result)
# 预期结果
expected_result = '订单1'
print('预期结果为:', expected_result)
# # 通过try抛出异常进行断言判断
try:
assert expected_result == actual_result
print('UI_0107 PASS,实际结果与预期结果一致,添加订单成功!')
except Exception as e:
print('UI_0107 FAIL,实际结果与预期结果不一致,添加订单失败!', format(e))
check_order()
7、用例UI-0108
用例UI-0108的前置条件是删除所有的订单、客户和药品并且订单数据依赖于客户和药品,必须先删除订单,然后方可删除客户和药品。
这里将在SMS系统中删除所有的订单、客户和药品写成公共方法byhysms_del放在lib目录中
#byhysms_del.py
from telnetlib import EC
from selenium.webdriver.common.by import By
from time import sleep
from selenium.webdriver.support.wait import WebDriverWait
def byhysms_del(driver,sms):
def def_info():
elements = driver.find_elements(By.CSS_SELECTOR,'.search-result-item-actionbar label:nth-last-of-type(1)')
if elements:
for element in elements:
element.click()
driver.switch_to.alert.accept()
sleep(1)
# 遍历页面
def del_page():
while True:
try:
def_info()
# 查找下一页按钮并点击
next_page_elem = WebDriverWait(driver, 5).until(EC.element_to_be_clickable("ul[class='pagination'] :nth-child(5) a"))
next_page_elem.click()
except Exception as e:
break # 如果没有下一页,则退出循环
if sms == 'order':
# 点击菜单栏订单,进入订单信息页面
order_menu = driver.find_element(By.CSS_SELECTOR, "a[href='#/orders']")
order_menu.click()
sleep(1)
del_page()
elif sms == 'medical':
# 点击菜单栏药品,进入药品信息页面
medical_menu = driver.find_element(By.CSS_SELECTOR,'a[href="#/medicines"]')
medical_menu.click()
sleep(1)
del_page()
elif sms == 'customer':
# 点击菜单栏客户,进入客户信息页面
customer_menu = driver.find_element(By.CSS_SELECTOR,'a[href="#/customers"]')
customer_menu.click()
sleep(1)
del_page()
用例UI-0108的代码:
#UI-0108.py
from selenium import webdriver
from selenium.webdriver.common.by import By
from byhySMS.lib.login_success import login_success
from byhySMS.lib.medical_add import medical_add
from byhySMS.lib.customer_add import customer_add
from byhySMS.lib.order_add import order_add
from byhySMS.lib.byhysms_del import byhysms_del
wd = webdriver.Chrome()
# 隐式等待、全局等待,如果找不到元素,每隔半秒钟再去界面上查看一次,直到找到该元素,或者过了10秒最大时长。
wd.implicitly_wait(10)
wd.get('http://127.0.0.1')
# 成功登录
login_success(wd, 'byhy', '88888888')
# 删除系统中所有的订单、客户、药品
byhysms_del(wd, 'order')
byhysms_del(wd, 'customer')
byhysms_del(wd, 'medical')
# 在系统中添加3种药品
medical_add(wd, '青霉素盒装1', 'YP-32342341', '青霉素注射液,每支15ml,20支装')
medical_add(wd, '青霉素盒装2', 'YP-32342342', '青霉素注射液,每支15ml,30支装')
medical_add(wd, '青霉素盒装3', 'YP-32342343', '青霉素注射液,每支15ml,40支装')
# 在系统中添加3个客户
customer_add(wd, '南京中医院1', '2551867851', '江苏省-南京市-秦淮区-汉中路-501')
customer_add(wd, '南京中医院2', '2551867852', '江苏省-南京市-秦淮区-汉中路-502')
customer_add(wd, '南京中医院3', '2551867853', '江苏省-南京市-秦淮区-汉中路-503')
# 在系统中添加1个订单
order_add(wd, '订单1', '南京中医院2', '青霉素盒装1', '100')
def check_order():
# 取出列表中新增的订单信息
order_name = wd.find_element(By.CSS_SELECTOR, "div[class='search-result-item']>div:nth-of-type(1)>:nth-child(2)")
# 实际结果
actual_result = order_name.text
print('列表中显示的订单名称为:', actual_result)
# 预期结果
expected_result = '订单1'
print('预期结果为:', expected_result)
# # 通过try抛出异常进行断言判断
try:
assert expected_result == actual_result
print('UI_0107 PASS,实际结果与预期结果一致,添加订单成功!')
except Exception as e:
print('UI_0107 FAIL,实际结果与预期结果不一致,添加订单失败!', format(e))
check_order()
学习自动化测试的时间不长,以上是本人听完课之后的练习作业,写在博客也是为了日后回顾,所以有幼稚的地方希望大家包涵,同时如果发现代码上的不足请在评论区留言,多多交流,共同进步!
标签:wd,黑羽,selenium,element,add,UI,result,import,白月 From: https://blog.csdn.net/lemom_666/article/details/142180239