叶子绿

分享自己的一些前端经验,做改变世界的产品。 再次出发…

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

selenium

selenium 分为好几个系列

  • Selenium IDE
  • Selenium Server (formerly the Selenium RC Server)
  • Selenium Client & WebDriver Language Bindings

详细的说明介绍在这里 简单来说, selenium ide 是 firefox 浏览器上的一款插件, 可以记录测试员的一系列操作, 并可以根据要求生成一系列命令和特定的脚本程序

(看个人喜好).

selenium rc 是用来运行selenium 脚本程序的..

webdriver 的说明在这里

selenium webdriver & python

开发人员自己要写测试用例, 这是个很好的习惯..本人推荐python系列. windows 下安装过程如下:

  • python
  • setuptools , pip
  • pip install -U selenium
  • selenium-server-standalone-XXX.jar

本人安装测试的时候折腾了好久, 不想让大家也这样折腾一次. 所以整理了下, 打了个包供大家下载.

包内容如下: firefox , selenium-ide, chromedriver, iedriver, selenium-server-standalone-XXX.jar, pip, setuptools.

python的安装, 请去官网上看说明. 另外个安装说明在这里

selenium+python文档

用例测试

以好康网的登陆过程为例一系列流程下来, selenium ide 生成的命令如下:

然后可以导出为python rc 或者 python webdriver 脚本, 可以手动修改脚本再运下.

python webdriver的版本如下:
   1:  from selenium import webdriver
   2:  from selenium.webdriver.common.by import By
   3:  from selenium.webdriver.support.ui import WebDriverWait
   4:  from selenium.common.exceptions import NoSuchElementException
   5:  import unittest, time, re
   6:   
   7:  class Login(unittest.TestCase):
   8:      def setUp(self):
   9:          self.driver = webdriver.Firefox()
  10:          self.driver.implicitly_wait(30)
  11:          self.base_url = "http://staging.fever38.com/"
  12:          self.verificationErrors = []
  13:          self.accept_next_alert = True
  14:   
  15:      def test_login(self):
  16:          driver = self.driver
  17:          driver.get(self.base_url + "/")
  18:          driver.find_element_by_css_selector("b.fb-login").click()
  19:   
  20:          driver.switch_to_window(driver.window_handles[-1])
  21:          WebDriverWait(driver, 10).until(lambda x: x.find_element_by_id("email"))
  22:          driver.find_element_by_id("email").clear()
  23:          driver.find_element_by_id("email").send_keys("your email")
  24:          driver.find_element_by_id("pass").clear()
  25:          driver.find_element_by_id("pass").send_keys("your password")
  26:          driver.find_element_by_id("persist_box").click()
  27:   
  28:   
  29:      def is_element_present(self, how, what):
  30:          try: self.driver.find_element(by=how, value=what)
  31:          except NoSuchElementException, e: return False
  32:          return True
  33:   
  34:      def is_alert_present(self):
  35:          try: self.driver.switch_to_alert()
  36:          except NoAlertPresentException, e: return False
  37:          return True
  38:   
  39:      def close_alert_and_get_its_text(self):
  40:          try:
  41:              alert = self.driver.switch_to_alert()
  42:              alert_text = alert.text
  43:              if self.accept_next_alert:
  44:                  alert.accept()
  45:              else:
  46:                  alert.dismiss()
  47:              return alert_text
  48:          finally: self.accept_next_alert = True
  49:   
  50:      def tearDown(self):
  51:          self.driver.quit()
  52:          self.assertEqual([], self.verificationErrors)
  53:   
  54:  if __name__ == "__main__":
  55:      unittest.main()
python selenium rc 的版本如下:
   1:  from selenium import selenium
   2:  import unittest, time, re
   3:   
   4:  class login2(unittest.TestCase):
   5:      def setUp(self):
   6:          self.verificationErrors = []
   7:          self.selenium = selenium("localhost", 4444, "*chrome", "http://staging.fever38.com/")
   8:          self.selenium.start()
   9:      
  10:      def test_login2(self):
  11:          sel = self.selenium
  12:          sel.open("/")
  13:          sel.click("css=b.fb-login")
  14:          sel.wait_for_pop_up("f3da5628edf910a", "30000")
  15:          sel.select_window("name=_e_0pS1")
  16:          sel.type("id=email", "your email")
  17:          sel.type("id=pass", "your password")
  18:          sel.click("id=persist_box")
  19:          sel.click("id=u_0_1")
  20:          sel.wait_for_page_to_load("30000")
  21:      
  22:      def tearDown(self):
  23:          self.selenium.stop()
  24:          self.assertEqual([], self.verificationErrors)
  25:   
  26:  if __name__ == "__main__":
  27:      unittest.main()

selenium-server 启动

@echo on
cmd.exe /k "%1" java -jar e:\selenium-server-standalone-2.33.0.jar
posted on 2013-07-22 12:04  叶子绿  阅读(850)  评论(1编辑  收藏  举报