• python3.6+selenium_多个测试用例


    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @Time : 2018/11/22 10:10
    # @File : unittest_test2_1.py
    
    '''多个用例写在同一个测试类'''
    import unittest
    from selenium import webdriver
    class SearchTests(unittest.TestCase):
        #初始化浏览器和url
        def setUp(self):
            self.driver = webdriver.Chrome()
            self.driver.implicitly_wait(30)
            self.driver.maximize_window()
            self.driver.get("https://www.baidu.com/?tn=78000241_12_hao_pg")
    
        #通过分类搜索产品,校验返回的产品数量是否正确
        def test_search_by_category(self):
            self.search_file = self.driver.find_element_by_name('wd')
            self.search_file.clear()
            self.search_file.send_keys('phones')
            self.search_file.submit()
            products = self.driver.find_element_by_xpath('//*[@id="5"]/h3/a')
            '''
            products = self.driver.find_element('xpath','//*[@id="5"]/h3/a')
            find_element()定位方法,
                by_id= "id"
                by_xpath = "xpath"
                by_link_text = "link text"
                by_partial_text = "partial link text"
                by_name = "name"
                by_tag_name = "tag name"
                by_class_name = "class name"
                by_css_selector = "css selector"
            等同于find_element_by方法
            find_elements()也等同于find_elements_by()方法
            '''
            #print('==========')
            print(products.text)
            #print('=========')
            #assert (u'iPhone - Apple (中国)')in products.text
            p=[products,]
            print(len(p))
            self.assertEqual(1,len(p))
    
        #再创建一个测试用例
        def test_search_by_name(self):
            self.search_file = self.driver.find_element_by_name('wd')
            self.search_file.clear()
            self.search_file.send_keys('salt shaker')
            self.search_file.submit()
            product = self.driver.find_element_by_xpath('//*[@id="1"]/h3/a')
            p = [product,]
            self.assertEqual(1,len(p))
    
        #清理所有的 初始化值
        def tearDown(self):
            self.driver.quit()
    
    #运行测试,传递verbosity参数,以便使详细的测试总量显示在控制台
    if __name__=='__main__':
        unittest.main(verbosity=2)
  • 相关阅读:
    Python面向对象:杂七杂八的知识点
    初学Python常见异常错误,总有一处你会遇到!
    Python GUI开发,效率提升10倍的方法!
    Python里三个最高逼格的调试神器
    你见过的最全面的 Python 重点
    Python使用数字与字符串的技巧
    python3的eval和exec的区别与联系
    Python规范:提高可读性
    mysql使用mysqldump和crontab定时备份
    spring cloud stream集成rabbitmq
  • 原文地址:https://www.cnblogs.com/xiuxiu123456/p/10384170.html
Copyright © 2020-2023  润新知