今天在写IOS UI自动化脚本,启动页有两张图需要滑动,第一反应就是想到了swipe函数。
于是就写了下面代码:
第一段是获取屏幕尺寸,由于测试机并不固定,所以还是建议动态去获取屏幕尺寸,避免换个手机测试就要更改代码
telPa = self.driver.get_window_size() self.width = telPa["width"] self.height = telPa["height"]
然后就是获取坐标移动
x1 =
int
(
self
.width *
3
/
4
)
y1 =
int
(
self
.height *
1
/
2
)
x2 =
int
(
self
.width *
1
/
4
)
self
.driver.swipe(x1, y1, x2, y1
,
500
)
然后运行,我发现了什么。。。。无论是x1到x2, 还是x2移动到x1,都是从左向右。。。
点击源码,查看如下
# convenience method added to Appium (NOT Selenium 3) def swipe(self, start_x, start_y, end_x, end_y, duration=None): """Swipe from one point to another point, for an optional duration. :Args: - start_x - x-coordinate at which to start - start_y - y-coordinate at which to start - end_x - x-coordinate at which to stop - end_y - y-coordinate at which to stop - duration - (optional) time to take the swipe, in ms. :Usage: driver.swipe(100, 100, 100, 400) """ # `swipe` is something like press-wait-move_to-release, which the server # will translate into the correct action action = TouchAction(self) action \ .press(x=start_x, y=start_y) \ .wait(ms=duration) \ .move_to(x=end_x, y=end_y) \ .release() action.perform() return self # convenience method added to Appium (NOT Selenium 3) def flick(self, start_x, start_y, end_x, end_y): """Flick from one point to another point. :Args: - start_x - x-coordinate at which to start - start_y - y-coordinate at which to start - end_x - x-coordinate at which to stop - end_y - y-coordinate at which to stop :Usage: driver.flick(100, 100, 100, 400) """ action = TouchAction(self) action \ .press(x=start_x, y=start_y) \ .move_to(x=end_x, y=end_y) \ .release() action.perform() return self
两个函数试过之后,依旧如此,心塞塞。。。
搜索中,意外发现一条消息:
在appium1.5版本以下,swipe的方法中的end_x和end_y是实际要滑动的目的地坐标
但是在1.5版本以上的,end_x和end_y是相对于前面start_x和start_y坐标的偏移量。
更改代码,
x1 = int(self.width * 3/4) y1 = int(self.height * 1/2) x2 =tst.changeInt(x1) print x1,x2 self.driver.swipe(x1, y1, x2, 0,500)
运行OK
无fuck说