虫师Selenium2+Python_6、Selenium IDE

P155——创建测试用例
  1. 录制脚本
  2. 编辑脚本
  3. 定位辅助
P159——Selenium IDE 命令
在浏览器中打开URL,可以接受相对路径和绝对路径两种形式
open
open(url)
 
单击链接、按钮、复选框和单选框
click(elementLocator)
 
模拟键盘的输入,向指定的input中输入值
type(inputLocator,value)
 
根据optionSpecifier选项选择器来选择一个下拉菜单选项
select(dropDownLocator,optionSpecifier)
 
模拟单击浏览器的后退按钮
goBack()
 
选择一个弹出窗口
select(windowId)
 
根据指定时间暂停Selenium脚本执行
pause(millisenconds)
 
模拟页面元素事件被激活的处理动作
fireEvent(elementLocatore,evenName)
 
模拟单击浏览器关闭按钮
close
 
P162——断言与验证
P167——等待与变量
 
 
1、启用火狐插件Selenium IDE

 

2、点击红点进行脚本录制
3、使用Selenium IDE导出脚本,选择导出Python语言的脚本

 

 
4、在命令行中运行该脚本,报错。这是因为Python文件在第10行没有类名导致

 

5、导出脚本的源码如下,添加一个hello类名,再运行
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
 
class hello(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "https://www.baidu.com/"
self.verificationErrors = []
self.accept_next_alert = True
 
def test_(self):
driver = self.driver
driver.get(self.base_url + "/")
driver.find_element_by_id("kw").click()
driver.find_element_by_id("kw").clear()
driver.find_element_by_id("kw").send_keys(u"你好")
driver.find_element_by_id("su").click()
 
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException as e: return False
return True
 
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException as e: return False
return True
 
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True
 
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
 
if __name__ == "__main__":
unittest.main()
运行成功:

 

 
 
posted @ 2019-06-05 09:35  HenryZ.Tang  阅读(601)  评论(0编辑  收藏  举报