Generate TestNg.xml Dynamically
Generate TestNg.xml Dynamically
We can create a virtual testng.xml file yourself. In order to do this, you can use the classes found the packageorg.testng.xml: XmlClass, XmlTest, etc... Each of these classes correspond to their XML tag counterpart.
For example, suppose you want to create the following virtual file:
<suite name="TmpSuite" >
<test name="TmpTest" >
<classes>
<class name="test.failures.Child" />
<classes>
</test>
</suite>
You would use the following code:
XmlSuite suite = new XmlSuite();
suite.setName("TmpSuite");
XmlTest test = new XmlTest(suite);
test.setName("TmpTest");
List<XmlClass> classes = new ArrayList<XmlClass>();
classes.add(new XmlClass("test.failures.Child"));
test.setXmlClasses(classes) ;
And then you can pass this XmlSuite to TestNG:
List<XmlSuite> suites = new ArrayList<XmlSuite>();
suites.add(suite);
TestNG tng = new TestNG();
tng.setXmlSuites(suites);
tng.run();
Program
package org.nitin;
import java.util.ArrayList;
import java.util.List;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.TestListenerAdapter;
import org.testng.TestNG;
import org.testng.annotations.Test;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;
public class TCase1 {
@Test
public void TCase()
{
System.setProperty("webdriver.chrome.driver","C:\\Documents and Settings\\Administrator\\My Documents\\Downloads\\chromedriver_win32_2.1\\chromedriver.exe");
ChromeDriver f = new ChromeDriver();
f.get("http://mail.in.com");
f.findElementById("f_id").sendKeys("uname");
f.findElementById("f_pwd").sendKeys("pass");
f.findElementByClassName("signin");
XmlSuite suite = new XmlSuite();
suite.setName("OMS");
XmlTest test1 = new XmlTest(suite);
test1.setName("TCase1");
List<XmlClass> classes = new ArrayList<XmlClass>();
classes.add(new XmlClass("org.nitin.TCase1"));
List<XmlSuite> suite_a = new ArrayList<XmlSuite>();
suite_a.add(suite);
TestNG tng = new TestNG();
tng.setXmlSuites(suite_a);
tng.run();
/*XmlSuite suite = new XmlSuite();
suite.setName("OMS");
XmlTest test = new XmlTest(suite);
test.setName("Case1");
List<XmlClass> classes = new ArrayList<XmlClass>();
classes.add(new XmlClass("org.nitin.TCase1"));
test.setXmlClasses(classes) ;
List<XmlSuite> suites = new ArrayList<XmlSuite>();
suites.add(suite);
TestNG tng = new TestNG();
tng.setXmlSuites(suites);
tng.run();
*/
}
}
We can create a virtual testng.xml file yourself. In order to do this, you can use the classes found the packageorg.testng.xml: XmlClass, XmlTest, etc... Each of these classes correspond to their XML tag counterpart.
For example, suppose you want to create the following virtual file:
<suite name="TmpSuite" >
<test name="TmpTest" >
<classes>
<class name="test.failures.Child" />
<classes>
</test>
</suite>
You would use the following code:
XmlSuite suite = new XmlSuite();
suite.setName("TmpSuite");
XmlTest test = new XmlTest(suite);
test.setName("TmpTest");
List<XmlClass> classes = new ArrayList<XmlClass>();
classes.add(new XmlClass("test.failures.Child"));
test.setXmlClasses(classes) ;
And then you can pass this XmlSuite to TestNG:
List<XmlSuite> suites = new ArrayList<XmlSuite>();
suites.add(suite);
TestNG tng = new TestNG();
tng.setXmlSuites(suites);
tng.run();
Program
package org.nitin;
import java.util.ArrayList;
import java.util.List;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.TestListenerAdapter;
import org.testng.TestNG;
import org.testng.annotations.Test;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;
public class TCase1 {
@Test
public void TCase()
{
System.setProperty("webdriver.chrome.driver","C:\\Documents and Settings\\Administrator\\My Documents\\Downloads\\chromedriver_win32_2.1\\chromedriver.exe");
ChromeDriver f = new ChromeDriver();
f.get("http://mail.in.com");
f.findElementById("f_id").sendKeys("uname");
f.findElementById("f_pwd").sendKeys("pass");
f.findElementByClassName("signin");
XmlSuite suite = new XmlSuite();
suite.setName("OMS");
XmlTest test1 = new XmlTest(suite);
test1.setName("TCase1");
List<XmlClass> classes = new ArrayList<XmlClass>();
classes.add(new XmlClass("org.nitin.TCase1"));
List<XmlSuite> suite_a = new ArrayList<XmlSuite>();
suite_a.add(suite);
TestNG tng = new TestNG();
tng.setXmlSuites(suite_a);
tng.run();
/*XmlSuite suite = new XmlSuite();
suite.setName("OMS");
XmlTest test = new XmlTest(suite);
test.setName("Case1");
List<XmlClass> classes = new ArrayList<XmlClass>();
classes.add(new XmlClass("org.nitin.TCase1"));
test.setXmlClasses(classes) ;
List<XmlSuite> suites = new ArrayList<XmlSuite>();
suites.add(suite);
TestNG tng = new TestNG();
tng.setXmlSuites(suites);
tng.run();
*/
}
}
Subscribe to:
Post Comments
(
Atom
)
ReplyDeleteVery informative ..i suggest this blog to my friends..Thank you for sharing
Selenium Training in velachery | selenium training and placement in chennai |Selenium Training in chennai
The blog gave me idea to generate testNG.xml file dynamically My sincere thanks for sharing this post
ReplyDeleteSelenium Training in Chennai
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteWhat if i want to implement a listeners tag
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteExcellent way of writing and expressing your thoughts and ideas to the readers, very much impressed in your article. Keep doing more, waiting to read your next blog.
ReplyDeleteRegards:
software testing course in chennai
software testing training institute chennai
I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly.
ReplyDeletenebosh course in chennai
I am really thankful for posting such useful information. It really made me understand lot of important concepts in the topic. Keep up the good work!
ReplyDeleteOracle Training in Chennai | Oracle Course in Chennai
mmorpg oyunlar
ReplyDeleteinstagram takipçi satın al
tiktok jeton hilesi
tiktok jeton hilesi
antalya saç ekimi
Takipci Satın Al
instagram takipçi satın al
METİN2 PVP SERVERLAR
instagram takipçi satın al
Smm panel
ReplyDeletesmm panel
İS İLANLARİ
İnstagram Takipçi Satın Al
hirdavatciburada.com
Https://www.beyazesyateknikservisi.com.tr
Servis
tiktok jeton hilesi