public class FactoryTest {
#Test
#Parameters("Row")
public void run1(int row) throws MalformedURLException{
new Controller(row);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="methods">
<test thread-count="2" name="factory test" parallel="methods">
<classes>
<class name="RealPackage.FactoryTest">
<methods>
<include name="run1">
<parameter name="Row" value="1"/>
</include>
</methods></class>
</classes>
</test> <!-- OfficialTestName -->
</suite> <!-- Suite -->
This is an example of one of the tests I need to run. I need it to run in parallel with other tests. So in the test run1() I create a Controller(row) which initiates the test and I pass a row number to it. I want to run new Controller(1) and new Controller(2) and new Controller(3), etc all at the same time. I am able to do this if I change the java file to this:
public class OfficialTest {
#Test
public void run1() throws MalformedURLException{
new Controller(1);
}
#Test
public void run2() throws MalformedURLException{
new Controller(2);
}
#Test
public void run3() throws MalformedURLException{
new Controller(3);
}
#Test
public void run4() throws MalformedURLException{
new Controller(4);
}
#AfterMethod
public void close() {
System.out.println("closing");
}
}
but this isn't dynamic. I need to be able to run this using any range of number for the row. So I was thinking maybe I could generate an XML file that would take care of this but I still am not sure if it would even be able to run in parallel that way.
I was able to fix it with this:
public class ParallelTests
{
int row;
#Parameters({"Row"})
#BeforeMethod()
public void setUp(int rowParam) throws MalformedURLException
{
row = rowParam;
}
#Test
public void RunTest() throws InterruptedException, MalformedURLException
{
new Controller(row);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite thread-count="5" name="BlogSuite" parallel="tests">
<test name="Test 1">
<parameter name="Row" value="1"/>
<classes>
<class name="RealPackage.ParallelTests"/>
</classes>
</test>
<test name="Test 2">
<parameter name="Row" value="2"/>
<classes>
<class name="RealPackage.ParallelTests"/>
</classes>
</test>
<test name="Test 3">
<parameter name="Row" value="3"/>
<classes>
<class name="RealPackage.ParallelTests"/>
</classes>
</test>
<test name="Test 4">
<parameter name="Row" value="4"/>
<classes>
<class name="RealPackage.ParallelTests"/>
</classes>
</test>
<test name="Test 5">
<parameter name="Row" value="5"/>
<classes>
<class name="RealPackage.ParallelTests"/>
</classes>
</test>
</suite>
Related
I have facing one issue while executing the functions of a selenium class using TestNG XML + java.
The issue is, when I call both the test case functions under TC1 test name (say: <include name=“TC1” /> <include name=“TC2” /> ) am able to execute both the functions, but when I separate them like shown in below XML execution is getting failed, but no complication errors.
Below are the classes and XML I have created.
Main.class :
public class MainClass {
public static void main(String[] args) throws IOException {
TestNG runner = new TestNG();
List<String> suitefiles = new ArrayList<String>();
suitefiles.add(System.getProperty("user.dir") + “/Test.xml");
runner.setTestSuites(suitefiles);
runner.run();
}
}
Test.XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name=“Test” verbose="1" parallel="none" thread-count="1"
configfailurepolicy="skip">
<test name=“TC1” preserve-order="true"
enabled="true">
<groups>
<run>
<include name=“TC” />
</run>
</groups>
<classes>
<class name="com.callingfunction”>
<methods>
<include name=“TC1” />
</methods>
</class>
</classes>
</test>
<test name=“TC2” preserve-order="true"
enabled="true">
<groups>
<run>
<include name=“TC” />
</run>
</groups>
<classes>
<class name="com.callingfunction">
<methods>
<include name=“TC2” />
</methods>
</class>
</classes>
</test>
</suite>
CallingFunction.class
public class CallingFunction {
#BeforeSuite(alwaysRun = true)
public void prerequisite() {
// prerequisites
}
#Test(groups = { “TC” })
public void TC1(){
}
#Test(groups = { “TC” })
public void TC2() {
}
}
I am trying to run two test classes in parallel both are using data providers pointing to different excel sheets. Sequentially it runs fine, but while running it in parallel it seems like the test object class which is holding all the test data is getting overwritten. Below is the data provider
Class 1
#DataProvider(name = "tradeData" )
public Object[][] createMessage() {
return ExcelUtils
.getDataFromExcelSheet("TestSheet1", LOADTESTDATAPROVIDER);
}}
Class 2
#DataProvider(name = "tradeData")
public Object[][] createMessage() {
return ExcelUtils
.getDataFromExcelSheet("TestSheet2", LOADTESTDATAPROVIDER);
}
The data is being returned correctly from the excel but it seems like it is getting overwritten when placed into the object class. The data object class is being initialized from both test classes something like this
#Test(dataProvider = "tradeData")
public void connectAndPushMessage(String sellerName,
String buyerName,
String SIN){
testMessage tMessage = new testMessage();
tMessage.setSellerName(sellerName);
tMessage.setBuyerName(buyerName);
tMessage.setSIN(SIN);
}
Below is the testng.xml that I have used.
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Parallel Testing" parallel="classes" thread-count="2">
<test name="Test run 1">
<classes>
<class name="test.xxx.performance.InjectTrades_TestRun1"/>
<class name="test.xxx.performance.InjectTrades_TestRun2"/>
</classes>
</test>
</suite>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Parallel Testing" parallel="tests" thread-count="2">
<test name="Test run 1">
<classes>
<class name="test.xxx.performance.InjectTrades_TestRun1"/>
</classes>
</test>
<test name="test run 2">
<classes>
<class name="test.xxx.performance.InjectTrades_TestRun2"/>
</classes>
</test>
I am getting the same result from both xmls so rather six distinct messages being created/pushed into the system(based on two excel sheets) the code is pushing three distinct messages repeated twice. I was/am under the impression that testNg should have take care of this as both classes/tests are running as separate threads.
EDIT
Below is the snippet of the Test object class. All attributes are declared private
public class testMessage{
private String currentBusinessDate;
private String sellerName = "";
private String buyerName= "";
private String sin= "";
....
public testMessage() {
cTradeID = "T" + CommonUtils.randomNumber(12);
sRef = CommonUtils.randomNumber(12);
requestId = "1" + CommonUtils.randomNumber(5);
settlementRequestStateCode = "New";
currencyTypeCode = "USD";
}
public String getCurrentBusinessDate() {
return currentBusinessDate;
}
public void setCurrentBusinessDate(String currentBusinessDate) {
this.currentBusinessDate = currentBusinessDate;
}
public String getsellerName () {
return currentBusinessDate;
}
public void setsellerName (String currentBusinessDate) {
this.currentBusinessDate = currentBusinessDate;
}
public String getBuyerName () {
return currentBusinessDate;
}
public void setBuyerName (String currentBusinessDate) {
this.currentBusinessDate = currentBusinessDate;
}
....
Used TestNG version is not mentioned, also ExcelUtils.getDataFromExcelSheet implementation is not shared, but both these 2 things might be related to such unexpected behavior.
I'm going to share my experiment result in order to confirm, that there is no issue with the TestNG parallelization itself, at least for TestNG 7.5.
Test Class 1
class T1 {
#Test(dataProvider = "tradeData")
public void connectAndPushMessage(String arg) {
System.out.println(arg);
}
#DataProvider(name = "tradeData")
public Object[][] createMessage() {
return new Object[]{
{ "T1.createMessage-data-1" },
{ "T1.createMessage-data-2" },
{ "T1.createMessage-data-3" },
};
}
}
Test Class 2
class T2 {
#Test(dataProvider = "tradeData")
public void connectAndPushMessage(String arg) {
System.out.println(arg);
}
#DataProvider(name = "tradeData")
public Object[][] createMessage() {
return new Object[]{
{ "T2.createMessage-data-1" },
{ "T2.createMessage-data-2" },
{ "T2.createMessage-data-3" },
};
}
}
testng.xml
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Parallel Testing" parallel="tests" thread-count="2">
<test name="Test run 1">
<classes>
<class name="samples.T1"/>
</classes>
</test>
<test name="test run 2">
<classes>
<class name="samples.T2"/>
</classes>
</test>
</suite>
Output:
T1.createMessage-data-1
T2.createMessage-data-1
T2.createMessage-data-2
T1.createMessage-data-2
T2.createMessage-data-3
T1.createMessage-data-3
The same for
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Parallel Testing" parallel="classes" thread-count="2">
<test name="Test run 1">
<classes>
<class name="samples.T1"/>
<class name="samples.T2"/>
</classes>
</test>
</suite>
Try to recheck this with TestNG 7.5, and look at your ExcelUtils.getDataFromExcelSheet method implementation, it should be thread-safe.
I want to Skip (so I will see at the report that the tests / classes were skipped) with ITestListener, what I did is the listener class like so:
#Override
public void onStart(ITestContext context) {
String deviceName = context.getCurrentXmlTest().getParameter("deviceName");
System.out.println("device name : "+deviceName);
String className = context.getClass().getSimpleName();
System.out.println("class name : "+className);
if(!deviceName.contains("Galaxy S8")) {
System.out.println("Skipping class name : "+className);
throw new SkipException("Skipping class: " + className);
}
}
and I have test class :
#Test(priority = 1)
public void installAndDenyPhoneAccess() {
onePlus6ProInstallProcess.denyPhoneAccess();
}
at the testng.xml I added the listener:
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel = "tests"
configfailurepolicy="continue">
<listeners>
<listener class-name="com.qa.listeners.TestListener" />
</listeners>
<test name="Samsung Note 10 - 5G">
<listeners>
<listener class-name="com.qa.listeners.TestListener" />
</listeners>
<parameter name="emulator" value="false" />
<parameter name="platformName" value="Android" />
<parameter name="udid" value="RPM" />
<parameter name="deviceName" value="Note 10 - 5G" />
<parameter name="systemPort" value="10000" />
<parameter name="chromeDriverPort" value="11000" />
<classes>
<class name="note5.Installation"/>
</classes>
</test>
<test name="Redmi 6A">
<listeners>
<listener class-name="com.qa.listeners.TestListener" />
</listeners>
<parameter name="emulator" value="false" />
<parameter name="platformName" value="Android" />
<parameter name="udid" value="192b" />
<parameter name="deviceName" value="Redmi 6A" />
<parameter name="systemPort" value="10000" />
<parameter name="chromeDriverPort" value="11000" />
<classes>
<class name="redmi6A.Installation"/>
</classes>
</test>
<test name="OnePlus 6 Pro">
<parameter name="emulator" value="false" />
<parameter name="platformName" value="Android" />
<parameter name="udid" value="73" />
<parameter name="deviceName" value="onePlus 6Pro" />
<parameter name="systemPort" value="10000" />
<parameter name="chromeDriverPort" value="11000" />
<classes>
<class name="onePlus6Pro.Installation"/>
</classes>
</test>
<test name="Galaxy S8">
<parameter name="emulator" value="false" />
<parameter name="platformName" value="Android" />
<parameter name="udid" value="ce3cb10c" />
<parameter name="deviceName" value="Galaxy S8" />
<parameter name="systemPort" value="10000" />
<parameter name="chromeDriverPort" value="11000" />
<classes>
<class name="galaxyS8.Installation"/>
</classes>
</test>
<test name="Samsung A21s">
<listeners>
<listener class-name="com.qa.listeners.TestListener" />
</listeners>
<parameter name="emulator" value="false" />
<parameter name="platformName" value="Android" />
<parameter name="udid" value="RdM" />
<parameter name="deviceName" value="Samsung A21s" />
<parameter name="systemPort" value="10000" />
<parameter name="chromeDriverPort" value="11000" />
<classes>
<class name="a21S.Installation"/>
</classes>
</test>
now the tests are not skipped and anyhow not all of them when runing together, only when run two.
So how to skip it? and also how to skip the all class if it can?
IAnnotationTransformer is used to modify the #Test annotation. And there is no option to skip a method using the #Test annotation. In order to forcefully skip it, you need to throw a SkipException.
From the code, it seems that you need to skip the test based on the class name alone and method names are not relevant. So I would suggest the use of ITestListener by overriding the onTestStart method as below:
public class MyTestListener implements ITestListener {
#Override
public void onTestStart(ITestResult result) {
String className = result.getTestClass().getName();
if(className.contains("abc") || className.contains("xyz")) {
throw new SkipException("Skipping class: " + className);
}
}
}
If you want to do this at the method level, you could use IInvokedMethodListener:
public class MyListener implements IInvokedMethodListener {
#Override
public void beforeInvocation(IInvokedMethod method, ITestResult result) {
Map<String, String> paramMap = method.getTestMethod()
.findMethodParameters(result.getTestContext().getCurrentXmlTest());
String className = method.getTestMethod().getTestClass().getName();
if(className.contains(paramMap.get("deviceName"))) {
throw new SkipException("Skipping class: " + className);
}
}
}
I'm doing some tests using Java + TestNG, but I noticed that the tests are not executing the #AfterTest method. The browser remains open when the other tests are running (when it runs the first test, CreateNewUserWithValidData(), this one doesn't call the #AfterTest method, causing that the other tests fail). I need that every test call the #AfterTest method.
My testng.xml file has the following structure:
<suite name="Sample Tests" verbose="1" >
<listeners>
<listener class-name="Utilities.Listeners.TestListener"></listener>
<listener class-name="Utilities.Listeners.AnnotationTransformer"></listener>
</listeners>
<test name="Regression" >
<classes>
<class name="Tests.AutomationPracticesTests">
<methods>
<include name="CreateNewUserWithValidData" />
<include name="LoginWithAValidUser" />
<include name="LoginWithAnInvalidUser" />
</methods>
</class>
</classes>
</test>
</suite>
My BaseTest class looks like this.-
public class BaseTest {
protected String baseURL;
protected WebDriver driver;
protected WebDriverWait wait;
protected APAuthenticationPage apAuthenticationPage;
protected APCreateAccountPage apCreateAccountPage;
protected APHomePage apHomePage;
protected APMyAccountPage apMyAccountPage;
protected APShoppingCartAddressesPage apShoppingCartAddressesPage;
protected APShoppingCartOrderConfirmationPage apShoppingCartOrderConfirmationPage;
protected APShoppingCartOrderSummaryBankwirePage apShoppingCartOrderSummaryBankwirePage;
protected APShoppingCartPaymentMethodPage apShoppingCartPaymentMethodPage;
protected APShoppingCartShippingPage apShoppingCartShippingPage;
protected APShoppingCartSummaryPage apShoppingCartSummaryPage;
public WebDriver getDriver() {
return driver;
}
#BeforeTest(alwaysRun = true)
public void setUp() {
Log.info("I am in Before Method! Test is starting!");
driver = WebDriverFactory.getDriver(BrowserType.Chrome);
wait = new WebDriverWait(driver, 10);
driver.manage().window().maximize();
}
#BeforeMethod
public void initSetup() {
String propertiesFile = "data.properties";
PropertyReader propertyReader = new PropertyReader();
apAuthenticationPage = new APAuthenticationPage(driver);
apCreateAccountPage = new APCreateAccountPage(driver);
apHomePage = new APHomePage(driver);
apMyAccountPage = new APMyAccountPage(driver);
apShoppingCartAddressesPage = new APShoppingCartAddressesPage(driver);
apShoppingCartOrderConfirmationPage = new APShoppingCartOrderConfirmationPage(driver);
apShoppingCartOrderSummaryBankwirePage = new APShoppingCartOrderSummaryBankwirePage(driver);
apShoppingCartPaymentMethodPage = new APShoppingCartPaymentMethodPage(driver);
apShoppingCartShippingPage = new APShoppingCartShippingPage(driver);
apShoppingCartSummaryPage = new APShoppingCartSummaryPage(driver);
baseURL = propertyReader.getProperty(propertiesFile, "AUTOMATION_PRACTICE_URL");
}
#AfterTest(alwaysRun = true)
public void tearDown() {
Log.info("I am in After Method! Test is ending!");
driver.close();
driver.quit();
}
}
And my tests are the following ones.-
public class AutomationPracticesTests extends BaseTest {
// Properties
private String emailAddress, password;
// Tests
#Test(description = "It creates a new user in the store",
priority = 1)
public void CreateNewUserWithValidData(Method method) {
startTest(method.getName(), "It creates a new user in the store");
emailAddress = Mocks.personalData().get(0).getEmail();
password = Mocks.personalData().get(0).getPassword();
apHomePage.goTo(baseURL);
apHomePage.clickOnSignInButton();
apAuthenticationPage.fillCreateAccountForm(emailAddress);
apAuthenticationPage.clickOnCreateAccountButton();
apCreateAccountPage.fillRegisterForm(Mocks.personalData());
apCreateAccountPage.clickOnRegisterButton();
Assert.assertTrue(apMyAccountPage.isLoaded());
}
#Test(description = "It logins successfully in the store with a valid user",
priority = 2)
public void LoginWithAValidUser(Method method) {
apHomePage.goTo(baseURL);
apHomePage.clickOnSignInButton();
apAuthenticationPage.fillSignInForm(emailAddress, password);
apAuthenticationPage.clickOnSignInButton();
Assert.assertTrue(apMyAccountPage.isLoaded());
}
#Test(description = "It throws an error when the user attempts to login with an invalid user",
priority = 3)
public void LoginWithAnInvalidUser(Method method) {
apHomePage.goTo(baseURL);
apHomePage.clickOnSignInButton();
apAuthenticationPage.fillSignInForm(Mocks.invalidPersonalData().getEmail(), Mocks.invalidPersonalData().getPassword());
apAuthenticationPage.clickOnSignInButton();
Assert.assertEquals("Authentication failed.", apAuthenticationPage.IsErrorBannerDisplayed());
}
}
I'm suspecting that's something related to the testng.xml file (but, tbh, there are some things that I don't understand about how to configure correctly this file).
I'll appreciate any help to solve my problem. Thanks in advance!
It's not a bug. It work as expected.
BeforeTest
BeforeMethod
Method 1: CreateNewUserWithValidData
BeforeMethod
Method 2: LoginWithAValidUser
BeforeMethod
Method 3: LoginWithAnInvalidUser
AfterTest
If you want to close the browser before method 2, then you need to change AfterTest --> AfterMethod, and initialize browser in BeforeMethod
If you just want to change the testng.xml
<test name="test1">
<classes>
<class name="Tests.AutomationPracticesTests">
<methods>
<include name="CreateNewUserWithValidData"/>
</methods>
</class>
</classes>
</test>
<test name="test2">
<classes>
<class name="Tests.AutomationPracticesTests">
<methods>
<include name="LoginWithAValidUser"/>
</methods>
</class>
</classes>
</test>
<test name="test3">
<classes>
<class name="Tests.AutomationPracticesTests">
<methods>
<include name="LoginWithAnInvalidUser"/>
</methods>
</class>
</classes>
</test>
I have created 2 separate classes to test a webpage. But, unfortunately when I add them both to the testing.xml, only one of them execute and the other doesn't. The browsers open in parallel even after setting them to preserve-order="true" parallel="false" in the XML. I'm confused as to where I'm doing it wrong.
This is my XML file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" preserve-order="true" parallel="false">
<test name="Test">
<classes>
<class name="TestServiceNow.loginOne"/>
<class name="TestServiceNow.loginTwo"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
loginOne is as follows:
package TestServiceNow;
import org.testng.annotations.Test;
import ServiceNow.login;
public class loginOne extends loginTest{
#Test
public void test_Login(){
//Create Login Page object
objLogin = new login(driver);
//login to application
objLogin.loginGurukula("admin", "admin");
}
}
loginTwo is as follows:
import org.testng.annotations.Test;
import ServiceNow.login;
public class loginTwo extends loginTest{
#Test
public void test_Login_Fail(){
//Create Login Page object
objLogin = new login(driver);
//login to application
objLogin.loginGurukula("admin", "admin1");
}
}
The base class is as follows:
public class loginTest {
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
File file = new File("C:/Users/gattu_000/Documents/selenium-java-3.0.0-beta2/chromedriver_win32/chromedriver.exe");
WebDriver driver;
login objLogin;
#BeforeClass
public void a() {
driver = new ChromeDriver(capabilities);
capabilities.setCapability("marionette", true);
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
System.out.println("Before class called");
}
#BeforeTest
public void setup(){
System.out.println("Before test called");
driver.get("http://localhost:8080/#/login");
}
#AfterTest
public void close() {
System.out.println("After test called");
}
#AfterClass
public void b() {
System.out.println("After class called");
driver.close();
}
}
The results look like
After the Edit
You are extending loginTest by both loginOne and loginTwo. But in loginTest you initialized your driver. That's why two browser are opening. To get around this issue, you can initialize your driver inside a setup method like #BeforeTest or #BeforeSuite. As an example here's a code snippet -
#BeforeSuite
public void a() {
driver = new ChromeDriver(capabilities);
System.out.println("Before suite called");
}
Do other things as usual like before except the initialization part.
Edit
I missed something. You are closing your driver at the after test method. To run your tests properly remove the driver.close() from your after test method and place it to aftereSuite section.
The XML is supposed to be like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" preserve-order="true">
<test name="Test">
<classes>
<class name="TestServiceNow.loginOne"/>
</classes>
</test> <!-- Test -->
<test name="Test1">
<classes>
<class name="TestServiceNow.loginTwo"/>
</classes>
</test>
</suite> <!-- Suite -->
To launch the browser twice, we need to have 2 separate tests. (Possibly, this may be one of the solutions out of many)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Selenium Test Suite">
<test name="Selenium Test Suite">
<classes>
<class name="packagename.classname1"/>
<class name="packagename.classname1"/>
</classes>
</test>
</suite>
Which is proper. If you are getting null point don't use driver in all the class. because of that only you are getting null pointer i guess.