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


class as_SummaryQueryScript2(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_Aqianfcxkhquery")
    def test_Aqianfcxkhquery(self):
        """进行小区综合查询测试"""
        global driver
        driver = self.get_driver()
        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)

    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()

        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):
        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(2)
        driver.execute_script("document.body.style.zoom='0.6'")
        sleep(0.5)
        expect = '测试通过'
        tds = present_waits(driver, By.XPATH, './/table[@class="table-hover"]//td')
        for td in tds:
          if td.text == '物联用气量合计: 0':
            expect = '物联网用气量为0，测试不通过'
            print(expect)
        self.assertEqual(expect, '测试通过')

# 下拉框通用操作
def choose_border2(xpath):
    border = is_element_load(driver, xpath)
    if border:
        select_group1 = present_waits(driver, By.XPATH, xpath)
        for select1 in select_group1:
            if select1.is_enabled() and select1.is_displayed():
                select1.click()
                element = './/ul/li'
                if is_element_exist(select1, element):
                    last_one = select1.find_element_by_xpath('.//ul/li[last()]/a')
                    if last_one.is_displayed() and last_one.is_enabled():
                        last_one.click()

    def tearDown(self):
        pass
