I want to create xml in order to execute my classes - java

I have created a TestNG XML File, which I have called multiple classes. My concern is that I want to execute my test cases on all classes which I have arranged from top to bottom like below. However some of the bottom cases depend on upper cases, and whenever I run my XML File it selects the classes randomly. How would I go about doing this?
Type - 1
{
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite3">
<test name="Test3" preserve-order="true">
<classes>
<class name="com.netgearInsight.testReports.RegistrationTestCase"/>
<class name="com.netgearInsight.testReports.LoginTestCase"/>
<class name="com.netgearInsight.testReports.AddNetworkTestCase"/>
<class name="com.netgearInsight.testReports.AddDevicesTestCase"/>
<class name="com.netgearInsight.testReports.CreateSsidTestCase"/>
<class name="com.netgearInsight.testReports.DeleteDevicesTestCase"/>
<class name="com.netgearInsight.accountCheck.About"/>
<class name="com.netgearInsight.accountCheck.AccountManage"/>
<class name="com.netgearInsight.testRunEnd.GetTotalStatus"/>
<class name="com.netgearInsight.testRunEnd.SendReport"/>
</classes>
</test> <!-- Test3 -->
</suite> <!-- Suite3 -->
}
Type - 2
{
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite1">
<test thread-count="5" name="Test One" >
<classes>
<class name="com.netgearInsight.testReports.RegistrationTestCase"/>
<class name="com.netgearInsight.testReports.LoginTestCase"/>
</classes>
</test> <!-- Test -->
<test thread-count="5" name="Test Two" >
<classes>
<class name="com.netgearInsight.testReports.AddNetworkTestCase"/>
<class name="com.netgearInsight.testReports.AddDevicesTestCase"/>
<class name="com.netgearInsight.testReports.CreateSsidTestCase"/>
</classes>
</test> <!-- Test -->
<test thread-count="5" name="Test Three" >
<classes>
<class name="com.netgearInsight.testReports.UrlFilterTestCase"/>
<class name="com.netgearInsight.testReports.DeleteDomainTestCase"/>
<class name="com.netgearInsight.testReports.DeleteDevicesTestCase"/>
</classes>
</test> <!-- Test -->
<test thread-count="5" name="Test Four" >
<classes>
<class name="com.netgearInsight.testReports.AddDataVLAN"/>
<class name="com.netgearInsight.accountCheck.AccountManage"/>
<class name="com.netgearInsight.accountCheck.About"/>
<class name="com.netgearInsight.accountCheck.ChangePassword"/>
</classes>
</test> <!-- Test -->
<test thread-count="5" name="Test Five">
<classes>
<class name="com.netgearInsight.testRunEnd.GetTotalStatus"/>
<class name="com.netgearInsight.testRunEnd.SendReport"/>
</classes>
</test> <!-- Test -->`enter code here`
</suite> <!-- Suite -->
}

Related

How to run TestNG tests parallelly through xml with priority

I have a scenario where a class "Pre-Requisite" class have to be executed first and then I have 5 testng classes which have to use certain data gathered from "Pre-Requisite.java" class and execute all 5 classes parallelly. Eg. of an xml below:
<test name="Suite">
<classes>
<class name="Pre-Requisite"/> // To be run first
</test>
<test name="Suite">
<classes>
<class name="AClass"/> // To be run in parallel with below classes
<class name="BClass"/>
<class name="CClass"/>
<class name="DClass"/>
<class name="EClass"/>
</test>
</suite>
can someone please help me how do I achieve this?
You can mark the methods in the class "Pre-Requisite" with the TestNG annotations - "#BeforeClass" or "#BeforeSuite", so it will execute the code in the class "Pre-Requisite" first.
Then you can execute other classes in parallel through testng.xml, like:
<suite name="Suite">
<test name="Test1" parallel="classes">
<classes>
<class name="AClass"/>
<class name="BClass"/>
<class name="CClass"/>
<class name="DClass"/>
<class name="EClass"/>
</classes>
</test>
</suite>

TestNG Based Selenium Tests not running in Parallel

