#BeforeTest #AfterTest running only once - maven

I am working on web whatsapp automation for practice using selenium-maven-cucumber-testng . I have below issue.
Expected: I have many feature files and I want to run all the feature files one after another as below :
take first feature file -> open browser ->run steps mentioned in feature file -> close browser. take second feature file -> open browser - > run steps mentioned in feature file -> close browser and so on.
Result : Currently all the feature files are getting run and passing. But the browser is opening only once i.e.,
Open browser -> run feature file 1, feature file2, etc -> Close browser.
I want to run as explained in "Expected".
My Testng :
testng.xml
My RunnerClass :
Runner.java
My Hook :
Hook.java

BeforeTest and AfterTest will run only once as there is only one test block in the testng.xml. One way would be to create separate runners for each feature file and have separate test blocks.
<test name="Feature_File_1">
<classes>
<class name="Runner1"/>
</classes>
</test>
<test name="Feature_File_2">
<classes>
<class name="Runner2"/>>
</classes>
</test>
Also in the current runner class remove junit annotation #RunWith.

You need to change your TestNG hooks to Cucumber hooks.
Why is that?
Cuz you want to close the browser after each scenario as you elaborated in your question.
Please see below:
import cucumber.api.java.After;
import cucumber.api.java.Before;
public class Hooks {
#Before
public void beforeScenario(){
System.out.println("This will run before the Scenario");
}
#After
public void afterScenario(){
System.out.println("This will run after the Scenario");
}
}

Related

how to pass parameter to testng.xml from command line with maven

I am running appium test using testng
want to pass app path to desired capabilities as parameter to testng.xml file
how can i do this from command line with maven ?
Lets say you have a suite xml file which looks like below
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="sample_suite" verbose="1" parallel="false" thread-count="2">
<test name="sample_test">
<parameter name="name" value="Krishnan"/>
<classes>
<class name="ParameterisedSampleTestClass" />
</classes>
</test>
</suite>
And you would like to change the value of the parameter name to a different value other than Krishnan (which is what is defined in the suite xml file)
You basically do this by passing the JVM argument -Dname=John.
TestNG by default supports changing parameter values and accepts values at run via JVM arguments.
You just need to use the same name as your parameter name, for the JVM argument.
You can find more details in my blog post here
You can achieve this by providing JVM argument as Krishnan mention in post bellow and nice blog in link:
mvn -Dbrowser="chrome" test
and gather them in your code (eg. java) via
String broswser = System.getProperty(browser);
and then turn into desired capabilities afterwards:
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapability.setBrowserName(browser);

JUnit class is not displayed in JMeter

I have created a Java project with 1 class containing 2 simple tests, exported as jar and added the jar in my JMeter JUnit folder, now after restarting the JMeter i don't see my class in JMeter even after selecting Annotation 4 option.
This is my class:
package print;
import org.junit.Test;
public class PrintClass {
#Test public void test() {
System.out.println("Hello World..!");
}
}
Consider the following checklist:
Your JUnit test class should have either empty constructor or constructor taking a single string as an argument. Other options are not supported
You should place your .jar file(s) under "lib/junit" folder of your JMeter installation
If there are any dependencies you need to put them somewhere in JMeter classpath as well
JMeter restart will be required to pick the .jars up
In case of any problems first of all check jmeter.log file (normally it lives under "bin" folder of your JMeter installation and contains enough troubleshooting information)
See How to Use JUnit With JMeter article for more details.
Even I faced same kind of issue then I added the dependency jars to the lib file.
Make sure you copy these files at this location -> apache-jmeter-5.1.1\lib
1.Selenium webdriver jar file (selenium-server-standalone)
2. Junit4 Jar file (junit4)
Make sure you add Jar file from eclipse or any IDE to this location -> apache-jmeter-5.1.1\lib\junit
1. Jar file which contains the automation selenium code (Using Junit)
Restart Jmeter and continue which the normal process of adding thread group and adding Junit sampler etc...
This resolved my issue and I was able to run my scripts on Jmeter.
In eclipse make sure that you create a JUnit class, not just the class and add junit annotations to this, even i was facing the same issue, it got resolved when i was created a JUnit class and then uploaded my project in JMeter
Did you put the jar in :
/lib/junit
Check you jar by running below command to see if it is ok:
jar -tvf <your jar>
And its dependencies as described in:
JUnit test classes not showing up in JMeter
See this for more details:
http://jmeter.apache.org/usermanual/junitsampler_tutorial.html

Send email after my 'TestNG' tests have fully Executed? (Email sending code works)

So i need to send an email after every new test suite executions.
MVN CLEAN: deletes the 'target: surefire directory'
If i execute my test case, a new 'email-able report' will be created.
I have tried placing my send email report, in the #AfterSuite method however when the test executes it cannot find the report because only after the test suite has finished does the report generate.
any ideas on how to send the report after the test suite has finished, possible to use TestNG and my email method?
You could try using a SuiteListener insteadof #AfterSuite:
SuiteListener.java:
package org.example;
import org.testng.ISuite;
import org.testng.ISuiteListener;
public class SuiteListener implements ISuiteListener {
public void onStart(ISuite suite) {
}
public void onFinish(ISuite suite) {
// send report...
}
}
testng.xml:
<?xml version="1.0" encoding="UTF-8"?>
<suite name="TestNgMavenExampleSuite" parallel="false">
<listeners>
<listener class-name="org.example.SuiteListener" />
</listeners>
...
</suite>
I would suggest you to use Jenkins to executing your tests and sending email notification. Configuring that might take some time if you are doing it for the first time but it has lots of option.
however, for now you can use maven java command to execute your Java file after executing all the tests. Run this command after you maven test command.
mvn test-compile exec:java -Dexec.mainClass="com.gaurang.SendEmail" -Dexec.classpathScope="test"

Selenium Netbeans Project to runnable jar

I'm developing tests using junit, maven and Selenium inside Netbeans IDE. I don't have any main class inside the src folder. I can run the tests easily from the IDE, but I'm trying to pack all of them into one jar file (so I can later use with linux cron and schedule daily tests). I've searched around the web but so far my search hasn't been successful. Can anyone point me in some enlightment path please?
Add a class in your project that contains the main function and accepts test class names as parameter.
class RunTest
{
public static void main(String args[])
{
// Run the tests contained in the classes named in the args.
org.junit.runner.JUnitCore.main(args);
}
}
Now create the jar using maven including all the dependancies in pom.xml. you will be able to run tests through jar by passing test class names
for more information read this

Run TestNG single test from one Group

In testng.xml, I have included couple of test classes for grouping. Inside Test Classes I pass some parameters and group name:
#Parameters({"excelName", "excelTabName"})
#BeforeClass(alwaysRun=true)
public void setup(){/*Setup code here*/}
#Test(groups={"security"})
public void searchUser() {/*Test code here*/}
If I disable all the test class names apart from one which I would like to run, I use maven command like mvn -Dgroup=groupName test. It fires up the test class which is not commented in testng.xml
I would like to keep all those test classes enabled in testng.xml and still would like to run only one test when required. Any suggestions would be very much appreciated.
Looks like all you need, is to specify which methods to execute when running
<test name="MyTest">
<classes>
<class name="TestClass">
<methods>
<include name="searchUser" />
</methods>
</class>
</classes>
</test>
This will load only the test method searchUser() for execution.

Resources