I have created a test using the TestNG framework in Java in Eclipse and want to execute my tests through an XML file by running it through the testNG Suit.
the file system of my folder is illustrated in the following Link screenshot:
1
my java code:
package a;
import org.testng.annotations.Test;
public class Amir {
#Test
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Test Test");
}
}
my xml code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Test">
<classes>
<class name="a.Amir"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
When i execute above xml using TestNg Suit, on eclipse console i received log as:
[TestNG] Running:
C:\Users\Amir\workspace\SmartAir.co.il\testng.xml
===============================================
Suite
Total tests run: 1, Failures: 0, Skips: 1
===============================================
Could anyone help me on this please?
I found the answer: I have to delete the (String[] args) because TestNG tests do not take any arguments.
Related
I'm using selenium 3.8.1 and TestNG 6.9.2 version,while test execution before completing the #Test method another #Test method is starts,because of this i'm getting error in selenium script After completion of Test Cases execution.
One Class
public class LoginPage{
#Test(priority=0)
public void test1(){
System.out.println(first test);
}
#Test(priority=1)
public void test2(){
System.out.println(Second test);
}
}
Second Class
public class HomePage{
#Test(priority=0)
public void test3(){
System.out.println(first test);
}
#Test(priority=1)
public void test4(){
System.out.println(Second test);
}
}
testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Test" preserve-order="true">
<classes>
<class name="com.tests.day.modules.LoginPage"/>
<class name="com.tests.day.modules.HomePage"/>
</classes>
</test>
</suite>
After Executing the above using testng.xml file before completing the test2 of login page class,test3 is starting of HomePage,because of this i'm getting exception,Unable to Find the Elements.
The Annotations mentions about the preserve-order attribute of TestNG as follows:
By default, TestNG will run your tests in the order they are found in
the XML file. If you want the classes and methods listed in this file
to be run in an unpredictable order, set the preserve-order attribute
to false
I executed the same test similar to your code block and testng.xml as follows :
LoginPage
package testng_order_of_tests_execution;
import org.testng.annotations.Test;
public class LoginPage
{
#Test(priority=0)
public void test1(){
System.out.println("First Test");
}
#Test(priority=1)
public void test2(){
System.out.println("Second Test");
}
}
HomePage
package testng_order_of_tests_execution;
import org.testng.annotations.Test;
public class HomePage
{
#Test(priority=0)
public void test3(){
System.out.println("first test");
}
#Test(priority=1)
public void test4(){
System.out.println("second test");
}
}
testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Test" preserve-order="true">
<classes>
<class name="testng_order_of_tests_execution.LoginPage"/>
<class name="testng_order_of_tests_execution.HomePage"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
What I found as an output on my console was similar to yours as follows :
First Test
first test
Second Test
second test
This Console Output apparently gives us an impression that the sequence of execution was :
test1() -> test3() -> test2() -> test4()
But actually No
Looking at the Result of running suite you will get the actual sequence of execution as per the figure below :
So it's pretty clear that the actual sequence was :
test1() -> test2() -> test3() -> test4()
Trivia
You can be more granular in your observation with the testng-results.xml which is as follows :
<?xml version="1.0" encoding="UTF-8"?>
<testng-results skipped="0" failed="0" ignored="0" total="4" passed="4">
<reporter-output>
</reporter-output>
<suite name="Suite" duration-ms="61" started-at="2017-12-25T12:57:12Z" finished-at="2017-12-25T12:57:12Z">
<groups>
</groups>
<test name="Test" duration-ms="61" started-at="2017-12-25T12:57:12Z" finished-at="2017-12-25T12:57:12Z">
<class name="testng_order_of_tests_execution.HomePage">
<test-method status="PASS" signature="test3()[pri:0, instance:testng_order_of_tests_execution.HomePage#5419f379]" name="test3" duration-ms="4" started-at="2017-12-25T18:27:12Z" finished-at="2017-12-25T18:27:12Z">
<reporter-output>
</reporter-output>
</test-method> <!-- test3 -->
<test-method status="PASS" signature="test4()[pri:1, instance:testng_order_of_tests_execution.HomePage#5419f379]" name="test4" duration-ms="1" started-at="2017-12-25T18:27:12Z" finished-at="2017-12-25T18:27:12Z">
<reporter-output>
</reporter-output>
</test-method> <!-- test4 -->
</class> <!-- testng_order_of_tests_execution.HomePage -->
<class name="testng_order_of_tests_execution.LoginPage">
<test-method status="PASS" signature="test1()[pri:0, instance:testng_order_of_tests_execution.LoginPage#735b5592]" name="test1" duration-ms="14" started-at="2017-12-25T18:27:12Z" finished-at="2017-12-25T18:27:12Z">
<reporter-output>
</reporter-output>
</test-method> <!-- test1 -->
<test-method status="PASS" signature="test2()[pri:1, instance:testng_order_of_tests_execution.LoginPage#735b5592]" name="test2" duration-ms="2" started-at="2017-12-25T18:27:12Z" finished-at="2017-12-25T18:27:12Z">
<reporter-output>
</reporter-output>
</test-method> <!-- test2 -->
</class> <!-- testng_order_of_tests_execution.LoginPage -->
</test> <!-- Test -->
</suite> <!-- Suite -->
</testng-results>
In testng-results.xml you will observe that all the tests starts at 2017-12-25T12:57:12Z and ends at 2017-12-25T12:57:12Z. Though the time taken for Test Execution is even less then 1 second still you may observe the difference in the instancename as instance:testng_order_of_tests_execution.HomePage#5419f379 and instance:testng_order_of_tests_execution.LoginPage#735b5592. As our test was a single threaded test, hence we can conclude that the sequence of execution was proper and as per expectation. But the Console Output got mixed up.
Use group-by-instances="true" inside test tag of the testng.xml
Define your xml test tag like below:
<test name="Test" group-by-instances="true">
Or, you can also check below line of code:
<test name="Test" preserve-order="true" group-by-instances="true">
I have created a Selenium project named 'LearnAutomation' in Java and convert it to TestNG tests which created a testng.xml for me. Now i want to run it via batch file. So i created one as below -
cd E:\Workspace - Eclipse\LearnAutomation
set ProjectPath=E:\Workspace - Eclipse\LearnAutomation
echo %ProjectPath%
set classpath=%ProjectPath%\bin;%ProjectPath%\lib\*
echo %classpath%
java org.testng.TestNG %ProjectPath%\testng.xml
and save it as testng.bat file. On double clicking batch file, it gets launched and disappeared. (failed to run test)
My testng.xml looks like this -
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
<test name="Test">
<classes>
<class name="tests.Demo"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
My Demo class looks like this -
package tests;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class Demo {
#Test
public void batchFile() {
System.setProperty("webdriver.gecko.driver",
"C:\\Users\\srvashishtha\\Downloads\\geckodriver-v0.11.1-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
}
}
Is there any way to re-run failed tests using TestNG when same set of tests are run in different threads? I see there people use Retry listeners [https://stackoverflow.com/a/36320794/2020491 ] as in this link. But this does not seem to be working when i run same set of tests in multiple threads.
Below is my testng.xml where in com.example.SampleTest i have 2 tests in which 1 fails. i get the below result when run
===============================================
SampleTest
Total tests run: 5, Failures: 2, Skips: 1
===============================================
The one with test name="ONE", failed test runs for the second time but failed test is not run the second time for test with name="TWO" (when the class names are different for both the tests then this works as expected)
<suite name="SampleTest" verbose="1" parallel="tests" thread-count="1"><test name="ONE">
<classes>
<class name="com.example.SampleTest" />
</classes>
</test>
<test name="TWO">
<classes>
<class name="com.example.SampleTest" />
</classes>
</test>
<listeners>
<listener class-name="com.example.TestListener"/>
<listener class-name="com.example.RetryListener"/>
</listeners> </suite>
Below is my class file
#Test
public void testOne(){
Assert.assertTrue(false);
}
#Test
public void testTwo(){
Assert.assertTrue(true);
}
I installed NetBeans 8.0.1 on my computer and used it to create a test suite :
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="testing">
<test name="suite">
<packages>
<package name="myPackage.tests"/>
</packages>
</test>
</suite>
In myPackage.tests there is a test class, a very simple one :
[package, imports]
public class Tests001 {
public Tests001 () {
}
#Test
public void Fail() {
assertFalse(true);
}
In NetBeans, I launched the test by right-clicking the.xml file and selecting 'test file'. Everything works perfectly - or rather, it correctly fails - intended result for now.
But I need to launch it from console, so I run :
> java org.testng.TestNG testing.xml
And get :
===============================================
testing
Total tests run: 0, Failures: 0, Skips: 0
===============================================
No test is run. I can't find out why : test class and methods are public ; java is configured correctly (JDK 1.8.0_25) and I set CLASSPATH to
C:\Program Files\NetBeans 8.0.1\platform\modules\ext\testng-6.8.1-dist.jar
I must have forgotten something...
Or is there a way, from console, to ask NetBeans to proceed to test ?
In my experience, the suite.xml file needs this structure:
<suite name="mySuite" allow-return-values="true" parallel="tests"
verbose="1" thread-count="2">
<test name="TestStuff">
<groups>
<run>
<include name="Smoke.mytests"/>
</run>
</groups>
<packages>
<package name="com.myco.mytests"/>
</packages>
</test>
</suite>
I am having trouble reading parameter values from testng.xml inside a testng testcase in Eclipse IDE. I have browser initiated from BeforeClass and at #TEST method ,parameter values are coming as "NULL"...and it asks me to define my #Test parameters as Optional..
MyJavacode
public class headerValidation extends init {
WebDriver driver;
#BeforeClass
public void beforeClass() {
driver = initBrowser(BrowserType.FIREFOX, "http://www.abc123.com/");
}
#Test
#Parameters(value = { "loginID", "PasswordKey", "testURL" } )
public void testLogin(String loginID, String PasswordKey, String testURL) throws Exception {
try {
driver.get(testURL);
driver.findElement(By.id("login-b")).click();
driver.findElement(By.id("login_e")).sendKeys(loginID);
driver.findElement(By.id("login_p")).sendKeys(PasswordKey);
driver.findElement(By.name("submit")).click();
}//try
catch (Exception e) {
e.printStackTrace();
}//catch
My Testng XML file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
<test name="Test">
<parameter name="loginID" value="emailadd#add2.com"></parameter>
<parameter name="PasswordKey" value="21232131"></parameter>
<parameter name="testURL" value="www.abctest.com"></parameter>
<classes>
<class name="org.pa.qa.headerValidation"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
Am i missing something here?
If you right click on the file and run as testng test, the testng xml is not picked up by default, which explains why the parameters are not being picked up.
Two solutions :
Right click on the suite xml and trigger with Run as ->testng suite
OR
Go to Project->Properties->Testng-> Set this xml as your template xml and then you can run as testng test