Esempio n. 1
0
class TestBaiDu(unittest.TestCase):

    URL = Config().get('URL')
    excel = DATA_PATH + '/baidu.xlsx'

    def sub_setUp(self):
        # 初始页面是main page,传入浏览器类型打开浏览器
        self.page = BaiDuMainPage(browser_type='chrome').get(
            self.URL, maximize_window=True)
        #self.driver = webdriver.Chrome(executable_path=DRIVER_PATH+'\chromedriver.exe')
        #self.driver.get(self.URL)
    def sub_tearDown(self):
        #self.driver.quit()
        self.page.quit()

    def test_search(self):
        datas = ExcelReader(self.excel).data
        for d in datas:
            with self.subTest(data=d):
                self.sub_setUp()
                #self.driver.find_element(*self.locator_kw).send_keys(d['search'])
                #self.driver.find_element(*self.locator_su).click()
                self.page.search(d['search'])
                time.sleep(2)
                self.page = BaiDuResultPage(self.page)  # 页面跳转到result page
                links = self.page.result_links
                #links = self.driver.find_elements(*self.locator_result)
                for link in links:
                    logger.info(link.text)
                self.sub_tearDown()
Esempio n. 2
0
class TestBaiDu(unittest.TestCase):
    URL = Config().getByKeys('websites', 'URL')
    excel = DATA_PATH + '/data.xlsx'

    def sub_setUp(self):
        # 打开初始页面BaiDuMainPage,传入浏览器类型打开浏览器,获得当前页面的driver
        self.page = BaiDuMainPage(browser_type='chrome').get(
            self.URL, maximize_window=False)

    def sub_tearDown(self):
        self.page.quit()

    # 数据未分离测试单个查询条件
    def test_search2(self):
        """百度搜索测试"""
        with self.subTest(data='翟操'):
            print("搜索关键字:翟操")
            self.sub_setUp()
            self.page.search('翟操')
            time.sleep(2)
            self.page = BaiDuResultPage(self.page)
            self.assertEqual('百度_百度搜索', self.page.result_title, self)
            self.sub_tearDown()

    def test_search(self):
        '''
        测试百度搜索title中内容与查询关键字是否一致
        :return:
        '''
        datas = ExcelReader(self.excel).data
        for d in datas:
            with self.subTest(data=d):
                self.sub_setUp()
                # BaiDuMainPage调用自身的search方法。类似于self.driver.search()
                self.page.search(d['search'])
                time.sleep(2)
                # 跳转到结果页面
                self.page = BaiDuResultPage(self.page)
                # 断言title
                try:
                    self.assertEqual('{}_百度搜索'.format(d['expection']),
                                     self.page.result_title)
                    links = self.page.result_links
                    for link in links:
                        logger.info(link.text)
                except:
                    # 断言失败,截图
                    self.page.save_screen_shot()
                    raise AssertionError('Title Error')
                finally:
                    self.sub_tearDown()

    def test_out(self):
        self.assertEqual('Test', '1')
Esempio n. 3
0
class TestBaiDu(unittest.TestCase):
    URL = Config().get('URL')
    excel = DATA_PATH + '/baidu.xlsx'
    #URL = "http://www.baidu.com"  #直接写路径
    #定义读取driver的路径
    #abspath是当前脚本的绝对路径,路径带文件本身,dirname返回的是不带文件本身的路径,即文件所在路径
    #print(os.path.abspath(__file__))   #E:\Program\pythonProject\Test_Framework\src\test\test_baidu.py,去别就是后面有没有\test_baidu.py
    # base_path = os.path.dirname(os.path.abspath(__file__))+'\..\..'#加的'\..\..'起到的效果是向上返回两层
    # print(base_path)
    # driver_path = os.path.abspath(base_path+'\drivers\chromedriver.exe')
    # print(driver_path)

    #os.path.split(path)将path分割成目录和文件名二元组返回
    #获取当前运行脚本的绝对路径(去掉最后2个路径)
    #path3 = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
    #print(path3)
    """
    配置了PageObjecth后注释掉此段,common的browser、page,还有page的baidu_main_page.py和baidu_result_page.py
    #定义搜索id(firebug获得元素ID)
    locator_kw = (By.ID,'kw')
    locator_su = (By.ID,'su')
    locator_result = (By.XPATH, '//div[contains(@class, "result")]/h3/a')]
    """
    def setUp(self):
        #普通方式连接驱动,打开浏览器
        # self.driver = webdriver.Chrome(executable_path=DRIVER_PATH+'\chromedriver.exe')
        # self.driver.get(self.URL)
        #PageObject方式,初始页面是main page,传入浏览器类型打开浏览器
        self.page = BaiDuMainPage(browser_type='chrome').get(
            self.URL, maximize_window=False)

    def tearDown(self):
        # self.driver.quit()   #普通方式退出
        self.page.quit()  #PageObject方式退出

    """
    #每搜索不同的文字,就要写一个重复的代码
    def test_search_0(self):
        self.driver.find_element(*self.locator_kw).send_keys("selenium")
        self.driver.find_element(*self.locator_su).click()
        time.sleep(2)
        links = self.driver.find_element(*self.locator_result)#result是:WebElement (session="4deefab86ff7db61db83d4223ae700f3", element="0.03811039743044686-3")>,不可迭代即不可for循环。
        # print(links)
        logger.info(links)
        # for link in links:
        #     print(link.text)


    def test_search_1(self):
        self.driver.find_element(*self.locator_kw).send_keys("Python")
        self.driver.find_element(*self.locator_su).click()
        time.sleep(2)
        links = self.driver.find_element(*self.locator_result)
        # print(links)
        logger.info(links)
    """
    """
    subTest是Python3 unittest里带的功能,PY2中没有,PY2中要想使用,需要用unittest2库。
    subTest是没有setUp和tearDown的,所以需要自己手动添加并执行。
    把要搜索的内容,存储在其它的文件中(比如excel文件),参数化读取

    def test_search_2(self):
        datas = ExcelReader(self.excel).data
        print(datas)
        for d in datas:
            with self.subTest(data=d):
                self.setUp()
            self.driver.find_element(*self.locator_kw).send_keys(d['search'])#此处的search是title_line
            self.driver.find_element(*self.locator_su).click()
            time.sleep(2)

            links = self.driver.find_element(*self.locator_result)
            logger.info(links)
            self.tearDown()
    """

    # 用了PageObject后的写法
    def test_search_3(self):
        datas = ExcelReader(self.excel).data
        for d in datas:
            with self.subTest(data=d):
                self.setUp()
                self.page.search(d['search'])
                time.sleep(2)
                self.page = BaiDuResultPage(self.page)  #页面跳转到result page
                links = self.page.result_links
                logger.info(links)
                self.tearDown()
