Using TestNG to file Selenium test - But NOT Parallel - selenium

This is my first question here, and I will be very happy to be helped.
I have been using TestNG as a part of my framework for a long time now.
And the question I have today is about the testng.xml configuration - to NOT run tests in parallel. And NO, none of my tests are dependent, they are all independent. But, this is for my requirement.
My testng.xml file looks like this:
<suite name="Smoke Test Suite" verbose="3" parallel="tests" thread-count="2">
<test name="Run on Firefox" preserve-order="true">
<parameter name="browser" value="firefox"/>
<classes>
<class name="com.test1"/>
<class name="com.test2"/>
<class name="com.test3"/>
<class name="com.test4"/>
</classes>
</test>
<test name="Run on IE9" preserve-order="true">
<parameter name="browser" value="iexplore"/>
<classes>
<class name="com.test1"/>
<class name="com.test2"/>
<class name="com.test3"/>
<class name="com.test4"/>
</classes>
</test>
<test name="Run on Google Chrome" preserve-order="true">
<parameter name="browser" value="chrome"/>
<classes>
<class name="com.test1"/>
<class name="com.test2"/>
<class name="com.test3"/>
<class name="com.test4"/>
</classes>
</test>
I want the Tests to run in Parallel, but the classes to run one after the other.
What I am currently seeing is that when the test is fired off, I have 8 instance of FF, IE9 and Chrome open up. How can I configure it so that "test1" is executed, the browser is closed, new instance opened and then "test2" is run and so forth?
The reason for me having to do this is that, my app has multiple windows opened during each test. And IE9 (being the evil browser it is) does not know how to handle the situation, panics and loses focus on the window midway through the test. It has been suggested, and I have found the same - that it is best to have one instance of IE9 running with nothing else interrupting it.
Suggestions and solutions will be gratefully accepted.
NOTE: All classes are in the same package.
Thanks,
-Alister

You can create Three objects for DefaultSelenium in your #Before method.One for IE, One for FF and One for chrome.
If you are using webdriver you can create 3 separate drivers for the same.

You could use three separate suites (xml files) then add them one after the other in the command line. That has the effect of running them in sequence.

Related

Selenium run same test on different platforms in parallel

I have three classes with different tests that I want to execute on chrome browser. The most important thing is that I want to run in parallel on Windows and macOS. How should I do it on Selenium 4 with Hub - > node setup?
My testng file looks like this
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="Suite 1" parallel="classes" verbose="1" >
<test name="Test windows and macOS">
<classes>
<class name="test1"/>
<class name="test2"/>
<class name="test3"/>
</classes>
</test>
</suite>
When test is executed tests are running only on Windows with two browser windows.
Selenium documentation doesn't explain how to do this.

How to get logs for each test tag in a file in parallel run in Selenium which is running based on test parameter

I have a testng.xml which will create a Claim for 2 companies parallel, the parameter here is company name. A gist of test tags in testng.xml are as below:
<suite name="Suite" thread-count="2" parallel="tests">
<test name="Test1">
<parameter name="TestParam" value="CompanyName1" />
<classes>
<class name="CreateClaim" />
</classes>
</test>
<test name="Test2">
<parameter name="TestParam" value="CompanyName2" />
<classes>
<class name="CreateClaim" />
</classes>
</test>
When I run this testng.xml, logs are generated haphazardly, mixing the logs of two test tags.
Is there any way in which I can save the logs of first test tag in one file and second test tag in other file?
It will be easy for us to check logs of each company specifically.

Run same Selenium/Testng tests concurrently

