首先,將CordovaLib複製到您的插件項目作爲庫。 添加類似以下內容到您的Test類:
首先,一個活動,不要忘記將它添加到AndroidMainfest.xml進行測試。
public class TestActivity extends CordovaActivity {
CordovaWebView getWebView(){
return this.appView;
}
CordovaInterface getInterface(){
return this.cordovaInterface;
}
public void init(){
super.init();
}
}
在您的測試類:
@Rule
public ActivityTestRule<CordovaActivity> mActivityRule =
new ActivityTestRule<CordovaActivity>(CordovaActivity.class);
private CordovaWebView wv;
private CordovaInterface cordovaInterface;
private YourCordovaPlugin km;
private TestActivity activity;
@Before
public void Init() throws InterruptedException {
km = new KM1930CordovaPlugin();
activity = mActivityRule.getActivity();
Runnable action = new Runnable() {
@Override
public void run() {
activity.init();
wv = mActivityRule.getActivity().getWebView();
cordovaInterface = mActivityRule.getActivity().getInterface();
km = new YourCordovaPlugin();
km.initialize(cordovaInterface,wv);
wv.getPluginManager().addService(new PluginEntry("YourServiceName",km));
synchronized (this) {
this.notify();
}
}
};
synchronized(action) {
activity.runOnUiThread(action);
action.wait() ;
}
}
然後你就可以通過調用execute
方法添加你的測試方法和調用方法。
@Test
public void TestBluetooth() throws JSONException {
this.km.execute("echo","[]",new CallbackContext("0",wv));
}
找到方法有什麼好運? –
@EaswaramoorthyK No.僅限使用cordova-plugin-test-framework。 – vrs
好的。這很好,但無法找到代碼覆蓋率或將測試結果作爲報告導出。 –