一、安装步骤
1、 打开Firefox浏览器
2、 打开https://addons.mozilla.org/en-US/firefox/addon/selenium-ide/,点击“AddTo Firefox”进行安装
3、 然后再“Tools”菜单下可以看到“SeleniumIDE”,使用快捷键Ctl+ Alt + S可以直接打开
4、 至此,SeleniumIDE就安装好了。
二、使用步骤举例
1、打开Firefox,按Ctl+ Alt + S打开SeleniumIDE
2、确保SeleniumIDE处于录制状态
3、在Firefox打开https://www.baidu.com
4、在搜索框中输入“Pythonselenium”
5、点击“百度一下”进行搜索
6、点击“红色”按钮停止录制
7、将测试用例的名称修改为“UC-001”
8、输出为Python代码,以供自己修改
9、导出后的代码如下:
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.byimport By
from selenium.webdriver.common.keysimport Keys
from selenium.webdriver.support.uiimport Select
from selenium.common.exceptionsimport NoSuchElementException
from selenium.common.exceptionsimport NoAlertPresentException
import unittest, time,re
class UC001(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_u_c001(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("Python Selenium")
driver.implicitly_wait(2)
driver.find_element_by_id("su").click()
driver.find_element_by_id("kw").click()
driver.find_element_by_id("kw").clear()
driver.find_element_by_id("kw").send_keys("webdriver")
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()