0

I'm making a Selenium WebDriver java program. I have 25 application and 4 environments. I need to be able to pass something like -app app1 app2 app3 ... appn -env env1 env2 envn

I need to be able to pass either, neither or both arguments. Right now I have it being able to pass one app and one env in that order but I need to be able to do it in either order and with the either neither or both possibility. Here's what I have so far. With this I can either pass no arguments and runs every app for every environment (which is what I want) or I can pick app1 env1 in that order for that specific test.

 public static Application chooseAppTest(String[] args) 
    {
        Application application = null;

        switch (Application.valueOf(args[0]))
        {
        case ACCOUNTINVENTORY:
            new AccountInventory(Environment.valueOf(args[1]));
            AccountInventory.accountInventoryDatabaseTests(testResults);
            break;

if (args.length == 0)
    {
       LogIn.loginTest(testResults);
       DatabaseTest.testResults(testResults);
       LinkTest.linkTests(testResults);
    }
    else 
    {
            // First choose application, then choose environment
        Application.chooseAppTest(args);
    }
2
  • and thats not the whole chooseAppTest method. I just put part of it so you can see what the if/else statement is doing Commented Jul 29, 2014 at 13:13
  • 3
    I would probably use Apache Commons CLI. Commented Jul 29, 2014 at 13:14

3 Answers 3

1

I don't think recursion is needed. You can do something like this:

public static void main (String[] args)
{
    List<String> apps = new LinkedList<>();
    List<String> envs = new LinkedList<>();
    List<String> current = null;
    // parse arguments
    for (String arg : args)
    {
        if (arg.equals("-app")) current = apps;
        else if (arg.equals("-env")) current = envs;
        else if (current != null) // add argument
            current.add(arg);
    }
    // parsing finished
    Application.doSomethingWith(apps, envs);
}
Sign up to request clarification or add additional context in comments.

2 Comments

this is really close to what I need and thank you. Can you tell from the code I put above what I would have to do to the chooseAppTest method so the "doSomethingWith" becomes that?
@user3887792 It would be helpful if you describe the methods you call better. But I don't think it's difficult, you just must call for (String str : apps) Application.addApp(str) and for (String str : envs) Application.addEnv(str); if you have methods like this.
0

It is not necessary or advantagious to use recursion. You can read all the arguments into an array and process them from there. Other than that, I'm not sure how you would proceed. With the arguments arranged in this way, how do you know which environment goes with which application?

Comments

0

As Elliott commented, have you looked at Apache Commons CLI? It's a command line parser.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.