from BeautifulReport import BeautifulReport
from PublicScript import *
from BasePage import basepage


class as_AuditQueryScript2(basepage):
    def save_img(self, img_name):
        self.driver.get_screenshot_as_file("./img/" + img_name + ".png")

    @classmethod
    def setUpClass(cls):
        cls.set_driver()

    @BeautifulReport.add_test_img("test_Ayonghjquery")
    def test_Ayonghjquery(self):
        """填写所有内容后进行用户稽查"""
        global driver
        driver = self.get_driver()
        is_element_load(driver, '//*[@id="top"]/div/div[1]/h3')
        present_wait(driver, By.ID, "vc-tree-name-综合查询").click()
        present_wait(driver, By.ID, "vc-tree-name-稽查查询").click()
        target_list = find_li(driver)
        if '用户稽查' not in target_list:
            print('当前页面不存在此测试项')
            self.assertEqual(1, 1)
        else:
            self.click_topbutton(driver, '用户稽查')
            self.public_ctrl(driver)

    @BeautifulReport.add_test_img("test_Bgouqjcquery")
    def test_Bgouqjcquery(self):
        """填写所有内容后进行购气稽查"""
        present_wait(driver, By.ID, "vc-tree-name-稽查查询").click()
        target_list = find_li(driver)
        if '购气稽查' not in target_list:
            print('当前页面不存在此测试项')
            self.assertEqual(1, 1)
        else:
            self.click_topbutton(driver, '购气稽查')
            self.public_ctrl(driver)

    @BeautifulReport.add_test_img("test_Ccuowcxquery")
    def test_Ccuowcxquery(self):
        """填写所有内容后进行错误查询"""
        present_wait(driver, By.ID, "vc-tree-name-稽查查询").click()
        target_list = find_li(driver)
        if '错误查询' not in target_list:
            print('当前页面不存在此测试项')
            self.assertEqual(1, 1)
        else:
            self.click_topbutton(driver, '错误查询')
            self.public_ctrl(driver)

    @BeautifulReport.add_test_img("test_Dyongqjcquery")
    def test_Dyongqjcquery(self):
        """填写所有内容后进行用气稽查"""
        present_wait(driver, By.ID, "vc-tree-name-稽查查询").click()
        target_list = find_li(driver)
        if '用气稽查' not in target_list:
            print('当前页面不存在此测试项')
            self.assertEqual(1, 1)
        else:
            self.click_topbutton(driver, '用气稽查')
            self.public_ctrl(driver)

    def test_zZZZ_END(self):
        """当前测试模块结束，关闭浏览器"""
        driver.quit()

    def click_topbutton(self, driver, but_name):
        top_button_group = present_waits(driver, By.XPATH, '//div[@class = "flex foot_tabset"]//li/button')
        for target_button in top_button_group:
            if target_button.text == but_name:
                target_button.click()

        judeg = is_element_load(driver, '//div[@class="button_spacing button_shrink_bottom"]')
        if judeg:
            topbuttons = present_waits(driver, By.XPATH, '//div[@class="button_spacing button_shrink_bottom"]')
            for topbutton in topbuttons:
                if topbutton.is_displayed():
                    topbutton.click()
                    break

    def public_ctrl(self, driver):
        #  选择框
        choose_border(driver, '//div[@class="btn-group select_list select"]')
        choose_border(driver, '//div[@class="btn-group"]')
        #  输入框
        input_border(driver, '//div[@class="col-sm-2 form-group"]/input')
        input_border(driver, '//div[@class="col-sm-2 flex-row"]//input')
        input_border(driver, '//div[@class="col-sm-2"]/input')
        input_border(driver, '//div[@class="col-sm-2 form-group flex-row"]//input')
        #  选择小区
        area_choose(driver)
        #  日期框
        data_border(driver, '//div[@class="datepicker"]')
        search_buttons = present_waits(driver, By.XPATH, '//button[text()="查询"]')
        for search_button in search_buttons:
            if search_button.is_displayed():
                search_button.click()
                break
        sleep(1)
        result = is_element_load(driver, '//p[contains(text(),"错误码")]')
        expect = '测试'
        if not result:
            print('查询功能未报错，测试通过')
            expect = '查询功能未报错，测试通过'
        else:
            print('查询功能报错，测试失败')
        self.assertEqual(expect, '查询功能未报错，测试通过')

    def tearDown(self):
        pass
