[JVM] Passing parameters to test suite?

52 views
Skip to first unread message

anar...@gmail.com

unread,
Feb 18, 2018, 5:38:21 PM2/18/18
to Cukes
I have a project with Cucumber on Java + Maven and I need to pass a username and password so it can log in to the application under test.
What would be the best way to do this? I really don't like the idea of hard-coding the information.
I was thinking maybe adding them in Maven's pom.xml somewhere, or passing them as arguments when executing the mvn command to run the suite. But I don't know how to do either. Or maybe there's a better way I haven't thought of.

Thanks in advance!

Aslak Hellesøy

unread,
Feb 18, 2018, 5:55:45 PM2/18/18
to Cukes
Use Java system properties or environment variables.

System properties can be passed with -Dfoo=bar and can be accessed with System.getProperty("foo")
How to set environment variables depends on your OS/shell, and can be red from Java with System.getenv("foo")

Aslak
 

Thanks in advance!

Koen Prins

unread,
Feb 19, 2018, 1:08:19 AM2/19/18
to cu...@googlegroups.com
Please allow me to expand a bit as I struggled with something similar recently. Your answer is completely correct but I think a small example might not hurt.


The Java system parameters can be set in various ways, this method is using Maven command line. 
Using maven and you do not want to hardcode the username and password (which is smart either way), you can set a system parameter using the `-D` prefix in the maven command. 
The command would then look something like this:
`mvn test -Dusername=anarelle -Dpassword=seCTIaDentIm`

The java code to use this would look a bit like this:
`
public void runTest(){
String username = System.getProperty("username");
String password = System.getProperty("password");

//here is a bit of RestAssured to store the cookie with, there is a similar thing in Selenium that I cannot remember on the spot ;-)
Response response = loginToWebsite(username, password); //this method does the login things with your website to retrieve a cookie
Map<String, String> allCookies = response.getCookies(); //and here you store all the cookies in a map

}
`

The point is of course that you can set a variable that is accessible on *that specific maven run* using the -D prefix and it is super useful for all kinds of things, not just runtime username passwords.

Another way to do all this is with Maven profiles, read up on those as they are similarly super useful.
Final note: no matter what, you will have to store your username/password somewhere in your codebase if you want to do full automation because else you will have to manually type the command every time you want to run the test. The real best reason to use a parameter is so you do not have to touch the code for running a test with a different username or if a password changes.

Good luck!


The Cucumber logo is the intellectual property of Cucumber Ltd, a limited company registered in Scotland, number 456793.


UK Headquarters: Cucumber Ltd, Drumsyniebeg, Lochgoilhead, Cairndow, Argyll, PA24 8AN UK.


CONFIDENTIALITY NOTICE: The information in this e-mail is confidential and privileged; it is intended for use solely by the individual or entity named as the recipient hereof. Disclosure, copying, distribution, or use of the contents of this e-mail by persons other than the intended recipient is strictly prohibited and may violate applicable laws. If you have received this e-mail in error, please delete the original message and notify us by email immediately. Thank you. Cucumber Ltd.

--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages