Appium 客户端类库

发表于:2018-10-09 10:30

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:煦色韶光    来源:51testing采编


  当前包名(package)
  获取当前包名(package)。仅支持 Android 。
  # ruby
  current_package
  # python
  driver.current_package
  // java
  driver.getCurrentPackage();
  // javascript
  driver.getCurrentPackage().then(function (package) { /*...*/ })
  // php
  $this->currentPackage();
  // c#
  driver.GetCurrentPackage();
  点击操作 / 多点触控操作
  用于生成点击操作的 API。这部分文档的内容将会很快被补充进来。
  # ruby
  touch_action = Appium::TouchAction.new
  element  = find_element :accessibility_id, 'Buttons, Various uses of UIButton'
  touch_action.press(element: element, x: 10, y: 10).perform
  # python
  action = TouchAction(driver)
  action.press(element=el, x=10, y=10).release().perform()
  // java
  TouchAction action = new TouchAction(driver)
  .press(mapview, 10, 10)
  .release().
  perform();
  // javascript
  var action = new wd.TouchAction(driver);
  action
  .tap({el: el, x: 10, y: 10})
  .release();
  return action.perform(); // returns a promise
  // php
  $action = $this->initiateTouchAction();
  ->press(array('element' => $el))
  ->release()
  ->perform();
  $action1 = $this->initiateTouchAction();
  $action1->press(array('element' => $els[0]))
  ->moveTo(array('x' => 10, 'y' => 0))
  ->moveTo(array('x' => 10, 'y' => -75))
  ->moveTo(array('x' => 10, 'y' => -600))
  ->release();
  $action2 = $this->initiateTouchAction();
  $action2->press(array('element' => $els[1]))
  ->moveTo(array('x' => 10, 'y' => 10))
  ->moveTo(array('x' => 10, 'y' => -300))
  ->moveTo(array('x' => 10, 'y' => -600))
  ->release();
  $multiAction = $this->initiateMultiAction();
  $multiAction->add($action1);
  $multiAction->add($action2);
  $multiAction->perform();
  // c#
  ITouchAction action = new TouchAction(driver);
  action.Press(el, 10, 10).Release();
  action.Perform ();

  滑动屏幕
  模拟用户滑动屏幕的操作。
  # ruby
  swipe start_x: 75, start_y: 500, end_x: 75, end_y: 0, duration: 0.8
  # python
  driver.swipe(start_x=75, start_y=500, end_x=75, end_y=0, duration=800)
  // java
  driver.swipe(75, 500, 75, 0, 0.8)
  // javascript
  function swipe(opts) {
  var action = new wd.TouchAction(this);
  action
  .press({x: opts.startX, y: opts.startY})
  .wait(opts.duration)
  .moveTo({x: opts.endX, y: opts.endY})
  .release();
  return action.perform();
  }
  wd.addPromiseChainMethod('swipe', swipe);
  // ...
  return driver.swipe({ startX: 75, startY: 500,
  endX: 75,  endY: 0, duration: 800 });
  // php
  $this->swipe(75, 500, 75, 0, 800);
  // c#
  todo: c#
  捏(Pinch)手势
  在屏幕上使用捏(Pinch)手势。
  # ruby
  pinch 75
  # python
  driver.pinch(element=el)
  // java
  driver.pinch(element);
  // javascript
  function pinch(el) {
  return Q.all([
  el.getSize(),
  el.getLocation(),
  ]).then(function(res) {
  var size = res[0];
  var loc = res[1];
  var center = {
  x: loc.x + size.width / 2,
  y: loc.y + size.height / 2
  };
  var a1 = new wd.TouchAction(this);
  a1.press({el: el, x: center.x, y:center.y - 100}).moveTo({el: el}).release();
  var a2 = new wd.TouchAction(this);
  a2.press({el: el, x: center.x, y: center.y + 100}).moveTo({el: el}).release();
  var m = new wd.MultiAction(this);
  m.add(a1, a2);
  return m.perform();
  }.bind(this));
  };
  wd.addPromiseChainMethod('pinch', pinch);
  wd.addElementPromiseChainMethod('pinch', function() {
  return this.browser.pinch(this);
  });
  // ...
  return driver.pinch(el);
  // ...
  return el.pinch();
  $this->pinch($el);
  // c#
  driver.Pinch(25, 25)
  放大屏幕(Zoom)
  在屏幕上使用放大手势。
  # ruby
  zoom 200
  # python
  driver.zoom(element=el)
  // java
  driver.zoom(element);
  // javascript
  function zoom(el) {
  return Q.all([
  this.getWindowSize(),
  this.getLocation(el),
  ]).then(function(res) {
  var size = res[0];
  var loc = res[1];
  var center = {
  x: loc.x + size.width / 2,
  y: loc.y + size.height / 2
  };
  var a1 = new wd.TouchAction(this);
  a1.press({el: el}).moveTo({el: el, x: center.x, y: center.y - 100}).release();
  var a2 = new wd.TouchAction(this);
  a2.press({el: el}).moveTo({el: el, x: center.x, y: center.y + 100}).release();
  var m = new wd.MultiAction(this);
  m.add(a1, a2);
  return m.perform();
  }.bind(this));
  };
  wd.addPromiseChainMethod('zoom', zoom);
  wd.addElementPromiseChainMethod('zoom', function() {
  return this.browser.zoom(this);
  });
  // ...
  return driver.zoom(el);
  // ...
  return el.zoom();
  // php
  $this->zoom($el);
  // c#
  driver.Zoom(100, 200);
  滚动到
  滚动到指定的元素。
  # ruby
  element = find_element :accessibility_id, "Element ID"
  execute_script "mobile: scroll", direction: "down", element: element.ref
  # python
  driver.execute_script("mobile: scroll", {"direction": "down", "element": element.id})
  // java
  JavascriptExecutor js = (JavascriptExecutor) driver;
  HashMap<String, String> scrollObject = new HashMap<String, String>();
  scrollObject.put("direction", "down");
  scrollObject.put("element", ((RemoteWebElement) element).getId());
  js.executeScript("mobile: scroll", scrollObject);
  // javascript
  return driver.elementByAccessibilityId().then(function (el) {
  driver.execute("mobile: scroll", [{direction: "down", element: el.value}]);
  });
  // php
  $els = $this->elements($this->using('class name')->value('android.widget.TextView'));
  $this->scroll($els[count($els) - 1], $els[0]);
  // c#
  Dictionary<string, string> scrollObject = new Dictionary<string, string>();
  scrollObject.Add("direction", "down");
  scrollObject.Add("element", <element_id>);
  ((IJavaScriptExecutor)driver).ExecuteScript("mobile: scroll", scrollObject));
  拉取(pull)文件
  从设备上拉取文件。
  # ruby
  pull_file 'Library/AddressBook/AddressBook.sqlitedb'
  # python
  driver.pull_file('Library/AddressBook/AddressBook.sqlitedb')
  // java
  driver.pullFile("Library/AddressBook/AddressBook.sqlitedb");
  // javascript
  driver.pullFile("Library/AddressBook/AddressBook.sqlitedb")
  .then(function (base64File) { /*...*/ })
  // php
  $this->pullFile('Library/AddressBook/AddressBook.sqlitedb');
  // c#
  driver.PullFile("Library/AddressBook/AddressBook.sqlitedb");
  推送(push)文件
  推送文件到设备。
  # ruby
  data = "some data for the file"
  path = "/data/local/tmp/file.txt"
  push_file path, data
  # python
  data = "some data for the file"
  path = "/data/local/tmp/file.txt"
  driver.push_file(path, data.encode('base64'))
  // java
  byte[] data = Base64.encodeBase64("some data for the file".getBytes());
  String path = "/data/local/tmp/file.txt";
  driver.pushFile(path, data)
  // javascript
  driver.pushFile(path, data)
  // php
  $path = 'data/local/tmp/test_push_file.txt';
  $data = 'This is the contents of the file to push to the device.';
  $this->pushFile($path, base64_encode($data));
  // c#
  driver.PushFile("/data/local/tmp/file.txt", "some data for the file");
  设置
  在这你会找到关于获取或设置 appium 服务器设置的示例代码。如果想了解工作原理,以及支持哪些设置,请查看设置文档
  # ruby
  current_settings = get_settings
  update_settings someSetting: true
  # python
  current_settings = driver.get_settings()
  driver.update_settings({"someSetting": true})
  // java
  JsonObject settings = driver.getSettings()
  // java 客户端不支持设置任意的设置项,只能设置 appium 当前支持的部分。
  // 所以对于 `ignoreUnimportantViews`(译者注:忽略不重要的视图,即 android uiautomator 上的压缩后 xml )这个设置项,对应存在下面这个设置方法:
  driver.ignoreUnimportantViews(true);
  // javascript
  var settings = driver.settings();
  browser.updateSettings({'someSetting': true});
  // php
  $settings = $this->getSettings();
  $this->updateSettings(array('cyberdelia' => "open"));
  // c#
  Dictionary<String, Object>settings = driver.GetSettings();
  // .net 客户端不支持设置任意的设置项,只能设置 appium 当前支持的部分。
  // 所以对于 `ignoreUnimportantViews`(译者注:忽略不重要的视图,即 android uiautomator 上的压缩后 xml )这个设置项,对应存在下面这个设置方法:
  driver.IgnoreUnimportantViews(true);


   上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。
22/2<12
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号