body_page.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. '''
  4. @File : body_page.py
  5. @Time : 2024/11/12 08:14:19
  6. @Author : dulip3ng
  7. @Version : 1.0
  8. @Desc : None
  9. '''
  10. from playwright.sync_api import Page, Error
  11. from pages.locator.body_page_locator import BodyPageLocator
  12. from pages.base_page import BasePage
  13. from common.field_type import FieldType
  14. class BodyPage(BasePage):
  15. def change_sheet(self, sheet_name:str, order:int=1):
  16. """
  17. 切换单据体页签
  18. :param sheet_name: 页面名称
  19. :param order: 可选参数,存在同名页签时指明第几个,从1开始
  20. :return: None
  21. """
  22. self.page.locator(BodyPageLocator.SHEET_ARGS_LOC % (sheet_name, order)).click()
  23. def click_button(self, button_name:str, sub_button_name:str=None):
  24. """
  25. 点击单据体按钮或子按钮
  26. 如:新增行
  27. **用法:**
  28. BodyPage.click_button("新增行")
  29. BodyPage.click_button("新增行", "复制行")
  30. :param button_name: 按钮名称
  31. :param sub_button_name: 可选参数,子按钮名称
  32. :return: None
  33. """
  34. if not sub_button_name:
  35. self.page.locator(BodyPageLocator.BUTTON_ARGS_LOC % button_name).click()
  36. else:
  37. self.page.locator(BodyPageLocator.BUTTON_ARROW_ARGS_LOC % button_name).hover()
  38. self.page.locator(BodyPageLocator.SUB_BUTTON_ARGS_LOC % sub_button_name).click()
  39. self._click_blank()
  40. def click_line(self, row:int):
  41. """
  42. 选中单据体行(通过点击行序号)
  43. :param row: 行号,从1开始
  44. :return: None
  45. """
  46. self.page.locator(BodyPageLocator.ACTIVATE_FIELD_ARGS_LOC % ("序号", row-1, 1)).click()
  47. def _activate_field(self, field_name:str, row:int, order:int=1):
  48. """
  49. 激活字段,单据体字段未激活状态时统一为span元素,无法识别字段类型,无法针对不同字段类型操作
  50. :param field_name: 字段名
  51. :param row: 行号,从1开始
  52. :param order: 可选参数,存在同名字段时指明第几个,从1开始
  53. :return: None
  54. """
  55. self.page.locator(BodyPageLocator.ACTIVATE_FIELD_ARGS_LOC % (field_name, row-1, order)).click(force=True)
  56. def _set_text(self, field_name:str, value:str, row:int, order:int=1):
  57. """
  58. 单据体文本字段赋值
  59. :param field_name: 字段名
  60. :param value: 字段值
  61. :param row: 行号
  62. :param order: 可选参数,存在同名字段时指明第几个,从1开始
  63. :return: None
  64. """
  65. self._activate_field(field_name, row, order)
  66. self.page.locator(BodyPageLocator.TEXT_FIELD_ARGS_LOC % (field_name, row-1, order)).fill(value)
  67. self._click_blank()
  68. def _set_select(self, field_name:str, value:str, row:int, order:int=1):
  69. """
  70. 单据体下拉列表字段赋值
  71. :param field_name: 字段名
  72. :param value: 选项值
  73. :param row: 行号
  74. :param order: 可选参数,存在同名字段时指明第几个,从1开始
  75. :return: None
  76. """
  77. self.page.locator(BodyPageLocator.ACTIVATE_FIELD_ARGS_LOC % (field_name, row-1, order)).click()
  78. self.page.locator(BodyPageLocator.SELECT_ITEM_ARGS_LOC % value).click()
  79. self._click_blank()
  80. def _set_base(self, field_name:str, value:str, row:int, order:int=1):
  81. """
  82. 单据体基础资料类型字段赋值
  83. :param field_name: 字段名
  84. :param value: 字段值
  85. :param row: 行号
  86. :param order: 可选参数,存在同名字段时指明第几个,从1开始
  87. :return: None
  88. """
  89. self._activate_field(field_name, row, order)
  90. self.page.locator(BodyPageLocator.BASE_FIELD_ARGS_LOC % (field_name, row-1, order)).click()
  91. self.page.locator(BodyPageLocator.BASE_FIELD_ARGS_LOC % (field_name, row-1, order)).fill(value)
  92. self.page.locator(BodyPageLocator.BASE_FIELD_ARGS_LOC % (field_name, row-1, order)).press_sequentially(" ")
  93. self.page.locator(BodyPageLocator.BASE_ITEM_ARGS_LOC % value).click()
  94. self._click_blank()
  95. def set_value(self, field_name:str, value:str, row:int, order:int=1, field_type:FieldType = None):
  96. """
  97. 单据体字段赋值统一方法,根据给定或自动识别的字段类型调用不同方法完成赋值
  98. **用法:**
  99. BodyPage.set_value("衬衣订单号", "26024021", 1)
  100. :param field_name: 字段名
  101. :param value: 字段值
  102. :param row: 行号
  103. :param order: 可选参数,存在同名字段时指明第几个,从1开始
  104. :param field_type: 可选参数,字段类型,不填时自动识别
  105. :return: None
  106. """
  107. if not field_type:
  108. field_type = self._parse_field(field_name, order)
  109. match field_type:
  110. case FieldType.TEXT:
  111. self._set_text(field_name, value, row, order)
  112. case FieldType.SELECT:
  113. self._set_select(field_name, value, row, order)
  114. case FieldType.BASE:
  115. self._set_base(field_name, value, row, order)
  116. def _parse_field(self, field_name:str, order:int=1):
  117. """
  118. 识别单据体字段类型
  119. :param field_name: 字段名
  120. :param order: 可选参数,存在同名字段时指明第几个,从1开始
  121. :return: 字段类型
  122. """
  123. self._activate_field(field_name, 1, order)
  124. data_role = self.page.locator(BodyPageLocator.PARSE_FILED_ARGS_LOC % (field_name, 0, order)).get_attribute("data-role")
  125. self._click_blank()
  126. match data_role:
  127. case "kdbosf7":
  128. return FieldType.BASE
  129. case "dropdownlist":
  130. return FieldType.SELECT
  131. case "kdtextfield" | "kdnumberfield" | "datepicker" | "kdmultilang" | "datetimepicker":
  132. return FieldType.TEXT
  133. def get_row_count(self):
  134. """
  135. 获取单据体总行数
  136. :return: 行数
  137. """
  138. return self.page.locator(BodyPageLocator.ROW_COUNT_LOC).inner_text()
  139. def get_value(self, field_name:str, row:int, order:int=1):
  140. """
  141. 获取单据体字段值,不区分字段类型
  142. :param field_name: 字段名
  143. :param row: 行号
  144. :param order: 可选参数,存在同名字段时指明第几个,从1开始
  145. :return: 字段值
  146. """
  147. return self.page.locator(BodyPageLocator.FIELD_VALUE_ARGS_LOC % (field_name, row-1, order)).inner_text()
  148. def set_checked(self, field_name:str, row:int):
  149. """
  150. 勾选单据体复选框
  151. :param field_name: 字段名
  152. :param row: 行号
  153. :return: None
  154. """
  155. self.page.locator(BodyPageLocator.CHECKBOX_FIELD_ARGS_LOC % (field_name, row-1)).click()
  156. self._click_blank()
  157. def set_text_in_body_head(self, field_name:str, value:str):
  158. """
  159. 单据体中单据头形式文本字段赋值
  160. 如:销售订单->物料数据->面料规格
  161. :param field_name: 字段名
  162. :param value: 字段值
  163. :return: None
  164. """
  165. self.page.locator(BodyPageLocator.TEXT_FIELD_IN_BODY_HEAD_ARGS_LOC % field_name).fill(value)
  166. self._click_blank()
  167. def set_select_in_body_head(self, field_name:str, value:str):
  168. """
  169. 单据体中单据头形式下拉列表字段赋值
  170. 如:销售订单->物料数据->货主类型
  171. :param field_name: 字段名
  172. :param value: 选项值
  173. :return: None
  174. """
  175. self.page.locator(BodyPageLocator.SELECT_FIELD_IN_BODY_HEAD_ARGS_LOC % field_name).click()
  176. self.page.locator(BodyPageLocator.SELECT_ITEM_ARGS_LOC % value).click()
  177. self._click_blank()
  178. def set_base_in_body_head(self, field_name:str, value:str):
  179. """
  180. 单据体中单据头形式基础资料字段赋值
  181. 如:销售订单->物料数据->面料来源
  182. :param field_name: 字段名
  183. :param value: 选项值
  184. :return: None
  185. """
  186. self.page.locator(BodyPageLocator.BASE_FIELD_IN_BODY_HEAD_SPAN_ARGS_LOC % field_name).click()
  187. self.page.locator(BodyPageLocator.BASE_FIELD_IN_BODY_HEAD_ARGS_LOC % field_name).fill(value)
  188. self.page.locator(BodyPageLocator.BASE_FIELD_IN_BODY_HEAD_ARGS_LOC % field_name).press_sequentially(" ")
  189. self.page.locator(BodyPageLocator.BASE_ITEM_ARGS_LOC % value).click()
  190. self._click_blank()
  191. def get_text_in_body_head(self, field_name:str, order:int=1):
  192. """
  193. 单据体中单据头形式文本字段取值
  194. 如:销售订单->物料数据->面料规格
  195. :param field_name: 字段名
  196. :param order: 可选参数,存在同名字段时指明第几个,从1开始
  197. :return: 字段值
  198. """
  199. # 字段值可能存在1. input中,2.span或其他元素中,目标元素不是input时input_value()方法报错,捕获并换为inner_text()获取
  200. try:
  201. return self.page.locator(BodyPageLocator.TEXT_VALUE_IN_BODY_HEAD_ARGS_LOC % (field_name, field_name, order)).input_value()
  202. except Error:
  203. return self.page.locator(BodyPageLocator.TEXT_VALUE_IN_BODY_HEAD_ARGS_LOC % (field_name, field_name, order)).inner_text()
  204. def get_select_in_body_head(self, field_name:str, order:int=1):
  205. """
  206. 单据体中单据头形式下拉列表字段取值
  207. 如:销售订单->物料数据->货主类型
  208. :param field_name: 字段名
  209. :param order: 可选参数,存在同名字段时指明第几个,从1开始
  210. :return: 字段值
  211. """
  212. return self.page.locator(BodyPageLocator.SELECT_VALUE_IN_BODY_HEAD_ARGS_LOC % (field_name, order)).inner_text()
  213. def get_base_in_body_head(self, field_name:str, order:int=1):
  214. """
  215. 单据体中单据头形式基础资料字段取值
  216. 如:销售订单->物料数据->面料来源
  217. :param field_name: 字段名
  218. :param order: 可选参数,存在同名字段时指明第几个,从1开始
  219. :return: 字段值
  220. """
  221. return self.page.locator(BodyPageLocator.BASE_VALUE_IN_BODY_HEAD_ARGS_LOC % (field_name, order)).inner_text()