stanfy / spoon-gradle-plugin

Gradle plugin for Spoon.
Apache License 2.0
353 stars 79 forks source link

Passing command line arguments with spoon #160

Closed pawankums closed 7 years ago

pawankums commented 7 years ago

Hi, I have an android automation framework using Espresso, Cucumber and Spoon. I run the tests using ./gradlew spoon command. I need to pass some argument to tests as userid & password. Not sure how to pass it. Tried to do so using ./gradlew spoon -DuserId="QAA_User1" -Dpassword="Pass123" in test referred these arguments using System.getProperty("userId") but it did not work. Test don't move further after System.getProperty("userId") statement. If anyone has worked on this scenario, plz help me out.

Thanks

artem-zinnatullin commented 7 years ago

You can pass such parameters as arguments for instrumentation runner https://github.com/stanfy/spoon-gradle-plugin#custom-instrumentation-arguments

pawankums commented 7 years ago

@artem-zinnatullin Thanks for your reply. I had looked it in the past but could not figure out how to use it. Could you please guide me how to use it in my case, i need to pass userId and password through command line. correct me if i am wrong in the steps to implement it

  1. Need to put in build.gradle spoon { instrumentationArgs = ["userId=qaa", "password=pass123"] }

  2. in the test script - onView(withId(R.id.username)).perform(typeText(System.getProperty("userId")))

  3. in the command line to execute the test ./gradlew spoon -PuserId="qaa" -Ppassword="pass123"

I tried with above steps but it did not work out.

artem-zinnatullin commented 7 years ago

Instrumentation arguments will be accessible by test runner, you can override AndroidJUnitRunner and specify it as testInstrumentationRunner in android section of build.gradle see example.

To access arguments you'll need to override onCreate(Bundle) method of AndroidJUnitRunner and get them from Bundle.

pawankums commented 7 years ago

Hi @artem-zinnatullin Thanks for your reply. I tried above steps and passing the params through command line as ./gradlew spoon -e userId 'qaa_user1' -e password 'pass123' but i am getting java.lang.IllegalArgumentException.

I am getting the params using Bundle testBundle = InstrumentationRegistry.getArguments(); userId = testBundle.getString("userId"); password = testBundle.getString("password");

Could you please let me know how to pass params in command line or if my syntax is wrong.

artem-zinnatullin commented 7 years ago

Can you show exception message?

On Mon, Aug 28, 2017, 8:06 AM pawankums notifications@github.com wrote:

Hi @artem-zinnatullin https://github.com/artem-zinnatullin Thanks for your reply. I tried above steps and passing the params through command line as ./gradlew spoon -e userId 'qaa_user1' -e password 'pass123' but i am getting java.lang.IllegalArgumentException.

I am getting the params using Bundle testBundle = InstrumentationRegistry.getArguments(); userId = testBundle.getString("userId"); password = testBundle.getString("password");

Could you please let me know how to pass params in command line or if my syntax is wrong.

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/stanfy/spoon-gradle-plugin/issues/160#issuecomment-325380195, or mute the thread https://github.com/notifications/unsubscribe-auth/AA7B3LN7ZLATAKxPzkLFlKZ1McnGivzvks5sctdvgaJpZM4PDEU7 .

pawankums commented 7 years ago

@artem-zinnatullin Below is the error when i try to use the params using the above steps:

Exception occurred while dumping: java.lang.IllegalArgumentException: Unknown package: com.ecarlist.android.truetarget at com.android.server.pm.Settings.isOrphaned(Settings.java:4134) at com.android.server.pm.PackageManagerService.isOrphaned(PackageManagerService.java:18091) at com.android.server.pm.PackageManagerService.deletePackage(PackageManagerService.java:15508) at com.android.server.pm.PackageInstallerService.uninstall(PackageInstallerService.java:888) at com.android.server.pm.PackageManagerShellCommand.runUninstall(PackageManagerShellCommand.java:792) at com.android.server.pm.PackageManagerShellCommand.onCommand(PackageManagerShellCommand.java:118) at android.os.ShellCommand.exec(ShellCommand.java:94) at com.android.server.pm.PackageManagerService.onShellCommand(PackageManagerService.java:18349) at android.os.Binder.shellCommand(Binder.java:468) at android.os.Binder.onTransact(Binder.java:367) at android.content.pm.IPackageManager$Stub.onTransact(IPackageManager.java:2387) at com.android.server.pm.PackageManagerService.onTransact(PackageManagerService.java:3031) at android.os.Binder.execTransact(Binder.java:565) ‘Tests failed’

artem-zinnatullin commented 7 years ago

Unknown package: com.ecarlist.android.truetarget

pawankums commented 7 years ago

This error comes when i pass parameters to already working command ./gradlew spoon as ./gradlew spoon -e userId 'qaa_user1' -e password 'pass123'

I know that we can specify args in spoon task as spoon { instrumentationArgs = ["userId=qaa", "password=pass123"] }

but not sure how to pass these params through command line and how to access them in the program.

pawankums commented 7 years ago

@artem-zinnatullin i tried your suggestions and added spoon { instrumentationArgs = ["userId=qaa", "password=pass123"] } and accessed these args in onCreate(Bundle ..). But this is like hardcoding the values in build.gradle whereas i want to pass the values through command line with ./gradlew spoon command.

jainsahab commented 7 years ago

The below code snippet should be able to solve the hardcoding problem.

if (project.hasProperty('userId') && project.hasProperty('password')) {
      instrumentationArgs = ["userId=${project.userId}", "password=${project.password}"]
}

and from command line pass arguments like this ./gradlew spoon -PuserId=qaa_user1 -Ppassword=pass123

pawankums commented 7 years ago

Hey @jainsahab thanks for your reply.. if (project.hasProperty('userId') && project.hasProperty('password')) { instrumentationArgs = ["userId=${project.userId}".toString(), "password=${project.password}".toString()] } Worked for me