我在编写用于启动量角器测试的npm脚本时遇到了麻烦。 文档https://github.com/angular/protractor/blob/master/docs/server-setup.md之后似乎首先要运行
./node_modules/.bin/protractor test/integration/conf.js
并在我开始实际测试时保持运行
"scripts": {
"protractor-update": "./node_modules/.bin/webdriver-manager update",
"protractor-start": "./node_modules/.bin/webdriver-manager start",
"protractor-init": "npm run protractor-update && npm run protractor-start",
"test-protractor": "npm run protractor-start && ./node_modules/.bin/protractor test/integration/conf.js",
},
我希望这两个步骤在一个npm脚本中执行,所以我最终会得到这样的结果:
./node_modules/.bin/webdriver-manager start
现在显然问题是> FATAL EXCEPTION: main
> java.lang.RuntimeException: Unable to start activity
> ComponentInfo{com.myapp/com.myapp.MainActivity}:
> java.lang.NullPointerException
> at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
> at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
> at android.app.ActivityThread.access$600(ActivityThread.java:141)
> at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
> at android.os.Handler.dispatchMessage(Handler.java:99)
> at android.os.Looper.loop(Looper.java:137)
> at android.app.ActivityThread.main(ActivityThread.java:5041)
> at java.lang.reflect.Method.invokeNative(Native Method)
> at java.lang.reflect.Method.invoke(Method.java:511)
> at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
> at dalvik.system.NativeStart.main(Native Method)
> Caused by: java.lang.NullPointerException
> at
> com.myapp.frags.FirstFragment.onCreateView(FirstFragment.java:91)
> at android.app.Fragment.performCreateView(Fragment.java:1695)
> at
> android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
> at
> android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
> at android.app.BackStackRecord.run(BackStackRecord.java:682)
> at
> android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
> at android.app.Activity.performStart(Activity.java:5113)
> at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
> at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
> at android.app.ActivityThread.access$600(ActivityThread.java:141)
> at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
> at android.os.Handler.dispatchMessage(Handler.java:99)
> at android.os.Looper.loop(Looper.java:137)
> at android.app.ActivityThread.main(ActivityThread.java:5041)
> at java.lang.reflect.Method.invokeNative(Native Method)
> at java.lang.reflect.Method.invoke(Method.java:511)
> at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
> at dalvik.system.NativeStart.main(Native Method)
**My Fragment layout:**
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/first_frg_layout"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mRelative"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
>
<android.support.design.widget.CoordinatorLayout
android:layout_height="match_parent"
android:layout_width="match_parent">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.melnykov.fab.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="16dp"
android:src="@drawable/ic_done"
fab:fab_colorNormal="@color/primary"
/>
</FrameLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>
没有返回退出代码,所以它永远不会发出下一个命令。
实现这一目标的正确方法是什么?
答案 0 :(得分:4)
我和你有同样的问题。这是我在浏览StackOverflow并在同事的帮助下找到的解决方案。
在package.json中:
"scripts": {
"e2e": "bash run-e2e-tests.sh"
}
bash文件:
# Start selenium server and trash the verbose error messages from webdriver
webdriver-manager start 2>/dev/null &
# Wait 3 seconds for port 4444 to be listening connections
while ! nc -z 127.0.0.1 4444; do sleep 3; done
# run protractor
protractor test/e2e/conf.js
答案 1 :(得分:1)
答案在于shell脚本基础知识。
使用&amp;
将webdriver放在后台所以例如
webdriver-manager start &
这将运行webdriver-manager,然后立即将控制权返回给shell,允许您输入后续命令。
您需要在通过某种机制(例如kill %1
或pkill webdriver-manager
)完成测试后终止webdriver-manager。否则你最终会有几十个未使用的webdriver进程在运行...
答案 2 :(得分:0)
您可以同时使用 解决您的问题。
使用以下命令同时安装。
npm install concurrently --save
然后在package.json中使用,如下所示:
"e2e:all-steps": "concurrently -k -s first \"./node_modules/.bin/webdriver-manager update\" \"./node_modules/.bin/webdriver-manager start\" \"npm run protractor-update && npm run protractor-start\" \"npm run protractor-start && ./node_modules/.bin/protractor test/integration/conf.js\""
希望有帮助。 :-)