I'm currently automating web app using Selenium WebDriver framework together with TestNG. I want to provide parameters for each test classes within testing.xml file, but on the web resources, I've seen only approach where certain parameters are predefined to use for single classes like:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parameterization Test Suite">
<test name="Testing Parameterization">
<parameter name="browser" value="Firefox"/>
<parameter name="username" value="testuser"/>
<parameter name="password" value="testpassword"/>
<classes>
<class name="com.parameterization.TestParameters" />
</classes>
</test>
</suite>
where the class name, clearly states only one TestParameters class.
Is there any way to set some parameters right there as global ones? So we can use them in any test class within the project?
I'm not really sure if I understand your question, but you can define parameters on suite level (for each test, in the example suite-param), or on test level:
As taken from https://howtodoinjava.com/testng/testng-test-parameters-through-testng-xml-and-parameters-annotation/
<suite name="Parameter test Suite" verbose="1">
<!-- This parameter will be passed to every test in this suite -->
<parameter name="suite-param" value="suite level parameter" />
<test name="Parameter Test one">
<classes>
<class name="com.howtodoinjava.test.ParameterTest">
<methods>
<include name="prameterTestOne" />
</methods>
</class>
</classes>
</test>
<test name="Parameter Test two">
<!-- This parameter will be passed this test only -->
<parameter name="test-two-param" value="Test two parameter" />
<classes>
<class name="com.howtodoinjava.test.ParameterTest">
<methods>
<include name="prameterTestTwo" />
</methods>
</class>
</classes>
</test>
<test name="Parameter Test three">
<!-- Overriding suite level parameter -->
<parameter name="suite-param" value="overiding suite parameter" />
<!-- Test specific parameter -->
<parameter name="test-three-param" value="test three parameter" />
<classes>
<class name="com.howtodoinjava.test.ParameterTest">
<methods>
<include name="prameterTestThree" />
</methods>
</class>
</classes>
</test>
</suite>
Related
In my TestNG.xml file , I have two different values for Test level parameter, "TestRailRunId".
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite thread-count="4" name="Change_Management_Regression_Suite" parallel="tests">
<listeners>
<listener class-name="com.sam.reporting.SuiteListener" />
<listener class-name="com.sam.reporting.Listeners" />
</listeners>
<parameter name="browser" value="Chrome" />
<parameter name="browserVersion" value="104" />
<test name="CM-04">
<parameter name="TestRailRunId" value="200" />
<classes>
<class name="com.sam.testdata.TC01"/>
</classes>
</test> <!-- Test -->
<test name="CM-04">
<parameter name="TestRailRunId" value="201" />
<classes>
<class name="com.sam.testdata.TC02"/>
</classes>
</test> <!-- Test -->
</suite>
So in my "SuiteListener" class I implements ISuiteListener class. So in it's onStart method,
I want to get the TestRailRunId value from current test.
IS this possible? If so please help.
Currently what I did was defining it in Suite level and using below code snippet ,get the value.
String TestRailRunId = suite.getXmlSuite().getParameter("TestRailRunId");
Here is the current full code.
#Override
public void onStart(ISuite suite) {
String TestRailRunId = suite.getXmlSuite().getParameter("TestRailRunId");
}
I need to pass a different parameter value for different classes inside a test. The following code gives me an error saying :
"The content of element type "classes" must match
'(class*,parameter*)'.".
Is there any other way I can pass the parameter value to be accessible by a class?
<suite name="Project">
<test thread-count="5" name="Test">
<classes>
<parameter name="URL" value="https://testRunner1.com" />
<class name="project.TestRunner1"/>
<parameter name="URL" value="https://testRunner2.com" />
<class name="project.TestRunner2"/>
</classes>
</test>
</suite>
There are two solutions.
1. Move the classes to different test as given below. Here you can have same name for the Parameters.
<suite name="Project">
<test thread-count="5" name="Test">
<classes>
<parameter name="URL" value="https://testRunner1.com" />
<class name="project.TestRunner1"/>
</classes>
</test>
<test thread-count="5" name="Test">
<parameter name="URL" value="https://testRunner2.com" />
<class name="project.TestRunner2"/>
</test>
</suite>
You need to change the parameter name in the class and change the xml as given below
I rewrote my code like below which worked.
<suite name="Project">
<test thread-count="5" name="Test">
<classes>
<class name="project.TestRunner1">
<parameter name="URL" value="https://testRunner1.com" />
</class>
<class name="project.TestRunner2">
<parameter name="URL" value="https://testRunner2.com" />
</class>
</classes>
</test>
</suite>
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
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.
I have a TestNG test suite that is working fine except for one thing: I cannot run the same test twice. The reason I want to run a test twice is that I am testing web services that log a user in/out, and I want to verify that the login works, then that the logout work, and then log them in again so that the subsequent tests, which require the user to be logged in, can proceed.
Here is the relevant section of my testng.xml:
<suite name="WebServiceTestSuite">
<test name="webServiceTest" verbose="10" parallel="none">
<classes>
<class name="com.abcco.webservice.DivisionTest">
<methods>
<include name="registrationCreateTest"></include>
<include name="registrationActivateTest"></include>
<include name="appUserLoginTest"></include>
<include name="appUserLogoutTest"></include>
<include name="appUserLoginTest"></include>
<include name="sessionValidateTest"></include>
<include name="sessionExtendTest"></include>
</methods>
</class>
</classes>
</test>
</suite>
As you can see, the "appUserLoginTest" is called twice. However, when I debug this I can clearly see that it is only actually run the first time it is called. After that the execution proceeds as if the second line calling it didn't exist.
This is actually by design in TestNG. I see two potential options:
First, look at using a DataProvider with your tests so they can be executed repeatedly with different parameters.
Or, alter the XML layout so that each method is part of its own tag. In this configuration, the XML will be longer, but you'll be able to invoke a test method as many times as you like as part of a suite.
EDIT: More details now.
In response to your comment, here's a sample XML layout that worked for me when I was up against this same problem. Try this:
<suite name="WebServiceTestSuite">
<test name="webServiceTest">
<classes>
<class name="com.abcco.webservice.DivisionTest">
<methods>
<include name="registrationCreateTest"></include>
</methods>
</class>
</classes>
</test>
<test name="webServiceTest">
<classes>
<class name="com.abcco.webservice.DivisionTest">
<methods>
<include name="registrationActivateTest"></include>
</methods>
</class>
</classes>
</test>
<test name="webServiceTest">
<classes>
<class name="com.abcco.webservice.DivisionTest">
<methods>
<include name="appUserLoginTest"></include>
</methods>
</class>
</classes>
</test>
<test name="webServiceTest">
<classes>
<class name="com.abcco.webservice.DivisionTest">
<methods>
<include name="appUserLogoutTest"></include>
</methods>
</class>
</classes>
</test>
<test name="webServiceTest">
<classes>
<class name="com.abcco.webservice.DivisionTest">
<methods>
<include name="appUserLoginTest"></include>
</methods>
</class>
</classes>
</test>
<test name="webServiceTest">
<classes>
<class name="com.abcco.webservice.DivisionTest">
<methods>
<include name="sessionValidateTest"></include>
</methods>
</class>
</classes>
</test>
<test name="webServiceTest">
<classes>
<class name="com.abcco.webservice.DivisionTest">
<methods>
<include name="sessionExtendTest"></include>
</methods>
</class>
</classes>
</test>
</suite>
I removed the verbose="10" parallel="none" from the <test> tags, as the layout that worked for me didn't have this... You may be able to add the verbose="10" to the <suite> tag to get any specific functionality there that you need. Have a go with this and post your results and I'll be happy to help further. Without seeing your runner configuration or knowing too much about the rest of your setup, there may be some other things to consider.
You'll find that this is a much more verbose XML layout, but it solved this same problem for me at the time. You could also consider re-arranging your code a bit and making an #Test method that calls all the methods on this XML as a single test, allowing you to reduce the length of your XML file.
Solving the problem of code duplication in TestNG test I could find only this solution:
Create a rootsuite.xml file like:
<suite name="Description">
<suite-files>
<suite-file path="../../Actions/Test1.xml"/>
<suite-file path="../../Actions/Test2.xml"/>
<suite-file path="Actions/Test3.xml"/>
<suite-file path="../../Common/Close Browser.xml"/>
</suite-files>
<suite-files>
<suite-file path="../../Actions/Test1.xml"/>
<suite-file path="../../Actions/Test2.xml"/>
<suite-file path="Actions/Test4.xml"/>
<suite-file path="../../Common/Close Browser.xml"/>
</suite-files>
</suite>
Important! You can not include duplicated suite-files inside Test1.xml, Test2.xml, Test3.xml, Test4.xml - all duplicated suites will be ignored.
Note the version of TestNG must be at least 6.9.9
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.9</version>
<scope>test</scope>
</dependency>