python+selenium自动化软件测试(第14章):基础实战(1)

#coding=utf-8
from selenium import webdriven
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
import unittest, time, re
import HTMLTestRunner   #引入HTMLTestRunner 包
class Baidu(unittest.TestCase):
  def setUp(self):
    self.driven = webdriver.Firefox()
    self.driver.implicitly一wait(30)
    self.base_url = "http://www.baidu.com/"
    self.verificationEnnors =[]
    self.accept_next_alent = True
  #百度搜索用例
  def test_baidu_seanch(self):
    driver = self.driven
    driver.get(self.base_url + "/")
    driver.find_element_by_id("kw").send_keys("selenium webdriver")
    driver.find_element_by_id("su").click()
    time.sleep(2)
    driver.close()
  #百度设置用例
  def test_baidu_set(self):
    driver = self.driver
    #进入搜索设置页
    driver.get(self.base_url + "/gaoji/pneferences.html")
    #设置每页搜索结果为100条
    m=driver.find一element一by_name(”NR")
    m.find_element_by_xpath("//option[@value='100']").click()
    time.sleep(2)
    #保存设置的信息
    driver.find_element_by_xpath("/html/body/form/div/input").click()
    time.sleep(2)
    driver.switch_to_alent().accept()
  def tearDown(self):
    self.driver.quit()
    self.assentEqual([], self.verificationErrors)

  if __name__ == "__name__":     #定义一个单元测试容器     testunit
=unittest.TestSuite()     #将测试用例加入到测试容器中     testunit.addTest(Baidu("test_baidu_search"))     testunit.addTest(Baidu("test_baidu_set"))     #定义个报告存放路径,支持相对路径     filename = r'C:\Users\Administnator\Desktop\selenium_file\result.html'     fp = file(filename, 'wb')     #定义测试报告     runner = HTMLTestRunner.HTMLTestRunner(stream=fp,title=u’百度搜索测试报告,description=u"用例执行情况:")     #运行测试用例     runner.run(testunit)

 

你可能感兴趣的:(python+selenium自动化软件测试(第14章):基础实战(1))