extent report with one master test class - selenium

i have created 2 different test classes (test1.java and test2.java), having multiple tests in each class. to simplify, i have created one master test class, in which i have created 2 tests and called the methods from test1page.java and test2page.java. now when i create the extent reports it only gives 2 results. i want all the test result which i have created in test1.java and test2.java.
below is my MasterTest.java
public class MasterTest extends TestBase {
#BeforeClass()
//before class method
#Test(priority=1)
// methods from the test1page.java class
#Test(priority=2)
// methods from the test2page.java class
#AfterClass
}
below is my xml file to run
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="smoke">
<listeners>
<listener class-name="report.TestListener" />
</listeners>
<test name="master">
<classes>
<class name="tests.MasterTest">
</class>
</classes>
</test>
</suite>

Related

When a #BeforeTest method fails, why is it not being listed in the testng-failed.xml?

I am using maven with testng 6.14.3.
Here is my code structure:
testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<suite name="set-3" parallel="tests" thread-count="10">
<listeners>
<listener class-name="org.uncommons.reportng.HTMLReporter" />
</listeners>
<test name="Customer Tests">
<groups>
<run>
<include name="abc"/>
</run>
</groups>
<classes>
<class name="apps.Test1_BeforeTest_Of_Test2"></class>
<class name="apps.Test2"></class>
</classes>
</test>
</suite>
Test1_BeforeTest_Of_Test2.java
public class Test1_BeforeTest_Of_Test2{
#BeforeTest(groups = {"abc"})
public void test1Method() throws Exception {
}
#AfterTest(groups={"abc"})
public void test1AfterMethod() throws Exception {
}
}
Test2.java
public class Test2{
#Test(groups = {"abc"})
public void test2Method(){
}
}
During my run, Test1_BeforeTest_Of_Test2 class fails. So, Test2 is marked as skipped.
But, when I look at the testng-failed.xml that is generated at the end of the run, the failed #BeforeTest class (Test1_BeforeTest_Of_Test2) is not included/listed:
testng-failed.xml
<?xml version="1.0" encoding="UTF-8"?>
<suite thread-count="10" name="Failed suite [set-3]" parallel="tests">
<listeners>
<listener class-name="org.uncommons.reportng.HTMLReporter"/>
</listeners>
<test name="Customer Tests(failed)">
<groups>
<run>
<include name="abc"/>
</run>
</groups>
<classes>
<class name="apps.Test2">
<methods>
<include name="test2Method"/>
</methods>
</class>
</classes>
</test>
</suite>
Is this expected behaviour? Or a bug/gap in testng-failed.xml?
Ideally, when we re-run the failed tests, we expect the #BeforeTest to run as well, because it is pre-req for Test 2.
TestNG currently seems to be honouring configurations to be considered in the testng-failed.xml if its part of the skipped test method's test class i.e., the configuration (which is perhaps what has caused a test to be skipped) needs to reside in the same java class as your skipped method for TestNG to consider it to be included.
In your example, that's not the case and the configuration method exists in a different test class (which is perfectly valid).
This looks like a bug in TestNG to me.
I have submitted a bug on your behalf on the TestNG project and will get it fixed in the upcoming version (7.5.0).
Defect : https://github.com/cbeust/testng/issues/2611

Error when calling dependency of one method of another class into other class in TestNG automation

I want to add dependency of one test method from class A to another testMethod in class B.
Whenever I am running method "testTwo()" from class B I am getting error as
"method B.testTwo() depends on nonexistent group A.Group1" , my class structure is as below .
public class A
{
#Test(groups={"A.Group1"})
public void testOne(){}
}
public class B
{
#Test(dependsOnGroups={"A.Group1"})
public void testTwo(){}
}
Can anyone please guide here ?,
also My test.xml looks like below
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="TEST-AUTOMATION" thread-count="1"
preserve-order="true" data-provider-thread-count="1">
<test name="TESTS">
<classes>
<class name="A"/>
<class name="B"/>
</classes>
</test>
</suite>

