自动化测试-2.seleniumIDE
一、安装步骤
1、 打开Firefox浏览器
2、 打开https://addons.mozilla.org/en-US/firefox/addon/selenium-ide/versions/,点击“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、在搜索框中输入“SeleniumIDE”
5、点击“百度一下”进行搜索
6、点击“红色”按钮停止录制
7、将测试用例的名称修改为“test_selenium_i_d_e”
8、输出为Python代码,以供自己修改
9、导出后的代码如下:
1 # -*- coding: utf-8 -*- 2 from selenium import webdriver 3 from selenium.webdriver.common.by import By 4 from selenium.webdriver.common.keys import Keys 5 from selenium.webdriver.support.ui import Select 6 from selenium.common.exceptions import NoSuchElementException 7 from selenium.common.exceptions import NoAlertPresentException 8 import unittest, time, re 9 10 class TestSeleniumIDE(unittest.TestCase): 11 def setUp(self): 12 self.driver = webdriver.Firefox() 13 self.driver.implicitly_wait(30) 14 self.base_url = "https://www.baidu.com/" 15 self.verificationErrors = [] 16 self.accept_next_alert = True 17 18 def test_selenium_i_d_e(self): 19 driver = self.driver 20 driver.get(self.base_url + "s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=SeleniumIDE&oq=%25E5%258D%259A%25E5%25AE%25A2%25E5%259B%25AD&rsv_pq=990abb04000248d9&rsv_t=149fiml8g0fDEtIWNR3XgW4MKqjwT6GkPBJ%2B8XBSbl%2B%2B1e71yMkYU9YDRto&rqlang=cn&rsv_enter=0&inputT=6748&rsv_sug3=93&rsv_sug1=58&rsv_sug7=100&rsv_sug2=0&rsv_sug4=7716") 21 driver.find_element_by_id("kw").click() 22 driver.find_element_by_id("su").click() 23 driver.find_element_by_css_selector("em").click() 24 25 def is_element_present(self, how, what): 26 try: self.driver.find_element(by=how, value=what) 27 except NoSuchElementException as e: return False 28 return True 29 30 def is_alert_present(self): 31 try: self.driver.switch_to_alert() 32 except NoAlertPresentException as e: return False 33 return True 34 35 def close_alert_and_get_its_text(self): 36 try: 37 alert = self.driver.switch_to_alert() 38 alert_text = alert.text 39 if self.accept_next_alert: 40 alert.accept() 41 else: 42 alert.dismiss() 43 return alert_text 44 finally: self.accept_next_alert = True 45 46 def tearDown(self): 47 self.driver.quit() 48 self.assertEqual([], self.verificationErrors) 49 50 if __name__ == "__main__": 51 unittest.main()