0

I'm wondering is it possible to put parameters in method when I use testng.xml. I know about put parameteres in test class. I use page object model approach. Here is my code

<suite name="dev-parametrization" verbose="1" >
  <test name="Paragraphs-Tests">
    <classes>
      <class name="com.java.tests.ParagraphsApiControllerTests">
        <methods>
          <include name="createParagraph">
            <parameter name="paragraphsURL" value="http://192.168.0.139:8880/paragraphs"/>
          </include>
        </methods>
      </class>
    </classes>
  </test>

Below test class

public class ParagraphsApiControllerTests {

Paragraphs paragraphs = new Paragraphs();

@Parameters({"paragraphsURL"})
@Test(priority = 1)
public void createParagraph() {
    paragraphs.createParagraph();
}

And my method - here I want to use parameter from xml. file. Is it possible? How can I do this?

public class Paragraphs {


    String paragraphsURL = "http://192.168.0.139:8880/paragraphs";
    String apiParagraphsURL = "http://192.168.0.139/api/paragraphs";

    public void createParagraph() {
        RestAssured.baseURI = paragraphsURL;
goney225
  • 91
  • 1
  • 1
  • 10

1 Answers1

1

Don't use the same method name in test class and in Paragraph class. I changed the test class method name from createParagraph to testCreateParagraph.

Testng.xml

<suite name="dev-parametrization" verbose="1" >
  <test name="Paragraphs-Tests">
    <classes>
      <class name="com.java.tests.ParagraphsApiControllerTests">
        <methods>
          <include name="testCreateParagraph">
            <parameter name="paragraphsURL" value="http://192.168.0.139:8880/paragraphs"/>
          </include>
        </methods>
      </class>
    </classes>
  </test>

Test class

public class ParagraphsApiControllerTests {

Paragraphs paragraphs = new Paragraphs();

@Parameters({"paragraphsURL"})
@Test(priority = 1)
public void testCreateParagraph(String paragraphsURL) {
    paragraphs.createParagraph(paragraphsURL);
}

Paragraph class

public class Paragraphs {

    public void createParagraph(String paragraphsURL) {
        RestAssured.baseURI = paragraphsURL;
    }
}

Refer this tutorial for more information

kaweesha
  • 803
  • 6
  • 16
  • OK I changed this. I added @Optional annotation in test class because I got error earlier public void testCreateParagraph(@Optional String paragraphsURL). Now in createParagraph I added only System.out.println(RestAssured.baseURI=paragraphsURL); and I got null. Test doesn't see value from paragraphsURL parameter in xml file. – goney225 Oct 13 '20 at 05:57
  • My mistake. Tests must be execute from xml. file. Everything is OK. Thanks for help! – goney225 Oct 13 '20 at 07:02
  • @goney225 - If you find this answer has solved your problem, please make this the accepted answer. – kaweesha Oct 14 '20 at 16:06