一、初识WEB-selenium自动化测试
针对bing网站的搜索功能进行自动化测试
# 从谷歌公司的一个项目selenium导入webdriver这段代码来驱动浏览器
chrome=webdriver.Chrome()
2、打开bing网站
chrome.get(‘http://cn.bing.com/’)
3、输入关键词
chrome.find_element_by_id(‘sb_form_q’).send_keys(‘51tesing’)
4、点击搜索按钮
chrome.find_element_by_id(‘search_icon’).click()
如今,大多数软件应用都是跑在浏览器中网站应用。不同公司和组织之间的测试效率迥异。在这个富交互和响应示处理随处可见的时代,很多组织都使用敏捷的方式来开发,因此测试自动化也称为软件项目的必备部分。测试自动化意味着使用软件工具来反复运行项目中的测试,并为回归测试提供反馈。
测试自动化有很多优点,大多数都与测试的可重复性和高执行率相关。市面上有一些商业或开源的工具来辅助测试自动化开发。Selenium应用是最广泛使用的开源方案。
二、针对海盗商城的登录功能进行自动化测试
importtime
fromseleniumimportwebdriver
fromselenium.webdriver.common.byimportBy
fromselenium.webdriver.support.selectimportSelect
1、登录
chrome=webdriver.Chrome()
chrome.implicitly_wait(10) # 由于页面稳定性较差,所以添加了一个隐式等待
chrome.maximize_window() # 窗口最大化
chrome.get(‘http://129.211.129.101:9007/index.phpm=user&c=public&a=login’)
chrome.find_element_by_id(‘username’).send_keys(‘XieChuang’) 该命令的输入方式已经过期
chrome.find_element(By.ID,‘username’).send_keys(‘XieChuang’)
chrome.find_element(By.ID,‘password’).send_keys(‘123456’)
chrome.find_element_by_class_name(‘login_btn fl’).click() # 登录的类名是复合类名,不能同时使用,fl只是一个左对齐的作用
chrome.find_element(By.CLASS_NAME,‘login_btn’).click()
2、点击’进入商城购物’
登录成功后不是立马进行页面的,所以此处添加一个时间等待
time.sleep(3)
第三种元素定位方式,linktext
chrome.find_element(By.LINK_TEXT,‘进入商城购物’).click()
3、搜索’iphone’
chrome.find_element(By.NAME,‘keyword’).send_keys(‘小米6’)
chrome.find_element(By.CLASS_NAME,‘btn1’).click()
4、点击商品图片
chrome.find_element(By.XPATH,’/html/body/div[3]/div[2]/div[3]/div[2]/div[1]/a/img’).click()
5、窗口切换
1、找到新窗口的名字
new_window=chrome.window_handles[-1]
2、切换到新窗口
chrome.switch_to.window(new_window)
6、把选择的商品加入购物车
chrome.find_element(By.ID,‘joinCarButton’).click() # 此时由于跳转了新窗口,所以无法进行操作
7、去购物车结算
chrome.find_element(By.CLASS_NAME,‘other_join’).click()
8、点击结算 css selector 定位方式:在两个class之前需要加.
chrome.find_element(By.CSS_SELECTOR,’.shopCar_btn_03.fl’).click()
9、添加新地址
chrome.find_element(By.CLASS_NAME,‘add-address’).click()
10、填写收货人信息
chrome.find_element(By.NAME,‘address[address_name]’).send_keys(‘XC’)
chrome.find_element(By.NAME,‘address[mobile]’).send_keys(‘15910100202’)
11、选择地区的下拉框
sheng=chrome.find_element(By.ID,‘add-new-area-select’)# 将省这个下拉框进行实例化
Select(sheng).select_by_visible_text(‘北京市’)# 将实例化的下拉框进行类型强制转换成Select型,再使用下拉框的属性进行选择
12、选择收货地区–市 由于下拉框中的ID是动态变化的,且class name又是同名的,所以使用find_elements来找到相同class name,再使用标签名来组合
shi=chrome.find_elements(By.CLASS_NAME,‘add-new-area-select’)[1]
Select(shi).select_by_visible_text(‘北京市’)
qu=chrome.find_elements(By.TAG_NAME,‘select’)[2]# 使用标签名来定位
Select(qu).select_by_visible_text(‘海淀区’)
chrome.find_element(By.NAME,‘address[address]’).send_keys(‘迈行大厦’)
chrome.find_element(By.NAME,‘address[zipcode]’).send_keys(‘100000’)
chrome.find_element(By.CLASS_NAME,‘aui_state_highlight’).click()
1、隐式等待
driver.implicitly_wait(10)
和time.sleep(10)
的区别
隐式等待是一种智能等待,可以自动判断需要等待时间。括号中的时间代表最大等待时间
隐式等待只需要在声明driver之后,书写一次即可影响后面的所有代码
time.sleep()
则需要在每次等待之前进行书写
2、窗口最大化
chrome.maximize_window()
3、窗口切换
- 找出新窗口的名字
new_window = driver.window_handles[-1]
- 切换到新窗口
driver.switch_to.window(new_window)
4、下拉框选择
- 定位下拉框
element=driver.find_element(By....)
- 把找到的页面元素,转换为下拉框的类型Select
select = Select(element)
- 调用Select类中的
select_by_*
方法
.select_by_value(选项的value属性的值)
.select_by_index(第几个选项)
.select_by_visible_text(选项的文本值)
5、find_element_by_*
的形式提示代码已过期
现对原有的定位方式进行修改为find_element(By.*)
注意:使用By之前需要导包from selenium.webdriver.common.by import By
6、find_elements
和find_element
的区别
find_elements
可以找到相同标签名的全部信息,再通过序号的形式找到对应的值
find_element
只能默认找到第一个标签名的信息
driver.find_elements(...)[0]
==diver.find_element(...)
三、针对海盗商城的修改个人信息功能进行自动化测试
# 1、登录
importtime
fromseleniumimportwebdriver
fromselenium.webdriver.common.byimportBy
fromselenium.webdriver.supportimportexpected_conditions
fromselenium.webdriver.support.waitimportWebDriverWait
chrome=webdriver.Chrome()
chrome.implicitly_wait(5)
chrome.maximize_window()
chrome.get(“http://129.211.129.101:9007/index.phpm=user&c=public&a=login”)
chrome.find_element(By.ID,‘username’).send_keys(‘XieChuang’)
chrome.find_element(By.ID,‘password’).send_keys(‘123456’)
submit方法:类似于click(),只能用于form表单中
当无法定位到按钮时,可以使用同一表单中的任意元素进行submit提交
chrome.find_element(By.ID,‘password’).submit()