Referring test groups in testng.xml - java

Let's say I've annotated my test class with a test group in TestNG:
#Test(group='smoke-tests')
public class CheckEnvironmentTest {
...
}
Is there a way to refer to this group in testng.xml? Something like this (if it was implemented):
<suite name="Suite1" verbose="1" >
<test name="Sequential" parallel="false" >
<test-group ref="smoke-tests" />
</test>
<test name="ParallelGroup" parallel="classes" >
<test-group ref="regular-tests" />
</test>
</suite>

You might want to do something like this
<test name="sample">
<groups>
<run>
<include name="smoke-tests"/>
</run>
</groups>
<classes>
<class name="CheckEnvironmentTest"/>
</classes>
</test>

Related

Unable to run TestNG classes in sequential order

I want my testng to run my classes in Sequential mode. I have mentioned suite name="WebData-Sanity" parallel="false" thread-count="1" in my testng.xml, Still its running parallel. All the test classes are starting at a time. How ever If I am putting individual classes in individual <test> its running sequential.
<suite name="WebData-Sanity" parallel="false" thread-count="1">
<test name="WebData-Sanity" preserve-order="`enter code here`true" parallel="false"
thread-count="1">
<groups>
<run>
<include name="Sanity" />
</run>
</groups>
<classes>
<class name="flipKartTest.FlipKartFirstPageTest" />
</classes>
</test>
<test name="Regression">
<classes>
<class name="flipKartTest.FlipKartHomePageTest" />
</classes>
</test>
</suite>
I want all my classes in Same <tests> tag

TestNG Based Selenium Tests not running in Parallel

I am using the below TestNG Config to enable parallel execution of Selenium tests.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Test-Automation" parallel="methods" thread-count="2" verbose="1">
<test name="Suite Test">
<classes>
<class name="SampleTest">
<methods>
<include name="firstTest"/>
<include name="secondTest"/>
<include name="thirdTest"/>
</methods>
</class>
</classes>
</test>
</suite>
Java Code:
#Test(dataProvider = "TestData")
public void firstTest(String data){
//Code
}
#Test(dataProvider = "TestData")
public void secondTest(String data){
//Code
}
#Test(dataProvider = "TestData")
public void thirdTest(String data){
//Code
}
The Selenium tests are expected to run in parallel. I expect 2 browsers to be open and run the test script.
But I see only 1 browser and all 3 tests run one after the other and not in parallel. I have tried using test, methods, class, instance options for "parallel" attribute.
Any help?
This is due to a bug in TestNG 6.13.1 [ See GITHUB-1636 for more details ]
I have fixed this in the latest SNAPSHOT of TestNG (6.14-SNAPSHOT) and this should be available for use in the released version of TestNG (6.14).
But until then, please alter your suite xml file to look like below :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Test-Automation" parallel="methods" thread-count="2" verbose="1">
<test name="Suite Test" parallel="methods" thread-count="2" verbose="1">
<classes>
<class name="SampleTest">
<methods>
<include name="firstTest"/>
<include name="secondTest"/>
<include name="thirdTest"/>
</methods>
</class>
</classes>
</test>
</suite>
The work-around is basically to add the attributes parallel="methods" thread-count="2" at the <test> level also.
Seperate all the test and then try with parallel="test"
<test name="Suite Test1">
<classes>
<class name="//..//packg name..SampleTest">
</class>
</classes>
</test>
<test name="Suite Test2">
<classes>
<class name="//..//SampleTest">
</class>
</classes>
</test>
<test name="Suite Test3">
<classes>
<class name="//..//packg name..SampleTest">
</class>
</classes>
</test>
</suite>

Selenium_testng_Java_passing data to both #beforetest and #test