Parallel execution with data provider

I am trying to run the test parallel using dataprovider. I have mentioned dataproviderthreadcount=3 in testng xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" data-provider-thread-count="3"parallel="methods">
<test name="Test">
<classes>
<class name="com.sample.test">
</class>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
Test methods:
#Test(dataProvider = "dp1", threadPoolSize=3,invocationCount=1)
public void Testsuitesample(String url, String add1, String add2){}
Result: 3 browser instances get opened and all three data is passing to only browser. Other browser's are still idle. Is it a way to resolve this?
You may need to set parallel to true in your data provider method like,
#DataProvider(parallel = true)
public Object[][] dp1() {
}
Also, the invocation count should be equal or greater than the thread pool size.

How to run test cases in different Java classes in a new window every time using TestNG and Selenium

I have 15 test methods in 3 Java classes (Selenium Script). I want to run each Test class with new window. I am using TestNg framework.
Here is the code of TestNG:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Fanfight Test" thread-count="10" parallel="methods">
<listeners>
<listener class-name="com.fanfight.test_case.ListenerClass">
</listener>
</listeners>
<test name="User Login" parallel="false">
<classes>
<class name="com.fanfight.test_case.UserLogin"></class>
</classes>
</test>
<test name="Contest Creation" parallel="false" >
<classes>
<class name="com.fanfight.test_case.ContestCreation"></class>
</classes>
</test>
<test name="User Profile Test" parallel="false" >
<classes>
<class name="com.fanfight.test_case.UserProfileTest"></class>
</classes>
</test>
<test name="Menu Bar Test" parallel="false">
<classes>
<class name="com.fanfight.test_case.MenuBarTest">
</class>
</classes>
</test>
<test name="Home Page Elements" parallel="false" >
<classes>
<class name="com.fanfight.test_case.HomePageElementTest"></class>
</classes>
</test>
</suite>
Without using parallel="false" my script is running in alphabetical order due to which selenium unable to find the path and execution got stuck.
Also please suggest how to make execution continue even after getting an exception during execution.
Add a setup and tear down method in each of the 3 test classes. The setup method should launch the browser and teardown method should close that browser instance.
class TestOne {
WebDriver driver;
#BeforeClass
public void setup(){
driver = new ChromeDriver();
}
#Test
public void testCase1(){
}
//.... Other test methods
#AfterClass
public void tearDown(){
driver.quit();
}
You can also create a parent class having just the setup and tear down methods , pseudo coded above. All 3 of your test class shall extend this parent class. It will be an optimised approach as the driver instantiation and destruction is now centralised to a single class.
And finally , change the parallel attribute in the suite tag of your testNG xml, in order to make them run parallel.
<suite name="Fanfight Test" thread-count="10" parallel="classes">

How can I print in log the number of tests running as part of one group in TestNG?

I need to print this before actual execution of tests start. Anyways, we get the count at last of execution.
Say for example if my testng.xml looks like:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1">
<test name="test1">
<groups>
<run>
<include name="functest" />
</run>
</groups>
<classes>
<class name="GroupTestExample" />
</classes>
</test>
</suite>
And there are 10 methods in class GroupTestExample which comes under group functest, it should print 10 in log as soon as it starts running.
Create a custom Listener which extends the org.testng.TestListenerAdapter. After that override the following method:
public class MyCustomListener extends TestListenerAdapter {
#Override
public void onStart(ITestContext testContext) {
super.onStart(testContext);
System.out.println("Number of Test Methods: " + testContext.getAllTestMethods().length);
}
}
You can add your custom listener by using #Listeners annotation on your main class of your test, like
#Listeners({MyCustomListener.class})
public class MyMainTestClass { ... }
You can find more information in TestNG doc:
TestNG Listeners -
http://testng.org/doc/documentation-main.html#testng-listeners