Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 以编程方式运行TestNG需要循环自动创建多个测试_Java_Xml_Eclipse_For Loop_Testng - Fatal编程技术网

Java 以编程方式运行TestNG需要循环自动创建多个测试

Java 以编程方式运行TestNG需要循环自动创建多个测试,java,xml,eclipse,for-loop,testng,Java,Xml,Eclipse,For Loop,Testng,我想知道是否有人能给我正确的方向。不确定我是否正确地为循环使用了Map或HashMap。我知道如果我只使用一个参数,它就可以工作,但当我以编程方式创建XML时,我喜欢使用1-100个主机参数。您能告诉我创建循环需要做什么,这样我就可以创建多个测试,参数的主机值为1-100。我的代码如下: package firsttestngpackage; import java.util.ArrayList; import java.util.HashMap; import java.util.List;

我想知道是否有人能给我正确的方向。不确定我是否正确地为循环使用了Map或HashMap。我知道如果我只使用一个参数,它就可以工作,但当我以编程方式创建XML时,我喜欢使用1-100个主机参数。您能告诉我创建循环需要做什么,这样我就可以创建多个测试,参数的主机值为1-100。我的代码如下:

package firsttestngpackage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.TestNG;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;

public class Test1 {

    WebDriver driver;
    WebDriverWait wait;

    private void testRunner(Map<String, String> testngParams) {
        TestNG testNG = new TestNG();
        XmlSuite suite = getXmlSuite();
        XmlTest test = getXmlTest(suite);
        test.setParameters(testngParams);
        List<XmlClass> classes = getXmlClasses();
        test.setXmlClasses(classes);
        List<XmlTest> tests = new ArrayList<XmlTest>();
        tests.add(test);
        suite.setTests(tests);
        List<XmlSuite> suites = new ArrayList<XmlSuite>();
        suites.add(suite);
        testNG.setXmlSuites(suites);
        testNG.run();
    }

    private XmlSuite getXmlSuite() {
        XmlSuite suite = new XmlSuite();
        suite.setName("Test Suite");
        return suite;
    }

    private XmlTest getXmlTest(XmlSuite suite) {
        XmlTest test = new XmlTest(suite);
        test.setName("test_with_firefox");
        return test;
    }

    private List<XmlClass> getXmlClasses() {
        List<XmlClass> classez = new ArrayList<XmlClass>();
        classez.add(new XmlClass("firsttestngpackage.Test5"));
        return classez;
    }

    public static void main(String args[]) {

        Test1 program = new Test1();
        Map<String, String> params = new HashMap<String, String>();

        //Need THIS CONFIGURATION LOOP TO WORK
        //NEED HELP HERE!!!!!!!!!!!!!!
        for (int hostnum = 1; hostnum <= 100; hostnum++){
            params.put("host", hostnum);
        }

        //THIS CONFIGURATION WORKS, BUT NO LOOP
        //params.put("host", "10");
        program.testRunner(params);
    }
}
这是预期的XML,但是是100倍

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestSuite" thread-count="3" >
  <test name="1">
  <parameter name="host" value="1" />
    <classes>
       <class name="firsttestngpackage.Test5">
       </class>
    </classes>
  </test>
  <test name="2">
  <parameter name="host" value="2" />
    <classes>
       <class name="firsttestngpackage.Test5">
       </class>
    </classes>
  </test>
  <test name="3">
  <parameter name="host" value="3" />
    <classes>
       <class name="firsttestngpackage.Test5">
       </class>
    </classes>
  </test>
 </suite>
我得到语法错误
-语法错误,插入维度以完成引用type

工作代码与循环之间的差异是,在工作代码中使用字符串10,但在循环中插入整数,但哈希映射为哈希映射

请尝试以下代码:

public static void main(String[] args) {

    /**
     * Get the number of hosts (for example from the arguments, 
     * or from a file, or from System.getenv, etc...)
     */
    Integer numberOfHosts = this.getNumberOfHosts();

    // Creating a new Suite
    XmlSuite suite = new XmlSuite();

    for (Integer i = 1; i <= numberOfHosts; i++) {

      // Creating a new Test
      XmlTest test = new XmlTest(suite);

      // Set Test name
      test.setName("test-number-" + i);

      // New list for the parameters
      Map<String, String> testParams = new HashMap<String, String>();

      // Add parameter to the list
      testParams.put("host", String.valueOf(i));

      // Add parameters to test
      test.setParameters(testParams);

      // New list for the classes
      List<XmlClass> classes = new ArrayList<XmlClass>();

      // Putting the classes to the list
      classes.add(new XmlClass("firsttestngpackage.Test5"));

      // Add classes to test
      test.setClasses(classes);

    }

    // New list for the Suites
    List<XmlSuite> suites = new ArrayList<XmlSuite>();

    // Add suite to the list
    suites.add(suite);

    // Creating the xml
    TestListenerAdapter tla = new TestListenerAdapter();
    TestNG tng = new TestNG();
    tng.setXmlSuites(suites);
    tng.addListener(tla);
    tng.run();

  }

我建议对for循环使用迭代器。谢谢peetya。这正是我要找的。