I have a selenium test which tests my target web application.
I want to run the same test on Jenkins concurrently on Nightly and Staging environments.
What is the better way to do that? I am ok to approach it on Jenkins or on Selenium Grid or any other way.
I explored Selenium Grid but they talk of executing different tests parallel on multiple envs. But my specific requirement is that I have to run SAME TEST CONCURRENTLY on the same browser instance or on different browser instance.
Ex: Consider this test, com.myorg.myapp.myTest.testLogin(String envURL)
I would like to run this test concurrently on Chrome (for ex) by passing Nightly and Staging URLs.
At present I am running first set of tests on Nightly followed by the same tests on Staging. This takes almost a day. I need to do them concurrently to save time.
You can create a testNG xml and add the same test to different suites likes this.
<suite name="mySuite" parallel="tests">
<test name="thread1">
<classes>
<class name="test.sample.MyTest"/>
</classes>
</test>
<test name="thread2">
<classes>
<class name="test.sample.MyTest"/>
</classes>
</test>
...
...
<test name="threadN">
<classes>
<class name="test.sample.MyTest"/>
</classes>
</test>
</suite>
If you need to pass the environment add the following and access it from the code of a test as described here (http://testng.org/doc/documentation-main.html#parameters)
<parameter name="env" value="staging"/>
This is a simple solution yet quite ugly and harder to maintain if you need a lot of threads. Another way is to create your XML in code by implementing IAlterSuiteListener. See TestNG Parallel execution with selenium for the code sample.

TestNG file is not reading parameter value if add another script

i am trying to run multiple test in sequence in in one by one different browser , for that i am defining browser parameter in testNg.xml file .
<test name="iexplore">
<parameter name="browser" value="iexplore"/>
<classes>
<class name="com.slingmedia.safe.testscripts.BRO_139"/>
<class name="com.slingmedia.safe.testscripts.BRO_140"/>
</classes>
</test>
its working fine and taking correct browser , but when i am defining all the test in other xml file and trying to run , browser value is not getting read.
<test name="iexplore">
<parameter name="browser" value="iexplore"/>
<suites-file>
<suite path="Sample3.xml" />
</suites-files>
</test>

Parallel execution of two suites using web driver and Selenium

I have two testNG suites in my project. I want to run both of them in parallel.
Suite1:
class A
class B
Suite2:
class C
class D
I want to run SUITE1 and SUITE2 in parallel on the same machine. However, Class B of SUITE1 should be run only after Class A. Same with the second suite: the first and second classes need to be run in serial.
How do I achieve this?
In order to achieve, you need to add annotation as #Test(priority=??). The default value will be zero for priority.
If you don't mention the priority, it will take all the test cases as "priority=0" and execute.
If we define priority as "priority=", these test cases will get executed only when all the test cases which don't have any priority as the default priority will be set to "priority=0"
You dont need two xml for your requirement. Follow these and you should have same test running parallel.
<suite name="Test-class Suite" parallel="tests" thread-count="2">
<test name="Test-class test 1">
<classes>
<class name="com.ClassA" />
<class name="com.ClassB" />
</classes>
</test>
<test name="Test-class test 2">
<classes>
<class name="com.ClassC" />
<class name="com.ClassD" />
</classes>
</test>
Make sure your parallel parameter have parallel='true' and thread-count as per your requirement.
to execute classes as specified order, need to use preserve-order="true". As per below xml file, ClassB is specified first so ClassB is executed first then ClassA as we specified preserve-order="true"
If you want to execute #Test methods in class as per required order, please use priority. like below
#Test(priority=1)
below is xml file for example..
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="tests" preserve-order="true">
<test name="TestA">
<classes>
<class name="com.test.ClassB"/>
<class name="com.test.ClassA"/>
</classes> </test> <!-- Test A-->
<test name="TestB">
<classes>
<class name="com.test.ClassD"/>
<class name="com.test.ClassC"/>
</classes> </test> <!-- Test B-->
</suite> <!-- Suite -->
*****updating as per comment
Hi,
To execute two suites, need to create two xml files and specify your suites in those xml files. Then create another xml file to call these suites xml files..
Lets say i have one suite in testng1.xml file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite1" parallel="false" preserve-order="true">
<test name="TestA">
<classes>
<class name="com.test.NewTest1"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
another suite in testng2.xml file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite2" parallel="false" preserve-order="true">
<test name="TestB">
<classes>
<class name="com.test.NewTest1"/>
</classes>
</test> <!-- Test -->
then use another testng.xml file and call these two file. by executing this file, two suite will be executed.
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="My test suite" preserve-order="true">
<suite-files>
<suite-file path="Testng1.xml"></suite-file>
<suite-file path="Testng2.xml"></suite-file>
</suite-files>
</suite>