根据testng.XML中提供的参数调用不同的分类

Invoke different classed based on the parameter provided in testng.XML

I have a query regarding testng module. My requirement is to invoke different classes based on the parameter provided in the testng.xml

Testng.XML

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<parameter name="selenium.test" value="Web" />
  <parameter name="selenium.testtype" value="Sanity" />
  <parameter name="selenium.env" value="UAT" />
  <parameter name="selenium.browser" value="IE" />
  <parameter name="selenium.pbrowser" value="Mozilla" />

  <test name =>
    <classes>
      <class name="test.DriverTest"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

1) 在上面的代码片段中,如果我的测试类型是理智,我想调用 Sanity.java 文件,如果其他测试类型是回归,我想调用 Regression.java

2) 我们能否将参数值作为输入传递给 testng.xml 中的其他 XML 标签 例子:如果 然后我希望根据参数值

动态地提供此值

3) 同样从这个参数输入如何调用并行浏览器测试?

1) In the above code snippet, if my test type is sanity, I want to invoke Sanity.java file where as if other test type is regression, I want to invoke Regression.java

TestNG 为您提供了一个名为 groups 的概念,它应该可以为您解决这个问题。对于每个 @Test 方法,向其添加 groups 属性,然后在您的套件 xml 文件中,您可以选择您想要 运行 的任何组。请参阅官方文档 here 了解更多信息。

2) Can we pass parameter value as input to other XML tags in testng.xml Example: If then i want this value to feeded dyanamically based on parameter value

不确定你在这里之后是什么,但是 testng 套件 xml 文件中的参数本质上总是静态的。它们不能更改,除非并且直到您更改套件 xml 文件(或)您使用 TestNG 侦听器,例如 org.testng.IAlterSuiteListener,在其中您根据其他东西捏造参数。这是一个示例,显示了我在说什么

期望注入参数的测试class

package com.rationaleemotions.Whosebug.qn46216357;

import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class SampleTestClass {

    @Test
    @Parameters("name")
    public void testMethod(String name) {
        System.err.println("Hello " + name);
    }
}

此侦听器查找以 jvm_ 开头且名称与套件 xml 中的参数匹配的 JVM 参数,如果找到,它将使用 JVM 中发送的内容更新参数值争论。所以使用这种方式,你可以真正实现你所追求的。

package com.rationaleemotions.Whosebug.qn46216357;

import org.testng.IAlterSuiteListener;
import org.testng.xml.XmlSuite;

import java.util.List;
import java.util.Map;

public class ParameterChangerListener implements IAlterSuiteListener {
    @Override
    public void alter(List<XmlSuite> suites) {
        XmlSuite suite = suites.get(0);

        for (Map.Entry<String, String> each : suite.getParameters().entrySet()) {
            //Check to see if we were given any JVM arguments with the prefix as "jvm_" followed
            //by the text of the parameter name.
            String sysValue = System.getProperty("jvm_" + each.getKey());
            if (sysValue != null && !sysValue.trim().isEmpty()) {
                each.setValue(sysValue);
            }
        }
    }
}

这是套件 xml 的样子

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="46216357_Suite" verbose="2">
    <listeners>
        <listener class-name="com.rationaleemotions.Whosebug.qn46216357.ParameterChangerListener"/>
    </listeners>
    <parameter name="name" value="Unknown"/>
    <test name="46216357_test">
        <classes>
            <class name="com.rationaleemotions.Whosebug.qn46216357.SampleTestClass"/>
        </classes>
    </test>
</suite>

如您所见,我为参数 name

定义了一个 Unknown 的值

因此,如果您从您最喜欢的 IDE 中右键单击套件 xml 和 运行,输出将如下所示:

...
... TestNG 6.12 by Cédric Beust (cedric@beust.com)
...
Hello Unknown
PASSED: testMethod("Unknown")

===============================================
    46216357_test
    Tests run: 1, Failures: 0, Skips: 0
===============================================

===============================================
46216357_Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

但是如果你 运行 同一个套件,通过传入 JVM 参数 -Djvm_name,那么输出会不同。这是一个 运行 输出,其中我将值作为 Rambo 传递:-Djvm_name=Rambo

...
... TestNG 6.12 by Cédric Beust (cedric@beust.com)
...
Hello Rambo
PASSED: testMethod("Rambo")

===============================================
    46216357_test
    Tests run: 1, Failures: 0, Skips: 0
===============================================

===============================================
46216357_Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

3) Also from this parameter input how to invoke parallel browser testing?

我会稍作改动,建议您将并行模式作为 JVM 参数来阅读,并基于此更改您的并行执行策略。 您基本上使用了与我上面分享的相同类型的 IAlterSuiteListener 实现,它可能如下所示:

import org.testng.IAlterSuiteListener;
import org.testng.xml.XmlSuite;

import java.util.List;

public class ParallelExecutorSetter implements IAlterSuiteListener {
    @Override
    public void alter(List<XmlSuite> suites) {
        XmlSuite suite = suites.get(0);
        String parallelMode = System.getProperty("parallelMode");
        XmlSuite.ParallelMode mode = XmlSuite.ParallelMode.getValidParallel(parallelMode);
        if (mode != null) {
            suite.setParallel(mode);
        }
    }
}

因此,您现在可以通过 JVM 参数传递并行模式来更改它。例如,-DparallelMode=methods 到 运行 parallel

中的所有 @Test 方法