1 selenium IDE--录制脚本
准备工作:firefox 浏览器安装了selenium IDE 插件
实例:打开百度搜索“软件测试”
- firefox浏览器打开网址:https://www.baidu.com/
- 点击浏览器插件【selenium】图标,弹出selenium IDE 录入界面,Base URL设置为:https://www.baidu.com/(默认为录制状态)
- 在步骤1中,直接输入“软件测试”关键字,点击【百度一下】进行搜索
- selenium IDE 点击【停止录制】红点,如下图:table中记录了我们刚才的操作
2 selenium IDE--回放脚本
- 回放脚本时需要先开启selenium服务,进入jar包目录,输入cmd命令:java -jar selenium-server-standalone-2.40.0.jar
- selenium IDE 点击【回放用例】按钮,如下图1:
- 保存为baidu_search.py脚本(附录脚本),如下图2:
# -*- coding: utf-8 -*-
'''
时间:2015.04.16
作者:chenfenping
功能:百度搜索软件测试实例演示selenium IDE的使用
'''
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 baidu_search(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_11(self): driver = self.driver # open | / | driver.get(self.base_url + "/") # click | id=kw | driver.find_element_by_id("kw").click() # type | id=kw | 软件测试 driver.find_element_by_id("kw").clear() driver.find_element_by_id("kw").send_keys(u"软件测试") # click | id=su | 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, e: return False return True def is_alert_present(self): try: self.driver.switch_to_alert() except NoAlertPresentException, 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()