0

I have xml suite that sends multiple tests and multiple parameters.

example:

        <test name="Create">       
        <classes>       
        <class name="TestClass">
            <methods>
                <parameter name="offerId" value="1234"/>
                <include name="testmethod"/>
            </methods>
        </class>                                          
      </classes>
      </test>
        <test name="Add">       
        <classes>       
        <class name="TestClass2">
            <methods>
                <include name="testmethod2"/>
            </methods>
        </class>                                          
      </classes>
      </test>

I need to run this class multiple times, each time with different offerId parameter. (e.g 1234,4567,7899)

I want to run this request only once, and it will irritate over all different parameter and run the whole suit again and again, and give result on the same report.

this is what I did:

@Test
public void runSuites2(){

    TestNG testng = new TestNG();
    List<String> suites=new ArrayList<String>();
    suites.add("c:/tests/testng1.xml");//path to xml..

    testng.setTestSuites(suites);
    testng.run();

}

so this will load and run the suit I need, but how to change the parameter inside the suite? (after it I will create for loop)

[currently I duplicated the xml and manually change the parameter for each test. and then run suite-of-suites]

the test:

@Parameters({ "offerId" })
@Test
public void testmethod(String offerId, ITestContext context) throws Exception {
    Reporter.log("offer ID is = " + offerId, true);
        }
Idan Daniel
  • 3
  • 1
  • 3
  • Can you add the testMethod code inside which you are using offerId parameter? – Sameer Arora Feb 06 '19 at 17:02
  • added the java test @SameerArora – Idan Daniel Feb 07 '19 at 08:59
  • please refer this and follow https://stackoverflow.com/questions/46224926/set-properties-file-to-testng-xml – thar45 Feb 07 '19 at 09:11
  • @IdanShabat let me know if the answer helps you :) – Sameer Arora Feb 07 '19 at 11:48
  • hi @SameerArora - Thanks for your input, but this did not helped. it did run the test from the properties file but I need to run the same test multiple time on the same parameter. when I add different value to the same parameter - it took the latest and does not run twice. – Idan Daniel Feb 11 '19 at 08:41
  • @gks please see my answer above – Idan Daniel Feb 11 '19 at 08:53
  • @IdanShabat whenever you update the value latest will update it . Above comments are not clear about what you are trying to do !! .Can you elaborate it in the question/comment to some extent . – thar45 Feb 11 '19 at 09:22
  • hi @gks , this is what im trying to do: 1.start run the suit - run some tests. 2. when it hit the desire class (test) use the first parameter. 3. continue with the suit and run some more parameters. 4.go back to the first step and run it again -- this until there is not more parameters left on the list – Idan Daniel Feb 11 '19 at 12:56

2 Answers2

2

In this case, you can use dataprovider or you can read the values from excel and tests will be run for each value in the dataprovider/excel sheet.
Providing you an example on how to use dataprovider for your test case.

@DataProvider(name = "offerId")
public static Object[][] voiceSearchTestData() {
    return new Object[][]{
            {1234},
            {2345},
            {4567}
    };
}

@Test(dataProvider = "offerId")
public void testmethod(int offerId, ITestContext context) throws Exception {
    Reporter.log("offer ID is = " + offerId, true);
}

So the above test will run 3 times, one for each value present in the dataprovider and you dont need to parameterise anything in the testng xml. You just need to mention the class name and all the tests will run automatically. You testng.xml should be like:

<test name="SampleTest">
    <classes>
        <class name="packageName.className" />
    </classes>
</test>
Sameer Arora
  • 4,439
  • 3
  • 10
  • 20
  • Hi @Sameer Arora. thanks for this answer as well. this way I do get to run the same test multiple times, but is there a way it will take the whole suit, and run the whole suit and not just the parameter? I have multiple test on the same suit so I want it to: 1.start run the suit - run some tests. 2. when it hit the desire class (test) use the first parameter. 3. continue with the suit and run some more parameters. 4.go back to the first step and run it again -- this until there is not more parameters left on the list – Idan Daniel Feb 11 '19 at 08:51
0

What the below code does: I want to add a list of parameters to each during runtime. These parameters are passed as maven runtime arguments. They are read using System.getProperty() method as shown below. Then these parameters are added to the <test> inside <suite> and testng is ran successfully. This can be really useful in other scenarios as well.

The below code reads the testng.xml file and adds parameter to

List<String> parameters = new ArrayList<>();
parameters = Arrays.asList(System.getProperty("parameters").split(",");

TestNG tng = new TestNG();
File initialFile = new File("testng.xml");
InputStream inputStream = FileUtils.openInputStream(initialFile);
Parser p = new Parser(inputStream);
List<XmlSuite> suites = p.parseToList();
for(XmlSuite suite:suites){
    List<XmlTest> tests = suite.getTests();
    for (XmlTest test : tests) {
         for (int i = 0; i < parameters.size(); i++) {
            HashMap<String, String> parametersMap = new HashMap<>();
            parametersMap.put("parameter",parameters.get(i));
            test.setParameters(parametersMap);
        }
    }
}
tng.setXmlSuites(suites);
tng.run();
halfer
  • 19,824
  • 17
  • 99
  • 186
Kishore Mohanavelu
  • 439
  • 1
  • 6
  • 17