如何将testNG实例携带到另一个class

How to carry testNG instance to another class

原样:

第 1 步:我正在 servelt.doPost 方法中创建 testng 实例。

public void  dummyDoPost(){


        TestListenerAdapter adapter = new TestListenerAdapter();

        testNG = new TestNG();
        List<Class> listnerClasses = new ArrayList<Class>();
        List<String> suiteNameList = new ArrayList<String>();
        Class[] classList = new Class[]{
                csvOperation.class
              };
        listnerClasses.add(straight2bank.csvOperation.class);
        testNG.setDefaultSuiteName("suite");
        testNG.setListenerClasses(listnerClasses);
        testNG.setTestClasses(classList);
        testNG.run();

第 2 步: 我创建了 class,它将读取 servelet 返回的用户平台选择(例如 ios、Android 或 Chrome)。

程序如下。 那又一个操作

Class B{
    public void platformController (Map<String,String> testDataValues){

        System.out.println("Platform Controller started.");
        String platformToBeExecuted = testDataValues.get("JourneyId");
        System.out.println("Journey ID returned to platformController " +platformToBeExecuted);

        if(platformToBeExecuted.contains("chrome")){
            System.out.println("Platform to be executed: Chrome");
            System.setProperty("webdriver.chrome.driver",pathToChromeDriver); 

            /****
            To remove message "You are using an unsupported command-line flag: --ignore-certificate-errors.
            Stability and security will suffer."
            Add an argument 'test-type'
            ChromeOptions options = new ChromeOptions();
            options.addArguments("test-type");
            *****/


            driver = new ChromeDriver();
            driver.get(urlOfApplication);
            System.out.println("3");

        }else if(platformToBeExecuted.contains("ie")){
            System.setProperty("webdriver.ie.driver", pathToIEDriver);
            driver = new InternetExplorerDriver();
            driver.get(urlOfApplication);
            System.out.println("2");

        }else if(platformToBeExecuted.contains("iOS")){
            System.out.println("Platform to be executed: iOS");
            System.out.println("Platform to be executed: iOS");
            suites.add(".//iostestng.xml");<-----------------
            dummyServletClass.testNG.setTestSuites(suites);
            dummyServletClass.testNG.run();


}

所以这里我使用 testng 执行了 iosTestng.xml。

为此:-

1) 我是否必须在 servelt class 中将 testng 声明为静态并在此处使用相同的内容?
2) 我是否需要在 class B 中为 testng 创建另一个实例?
3) 有什么方法可以在setTestClasses 中传递参数构造函数吗?

我很困惑,因为我们可能会在 运行 上并行地处理 运行 程序。运行。

如果每个 POST 调用基本上代表最终用户对 运行 一些测试的意图,那么我建议您求助于每次调用创建一个 TestNG 实例。这样,您就可以为每次调用隔离测试结果等

1) Do I have to declare testng as static in servelt class and use the same here ?

不,不要那样做。你最终会导致竞争条件。您应该改为在 POST 方法实现中将 TestNG 对象声明为本地数据成员。如果您不希望 POST 调用成为阻塞调用,您基本上可以让 POST 调用创建一个请求,将 运行 一些测试放入队列中,然后您可以进行轮询由一个单独的线程驱动的机制,该线程从队列中获取信息并使用 TestNG 运行s 它们。

2) Do I need to create an another instance for testng in class B?

是的,您需要这样做。本质上,这里的想法是将 TestNG 实例本地化到它正在执行的测试集,这样就不会有结果重叠、侦听器调用等。

3) Is there any way to pass an argument constructor in setTestClasses?

这个问题我不是很明白。你这是什么意思?请详细说明。