Difference between Testng class and normal Java class - selenium

What is the difference between Testng Class and a normal Java class. While creating an Automation suite in Selenium should we use Testng class or normal java class in Eclipse.

TestNg is not language it's a testing framework. See this
TestNG is a testing framework inspired from JUnit and NUnit
This is a framework to handle different kind of testing such as unit,end to end, functional and so more. It uses Java and provide supports and annotations to drive testings. That's it!

A TestNg class is a Java class, but a Java class does not have to be a TestNg class.
The following is from the TestNg documentation and gives an understanding what you can do with the TestNg class:
This class is the main entry point for running tests in the TestNG
framework. Users can create their own TestNG object and invoke it in
many different ways:
On an existing testng.xml
On a synthetic testng.xml, created entirely from Java
By directly setting the test classes
You can also define which groups to include or exclude, assign
parameters, etc...
From: http://testng.org/javadoc/org/testng/TestNG.html
TestNg is basically is a set of code written in Java that allows you to create tests.

A TestNG class is a Java class that contains a method annotated by this class: org.testng.annotations.Test;

Testng Class and a normal Java class
TestNG Class: TestNg class is nothing but any normal class with TestNG annotation and Annotation is a tag that represents the metadata which gives the addition information about class, methods and interface.
To know more about annotation Please refer this https://docs.oracle.com/javase/tutorial/java/annotations/
While creating Automation suite we use either JUNIT or TestNG. JUNIT or TestNG are testing framework. They have their own set of libraries and annotations which is useful to run our automation suite. JUNIT or TestNG makes easy to run automation suite or generate reports of build run. They provide the feature to setup or cleanup activity using Before and After annotation which is require in almost every autoamtion suite.
TestNG also provide us flexibility to execute our automation script based on our requirement like we can group of test cases, we can include or exclude methods, parallel execution of test methods and many more.
Please refer this to know more about TestNG.
http://testng.org/doc/documentation-main.html
I hope this will help.

Related

Is there any possible way of running testng test groups in #factory method annotation?

I have my framework on #factory annotation. The test suite initializes the factory class which in turn runs the #test annotated methods.
I need to run specific tests groups only which can be initialized at suite level.
or if there is any way of running a specific factory method from the complete suite

Create Kotlin annotation from Java #Test TestNG annotation

I am using Java TestNG annotation:
#Test(groups = ['integration'])
And I would like to create Kotlin annotation like:
#IntegrationTest
Is it doable ?
No, as of now Kotlin language doesn't have builtin tools that enable such use case.
You can resort to the annotation processing technique: write an annotation processor that replaces your custom #IntegrationTest with #Test from TestNG.
One drawback of annotation processing is that a processor is a black box to the tooling. For example, IDE won't treat the methods annotated with #IntegrationTest as tests because it doesn't know that they are going to be post-processed later.
Also annotation processing is a JVM-specific tool, so it isn't supported on other platforms.

how to execute Cucumber Step defination with TestNG annotation

