滑屏找元素

在APP测试时,经常需要滑屏,才能找到元素。所以我们把滑屏找元素,然后再操作进行一次封装。

base_action.py中

def scroll_page_one_time(self, direction):

screen_width = self.driver.get_window_size()["width"]
screen_height = self.driver.get_window_size()["height"]
center_x = screen_width * 0.5
center_y = screen_height * 0.5
down_x = center_x
down_y = screen_height * 0.75
up_x = center_x
up_y = screen_height * 0.
left_x = screen_width * 0.25
left_y = center_y
right_x = screen_width * 0.75
right_y = center_y

if direction == "up":
self.driver.swipe(down_x, down_y, up_x, up_y, 3000)
elif direction == "down":
self.driver.swipe(up_x, up_y, down_x, down_y, 3000)
elif direction == "left":
self.driver.swipe(right_x, right_y, left_x, left_y, 3000)
elif direction == "right":
self.driver.swipe(left_x, left_y, right_x, right_y, 3000)
else:
raise Exception("您输入的参数有误,请检查,direction参数必须是up/down/left/right")
def is_loc_exist_scroll_page(self, loc, direction):
page_source = ''
while True:
if self.driver.page_source == page_source:
return False
try:
self.find_element(loc)
return True
except Exception:
page_source = self.driver.page_source
self.scroll_page_one_time(direction)
第一个方法是滑屏,第二个是看页面是否有元素,如果当前页面有,就返回True。否则继续滑,直到滑到页面的尽头
swipe_page.py中
#设置中的测试语言
def click_language(self):
if self.is_loc_exist_scroll_page(self.language_btn, "up"):
self.click(self.ram_btn)
else:
assert False

def click_yuyan(self):
self.click(self.yuyan_btn)

def click_selected(self):
ele_sel = self.is_loc_exist_scroll_page(self.selected_lan_btn, "up")
self.click(self.selected_lan_btn)
test_swipe.py
def test_swipe_language(self):
self.languageSwipe.click_language()
self.languageSwipe.click_yuyan()
self.languageSwipe.click_selected()

所有滑屏找元素都可以用这个模板。
posted @ 2020-04-07 18:11  rosexiang  阅读(226)  评论(0编辑  收藏  举报