I have my TesTng class with 3 test (A,B, C) and this class extends base class which has #beforemethod and # aftermethod
Now I want to pass browser to # before method and email to method A
Below is my sample data.
Email has to be unique every time.
one of the ways is to use #Parameters annotation.
Code for #BeforeMethod is-
#BeforeMethod
#Parameters("browser")
public void testMethod1(String browser) {
//do your task here
}
Code for method A-
#Test
#Parameters("email")
public void A(String email) {
//implement your test logic here
}
Sample TestNG Sample-
<suite name="Suite1" verbose="1" >
<test name="Test1">
<parameter name="browser" value="firefox"/>
<parameter name="email" value="an-email-id"/>
<classes>
<class name="packagename.ClassName"/>
</classes>
</test>
</suite>
I found a solution for my problem please le me know if more efficient way is there.. I have userd # parameter (thanx optimist_creeper) and In testng.xml I have created different test
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Chrome_test">
<parameter name="Browser" value="chrome"></parameter>
<parameter name="email" value="a"></parameter>
<classes>
<class name="selenium.mail.gmail.BabyTest">
<methods>
<include name="A" />
<include name="B" />
<exclude name="C" />
</methods>
</class>
</classes>
</test> <!-- Test -->
<test name="FF_test">
<parameter name="Browser" value="Firefox"></parameter>
<parameter name="email" value="b"></parameter>
<classes>
<class name="selenium.mail.gmail.BabyTest">
<methods>
<exclude name="C" />
</methods>
</class>
</classes>
</test>
<test name="IE_test">
<parameter name="Browser" value="IE"></parameter>
<parameter name="email" value="c"></parameter>
<classes>
<class name="selenium.mail.gmail.BabyTest">
<methods>
<include name="A" />
</methods>
</class>
</classes>
</test>
sorry for wrong indentation, code with correct indentation wasn't displayed properly.

Unable to run the Regression group in testng.xml file for different browsers

I have configured the testng.xml file to run the Regression group in different browsers.Below is the testng.xml code for the same.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="SeleniumSuite" verbose ="1" thread-count = "1" parallel="false">
<**test** name="FirefoxTest">
<groups>
<run>
<include name="Regression"></include>
</run>
</groups>
<parameter name="browser" value="firefox" />
<classes>
<class name="Testscript.Program111_RediffLogin" />
</classes>
</test>
<test name="IETest">
<groups>
<run>
<include name="Regression"></include>
</run>
</groups>
<parameter name="browser" value="ie" />
<classes>
<class name="Testscript.Program111_RediffLogin" />
</classes>
</test>
</suite>
when I hover the mouse on the tag, it displays an error message as "The content of element type "test" must match "(method-selectors?,parameter*,groups?,packages?,classes?)".At the Test class level I have defined all the parameters properly for the regression test to run.But still I am seeing the error in the testng.xml file.Can any one of you look into this and help me!
Please find the test case I am using for the automation
#Test(groups={"Regression"},dataProvider = "hashmapdataprovider",dataProviderClass =Dataprovider.Dataprovider_Hashmap.class,priority=1 )
public void validLogin(Map<String,String> hm) throws IOException
{
pageobjects.Signin();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
OriginalExcelRW Excel = new OriginalExcelRW("F:\\anand_acer\\selenium\\rediffbooks.xlsx");
XSSFSheet s1 = Excel.Setsheet("Sheet1");
SoftAssert s_assert = new SoftAssert();
if (hm.get("Executionflow").contains("anand"))
{
pageobjects.Username1(hm.get(Excel.Readvalue(s1, 0, 2)));
pageobjects.pass1(hm.get(Excel.Readvalue(s1, 0, 3)));
//s_assert.assertEquals(hm.get(Excel.Readvalue(s1, 0, 2)), hm.get(Excel.Readvalue(s1, 0, 3)), "both the usssser Ideee and password doesnt matches");
//logger.info("Usssser Ideeee");
pageobjects.login();
s_assert.assertTrue(true, "login success");
//logger.info("The login was success");
System.out.println("Valid login is passed");
pageobjects.signout();
pageobjects.Signin();
//pageobjects.cleartext();
}
s_assert.assertAll();
}
Looks like from the error message you have posted they must be in a special order of indentation?
Here is an example of my XML which works
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite verbose="1" name="example suite 1">
<listeners>
<listener class-name="ReportListener.ReportListenerClass" />
</listeners>
<test name="Logout tests">
<classes>
<class name="com.emc.qe.u360.tests.LogoutTests" />
</classes>
</test>
<test name="Login tests">
<classes>
<class name="com.emc.qe.u360.tests.LoginPageTests" />
</classes>
</test>
</suite>

TestNG:Cannot find class in classpath, testng.xml

This is my testng.xml file, i named it differently, but it should still work:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="TEST_LINKS">
<test name="TECT на подбор спеца">
<classes>
<class name="Podbor_specalista_url_click_test" />
</classes>
</test>
<test name="TECT на клик блога">
<classes>
<class name="Blog_url_click_test" />
</classes>
</test>
</suite>
Now, i get the error, but where do I fix it?
i want to run the whole test suite consisting of these tests:
You need to give the fully qualified class name of the class you want to test in class tag, something like this
<suite thread-count="5" name="Ant suite" junit="false" annotations="JDK">
<test name="Ant test">
<classes>
<class name="com.sample.test.MyUnitTest"/>
</classes>
</test>
</suite>

Categories