When executing testng xml, it executes only 1 test in the suite & does not execute rest of the test cases. If I add all tests in single class in that suite, it executes all tests but when they are in separate classes, it will execute only 1 test. Below are testng xml, BasicTest & LoginTest- TestNG xml is as below:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite thread-count="1" verbose = "2" name="Suite" configfailurepolicy="skip">
<test name="all-tests">
<classes>
<class name="Test.DemoMavenEclipseProject.BasicTest"/>
<class name="Test.DemoMavenEclipseProject.LoginTest"/>
<class name="Test.DemoMavenEclipseProject.AllProductsTest"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
BasicTest -
package Test.DemoMavenEclipseProject;
import org.testng.ITestResult;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.io.FileHandler;
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;
import Actions.DriverManager;
import Actions.DriverManagerFactory;
import Actions.DriverType;
import Utility.Constants;
public class BasicTest {
DriverManager driverManager;
WebDriver driver;
ExtentReports report;
ExtentTest logger;
@BeforeSuite
public void setUp() throws Exception {
report=new ExtentReports("./Reports/TestReport.html");
driverManager = DriverManagerFactory.getDriverManager(DriverType.CHROME);
driver = driverManager.getWebDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get(Constants.URL);
driver.manage().window().maximize();
}
@AfterSuite
public void getResult(ITestResult result) throws IOException {
if (result.getStatus()==ITestResult.FAILURE) {
logger.log(LogStatus.FAIL, "TEST CASE FAILED IS " + result.getName()); // to add name in extent report
logger.log(LogStatus.FAIL, "TEST CASE FAILED IS " + result.getThrowable());
String screenshotPath = BasicTest.getScreenshot(driver, result.getName());
logger.addScreenCapture(screenshotPath);// adding screen shot
}
else if (result.getStatus()==ITestResult.SKIP) {
logger.log(LogStatus.SKIP, "Test Case SKIPPED IS " + result.getName());
}
else if (result.getStatus() == ITestResult.SUCCESS) {
logger.log(LogStatus.PASS, "Test Case PASSED IS " + result.getName());
}
}
public void tearDown() throws Exception {
report.endTest(logger);
//Flush the data to report
driver.quit();
report.flush();
}
public static String getScreenshot(WebDriver driver, String screenshotName) throws IOException {
String dateName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
TakesScreenshot ts = (TakesScreenshot) driver;
File source = ts.getScreenshotAs(OutputType.FILE);
// after execution, you could see a folder "FailedTestsScreenshots" under src folder
String destination = "./Reports/Screenshots/" + screenshotName + dateName + ".png";
File finalDestination = new File(destination);
FileHandler.copy(source, finalDestination);
// FileUtils.copyFile(source, finalDestination);
return destination;
}
}
LoginTest -
package Test.DemoMavenEclipseProject;
import org.testng.annotations.Test;
import com.relevantcodes.extentreports.LogStatus;
import Actions.login_action;
import PageObjects.homePage;
public class LoginTest extends BasicTest {
@Test
public void Login() throws Exception {
//Create object for Report with filepath
//Start the test
logger=report.startTest("LoginTest");
//Log the status in report
logger.log(LogStatus.INFO, "Login link is displayed");
login_action.Execute(driver);
if (homePage.link_Logout(driver).isDisplayed())
logger.log(LogStatus.PASS, "Logged into the site successfully");
else
logger.log(LogStatus.FAIL, "Login is unsuccessful");
//End the test
Thread.sleep(2000);
}
}
@Teston each test. Pretty good write-up here. If this solves your issue, I convert it to an answer you can accept. - Brian