#!/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)