RE: 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 Answers

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.