I am supposed to migrate on Cucumber. I do have project framework with Selenium, TestNG with Data Driven Framework, Maven. I am exploring Cucumber feasibility with TestNG annotation.
My question is, How we can create connection between #Test method and Step definition of cucumber. Let's example our code is written in #BeforeClass, #Test, #AfterClass method. So how we can migrate with Step definition.
Feature File :
Feature: Is it Friday yet?
Everybody wants to know when it's Friday
Scenario: Sunday isn't Friday
Given today is Sunday
When I ask whether it's Friday yet
Step Definition:
#Given("^today is Sunday$")
public void today_is_Sunday() {
// Write code here that turns the phrase above into concrete actions
System.out.println("this is demo1");
}
#When("^I ask whether it's Friday yet$")
public void i_ask_whether_is_s_Friday_yet() {
// Write code here that turns the phrase above into concrete actions
System.out.println("this is demo2");
}
Class Exection:
#CucumberOptions(features = "cfeature/firstDemo.feature", glue = { "mytest/Stepd" })
public class demo01 extends AbstractTestNGCucumberTests {
private TestNGCucumberRunner tcr;
#BeforeClass(alwaysRun = true)
public void beforeClass() throws Exception {
tcr = new TestNGCucumberRunner(this.getClass());
}
#Test(groups="cucumber", description="Runs CucumberFeature")
public void testdemo() {
System.out.println("Hello");
}
#AfterClass(alwaysRun = true)
public void afterClass() {
tcr.finish();
}
}
Console:
Hello
[33mUndefined scenarios:[0m
[33mcfeature/firstDemo.feature:4 [0m# Sunday isn't Friday
1 Scenarios ([33m1 undefined[0m)
5 Steps ([33m5 undefined[0m)
0m0.073s
You can implement missing steps with the snippets below:
As of now, #Test annotation is calling. But, How to replace it with Step Definition. Please assist.
Not sure what the confusion here. Here's how you can relate TestNG and cucumber terminologies.
<test> tag in TestNG can be visualized as a feature file in cucumber.
#Test method in TestNG can be visualized as a scenario in cucumber.
A Step definition in cucumber has nothing directly equivalent to in TestNG because, its part of a scenario. But for the sake of understanding you can visualize it as one line of code doing a logical operation in TestNG.
The default implementation of AbstractTestNGCucumberTests is as below:
It contains a data provider internally which provides one feature file at a time.
It contains a #Test method which is bound to the above mentioned data provider, which retrieves all the scenarios in the feature file and then runs them one after the other.
You can build your own variant of AbstractTestNGCucumberTests to do various different things (such as support concurrent scenario execution which is currently not available in Cucumber JVM bindings).
As an example you can take a look at Cucumber-roadrunner library that I built which uses the above concept to support parallel scenario execution and also provides thread safe reports.
With respect to the error you are facing viz., You can implement missing steps with the snippets below: is basically because cucumber jvm bindings perhaps isn't able to bind your feature file with a glue code (which is what you are providing via the #CucumberOptions annotation). You should perhaps take a closer look at the cucumber jvm bindings documentation to understand how to provide the correct values.
You can also take a look to gherkin with QAF which is pure TestNG implementation for gherkin. It is using TestNG (NOT cucumber runner) and provides you all the features of testNG including parallel execution, listeners, grouping, priority etc...
Each scenario converted as TestNG test and you can run scenarios as parallel. Furthermore you can also use inbuilt or custom data-providers while authoring BDD. No need additional runner just configure as usual using appropriate factory class for the BDD syntax you are using.

Run Junit Test on a dynamically loaded class

I have to get the source code of a class, compile it and then execute a Junit test on it. I managed in many ways to compile dynamically my code, generating a .class file, so that's not the problem.
Since this procedure is done dynamically, how can I invoke methods and constructors of this class in the JUnit test case without using Java reflection?

How to use TestNg in Selenium WebDriver?

How to use TestNg in Selenium WebDriver? Explain me what is the usage of that.
I am new Learner in Selenium WebDriver
Hi TestNG can be defined as
1.TestNG is a testing framework designed to simplify a broad range of testing needs, from unit testing (testing a class in isolation of the others) to integration testing (testing entire systems made of several classes, several packages and even several external frameworks, such as application servers).
2.For official TestNG documentation Please Click Here
Before you can use TestNG with selenium you have to install it first.Talking in consideration that you are working with eclipse (any version)
1.There are various ways to install TestNG either follow this or this or simply go to Help/Eclipse MarketPlace. under Find type Test NG and click on the install
now how to use Test NG in eclipse with selenium
#BeforeTest
public void TearUP(){
// preconditions for sample test
// like browser start with specific URL
}
#Test
public void SampleTest(){
// code for the main test case goes inside
}
#AfterTest
public void TearDown1(){
// thing to done after test is run
// like memory realese
// browser close
}
Some information for above code
TestNG have various annotations for more info on annotation go to the above link
#BeforeSuite: The annotated method will be run before all tests in this suite have run.
#AfterSuite: The annotated method will be run after all tests in this suite have run.
#BeforeTest: The annotated method will be run before any test method belonging to the classes inside the <test> tag is run.
#AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the <test> tag have run.
#BeforeGroups: The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked.
#AfterGroups: The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked.
#BeforeClass: The annotated method will be run before the first test method in the current class is invoked.
#AfterClass: The annotated method will be run after all the test methods in the current class have been run.
#BeforeMethod: The annotated method will be run before each test method.
#AfterMethod: The annotated method will be run after each test method.
One of the primary usage of selenium is to test ui functionality, and as a testing framework testNg has many techniques to run and report the tests and can be leveraged for ui testing with selenium. One of the tools effectively use this is selion (https://github.com/paypal/selion).