def test_xpath(app: wda.Client): with app: app.xpath("//*[@label='蓝牙']").click() assert app.xpath('//*[@label="设置"]').wait() assert app.xpath('//*[@label="设置"]').get().label == "设置" # test __getattr__ assert app.xpath('//*[@label="设置"]').label == "设置"
def test_session_orientation(c: wda.Client): c.orientation = wda.PORTRAIT with c.session('com.apple.mobilesafari') as s: assert s.orientation == wda.PORTRAIT s.orientation = wda.LANDSCAPE time.sleep(1.0) assert s.orientation == wda.LANDSCAPE # recover orientation s.orientation = wda.PORTRAIT
def test_send_keys_callback(c: wda.Client): def _handle_alert_before_send_keys(client: wda.Client, urlpath: str): if not urlpath.endswith("/wda/keys"): return if client.alert.exists: client.alert.accept() print("callback called") c.register_callback(wda.Callback.HTTP_REQUEST_BEFORE, _handle_alert_before_send_keys) c.send_keys("hello callback")
def test_error_callback(c: wda.Client): def err_handler(client: wda.Client, err): if isinstance(err, wda.WDARequestError): print("ERROR:", err) return wda.Callback.RET_ABORT # 直接退出 return wda.Callback.RET_CONTINUE # 忽略错误继续执行 return wda.Callback.RET_RETRY # 重试一下 c.register_callback(wda.Callback.ERROR, err_handler) c.send_keys("hello callback")
def test_element_scroll_visible(c: wda.Client): with c.session('com.apple.Preferences') as s: general = s(name='General') assert not general.get().visible general.scroll() assert general.get().visible time.sleep(1)
def test_element_tap_hold(c: wda.Client): s = c.session() s(name='Settings').tap_hold(2.0) assert s( classChain= '**/Icon[`name == "Weather"`]/Button[`name == "DeleteButton"`]').get( 2.0, raise_error=False)
def test_client_session_with_argument(client: wda.Client): """ In mose case, used to open browser with url """ with client.session('com.apple.mobilesafari', ['-u', 'https://www.github.com']) as s: # time.sleep(1.0) # s(id='URL').wait() assert s(name='Share', className="Button").wait()
def test_element_pinch(c: wda.Client): with c.session('com.apple.Maps') as s: def alert_callback(s): s.alert.accept() s.set_alert_callback(alert_callback) s(className='Button', name='Tracking').tap() time.sleep(5)
def make_screenrecord(c: wda.Client, t: tidevice.Device, output_video_path: str): _old_fps = c.appium_settings()['mjpegServerFramerate'] _fps = 10 c.appium_settings({"mjpegServerFramerate": _fps}) # Read image from WDA mjpeg server pconn = t.create_inner_connection(9100) # default WDA mjpeg server port sock = pconn.get_socket() buf = SocketBuffer(sock) buf.write(b"GET / HTTP/1.0\r\nHost: localhost\r\n\r\n") buf.read_until(b'\r\n\r\n') print("screenrecord is ready to begin") wr = imageio.get_writer(output_video_path, fps=_fps) def _drain(stop_event, done_event): while not stop_event.is_set(): # read http header length = None while True: line = buf.read_until(b'\r\n') if line.startswith(b"Content-Length"): length = int(line.decode('utf-8').split(": ")[1]) break while True: if buf.read_until(b'\r\n') == b'': break imdata = buf.read_bytes(length) im = imageio.imread(io.BytesIO(imdata)) wr.append_data(im) done_event.set() stop_event = threading.Event() done_event = threading.Event() threading.Thread(target=_drain, args=(stop_event, done_event), daemon=True).start() yield stop_event.set() done_event.wait() wr.close() c.appium_settings({"mjpegServerFramerate": _old_fps}) print("Output file:", output_video_path)
def test_text_contains_matches(c: wda.Client): with c.session('com.apple.Preferences') as s: s(text='Bluetooth').get() assert s(textContains="Blue").exists assert not s(text="Blue").exists assert s(text="Bluetooth").exists assert s(textMatches="Blue?").exists assert s(nameMatches="Blue?").exists assert not s(textMatches="^lue?").exists assert not s(textMatches="^Blue$").exists assert s(textMatches=r"^(Blue|Red).*").exists
def test_preferences(c: wda.Client): print("Status:", c.status()) print("Info:", c.info) print("BatteryInfo", c.battery_info()) print("AppCurrent:", c.app_current()) # page_source = c.source() # assert "</XCUIElementTypeApplication>" in page_source app = c.session(bundle_id) selector = app(label="蜂窝网络") el = selector.get() el.click() print("Element bounds:", el.bounds) logger.info("Take screenshot: %s", app.screenshot()) app.swipe_right() app.swipe_up() app(label="电池").scroll() app(label="电池").click()
def test_element_properties(c: wda.Client): with c.session('com.apple.mobilesafari', ['-u', 'https://www.github.com']) as s: time.sleep(1.0) u = None for e in s(id='URL').find_elements(): if e.className.endswith('Button'): u = e break assert u.label == 'Address' assert 'github' in u.value.lower() assert u.displayed == True assert u.visible == True assert u.enabled == True assert type(u.bounds) is wda.Rect u.clear_text() u.set_text('status.github.com\n') assert 'status' in u.value
def test_client_status(client: wda.Client): """ Example response { "state": "success", "os": { "version": "10.3.3", "name": "iOS" }, "ios": { "ip": "192.168.2.85", "simulatorVersion": "10.3.3" }, "build": { "time": "Aug 8 2017 17:06:05" }, "sessionId": "xx...x.x.x.x.x.x" # added by python code } """ st = client.status() # json value assert st['state'] == 'success'
def test_callback(app: wda.Client): event = threading.Event() def _cb(client: wda.Client, url: str): if url.endswith("/sendkeys"): pass assert isinstance(client, wda.Client) assert url.endswith("/status") event.set() app.register_callback(wda.Callback.HTTP_REQUEST_BEFORE, _cb) # app.register_callback(wda.Callback.HTTP_REQUEST_AFTER, lambda url, response: print(url, response)) app.status() assert event.is_set(), "callback is not called" # test remove_callback event.clear() app.unregister_callback(wda.Callback.HTTP_REQUEST_BEFORE, _cb) app.status() assert not event.is_set(), "callback is should not be called" event.clear() app.unregister_callback(wda.Callback.HTTP_REQUEST_BEFORE) app.status() assert not event.is_set(), "callback is should not be called" event.clear() app.unregister_callback() app.status() assert not event.is_set(), "callback is should not be called"
def test_invalid_session(c: wda.Client): app = c.session("com.apple.Preferences") # kill app here app.session_id = "no-exists" app(label="Haha").exists
def test_xpath(c: wda.Client): c.xpath("//Window/Other/Other").exists
def test_element_scroll_direction(c: wda.Client): with c.session('com.apple.Preferences') as s: s(className='Table').scroll('up', 0.1)
def test_element_name_matches(c: wda.Client): s = c.session("com.apple.Preferences") assert s(nameMatches='^蓝牙').exists info = s(nameMatches='^蓝牙').info assert info['label'] == '蓝牙'
def test_client_screenshot(client: wda.Client): wda.DEBUG = False client.screenshot()
def test_client_session_without_argument(client: wda.Client): s = client.session('com.apple.Health') session_id = client.status()['sessionId'] assert s.id == session_id assert s.bundle_id == 'com.apple.Health' s.close()
def test_app_operation(c: wda.Client): c.session("com.apple.Preferences") appinfo = c.app_current() assert appinfo['bundleId'] == 'com.apple.Preferences'
def test_open_safari(c: wda.Client): """ session操作 """ app = c.session("com.apple.mobilesafari") app.deactivate(3) # 后台3s app.close() # 关闭
def test_main(c: wda.Client): t = tidevice.Device() with make_screenrecord(c, t, "output.mp4"): app = c.session("com.apple.Preferences") app(label="蓝牙").click() c.sleep(1)
def test_session_invalid_with_autofix(c: wda.Client): c.session("com.apple.Preferences") c.session_id = "123" assert c.app_current().bundleId == "com.apple.Preferences" assert isinstance(c.info, dict) assert c.session_id != "123"