filter_page.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. '''
  4. @File : filter_page.py
  5. @Time : 2024/11/21 14:55:10
  6. @Author : dulip3ng
  7. @Version : 1.0
  8. @Desc : None
  9. '''
  10. import re
  11. import time
  12. from pages.locator.filter_page_locator import FilterPageLocator
  13. from playwright.sync_api import Page, expect
  14. from common.field_type import FieldType
  15. from common.condition_row import ConditionRow
  16. from pages.base_page import BasePage
  17. class FilterPage(BasePage):
  18. """
  19. 过滤弹出框页面模型,适用于普通过滤弹出框,如在列表中点击过滤后弹出的过滤框
  20. """
  21. def check_all_org(self):
  22. """
  23. 勾选所有组织复选框,如果已经是勾选状态则不操作
  24. **用法:**
  25. FilterPage.check_all_org()
  26. :return:
  27. """
  28. # 多数情况下勾选所有组织是过滤界面第一个操作,偶发点击失效情况,加入2s等待时间确保稳定性
  29. time.sleep(2)
  30. locator_click = self.page.locator(FilterPageLocator.ALL_ORG_LOC)
  31. locator_get_state = self.page.locator(FilterPageLocator.ALL_ORG_GET_CHECK_STATE_LOC)
  32. expect(locator_click).to_be_enabled()
  33. print(locator_get_state.is_checked())
  34. if not locator_get_state.is_checked():
  35. locator_click.click()
  36. def uncheck_all_org(self):
  37. """
  38. 取消勾选所有组织复选框,如果已经未勾选状态则不操作
  39. **用法:**
  40. FilterPage.uncheck_all_org()
  41. :return:
  42. """
  43. # 多数情况下勾选所有组织是过滤界面第一个操作,偶发点击失效情况,加入2s等待时间确保稳定性
  44. time.sleep(2)
  45. locator_click = self.page.locator(FilterPageLocator.ALL_ORG_LOC)
  46. locator_get_state = self.page.locator(FilterPageLocator.ALL_ORG_GET_CHECK_STATE_LOC)
  47. expect(locator_click).to_be_enabled()
  48. print(locator_get_state.is_checked())
  49. if locator_get_state.is_checked():
  50. print("22")
  51. locator_click.click()
  52. def check_org(self, *orgs:str):
  53. """
  54. 根据给定组织编码或名称,勾选组织
  55. **用法:**
  56. FilterPage.check_org("100.1")
  57. FilterPage.check_org("100.1", "鲁泰纺织股份")
  58. :param orgs: 组织编码或名称,可传多个,模糊匹配
  59. :return:
  60. """
  61. self.page.locator(FilterPageLocator.ORG_ARROW_LOC).click()
  62. for org in orgs:
  63. self.page.locator(FilterPageLocator.ORG_MULTI_ITEM_ARGS_LOC % org).click()
  64. self.page.locator(FilterPageLocator.ORG_SELECT_OK_BTN_LOC).click()
  65. def check_entity(self, *entitys_name:str):
  66. """
  67. 根据给定实体,勾选实体
  68. **用法:**
  69. FilterPage.check_entity("明细信息")
  70. FilterPage.check_entity("明细信息", "财务信息")
  71. **注意:**
  72. 未进行勾选状态判断,未勾选状态下调用一次为勾选,再次调用为取消勾选
  73. :param entitys_name: 实体名称
  74. :return:
  75. """
  76. for entity in entitys_name:
  77. self.page.locator(FilterPageLocator.ENTITY_CHECKBOX_ARGS_LOC % entity).click()
  78. def click_tool_bar_button(self, button_name:str):
  79. """
  80. 点击过滤框页面菜单行按钮
  81. **用法:**
  82. FilterPage.click_tool_bar_button("删除全部")
  83. :param button_name: 按钮名称
  84. :return:
  85. """
  86. self.page.locator(FilterPageLocator.TOOL_BAR_BTN_ARGS_LOC % button_name).click()
  87. def set_condition(self, row:int, condition_name:str):
  88. """
  89. 按行设置过滤条件项
  90. **用法:**
  91. FilterPage.set_condition(1, "基本信息-衬衣订单号")
  92. **注意:**
  93. 部分单据中存在相同字段名在不同页签的情况,此时模糊匹配无法找到正确选项值,推荐使用条件项全称精确匹配
  94. :param row: 行号,从1开始
  95. :param condition_name: 条件项全称
  96. :return:
  97. """
  98. self.click_tool_bar_button("新增行")
  99. locator = self.page.locator(FilterPageLocator.FILTER_CONDITION_ARGS_LOC % ((row - 1) * 35))
  100. locator.fill(condition_name)
  101. # 条件项赋值后默认不弹出选项框,输入空格再删除,以便弹出选项框进行选择
  102. locator.press_sequentially(" ")
  103. locator.press("Backspace")
  104. self.page.locator(FilterPageLocator.CONDITION_ITEM_ARGS_LOC % condition_name).click()
  105. def set_operator(self, row:int, operator:str):
  106. """
  107. 设置过滤条件操作符号
  108. **用法:**
  109. FilterPage.set_operator(1, "包含")
  110. :param row: 行号
  111. :param operator: 操作符名称
  112. :return:
  113. """
  114. self.page.locator(FilterPageLocator.COMPARE_TYPE_ARGS_LOC % ((row - 1) * 35)).click()
  115. self.page.locator(FilterPageLocator.COMPARE_ITEM_ARGS_LOC % operator).click()
  116. def _set_text(self, row:int, value:str):
  117. """
  118. 条件值字段,文本字段类型赋值
  119. **用法:**
  120. FilterPage.set_text(1, "内销")
  121. 使用FilterPage.set_condition_value()方法代替,此方法无需区分字段类型
  122. :param row: 行号,从1开始
  123. :param value: 条件值
  124. :return:
  125. """
  126. self.page.locator(FilterPageLocator.CONDITION_TEXT_FIELD_ARGS_LOC % ((row - 1) * 35)).fill(value)
  127. def _set_date(self, row:int, value:str):
  128. """
  129. 条件值字段,日期字段类型赋值
  130. **用法:**
  131. FilterPage.set_date(1, "2024-11-25")
  132. 使用FilterPage.set_condition_value()方法代替,此方法无需区分字段类型
  133. :param row: 行号,从1开始
  134. :param value: 条件值,格式"2024-11-25"
  135. :return:
  136. """
  137. self.page.locator(FilterPageLocator.CONDITION_DATE_FIELD_ARGS_LOC % ((row - 1) * 35)).fill(value)
  138. self.click_blank()
  139. def _set_select(self, row:int, value:str):
  140. """
  141. 条件值字段,下拉列表字段类型赋值
  142. **用法:**
  143. FilterPage.set_select(1, "创建")
  144. 使用FilterPage.set_condition_value()方法代替,此方法无需区分字段类型
  145. :param row: 行号,从1开始
  146. :param value: 条件值
  147. :return:
  148. """
  149. self.page.locator(FilterPageLocator.CONDITION_SELECT_FIELD_ARGS_LOC % ((row - 1) * 35)).click()
  150. self.page.locator(FilterPageLocator.CONDITION_SELECT_ITEM_ARGS_LOC % value).click()
  151. def _set_base(self, row:int, value:str):
  152. """
  153. 条件值字段,基础资料字段类型赋值
  154. **用法:**
  155. FilterPage.set_base(1, "P010001")
  156. 使用FilterPage.set_condition_value()方法代替,此方法无需区分字段类型
  157. :param row: 行号,从1开始
  158. :param value: 条件值
  159. :return:
  160. """
  161. # CONDITION_BASE_FIELD_ARGS_LOC input元素可操作性检查不通过,CONDITION_BASE_FIELD_SPAN_ARGS_LOC span劫持,这里直接对span进行点击
  162. self.page.locator(FilterPageLocator.CONDITION_BASE_FIELD_SPAN_ARGS_LOC % ((row - 1) * 35)).click()
  163. self.page.locator(FilterPageLocator.CONDITION_BASE_FIELD_ARGS_LOC % ((row - 1) * 35)).fill(value)
  164. self.page.locator(FilterPageLocator.CONDITION_BASE_FIELD_ARGS_LOC % ((row - 1) * 35)).press_sequentially(" ")
  165. self.page.locator(FilterPageLocator.CONDITION_BASE_ITEM_ARGS_LOC % value).press_sequentially(" ")
  166. def _set_text_area(self, row:int, value:str):
  167. """
  168. 条件值字段,大文本字段类型赋值
  169. **用法:**
  170. FilterPage.set_text_area(1, "大文本字段值1234567890")
  171. 使用FilterPage.set_condition_value()方法代替,此方法无需区分字段类型
  172. :param row: 行号,从1开始
  173. :param value: 条件值
  174. :return:
  175. """
  176. self.page.locator(FilterPageLocator.CONDITION_TEXTAREA_ARGS_LOC % ((row - 1) * 35)).fill(value)
  177. def set_logic(self, row:int, value:str):
  178. """
  179. 设置逻辑符号,如并且
  180. **用法:**
  181. FilterPage.set_logic(1, "并且")
  182. :param row: 行号,从1开始
  183. :param value: 逻辑符号,取并且,或者
  184. :return:
  185. """
  186. self.page.locator(FilterPageLocator.LOGIC_ARGS_LOC % ((row - 1) * 35)).click()
  187. self.page.locator(FilterPageLocator.COMPARE_ITEM_ARGS_LOC % value).click()
  188. def _parse_condition_value_field_type(self, row:int) -> FieldType:
  189. """
  190. 根据条件项元素属性,自动解析条件值项字段类型
  191. **用法:**
  192. FilterPage._parse_condition_value_field_type(1)
  193. **注意:**
  194. 1.金蝶大版本升级后,判断依据可能会调整
  195. 2.当前支持v8.2
  196. :param row: 行号,从1开始
  197. :return:
  198. """
  199. data_role = self.page.locator(FilterPageLocator.PARSE_CONDITION_VALUE_FIELD_TYPE_ARGS_LOC % ((row - 1) * 35)).get_attribute("data-attdata")
  200. pattern = "xtype\":\"([a-zA-Z0-9]+)"
  201. match_strs = re.findall(pattern, data_role)
  202. if match_strs:
  203. match match_strs[0]:
  204. case "textfield":
  205. return FieldType.TEXT
  206. case "bosf7field":
  207. return FieldType.BASE
  208. case "kdeditcombo" | "kdcombo":
  209. return FieldType.SELECT
  210. case "kdmulticombo":
  211. return FieldType.MULTISELECT
  212. case "kdtextarea" | "kdlangtextarea" | "kdtabpage":
  213. return FieldType.TEXTAREA
  214. case "datefield":
  215. return FieldType.DATE
  216. return FieldType.UNKNOWN
  217. def set_condition_value(self, row:int, value:str, field_type: FieldType = None):
  218. """
  219. 条件值字段赋值,不给定字段类型时自动解析字段类型,推荐不给定,方便统一调用形式
  220. **用法:**
  221. FilterPage.set_condition_value(1, "26024001")
  222. FilterPage.set_condition_value(1, "26024001", FieldType.TEXT)
  223. :param row: 行号,从1开始
  224. :param value: 条件值
  225. :param field_type: 条件值字段类型,可选
  226. :return:
  227. """
  228. if field_type is None:
  229. field_type = self._parse_condition_value_field_type(row)
  230. match field_type:
  231. case FieldType.TEXT:
  232. self.set_text(row, value)
  233. case FieldType.SELECT:
  234. self.set_select(row, value)
  235. case FieldType.BASE:
  236. self.set_base(row, value)
  237. case FieldType.DATE:
  238. self.set_date(row, value)
  239. case FieldType.TEXTAREA:
  240. self.set_text_area(row, value)
  241. case _:
  242. raise TypeError("未匹配到过滤页面过滤条件输入框字段类型。")
  243. def click_ok_button(self):
  244. """
  245. 点击确定,开始过滤
  246. **用法:**
  247. FilterPage.click_ok_button()
  248. :return:
  249. """
  250. self.page.locator(FilterPageLocator.FILTER_OK_BUTTON_LOC).click()
  251. def set_condition_row(self, row:int, condition_name:str, condition_value:str=None, operator:str=None, logic:str=None):
  252. """
  253. 设置一行过滤条件
  254. **用法:**
  255. FilterPage.set_condition_row(1, "创建日期", operator="今天")
  256. FilterPage.set_condition_row(1, "衬衣订单号", "26024001")
  257. FilterPage.set_condition_row(1, "衬衣订单号", "26024", "包含")
  258. FilterPage.set_condition_row(1, "衬衣订单号", "26024", "包含", "或者")
  259. :param row: 行号,从1,开始
  260. :param condition_name: 过滤条件名
  261. :param condition_value: 过滤条件值,可选
  262. :param operator: 操作符,可选
  263. :param logic: 逻辑符,可选
  264. :return:
  265. """
  266. self.set_condition(row, condition_name)
  267. if operator:
  268. self.set_operator(row, operator)
  269. if condition_value:
  270. self.set_condition_value(row, condition_value)
  271. if logic:
  272. self.set_logic(row, logic)
  273. def filter(self, *condition_row: ConditionRow):
  274. """
  275. 根据条件对象进行过滤,默认勾选全部组织
  276. **用法:**
  277. cond1 = ConditionRow(1, "基本信息-衬衣订单号", "123456", "包含", "或者")
  278. cond2 = ConditionRow(2, "单据状态", "已审核")
  279. FilterPage.filter(cond1, cond2)
  280. :param condition_row: 条件对象,ConditionRow(row:int, condition_name:str, condition_value:str=None, operator:str=None, logic:str=None), 可传多个
  281. :return:
  282. """
  283. self.check_all_org()
  284. self.click_tool_bar_button("全部删除")
  285. time.sleep(1)
  286. for cr in condition_row:
  287. self.set_condition_row(cr.row, cr.condition_name, cr.condition_value, cr.operator, cr.logic)
  288. self.click_ok_button()