Currently I am using Selenium Webdriver, and for annotation I am using testNG, In my code I have 100 test scripts with #test annotation ..I just want to run only one of my test case 100 times ..how I can do it any suggestion with proper example is much appreciated eg
run only script number 5 hundred times
In the #Test annotation - you can add a invocationCount attribute with the number of times you want to run it.
#Test(invocationCount = 100)
public void testMethod() {
}
Related
I'm confused in difference between Tests and Steps in testng extent report.
I have 2 test cases as 1 pass and 1 fail. In extent report under Test: 1 test(s) passed 1 test(s) failed, 0 others and under Steps: 1 step(s) passed
2 step(s) failed, 0 others
So would anyone clarify what is the difference between both ?
Attaching code snippet and testng extent report
#Test
public void demoTestPass()
{
test = extent.createTest("demoTestPass", "This test will demonstrate the PASS test case");
Assert.assertTrue(true);
}
#Test
public void demoTestFail()
{
test = extent.createTest("demoTestFail", "This test will demonstrate the FAIL test case");
Assert.assertEquals("Hi", "Hello");
}
Please click for Extent report here.
Any clarification would be much appreciated.
Difference Between Tests and Steps in extentReport:
Tests defines: Total test section which you have created in your Report: With the syntax like : extentReport.createTest("name of section");
Steps defines : Total number of log which you have generated in Script, With the syntax like : testlog.info() OR testlog.pass() OR testlog.fail() where testlog is object of ExtentTest class
Example:
In this report, there are 3 section which has been created and its showing as Tests. And Steps defines numbers of logs which has been passed in those Test.
Your case :
Test: 1 test(s) passed 1 test(s) failed, 0 others and under Steps: 1 step(s) passed 2 step(s) failed, 0 others
Test include 1 pass and 1 fail, because of its get failure in Steps. Your Steps include 1 pass and 2 fails and its reflected on Test.
Test(startTest("test name")) is something that is used to create a new test in extent reports.
Steps denotes that how many messages (test. Pass("pass message"), test. Fail ("fail message), test. Info ("info message")) you've logged to reports.
Consider you've two test methods and each test method has 1pass and 1 info messages.
So, in the extent reports, it'll show like 2 tests, total 4 steps.
2 pass steps and 2 info steps
I have a TestNG test Suit for a JAVA Project and, In there I have a
#Test(DataProvider="ListOfObjects") annotated Method. Which provides method with around 20 rows of data.( Hence the method runs 20 times.)
Now, I want to run this class for 2hrs (part of SOAK related test.) On average the Class takes around 10 mins for single run. So I am thinking or running the whole class for 12 times, and thus thinking of using #Test(invocationCount = 20) on the Class itself.
Any Better Ideas ?
Found an Embarrassingly simple solution:
Repeating the whole Test Suit as follows
#Test
public void RepeatTestSuite() {
long startTime = new Date().getTime();
while(!isTestFinished(startTime)) {
List<String> suites = new ArrayList<String>();
suites.add("./SOAK_all41.xml"); //path of .xml file to be run-provide complete path
TestNG tng = new TestNG();
tng.setTestSuites(suites);
tng.run(); //run test suite
}
You could extract the test in a method, then create a test method with a reasonably high invocation count. In that test method, compare the time with a variable holding the timestamp of the first run and if it has run for more than 20 minutes skip the test.
I'm making a GUI for use of TestNG, currently I have a dropdown box which uses then allows you to press a button which I want to run a group of tests
#Test(groups = {"Group1"})
public void Test()
//Test Data
#Test(groups = {"Group1"})
public void Test2()
//Test Data
#Test(groups = {"Group2"})
public void Test3()
//Test Data
I'm currently running this code via the run configurations and calling the group to run there.
Is there any way I can do this via a button press so the user can press run Group1 and then run Group2 in an executable program?
You can create a synthetic testng.xml in the Java code using the group inputs. But this might get messy with large size testng.xml.
You can also call testng from the command line (Have to figure out this from your GUI code) which has many parameters available like include groups, exclude groups etc etc.
java org.testng.TestNG testng1.xml [testng2.xml testng3.xml ...] -groups "grp1,grp2"
Refer to [http://testng.org/doc/documentation-main.html#running-testng}
You can make use of BeanShell feature that TestNG provides for doing this. You can define a JVM argument that reads the value set using the drop down selected option and then run it.
You can read more about using Beanshell and TestNG from this blog post that I created.
I have a testNG method just like this:
#Test(dataProvider="takeMyProvider")
public void myTest(String param1, String param2){
System.out.println(param1 + " " + param2);
}
My dataprovider returns 10 elements. My method will be executed 10 times in one thread. How is it possible to parallel this? For example
I want to have 5 methods in parallel. The webdriver should open 5 browsers at the same time. After these 5 tests in parallel the other 5 test should be executed
or
the webdriver should open 10 browsers and do all 10 elements parallel
Does anybody have an idea?
You can define the parallelism via a suite file in TestNG. Example following runs methods in parallel with 10 threads:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="MySuiteNameHere" parallel="methods" thread-count="10">
<test name="Selenium Tests">
<classes>
<class name="foo.bar.FooTest"/>
</classes>
</test>
</suite>
You also need to note that your data provider can is thread safe to allow it to not force the method to run sequentially.
// data providers force single threaded by default
#DataProvider(name = "takeMyProvider", parallel = true)
Be careful, though. TestNG does not create new instances of the class object when running with parallel methods. That means that if you save values on the test class object you can run into threading issues.
Also note, if you set the thread count to 5, it does not wait for the first 5 to all be finished and then start up the next 5. It basically puts all the test methods into a queue and then starts up x threads. Each thread then simply polls the next element from the queue when it is available.
TestNG's #Test annotation already has what you want... To some degree:
// Execute 10 times with a pool of 5 threads
#Test(invocationCount = 10, threadPoolSize = 5)
What this won't do is fit your first scenario exactly, that is, run the first 5, wait for them to finish, run the other 5.
many thx for your feedback and useful tipps.
My tests ran - maybe - in any parallel way but only in one browser instance.
Lets jump in in detail:
My dataprovider returns an object[][]
#Dataprovider(name = "takeMyProvider", parallel = true)
public object[][] myProvider(){
return new object[][]{{"1", "name1"}, {"2", "name2"} {"3", "name3"}}
}
This test method is executed three times
#Test(dataProvider="takeMyProvider")
public void myTest(String param1, String param2){
System.out.println(param1 + " " + param2);
}
but just in one browser instance. Thats not what I want.
I want testNG to start 3 chrome instances and doing the 3 tests in parallel.
Btw I am running the tests on a selenium grid. Maybe with 100 nodes.
It would be perfect when 100 nodes doing this test in parallel. Or even 1.000, depends on the dataprovider.
Does anybody have an idea?
Best regards
I have a java file which has 7 junit tests to run. If I run all the tests at once all but 1 passes. If I comment out certain tests and that one test always passes.
Can anybody offer any suggestions as to what could be causing this?
My first thought was something in the test Setup or cleanup but I am not sure what it could be. All I do in the clean up is exit the driver and output the time taken to run the test.
In the setup I set up the driver, the time started, create a firefox profile and read in some data from a properties file to use in the tests.
If it was the setup / cleanup surely the other 6 tests would also be effected? The test that fails is a simple test to check that entering an invalid card type displays an error message on the page.
UPDATE:
I've renamed the test so it runs first and now all 7 pass each time. What could be causing this? Do I need to set something in my test cleanup to get it back to a default state?
My test cleanup:
#After
public void testCleanup() throws IOException {
driver.quit();
endTime = System.currentTimeMillis();
long totalTime = ((endTime - startTime)/1000)/60;
System.out.println();
System.out.println("Test Suite Took: " + totalTime + " Minutes.");
}