I am using the below TestNG Config to enable parallel execution of Selenium tests.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Test-Automation" parallel="methods" thread-count="2" verbose="1">
<test name="Suite Test">
<classes>
<class name="SampleTest">
<methods>
<include name="firstTest"/>
<include name="secondTest"/>
<include name="thirdTest"/>
</methods>
</class>
</classes>
</test>
</suite>
Java Code:
#Test(dataProvider = "TestData")
public void firstTest(String data){
//Code
}
#Test(dataProvider = "TestData")
public void secondTest(String data){
//Code
}
#Test(dataProvider = "TestData")
public void thirdTest(String data){
//Code
}
The Selenium tests are expected to run in parallel. I expect 2 browsers to be open and run the test script.
But I see only 1 browser and all 3 tests run one after the other and not in parallel. I have tried using test, methods, class, instance options for "parallel" attribute.
Any help?
This is due to a bug in TestNG 6.13.1 [ See GITHUB-1636 for more details ]
I have fixed this in the latest SNAPSHOT of TestNG (6.14-SNAPSHOT) and this should be available for use in the released version of TestNG (6.14).
But until then, please alter your suite xml file to look like below :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Test-Automation" parallel="methods" thread-count="2" verbose="1">
<test name="Suite Test" parallel="methods" thread-count="2" verbose="1">
<classes>
<class name="SampleTest">
<methods>
<include name="firstTest"/>
<include name="secondTest"/>
<include name="thirdTest"/>
</methods>
</class>
</classes>
</test>
</suite>
The work-around is basically to add the attributes parallel="methods" thread-count="2" at the <test> level also.
Seperate all the test and then try with parallel="test"
<test name="Suite Test1">
<classes>
<class name="//..//packg name..SampleTest">
</class>
</classes>
</test>
<test name="Suite Test2">
<classes>
<class name="//..//SampleTest">
</class>
</classes>
</test>
<test name="Suite Test3">
<classes>
<class name="//..//packg name..SampleTest">
</class>
</classes>
</test>
</suite>

Add cross browser for multiple packages using TestNG and Java

I am using xml file to run all my packages of a project parallel, how to add cross browser to this xml file:
<suite name="Web Smoke Tests" verbose="1" preserve-order="true"
parallel="tests" thread-count="4">
<test name="Test2">
<classes>
<class name="package.class2"></class>
</classes>
</test>
<test name="Test1">
<classes>
<class name="package.class1"></class>
</classes>
</test>
<test name="Test">
<classes>
<class name="package.class"></class>
</classes>
</test>
You can refer the following sample for achieving this:
Modify your xml file based on the below sample:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel" thread-count="4" parallel="tests">
<test name="SingleTestChrome">
<parameter name="config" value="parallel.conf.json"/>
<parameter name="environment" value="chrome"/>
<classes>
<class name="com.browserstack.SingleTest"/>
</classes>
</test>
<test name="SingleTestFirefox">
<parameter name="config" value="parallel.conf.json"/>
<parameter name="environment" value="firefox"/>
<classes>
<class name="com.browserstack.SingleTest"/>
</classes>
</test>
<test name="SingleTestSafari">
<parameter name="config" value="parallel.conf.json"/>
<parameter name="environment" value="safari"/>
<classes>
<class name="com.browserstack.SingleTest"/>
</classes>
</test>
<test name="SingleTestIE">
<parameter name="config" value="parallel.conf.json"/>
<parameter name="environment" value="ie"/>
<classes>
<class name="com.browserstack.SingleTest"/>
</classes>
</test>
</suite>
Sample Configuration file:
{
"server": "hub-cloud.browserstack.com",
"user": "BROWSERSTACK_USERNAME",
"key": "BROWSERSTACK_ACCESS_KEY",
"capabilities": {
"build": "testng-browserstack",
"name": "parallel_test",
"browserstack.debug": true
},
"environments": {
"chrome": {
"browser": "chrome"
},
"firefox": {
"browser": "firefox"
},
"safari": {
"browser": "safari"
},
"ie": {
"browser": "internet explorer"
}
}
}
Reference: https://github.com/browserstack/testng-browserstack

Selenium_testng_Java_passing data to both #beforetest and #test

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.

TestNG:Cannot find class in classpath, testng.xml

This is my testng.xml file, i named it differently, but it should still work:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="TEST_LINKS">
<test name="TECT на подбор спеца">
<classes>
<class name="Podbor_specalista_url_click_test" />
</classes>
</test>
<test name="TECT на клик блога">
<classes>
<class name="Blog_url_click_test" />
</classes>
</test>
</suite>
Now, i get the error, but where do I fix it?
i want to run the whole test suite consisting of these tests:
You need to give the fully qualified class name of the class you want to test in class tag, something like this
<suite thread-count="5" name="Ant suite" junit="false" annotations="JDK">
<test name="Ant test">
<classes>
<class name="com.sample.test.MyUnitTest"/>
</classes>
</test>
</suite>

Categories