Appium 客户端类库
Appium 支持以下语言的客户端类库:
语言 | 源码 |
---|---|
Ruby | GitHub |
Python | GitHub |
Java | GitHub |
JavaScript | GitHub |
PHP | GitHub |
C# | GitHub |
Objective-C | GitHub |
注意,一些方法类似 endTestCoverage()
目前并不能完全支持。当这个问题被解决后,适当的覆盖率支持才会被添加。如果你仍然想使用这些方法,请参考 GitHub 上关于 bindings 的文档。
锁定
锁定屏幕。
# ruby
lock 5
# python
driver.lock(5)
// java
driver.lockScreen(3);
// javascript
driver.lock(3)
// php
$this->lock(3);
// c#
driver.LockDevice(3);
// objective c
[driver lockDeviceScreen:3];
将应用切换至后台
将当前的应用切换到后台。
# ruby
background_app 5
# python
driver.background_app(5)
// java
driver.runAppInBackground(5);
// javascript
driver.backgroundApp(5)
// php
$this->backgroundApp(5);
// c#
driver.BackgroundApp(5);
// objective c
[driver runAppInBackground:3];
收起键盘
收起键盘。
注意: 在 iOS,这辅助功能并不能保证一定有效。因为没有用于隐藏键盘的 automation hook,而且应用是允许用户去使用各种策略去收起键盘的,无论是点击键盘以外的区域,还是向下滑动诸如此类...相比于使用该方法,我们更加鼓励你去思考 用户 在应用中是如何收起键盘,并告诉 Appium 去执行,代替掉内些操作(滑动,点击一个固定的坐标,等等...)。话虽如此,但默认的行为是可能是最能帮助到你的。
# ruby
hide_keyboard
# python
driver.hide_keyboard()
// java
driver.hideKeyboard();
// javascript
driver.hideKeyboard()
// php
$this->hideKeyboard();
$this->hideKeyboard(array('strategy' => 'pressKey', 'key' => 'Done'));
// c#
driver.HideKeyboard("Done");
// objective c
[driver hideKeyboard];
启动 Activity
在当前 app 打开一个 activity,或者新打开一个应用并启动一个 acticity, 仅支持 Android。
// java
driver.startActivity("appPackage","com.example.android.apis", null, null);
// javascript
driver.startActivity({appPackage: 'com.example.android.apis', appActivity: '.Foo'}, cb);
# python
driver.start_activity('com.example.android.apis', '.Foo')
# ruby
start_activity app_package: 'io.appium.android.apis', app_activity: '.accessibility.AccessibilityNodeProviderActivity'
// c#
driver.StartActivity("com.example.android.apis", ".Foo");
// php
$this->startActivity(array("appPackage" => "com.example.android.apis",
"appActivity" => ".Foo"));
// objective c
[driver startActivity:@"com.example.android.apis" package:@".Foo"];
打开通知栏
打开通知栏,仅支持 Android。
// java
driver.openNotifications();
// javascript
driver.openNotifications(cb);
# python
driver.open_notifications()
# ruby
open_notifications
// c#
driver.OpenNotifications();
// php
$this->openNotifications();
// objective c
[driver openNotifications];
应用是否已安装
检查应用是否已被安装。
# ruby
is_installed? "com.example.android.apis"
# python
driver.is_app_installed('com.example.android.apis')
// java
driver.isAppInstalled("com.example.android.apis")
// javascript
driver.isAppInstalled("com.example.android.apis")
.then(function (isAppInstalled) { /*...*/ })
// php
$this->isAppInstalled('com.example.android.apis');
// c#
driver.IsAppInstalled("com.example.android.apis-");
// objective c
[driver isAppInstalled:@"com.example.android.apis-"];
安装应用
在设备上安装应用。
# ruby
install 'path/to/my.apk'
# python
driver.install_app('path/to/my.apk')
// java
driver.installApp("path/to/my.apk")
// javascript
driver.installApp("path/to/my.apk")
// php
$this->installApp('path/to/my.apk');
// c#
driver.InstallApp("path/to/my.apk");
// objective c
[driver installAppAtPath:@"path/to/my.apk"];
卸载应用
移除设备上的应用。
# ruby
remove 'com.example.android.apis'
# python
driver.remove_app('com.example.android.apis')
// java
driver.removeApp("com.example.android.apis")
// javascript
driver.removeApp("com.example.android.apis")
// php
$this->removeApp('com.example.android.apis');
// c#
driver.RemoveApp("com.example.android.apis");
// objective c
[driver removeApp:@"com.example.android.apis"];
摇一摇
模拟摇晃设备的操作。
# ruby
shake
# python
driver.shake()
// java
driver.shake()
// javascript
driver.shake()
// php
$this->shake();
// c#
driver.ShakeDevice();
// objective c
[driver shakeDevice];
关闭应用
关闭应用。
# ruby
close_app
# python
driver.close_app();
// java
driver.closeApp()
// javascript
driver.closeApp()
// php
$this->closeApp();
// c#
driver.CloseApp();
// objective c
[driver closeApp];
启动(Launch)
为 desired capabilities 启动一个 session。请注意只有设置了 autoLaunch=false 关键字时才会生效。使用 start_activity
这个参数不是为了随意启动一个应用或 activities。这是在你设置了 autoLaunch=false 后,用来继续执行初始化("launch")流程的。
# ruby
launch_app
# python
driver.launch_app()
// java
driver.launchApp()
// javascript
driver.launchApp()
// php
$this->launchApp();
// c#
driver.LaunchApp();
// objective c
[driver launchApp];
重置
重置应用。
# ruby
driver.reset
# python
driver.reset()
// java
driver.resetApp()
// javascript
driver.resetApp()
// php
$this->reset();
// c#
driver.ResetApp();
// objective c
[driver resetApp];
可用的 Contexts
列出所有可用的 contexts。
# ruby
context_array = available_contexts
# python
driver.contexts
// java
driver.getContextHandles()
// javascript
driver.contexts().then(function (contexts) { /*...*/ })
// php
$this->contexts();
// c#
driver.GetContexts()
// objective c
NSArray *contexts = driver.allContexts;
当前 context
列出当前的 context。
# ruby
context = current_context
# python
driver.current_context
// java
driver.getContext()
// javascript
driver.currentContext().then(function (context) { /*...*/ })
// php
$this->context();
// c#
driver.GetContext()
// objective c
NSString *context = driver.context;
切换至默认的 context
切换回默认的 context。
# ruby
switch_to_default_context
# python
driver.switch_to.context(None)
// java
driver.context();
// javascript
driver.context()
// php
$this->context(NULL);
// c#
driver.SetContext();
// objective c
[driver setContext:nil];
应用的字符串
获得应用的字符串。
# ruby
app_strings
# python
driver.app_strings
// java
driver.getAppStrings();
// javascript
driver.getAppStrings().then(function (appStrings) { /*...*/ })
// php
$this->appStrings();
$this->appStrings('ru');
// c#
driver.GetAppStrings();
// objective c
[driver appStrings];
[driver appStringsForLanguage:"@ru"];
按键事件
给设备发送按键事件。
# ruby
key_event 176
# python
driver.keyevent(176)
// java
driver.sendKeyEvent(AndroidKeyCode.HOME);
// javascript
driver.deviceKeyEvent(wd.SPECIAL_KEYS.Home)
// php
$this->keyEvent('176');
// c#
driver.KeyEvent("176");
// objective c
NSError *err;
[driver triggerKeyEvent:176 metastate:0 error:&err];
当前 Activity
获取当前的 Acticity,但仅支持 Android。
# ruby
current_activity
# python
driver.current_activity
// java
driver.currentActivity();
// javascript
driver.getCurrentActivity().then(function (activity) { /*...*/ })
// php
$this->currentActivity();
// c#
driver.GetCurrentActivity();
// objective c
NSError *err;
[driver currentActivity];
点击操作 / 多点触控操作
用于生成点击操作的 API。这部分文档的内容将会很快被补充进来。
# ruby
touch_action = Appium::TouchAction.new
element = find_element :name, '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 :name, "Element Name"
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.elementByName().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-client doesn't support setting arbitrary settings, just settings which are already provided by appium.
// So for the 'ignoreUnimportantViews' setting, the following method exists:
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();
// dotnet-driver doesn't support setting arbitrary settings, just settings which are already provided by appium.
// So for the 'ignoreUnimportantViews' setting, the following method exists:
driver.IgnoreUnimportantViews(true);
Appium 桌面应用
Appium 的桌面应用支持 OS X 与 Windows。
网友评论