Parallel run using testng - selenium-webdriver

I am trying to execute parallel run using testng.
i am trying to run test 4 time parallel. But it runs only one time.
testng.xml
<suite name="My suite" parallel="methods" thread-count="4">
<test name="Default test" group-by-instances="true">
<classes>
<class name="org.com.DemoClass"/>
</classes>
</test> <!-- Default test -->
</suite> <!-- Default suite -->
Demo class.
public class DemoClass {
#Test
public void method1() throws InterruptedException {
long id = Thread.currentThread().getId();
System.out.println("Before test-method. Thread id is: " + id);
}
}
Please provide your valuable suggestion.

You can try adding parameters to #Test annotation:
#Test(threadPoolSize = 4, invocationCount = 4)
That will invoke your test four times from 4 different threads.

If your need is that each of the #Test annotated methods that you write need to be executed "n" number of times (in parallel or in sequential order) without you having to explicitly add the invocationCount attribute on your own, you should be leveraging an IAnnotationTransformer using which you can on the fly change values of your #Test annotated method. You can try injecting this listener either via the suite file using the <listeners> tag or via Service Loaders. For more general information on listeners you can perhaps take a look at this blog post of mine.

Related

camel snmp can't resive snmpversion=3 info

When I use came-snmp resive snmp info which version is 3, it can't go to the process method.
#Component
public class SnmpCollect extends RouteBuilder {
#Override
public void configure() throws Exception {
from("snmp:0.0.0.0:162?protocol=udp&type=TRAP&snmpVersion=3&securityName=test").process(new Processor() {
#Override
public void process(Exchange arg0) throws Exception {
}
}
}
Camel xml config:
<camelContext id="camelContext" xmlns="http://camel.apache.org/schema/spring">
<routeBuilder ref="snmpCollect"/>
</camelContext>
But when the snmp info which version is 1 or 2 is coming, it can go to the process method.
What's wrong with it, and how to make it work for "snmpVersion=3" info?
Camel Version is 2.20.1
Let me try to answer your question by providing some info based in what I've found.
Seems that he requirements and interfaces from v1 and v2 version differ from v3, so it doesn't like to work just updating the version. The mainly difference, from what I've seen, is that you need to provide a security model to v3. I saw that you are passing it via parameters, but have you got the chance to check the security requirements?
When I use the TrapTest where is on the camel-snmp github “github.com/apache/camel/blob/master/components/camel-snmp/s‌​rc/…”,it‘s ok.But when I change the snmpVersion to SnmpConstants.version3,it's also error
That's because the interface changed and the test should rely on ScopedPDU model instead of the base class PDU. Also the security model isn't set up in this test:
org.snmp4j.MessageException: Message processing model 3 returned error: Unsupported security model
Unfortunately there isn't any example using camel-snmp with v3, but you could take a look into this example using the inner component snmp4j.

Global variables are set to null after first class execution in Test NG

I am trying to build an automation framework using TestNG where I can be able to run multiple classes inside a suite one after another. I have a base class (TestBase.java) where I have all global (class) variables and BeforeSuite and AfterSuite methods which are initializing and destroying these global variables like WebDriver object that will be used across all test classes.
My testng.xml is like this:
<suite name="ShunyaAutomation" >
<test name="ShunyaAutomation_Firefox" preserve-order="true" >
<parameter name="browser" value="firefox"/>
<parameter name="runType" value="local"/>
<classes >
<class name="org.shunya.Test1" />
<class name="org.shunya.Test2" />
</classes>
</test>
</suite>
I want the #BeforeSuite method to initialize the WebDriver object and others, which it does, and then all #Test methods in Test1.java and Test2.java should be able to use the Webdriver object.
When I run this, the #Test methods in Test1.java class successfully run but the moment execution reaches to Test2.java, all these objects including the WebDriver object initialized by the #BeforeSuite method are nullified and I get a NullPointer exception on these objects. Can anyone suggest how to achieve this?
*I could do it by declaring all variables as static. But I don't want to make all these variables as static as I want to be able to run several suites simultaneously using Selenium Grid and TestNG parallel execution.
#BeforeSuite methods are always run only once for all the suite.
If you have a TestBase class between Test1 and Test2 which has the #BeforeSuite method, TestNG will:
Create a Test1 instance
Create a Test2 instance
Call #BeforeSuite method from TestBase only once, and will take the Test1 OR Test2 instance for that.
A the end, one instance will have WebDriver object initialized, the other not.
As #Mona suggested it, you should use #BeforeClass instead.

Selenium Grid2 default maxSession doesn't seem to working as expected when running testng suite (parallel="classes")

Actual -
The behavior I see is that it runs all the tests in the class sequentially one session (FireFox browser) at a time on 1 node.
Expected -
The class in this example "IntegrationTest" has 20 methods(#Test). I expect to see 5 tests in the class getting picked up, and run in parallel in 5 FireFox sessions on the 1 node.
Here's my testng suite file. Having thread-count as 1 makes sense as there's only 1 class I want to run.
<suite name="WebDriver Tests" parallel="classes" thread-count="1">
<test name="WebDriver Tests">
<classes>
<class name = "com.axiom.web.IntegrationTest" />
</classes>
</test>
And here's the grid2 commands that I run on the hub and the node.
Hub command -
java -jar selenium-server-standalone-2.43.0.jar -role hub -browserTimeout 60
Node command -
java -jar selenium-server-standalone-2.43.0.jar -role node -hub http://<host ip address>:4444/grid/register
Am I missing something here? What do I have to do to get maxSession work as it should? I believe maxSession takes precedence over maxInstances, but either way, specifying none, both or either in the node command didn't work for me. I am on Selenium version 2.43.1 and testng version 6.8.8.
Thanks and appreciate the help!
You need to configure the node with the number of FF sessions you want it to support.
See "Configuring the nodes" on this page - https://code.google.com/p/selenium/wiki/Grid2
You need to increase the thread-count in the testng suite file and specify that you want parallel="methods".
parallel="classes" and thread-count=1 means you want one total thread and that you want all methods in each class to run on the same thread. parallel="methods" means you want each method to have it's own thread. However, a single thread won't accomplish what you want, so you need to add more total threads.
Documentation for thread-count and the parallel setting is here - http://testng.org/doc/documentation-main.html#parallel-running
I will recommend you that add #BeforeMethod and have multiple driver instances being initialized in #BeforeMethod if you want to run in 5 browsers you will have to open 5 browsers i.e create 5 driver instances yourself in #BeforeMethod. Let me know if you need further help.

how to call testng.xml from java main method?

I have testng.xml file created.
Is there any way to run this file from java main method?
Something like -
Class test {
public static void main ( String [ ] args)
{
Run(testng.xml);
}
}
You can run testng directly from commandline, probably make a bat file on top of it or use jenkins to trigger it. Refer here
or
If you want it in main, then you can try
TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
List<String> suites = Lists.newArrayList();
suites.add("c:/tests/testng1.xml");//path to xml..
suites.add("c:/tests/testng2.xml");
testng.setTestSuites(suites);
testng.run();
Please try below code and make sure you have testNG jar added in your jar manifest file.
public static void main(String[] args)
{
org.testng.TestNG.main(args);
}
now you can pass all the parameters to your jar which are same for testNG jar file.
e.g.
java -jar yourjar.jar testng.xml
or
java -jar yourjar.jar -testclass org.test.xyz.java
This work for me. More details here.
// Create object of TestNG Class
TestNG runner=new TestNG();
// Create a list of String
List<String> suitefiles=new ArrayList<String>();
// Add xml file which you have to execute
suitefiles.add("C:\\Automation Test\\Git\\vne_automation\\testng.xml");
// now set xml file for execution
runner.setTestSuites(suitefiles);
// finally execute the runner using run method
runner.run();
Hope this helps!
All the answers above works like a charm but there is one limitation that
it would run unless and untill you remove the scope tag in pom.xml
In pom.xml there would be maven dependancy for testNg and in there
there would be one scope tag which limits our jar to only upto test
Just remove that one
I didn't get any error and XML path is correct. But It just end execution with failed test cases. It does not open firefox browser.
I am using testng annotation (i.e. #Before Suite,#Test, #group etc ) inside classes whereas it successfully execute using Testng Suite in eclipse.
<!-- suite name="Example" parallel="methods" thread-count="2" parallel="false" preserve-order="true"> -->
<suite name="Example Donate System" verbose='1'>
<test name="Agency" >
<packages>
<package name="com.Donatesystem.AgencyController" />
</packages>
</test>
</suite>

How to inject the #Context when consuming FIQL queries in Apache CXF - JAX-RS?

I am trying to implement a FIQL search queries and I am following the guide at http://cxf.apache.org/docs/jax-rs-advanced-features.html#JAX-RSAdvancedFeatures-FIQLsearchqueries.
The issue is that the SearchContext is always null and I am getting a NullPointerException at runtime.
My sample code is :
#Context
private SearchContext searchContext;
#GET
#Path("/actor/")
#Override
public List<Actor> getActorBy() {
SearchCondition<District> sc = searchContext.getCondition(Actor.class);
}
I am getting the NullPointerException when executing the method getCondition on searchContext.
The others rest calls are working.
Is there a way to inject or configure the SearchContext so I can be able to execute a fiql search query ?
There seems to be some problem injecting #Context(s)... I was able to get this working by
public List<Actor> getActorBy(#Context SearchContext searchContext) {
...
You need to add a SearchContextProvider to your jaxrs:server
example:
<jaxrs:server id="myWebServices" address="/ws">
[...]
<jaxrs:providers>
[...]
<bean class="org.apache.cxf.jaxrs.ext.search.SearchContextProvider"/>
</jaxrs:providers>
</jaxrs:server>

Resources