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];

将 app 置于后台

把当前应用放到后台去

# 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];

收起键盘

收起键盘

# 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

在当前应用中打开一个 activity 或者启动一个新应用并打开一个 activity 。 只能在 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"];

打开通知栏 (Notifications)

打开下拉通知栏 只能在 Android 上使用

// java
driver.openNotifications();
// javascript
driver.openNotifications(cb);
# python
driver.open_notifications()
# ruby
openNotifications
// 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"];

摇晃 (Shake)

模拟设备摇晃

# 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 关键字时才能生效。这不是用于启动指定的 app/activities ————你可以使用 start_activity 做到这个效果————这是用来继续进行使用了 autoLaunch=false 关键字时的初始化 (Launch) 流程的。

# ruby
launch
# python
driver.launch_app()
// java
driver.launchApp()
// javascript
driver.launchApp()
// php
$this->launchApp();
// c#
driver.LaunchApp();
// objective c
[driver launchApp];

重置 (Reset)

应用重置

(翻译者注:相当于卸载重装应用)

# ruby
reset
# python
driver.reset()
// java
driver.resetApp()
// javascript
driver.resetApp()
// php
$this->reset();
// c#
driver.ResetApp();
// objective c
[driver resetApp];

可用上下文 (context)

列出所有的可用上下文

翻译备注:context可以理解为 可进入的窗口 。例如,对于原生应用,可用的context和默认context均为NATIVE_APP。详情可查看对混合应用进行自动化测试

# 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)

列出当前上下文

# 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)

将上下文切换到默认上下文

# 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];

应用的字符串 (App Strings)

获取应用的字符串

# ruby
strings = 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"];

按键事件 (Key Event)

给设备发送一个按键事件

# 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

获取当前 activity。只能在 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];

触摸动作(TouchAction) / 多点触摸动作(MultiTouchAction)

生成触摸动作的接口。这部分文档很快将会补充更多的内容进来。

# 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 ();

滑动(Swipe)

模拟用户滑动

# ruby
swipe start_x: 75, start_y: 500, end_x: 75, end_y: 0, duration: 0.8
# python
driver.swipe(start=75, starty=500, endx=75, endy=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)

捏屏幕 (双指往内移动来缩小屏幕)

# 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);

滑动到 (Scroll To)

滑动到某个元素。

# ruby
element = find_element :name, 'Element Name'
execute_script "mobile: scrollTo", :element => element.ref
# python
todo: python
// java
WebElement element = driver.findElement(By.name("Element Name"));
HashMap<String, String> arguments = new HashMap<String, String>();
arguments.put("element", element.getId());
(JavascriptExecutor)driver.executeScript("mobile: scrollTo", arguments);
// javascript
return driver.elementByName().then(function (el) {
return driver.execute('mobile: scrollTo', {element: el.value});
});
// php
$els = $this->elements($this->using('class name')->value('android.widget.TextView'));
$this->scroll($els[count($els) - 1], $els[0]);
// c#
todo: csharp

拉出文件 (Pull File)

从设备中拉出文件

# 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 file)

推送文件到设备中去

# 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 的服务器设置。 想知道它如何工作,以及它支持哪些设置,请查看关于设置的文档

current_settings = get_settings
update_settings someSetting: true
current_settings = driver.get_settings()
driver.update_settings({"someSetting": true})
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);
var settings = driver.settings();
browser.updateSettings({'someSetting': true});
$settings = $this->getSettings();
$this->updateSettings(array('cyberdelia' => "open"));
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.