java - 如何使用 Maven/testng 将参数发送到方法?

标签 java maven selenium-webdriver cmd testng

如何通过控制台向方法发送参数?

我有一个如下所示的方法:

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

我想发送String通过控制台的 URL 参数。

例如,我希望能够输入 mvn clean test, www.google.com在控制台中,程序应该连接到 Google 并执行测试。 这可能吗?如果是,请给我一些关于如何实现这一目标的建议。

最佳答案

这样做:

1) 在你的 scr/test/resouces 中放置一个 testConfig.properties:

url = ${url}

2)编写测试:

@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) 在您的 pom.xml 中添加以下内容:

<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)运行maven如下:

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

现在你将获得基于maven param的参数化url。

关于java - 如何使用 Maven/testng 将参数发送到方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41838670/

相关文章:

Java:有没有办法在 Windows 上使用 ESC 命令通过 USB 打印到收据打印机?

maven - 在预集成测试阶段使用 maven-tomcat7-plugin 部署到嵌入式 tomcat 时出现 403 错误

python - 使用 selenium 错误打开 Firefox

java - 使用 selenium pagefactory 时收到空指针异常

java - 问 - ReSTLet/Spring - NotWritablePropertyException 带有教程示例?

java - 调试时 JSP 可用的变量在哪里?

java - 在 Java 中将纪元转换为 ZonedDateTime

tomcat - 为不同的容器构建不同的 Maven Artifact

java - Spring mvc xml配置错误

java - 点击按钮不起作用