Appium 客戶端類庫
Appium 支持以下語言的客戶端類庫:
語言 | 源碼 |
---|---|
Ruby | GitHub |
Python | GitHub |
Java | GitHub |
JavaScript | GitHub |
PHP | GitHub |
C# | GitHub |
Objective-C | GitHub |
注意稠项,一些方法類似 endTestCoverage()
目前并不能完全支持悴能。當(dāng)這個問題被解決后窃躲,適當(dāng)?shù)母采w率支持才會被添加嫂伞。如果你仍然想使用這些方法方椎,請參考 GitHub 上關(guān)于 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];
將應(yīng)用切換至后臺
將當(dāng)前的應(yīng)用切換到后臺幔亥。
# 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空盼,而且應(yīng)用是允許用戶去使用各種策略去收起鍵盤的,無論是點擊鍵盤以外的區(qū)域新荤,還是向下滑動諸如此類...相比于使用該方法揽趾,我們更加鼓勵你去思考 用戶 在應(yīng)用中是如何收起鍵盤,并告訴 Appium 去執(zhí)行苛骨,代替掉內(nèi)些操作(滑動篱瞎,點擊一個固定的坐標(biāo),等等...)痒芝。話雖如此俐筋,但默認(rèn)的行為是可能是最能幫助到你的。
# 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
在當(dāng)前 app 打開一個 activity严衬,或者新打開一個應(yīng)用并啟動一個 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];
應(yīng)用是否已安裝
檢查應(yīng)用是否已被安裝赠幕。
# 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-"];
安裝應(yīng)用
在設(shè)備上安裝應(yīng)用。
# 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"];
卸載應(yīng)用
移除設(shè)備上的應(yīng)用询筏。
# 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"];
搖一搖
模擬搖晃設(shè)備的操作劣坊。
# ruby
shake
# python
driver.shake()
// java
driver.shake()
// javascript
driver.shake()
// php
$this->shake();
// c#
driver.ShakeDevice();
// objective c
[driver shakeDevice];
關(guān)閉應(yīng)用
關(guān)閉應(yīng)用。
# 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屈留。請注意只有設(shè)置了 autoLaunch=false 關(guān)鍵字時才會生效局冰。使用 start_activity
這個參數(shù)不是為了隨意啟動一個應(yīng)用或 activities。這是在你設(shè)置了 autoLaunch=false 后灌危,用來繼續(xù)執(zhí)行初始化("launch")流程的康二。
# ruby
launch_app
# python
driver.launch_app()
// java
driver.launchApp()
// javascript
driver.launchApp()
// php
$this->launchApp();
// c#
driver.LaunchApp();
// objective c
[driver launchApp];
重置
重置應(yīng)用。
# 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;
當(dāng)前 context
列出當(dāng)前的 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;
切換至默認(rèn)的 context
切換回默認(rèn)的 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];
應(yīng)用的字符串
獲得應(yīng)用的字符串味混。
# 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"];
按鍵事件
給設(shè)備發(fā)送按鍵事件产雹。
# 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];
當(dāng)前 Activity
獲取當(dāng)前的 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蔓挖。這部分文檔的內(nèi)容將會很快被補充進來。
# 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)文件
從設(shè)備上拉出文件拷获。
# 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)文件
推送文件到設(shè)備。
# 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");
設(shè)置
在這你會找到關(guān)于獲取或設(shè)置 appium 服務(wù)器設(shè)置的示例代碼减细。如果想了解工作原理匆瓜,以及支持哪些設(shè)置,請查看設(shè)置文檔
# 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 桌面應(yīng)用
Appium 的桌面應(yīng)用支持 OS X 與 Windows未蝌。