Require help in adding pattern/regex for classes while building testng.xml file dynamically

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Require help in adding pattern/regex for classes while building testng.xml file dynamically

Rejish R
Hi,

I am implementing IAlterSuiteListener to build my testng.xml file dynamically.  I am trying to achieve following from the script:
1) Run tests based on all the packages by providing pattern as package=.* or comma separated package name. --- it works 
2) Run tests based on groups, for this using method selector option. But as I understood to run tests based on groups we need to have <class> in xml file. Looking a way to add some pattern/regex to add classes to xml files. 
My scripts looks as below: 

@Override
    public void alter(List<XmlSuite> suites) {
        XmlSuite suite = suites.get(0);
        XmlTest xmlTest = new XmlTest(suite);
        xmlTest.setName("CommandLine_Test");

        String groupToRun = System.getProperty("groupToRun", suite.getParameter("groupToRun"));
        System.out.println("groupToRun: " + groupToRun);
        if (!groupToRun.trim().isEmpty()){
            XmlMethodSelector xmlMethodSelector = new XmlMethodSelector();
            XmlScript script = new XmlScript();
            script.setLanguage("beanshell");

            String s = "whichGroup = System.getProperty(\"groupToRun\", suite.getParameter(\"groupToRun\"))" +
                    "                if (whichGroup.trim().isEmpty()) {\n" +
                    "                        return true;\n" +
                    "                    }\n" +
                    "                boolean groupFlag = groups.containsKey(whichGroup);\n" +
                    "                return groupFlag";
            System.out.println("string s is: " + s);
            script.setScript(s);
            xmlMethodSelector.setScript(script);
            List<XmlMethodSelector> methodSelectors = Collections.singletonList(xmlMethodSelector);
            xmlTest.setMethodSelectors(methodSelectors);
        }


        String packages = System.getProperty("package", suite.getParameter("package"));
        System.out.println("packages: "+ packages.split(","));
        List<String> packages_list = Arrays.asList(packages.split(","));

        XmlPackage xmlPackage;

        List<XmlPackage> xmlPackageList = new LinkedList<>();
        for(String packageName: packages_list){
            System.out.println("pakage: " + packageName);
            xmlPackage = new XmlPackage(packageName);
            xmlPackageList.add(xmlPackage);
        }

        xmlTest.setXmlPackages(Collections.unmodifiableList(xmlPackageList));
        XmlClass xmlClass = new XmlClass(classes);
        List<XmlClass> xmlClassList = Collections.singletonList(xmlClass);
        System.out.println(suite.toXml());
    }


Looking for some hints.

Thanks in advance!

    

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Require help in adding pattern/regex for classes while building testng.xml file dynamically

Rejish R
Hi, 

Looking for any help/pointer.

Thanks!

On Monday, March 12, 2018 at 5:31:02 PM UTC+5:30, Rejish R wrote:
Hi,

I am implementing IAlterSuiteListener to build my testng.xml file dynamically.  I am trying to achieve following from the script:
1) Run tests based on all the packages by providing pattern as package=.* or comma separated package name. --- it works 
2) Run tests based on groups, for this using method selector option. But as I understood to run tests based on groups we need to have <class> in xml file. Looking a way to add some pattern/regex to add classes to xml files. 
My scripts looks as below: 

@Override
    public void alter(List<XmlSuite> suites) {
        XmlSuite suite = suites.get(0);
        XmlTest xmlTest = new XmlTest(suite);
        xmlTest.setName("CommandLine_Test");

        String groupToRun = System.getProperty("groupToRun", suite.getParameter("groupToRun"));
        System.out.println("groupToRun: " + groupToRun);
        if (!groupToRun.trim().isEmpty()){
            XmlMethodSelector xmlMethodSelector = new XmlMethodSelector();
            XmlScript script = new XmlScript();
            script.setLanguage("beanshell");

            String s = "whichGroup = System.getProperty(\"groupToRun\", suite.getParameter(\"groupToRun\"))" +
                    "                if (whichGroup.trim().isEmpty()) {\n" +
                    "                        return true;\n" +
                    "                    }\n" +
                    "                boolean groupFlag = groups.containsKey(whichGroup);\n" +
                    "                return groupFlag";
            System.out.println("string s is: " + s);
            script.setScript(s);
            xmlMethodSelector.setScript(script);
            List<XmlMethodSelector> methodSelectors = Collections.singletonList(xmlMethodSelector);
            xmlTest.setMethodSelectors(methodSelectors);
        }


        String packages = System.getProperty("package", suite.getParameter("package"));
        System.out.println("packages: "+ packages.split(","));
        List<String> packages_list = Arrays.asList(packages.split(","));

        XmlPackage xmlPackage;

        List<XmlPackage> xmlPackageList = new LinkedList<>();
        for(String packageName: packages_list){
            System.out.println("pakage: " + packageName);
            xmlPackage = new XmlPackage(packageName);
            xmlPackageList.add(xmlPackage);
        }

        xmlTest.setXmlPackages(Collections.unmodifiableList(xmlPackageList));
        XmlClass xmlClass = new XmlClass(classes);
        List<XmlClass> xmlClassList = Collections.singletonList(xmlClass);
        System.out.println(suite.toXml());
    }


Looking for some hints.

Thanks in advance!

    

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.