As i convey described inwards my previous post, We tin john configure our webdriver examine or webdriver examine suits for software testing projection inwards testng.xml file. In my previous post, nosotros convey seen how to exercise testng.xml file to operate unmarried examine flat for software spider web application. Also If you lot don't know how to create in addition to operate commencement TestNG-WebDriver test, You tin john VIEW THIS POST. Now supposing you lot convey two/multiple classes inwards your examine suite for software spider web application in addition to thus how volition you lot operate them? We tin john operate both the classes inwards same examine equally good inwards 2 unlike tests too for software spider web application.
First of all, Create iii classes nether projection = TestNGOne in addition to packet = TestNGOnePack equally bellow.
- "BaseClassOne" volition live on used for initializing in addition to closing webdriver instance,
- "ClassOne" in addition to "ClassTwo" volition live on used equally examine classes.
1. BaseClassOne.java
package TestNGOnePack; import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.AfterSuite; import org.testng.annotations.BeforeSuite; populace flat BaseClassOne { //Declared equally populace static to role same webdriver event publicly populace static WebDriver driver = novel FirefoxDriver(); //@BeforeSuite notation describes this method has to operate earlier all suites @BeforeSuite populace void setup() throws Exception { driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); driver.get(" "); } //@AfterSuite notation describes this method has to operate afterwards execution of all suites @AfterSuite populace void tearDown() throws Exception { driver.quit(); } }
Above flat volition live on used equally base of operations flat to initialize in addition to closed webdriver instance.2. ClassOne.java
package TestNGOnePack; import org.testng.annotations.Test; populace flat ClassOne extends TestNGOnePack.BaseClassOne{ //@Test notation describes this method equally a examine method @Test populace void testmethodone() { String championship = driver.getTitle(); System.out.print("\nCurrent page championship is : "+title); String Workdir = System.getProperty("user.dir"); String Classpackname = this.getClass().getName(); System.out.print("\n'"+Workdir+" -> "+Classpackname+" -> testmethodone' has been executed successfully"); } }
Above ClassOne is inherited from BaseClassOne.
3. ClassTwo.java
package TestNGOnePack; import org.testng.annotations.Test; populace flat ClassTwo extends TestNGOnePack.BaseClassOne{ //@Test notation describes this method equally a examine method @Test populace void testmethodone() { driver.navigate().to(" "); String championship = driver.getTitle(); System.out.print("\nCurrent page championship is : "+title); String Workdir = System.getProperty("user.dir"); String Classpackname = this.getClass().getName(); System.out.print("\n'"+Workdir+" -> "+Classpackname+" -> testmethodone' has been executed successfully"); } }
Above ClassTwo is also inherited from BaseClassOne.
Now your flat construction volition live on looks similar bellow inwards eclipse.
Configure testng.xml to operate ii classes inwards i test
Now allow me exhibit you lot how to configure testng.xml to operate both classes inwards unmarried test. Copy-paste bellow given lines inwards your testng.xml file in addition to and thus Run it equally TestNg Suite. Here, both classes are included inwards unmarried examine (Test One).
<suite name="Suite One" >
<test name="Test One" >
<classes> <class name="TestNGOnePack.ClassOne" /> <class name="TestNGOnePack.ClassTwo" /> </classes>
</test>
</suite>
When execution completed, View execution effect reports. It volition looks similar bellow.
Configure testng.xml to operate ii classes inwards ii tests
Now if you lot wants to operate both classes equally split examine in addition to thus you lot convey to configure your testng.xml file equally bellow.
Here, "ClassOne" is included inwards 'Test One" examine in addition to "ClassTwo" is included inwards 'Test Two" test. Now operate bellow given testng.xml file in addition to verify result.
<suite name="Suite One" >
<test name="Test One" > <classes> <class name="TestNGOnePack.ClassOne" /> </classes> </test> <test name="Test Two" > <classes> <class name="TestNGOnePack.ClassTwo" /> </classes> </test>
</suite>
Here, "ClassOne" is included inwards 'Test One" examine in addition to "ClassTwo" is included inwards 'Test Two" test. Now operate bellow given testng.xml file in addition to verify result.
Now compare both the results. In 1st example, Both classes are executed nether same examine but inwards sec illustration both classes are executed nether split tests. This agency you lot tin john configure testng.xml file equally per your requirement.