class TestBaiDu(unittest.TestCase):
    # URL = "https://www.baidu.com/";
    URL = Config().get("URL")
    excel = DATA_PATH + '/baidu.xlsx'

    #获取浏览器驱动的存放的目录
    # current_path = os.path.abspath(__file__)  #当前文件的绝对路径
    # current_dir_path = os.path.dirname(current_path)
    # base_path = current_dir_path + "\..\..\.."
    # print('current_dir_path----------',current_dir_path)
    # driver_path = os.path.abspath(base_path + "\drivers\chromedriver.exe")
    driver_path = os.path.abspath(DRIVER_PATH + "\chromedriver.exe")
    # print('driver_path------',driver_path)

    locator_kw = (By.ID, 'kw')
    locator_su = (By.ID, 'su')
    locator_result = (By.XPATH, '//div[contains(@class, "result")]/h3/a')

    # def setUp(self):
    #     self.driver = webdriver.Chrome(self.driver_path)
    #     self.driver.get(self.URL)
    #
    # def tearDown(self):
    #     self.driver.quit()

    def sub_setUp(self):
        self.driver = webdriver.Chrome(executable_path=DRIVER_PATH +
                                       '\chromedriver.exe')
        self.driver.get(self.URL)

    def sub_tearDown(self):
        self.driver.quit()

    def sub_setUp_po(self):
        self.page = BaiDuMainPage(browser_type='chrome').get(
            self.URL, maximize_window=False)

    def sub_tearDown_po(self):
        self.page.quit()

    # def test_search_0(self):
    #     self.driver.find_element(By.ID,"kw").send_keys("python")
    #     self.driver.find_element(By.ID,"su").click()
    #     time.sleep(2)
    #
    #     #进入python官网
    #     self.driver.find_element(By.XPATH,'//div[@id="1"]/h3/a[1]').click()

    #定位不到python官网的元素   ???
    # self.driver.find_element(By.ID,"id-search-field").send_keys("pip")
    # self.driver.find_element(By.ID,"submit").click()
    #
    # time.sleep(2)
    #
    # links = self.driver.find_element(By.XPATH,'//ul[@class="list-recent-events menu"]/li/h3/a')
    # for link in links:
    #     print('links-------',link)

    # def test_search_1(self):
    #     self.driver.find_element(By.ID, "kw").send_keys("Python selenium")
    #     self.driver.find_element(By.ID, "su").click()
    #     time.sleep(2)
    #     links = self.driver.find_elements(*self.locator_result)
    #     for link in links:
    #         # print(link.text)
    #         logger.info(link.text)

    #   参数化
    def test_search(self):
        datas = ExcelReader(self.excel).data
        logger.info(datas)
        for d in datas:
            with self.subTest(data=d):
                self.sub_setUp()
                self.driver.find_element(*self.locator_kw).send_keys(
                    d['search'])
                self.driver.find_element(*self.locator_su).click()
                time.sleep(2)
                links = self.driver.find_elements(*self.locator_result)
                for link in links:
                    logger.info(link.text)
                self.sub_tearDown()

    #使用PO思想后对test_search方法进行改造
    def test_search_po(self):
        datas = ExcelReader(self.excel).data
        for d in datas:
            with self.subTest(data=d):
                self.sub_setUp_po()
                self.page.search(d['search'])
                time.sleep(2)
                self.page = BaiDuResultPage(self.page)  #页面跳转到result页面
                self.page.save_screen_shot()
                links = self.page.result_links
                for link in links:
                    logger.info(link.text)
                self.sub_tearDown_po()