动态创建TestNG.xml文件并传递参数

4

我需要使用动态的testng.xml文件来执行测试脚本,这意味着我需要通过代码创建testng.xml文件并将参数传递给@ Test方法。

为此,我创建了两个Java文件DynamicTestNG.java,它应该生成testng.xml文件并运行SampleClass.java,其中包含@ Test方法和参数。

DynamicTestNG.java

public class DynamicTestNG {

public void runTestNGTest(Map<String,String> testngParams) {

    //Create an instance on TestNG
     TestNG myTestNG = new TestNG();

    //Create an instance of XML Suite and assign a name for it.
     XmlSuite mySuite = new XmlSuite();
     mySuite.setName("MySuite");

    //Create an instance of XmlTest and assign a name for it.
     XmlTest myTest = new XmlTest(mySuite);
     myTest.setName("MyTest");

    //Add any parameters that you want to set to the Test.
     myTest.setParameters(testngParams);

    //Create a list which can contain the classes that you want to run.
     List<XmlClass> myClasses = new ArrayList<XmlClass> ();
     myClasses.add(new XmlClass("SampleClass"));

    //Assign that to the XmlTest Object created earlier.
     myTest.setXmlClasses(myClasses);

    //Create a list of XmlTests and add the Xmltest you created earlier to it.
     List<XmlTest> myTests = new ArrayList<XmlTest>();
     myTests.add(myTest);

    //add the list of tests to your Suite.
     mySuite.setTests(myTests);

    //Add the suite to the list of suites.
     List<XmlSuite> mySuites = new ArrayList<XmlSuite>();
     mySuites.add(mySuite);

    //Set the list of Suites to the testNG object you created earlier.
     myTestNG.setXmlSuites(mySuites);

    TestListenerAdapter tla = new TestListenerAdapter();
    myTestNG.addListener(tla);

    //invoke run() - this will run your class.
     myTestNG.run();
    }

public static void main (String args[])
{
    DynamicTestNG dt = new DynamicTestNG();

    //This Map can hold your testng Parameters.
     Map<String,String> testngParams = new HashMap<String,String> ();

     testngParams.put("searchtext1", "testdata1");
     testngParams.put("searchtext2", "testdata2");

     dt.runTestNGTest(testngParams);
}

}

SampleClass.java

  public class SampleClass {

private WebDriver driver;

    @BeforeTest
    public void setUp()
    {
        System.setProperty("webdriver.chrome.driver","C:\\Users\\AK5040691\\Desktop\\IE driver\\chromedriver.exe");
        driver = new ChromeDriver();

        driver.manage().window().maximize();
        driver.navigate().to("http://executeautomation.com/blog/custom-testng-library-for-appium/#more-1562");
    }

    //@Parameters({"searchText1","searchText2"})
    //@Test
    public void searchText(String text1, String text2)
    {
        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
        driver.findElement(By.className("search-field")).sendKeys(text1);

        driver.findElement(By.className("search-field")).clear();

        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
        driver.findElement(By.className("search-field")).sendKeys(text2);       
    }
}

它无法运行。请告诉我这里的错误。


尝试使用完整的类名,包括包名在内。我的类集合添加一个新的XmlClass对象,其类名为"SampleClass"。 - murali selenium
你能详细说明一下“它没有运行”的情况吗?出现了什么错误? - user1207289
2个回答

4

您需要在SampleClass文件中取消注释@Test注释。如果您的SampleClass在一个包中,则在此语句中必须指定绝对包名称+类名称。

myClasses.add(new XmlClass("com.some.package.SampleClass"));

一般来说,TestNG类会有一个后缀或前缀标签为“Test”,这样surefire插件才能将它们包含在执行流程中,如果您正在使用maven的话。


0
您可以使用带有类对象参数的构造函数。

myClasses.add(new XmlClass(SampleClass.class));


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接