How to Pass Parameters to multiple @Test Methods from Testng.xml file

Answered

Hi All,
I have scenario where the test runs for multiple sites depends on country.. Here I want to run the same suite for multiple times based on the county name.. and in other classes I have Single/Different @Test methods .For all the @Test methods I need to pass the same single parameter from testng.xml file. How can I achieve this by TestNg.Here I am not using any excel sheets/data provider. All I want to pass the data from XML file only.
Please provide me a solution for this.Below is the snippet.
//Test1.java


public class Test1 extends Keywords{

//    private static WebDriver driver = null;
@Parameters({"browser", "country"})

@BeforeSuite

public void beforeMethd(String browser,String country){

openBrowser(browser,country);
}

@AfterSuite

public void afterMethod(){

driver.close();

//driver.quit();

}

}
//Test2.java

public class Test2 extends Keywords{

//    private static WebDriver driver = null;

private String country;

@BeforeClass

@Parameters("country")

public void beforeMethod(String country){

this.country=country;

}
@Test

public void test1(){

system.out.println(country);
}

@Test

public void test2(){

system.out.println(country);

}
@Test

public void test3(){

system.out.println(country);

}
}

//testng.xml

 


<suite name="SmokeTest-U.K">

<parameter name="browser" value="mozilla">

</parameter>

<parameter name="country" value="UK">

</parameter>

<test name="Application Test1 for U.K">

<classes>

<class name="Test1" />

</classes>

</test>
<test name="Application Test2 -U.K">

<parameter name="country" value="UK"/>

<classes>

<class name="Test2" />

</classes>

</test>
</suite>

Here I want to run the same code for 4 or 5 different countries .How can I achieve this.Please can anyone provide me a solution for this.

Add Comment
  • 2 Answer(s)
    Best answer

    HI Ravinder,

    What you are trying to do is right here but I think your xml is messed up, I have not given it a try so may not work but just to give you an idea on it.

    
    <suite name="Suite">
    
    <test name="Application Test1 for U.K">
    
    <parameter name="browser" value="mozilla"/>
    
    <parameter name="country" value="UK"/>
    
    <classes>
    
    <class name="Test1" />
    
    <class name="Test2" />
    
    </classes>
    
    </test>
    
    <test name="Application Test1 for U.S">
    
    <parameter name="browser" value="IE"/>
    
    <parameter name="country" value="US"/>
    
    <classes>
    
    <class name="Test1" />
    
    <class name="Test2" />
    
    </classes>
    
    </test>
    
    </suite>
    
    

    Take a look at this URL as well:
    http://www.toolsqa.com/selenium-webdriver/testng-parameters-data-provider/

    Best Regards,
    Lakshay Sharma

    Professor Answered on October 15, 2014.
    Add Comment

    Thank you Lakshay..
    Did the same and Removed @BeforeSuite and @AfterSuite instead I have used @BeforeTest and @AfterTest..Working fine now.
    One more thing that TestNg can’t able to provide reliable results when dependsOnMethods and priority used on @Test methods.
    In the above example if I write like this below,

    @Test(priority=1)
    public void test1(){
    system.out.println(country);
    }
    @Test(dependsOnMethods="test1",priority=3)
    public void test2(){
    system.out.println(country);
    }
    @Test(priority=2)
    public void test3(){
    system.out.println(country);
    }
    @Test(dependsOnMethods="test3",priority=4)
    public void test4(){
    system.out.println(country);
    }

    If  test1() fails test2() should not execute..if test1() passes the immediatley test2() should execute.But this is not happening when we use dependsOnMethods and priority at same time.
    Is there any alternate/workaround for this.

    Train Answered on October 15, 2014.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.