zoukankan      html  css  js  c++  java
  • selenium实战学习第一课

     1 #-*- coding:utf-8 -*-
     2 __author__ = "carry"
     3 
     4 from selenium import webdriver
     5 from selenium.webdriver.common.by import By
     6 from selenium.webdriver.common.keys import Keys
     7 from selenium.webdriver.support.ui import Select
     8 from selenium.common.exceptions import NoSuchAttributeException
     9 import unittest,time,re
    10 import HTMLTestRunner #引入HTMLTestRunner
    11 class Baidu(unittest.TestCase):
    12     def setUp(self):
    13         self.driver = webdriver.Firefox()
    14         self.driver.implicitly_wait(30)
    15         self.base_url = "http://www.baidu.com"
    16         self.verificationErrors = []
    17         self.accept_next_alert =True
    18     #百度搜索用例
    19     def test_baidu_search(self):
    20         driver = self.driver
    21         driver.get(self.base_url+"/")
    22         driver.find_element_by_id("kw").send_keys("selenium")
    23         driver.find_element_by_id("su").click()
    24         time.sleep(2)
    25         driver.close()
    26     #百度设置用例
    27     def test_baidu_set(self):
    28         driver = self.driver
    29     #进入搜素设置页
    30         driver.get(self.base_url+"/gaoji/preferences.html")
    31     #设置每页搜索结果为50页
    32         m = driver.find_element_by_name("NR")
    33         m.find_element_by_xpath("//option[@value='50']").click()
    34         time.sleep(2)
    35     #保存设置的信息
    36         driver.find_element_by_xpath("/html/body/form/div/input").click()
    37         time.sleep(2)
    38         driver.switch_to_alert().accept()
    39     def tearDown(self):
    40         self.driver.quit()
    41         self.assertEqual([],self.verificationErrors)
    42 if __name__ == "__main__":
    43     #定义一个单元测试容器
    44     testunit = unittest.TestSuite()
    45     #将测试用例加入到测试容器中
    46     testunit.addTest(Baidu("test_baidu_search"))
    47     testunit.addTest(Baidu("test_baidu_set"))
    48     #定义一个报告存放路径,支持相对路径
    49     filename = r'D:selenium_file
    esult.html'
    50     fp = file(filename,'wb')
    51     #定义测试报告
    52     runner = HTMLTestRunner.HTMLTestRunner(
    53         stream=fp,
    54         title=u'百度搜索测试报告',
    55         description=u'用例执行情况:'
    56     )
    57     #运行测试用例
    58     runner.run(testunit)
  • 相关阅读:
    金蝶软件常用基础SQL数据表
    改变cxgrid行字体颜色
    C#默认修饰符
    const和static
    怎么写动态游标
    Report Machine支持导出PDF
    C1TrueDBGrid注册码
    免费在线思维导图工具Mindv V1.1.0发布
    支持中文输入的免费的Silverlight Richtextbox
    云计算浅谈之一:云计算介绍
  • 原文地址:https://www.cnblogs.com/lxs1314/p/7222328.html
Copyright © 2011-2022 走看看