Reputation: 23
I've method like this,
public void beforeTest(String a, String b, String c, String d) {
x = getURL(a, b, c, d);
--------
--------
}
I now want to pass String[] instead of individual Strings as arguments type, so tried this,
public void beforeTest(String[] args){
x = getURL(a, b, c, d);
--------
--------
}
and passed a,b,c,d as command line arguments, my code isn't working, is it the right approach?
Thank you all, I did try getURL(args[0].......) approach and unfortunately I'm still getting the same error,
[testng] FAILED CONFIGURATION: @BeforeMethod beforeTest [testng] org.testng.TestNGException: [testng] Method beforeTest requires 1 parameters but 4 were supplied in the @Configuration annotation.
By the way, my getURL method is defined as below,
protected String getURL(String a, String b, String c, String d) {
if (condition){
return a
} else if (condition) {
return b
} ....
Upvotes: 0
Views: 160
Reputation: 23361
To do what you need you have to change your method.
public void beforeSuite(String...args){
x = getURL(args[0], args[1], args[2], args[3]);
--------
--------
}
But keep in mind that this definition (String...args
) on a method can have N number of arguments, which means that if you pass less than 4 arguments you will get an ArrayIndexOutOfBoundsException.
Upvotes: 0
Reputation: 15408
public void beforeSuite(String[] args){
x = getURL(args[0], args[1], args[2], args[3]);
--------
--------
}
But why not give getUrl
similar type of signature with parameter type String[]
. Then you could again pass the args
invoking getUrl(args)
. You can also declare both method with varargs
parameter: beforeSuite(String... args)
and getURL(String... args)
Upvotes: 0
Reputation: 25873
I think shortest way to do that is
x = getURL(args);
This will only work if getURL
is declared as getURL(String...)
or getURL(String[])
Upvotes: 0
Reputation: 44439
You might want to use varargs instead. It allows you to treat the input as an array and doesn't require the caller to create an array himself.
public void beforeSuite(String... args){
x = getURL(args[0], args[1], args[2], args[3]);
}
You can call this like this:
beforeSuite("test", "test", "test again", "more tests", "even more tests");
// Notice how I can specify more than 4 (or less, or equal to 4) strings
// without changing the method signature
The major issue is accessing your variables though: args[0]
instead of args
. args
is just an array, but args[0]
is a value in the array.
Upvotes: 1
Reputation: 39457
Try this.
String a = args[0];
String b = args[1];
String c = args[2];
String d = args[3];
Passing of the 4 strings as command line arguments is OK.
Upvotes: 2