0

How can I send parameters to methods via the console?

I have a method that looks like this:

@Test
public void test() throws InterruptedException {
    botClass.Desktop(driver, "same url");       
}

I want to send a String URL parameter through the console.

For example, I want to be able to type mvn clean test, www.google.com in the console, and the program should connect to Google and perform the test. Is this possible? If yes, please give me some advice on how to accomplish this.

Salem
  • 13,516
  • 4
  • 51
  • 70
Kirill Kogan
  • 35
  • 11

2 Answers2

1

Do like this:

1) in your scr/test/resouces place a testConfig.properties:

url = ${url}

2) write the test:

@Test
    public void test() throws Exception{

        String urlParam = null;

        try {

            Properties prop = new Properties();
            InputStream input = null;
            input = getClass().getClassLoader().getResourceAsStream("testConfig.properties");

            prop.load(input);

            urlParam = prop.getProperty("url");

        }catch (Exception e){}

        // use urkParam
    }

3) in your pom.xml add this:

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.0.2</version>
            </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.12.4</version>
                </plugin>
        </plugins>
        <testResources>
            <testResource>
                <directory>src/test/resources</directory>
                <filtering>true</filtering>
            </testResource>
        </testResources>
    </build>

4) run maven as follows:

mvn clean test resources:testResources -Durl=www.google.com

Now you will get parameterized url based on maven param.

Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63
0

i found a easier way to resolve it without change nothing in the porm file. i wrote in my class String valueFromMaven=System.getProperty("URL"); and in the console i start the test with mvn clean test -DURL=www.google.com .

Kirill Kogan
  • 35
  • 11