How to implement and run cucumber test files using testng - selenium

Trying to implement selenium + Cucumber + Testng instead of Junit.
My queries are
What is the alternate for #Runwith(Cucumber.class) in testng
How to run the class file which contains the path to feature file
package runner;
import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;
#CucumberOptions(features="src/main/java/testCases/Cucumber/Login_Cucumber.Feature",glue="")
public class TestRunner extends AbstractTestNGCucumberTests {
}

TestNg uses #CucumberOptions tag to declare parameters
#CucumberOptions(plugin = "json:target/cucumber-report.json")
public class RunCukesTest extends AbstractTestNGCucumberTests {
}
or
#CucumberOptions(features = "src/test/resources/features/Download.feature",
glue = "uk.co.automatictester.jwebfwk.glue",
format = {"pretty"})
Check this out: https://github.com/cucumber/cucumber-jvm/tree/master/examples/java-calculator-testng
Also a possible dup of :How to integrate the cucumber in testNG?

Install TestNG Eclipse Plugin. Afterwards you should be able to run TestNG Test.

First of all, Cucumber have .feature files and not test files.
Answer to your first question: 1. What is the alternate for #Runwith(Cucumber.class) in testng? "You don't need #RunWith while running with TestNG"
I didn't understand your second question but you need to understand that Cucumber runs end execute the Runner class by default and you have already defined feature files in #CucumberOptions section.
To make it more clear you can easily implement and Run Cucumber project using TestNG. The whole game is in your pom.xml file and Runner class.
Following detail also explains that you can run each scenario in cucumber as a test using TestNG.
How? It's explained below:
First of all, update your Cucumber Maven dependencies from info.cukes to io.cucumber dependencies
Following Java code in Cucumber Runner Class worked perfectly for me to run each scenario as TestNG test in feature files:
#CucumberOptions(features = "src/test/resources", plugin = "json:target/cucumber-report-feature-composite.json")
public class TestRunner {
private TestNGCucumberRunner testNGCucumberRunner;
#BeforeClass(alwaysRun = true)
public void setUpClass() throws Exception {
testNGCucumberRunner = new TestNGCucumberRunner(this.getClass());
}
#Test(groups = "cucumber scenarios", description = "Runs Cucumber
Scenarios", dataProvider = "scenarios")
public void scenario(PickleEventWrapper pickleEvent, CucumberFeatureWrapper
cucumberFeature) throws Throwable{
testNGCucumberRunner.runScenario(pickleEvent.getPickleEvent());
}
#DataProvider
public Object[][] scenarios() {
return testNGCucumberRunner.provideScenarios();
}
#AfterClass(alwaysRun = true)
public void tearDownClass() throws Exception {
testNGCucumberRunner.finish();
}
}
Run with mvn clean test command and see the magic :)
I would be happy to see your problem resolved. Please let me know if this issue is still not resolved.
Reference: https://github.com/cucumber/cucumber-jvm/blob/master/testng/README.md
I followed this approach: https://github.com/cucumber/cucumber-jvm/blob/master/examples/java-calculator-testng/src/test/java/cucumber/examples/java/calculator/RunCukesByCompositionTest.java

import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;
#CucumberOptions(features="src/test/resources/features",glue="stepDefinitions",tags="#Test01",plugin= {"pretty", "html:target/cucumber-reports" },monochrome=true)
public class RunnerTest extends AbstractTestNGCucumberTests{
}
It will work for sure.

This perfectly worked for me
package com.shyom.cucumberOptions;
import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
#CucumberOptions(
plugin = "json:target/cucumber-report.json",
features = "/shyom/src/test/java/feature",
glue="stepDefinations"
)
public class TestRunner extends AbstractTestNGCucumberTests{
}

Related

Java Selenium 'Cannot resolve symbol Test' TestNG

I have a problem with TestNG. I cannot run a test.
I am getting this error:
POM.xml has no errors.
Here is the code in test page:
import Pages.SearchPage;
import org.testng.annotations.Test;
import core.Web.AllListeners.*;
public class Search extends Listener {
#Test(groups = "Regression")
public void ticketBookingFunctionality() {
new SearchPage()
.openUrl()
.inputCaption("Comic")
.selectCityByValue()
.inputDateFrom("2020-01-01")
.inputDateTo("2021-07-05")
.clickButtonSearch()
.clickButtonBuy()
.chooseTicket()
.choosePrice()
.pushButtonFindTickets()
.closeLoginPopup();
}
}
Where can be the problem?
You need to add the following testng related jar files within your project.

How to run Karate from java application not from JUnit test

I managed to run Karate tests using Junit. But what I want is to run Karate from java application instead of Junit runner.
Currently I'am running from JUnit:
import com.intuit.karate.cucumber.CucumberRunner;
import com.intuit.karate.cucumber.KarateStats;
import cucumber.api.CucumberOptions;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
#CucumberOptions(tags = {"~#ignore"})
public class TestParallel {
#Test
public void testParallel() {
KarateStats stats = CucumberRunner.parallel(getClass(), 5, "target/surefire-reports");
assertTrue("scenarios failed", stats.getFailCount() == 0);
}
}
I tried calling the Junit class from my application main using the code below:
JUnitCore junit = new JUnitCore();
Result result = junit.run(TestParallel.class);
But I have this error:
java.lang.NoClassDefFoundError: com/intuit/karate/cucumber/CucumberRunner
Yes, please use the Java API, documented here: https://github.com/intuit/karate#java-api
Note that you won't get reports if you go down this path.
I solved the problem by removing the test scope from the karate dependencies in the pom.
Everything works fine including the reports and the output.

How to use selenium-server-standalone-2.0rc2 while creating script with RC

package com.html;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
import junit.framework.TestCase;
public class Html5 extends TestCase{`enter code here`
Selenium selenium1;
public void setUp()
{
selenium1=new DefaultSelenium("localhost",4444,"*firefox","http://live.com");
selenium1.start();
}
}
Error appearing in com.thoughtworks.selenium.DefaultSelenium; and DefaultSelenium("localhost",4444,"*firefox","http://live.com"); line.
Please suggest.
First :
What the enter code here string does there ?
Secondly :
If there is an error in the import com.thoughtworks.selenium.DefaultSelenium; and in the new DefaultSelenium, it's certainly that the jars are not in your classpath
selenium-server-standalone contains the Selenium server classes, but not the client ones, where DefaultSelenium is. You'll have to bring the client jars in your classpath, that is selenium2-java for this version I think
I think you need to give Path to firefox.exe in your Constructor..So
selenium1 = new DefaultSelenium("localhost",4444,"*firefox","http://live.com"); Goes like
selenium1 = new DefaultSelenium("localhost",4444,"*firefox C:\Documents and Settings\Mozilla Firefox\firefox.exe","http://live.com");
Try this once.

Robotium BDD with Cucumber

I want to know if you guys know how to make BDD tests with Robotium.
As I research Robotium works with a different Virtual Machine (Dalvik) so I cannot run as Junit Test (Only with Android Junit Test). So I found a possible solution to run Robotium with Junit with RoboRemote https://github.com/groupon/robo-remote. But when i tried to integrate with cucumber the tests became unstable.
So you guys know some way to make BDD tests using Robotium?
I have successfully integrated Robotium using Cucumber-JVM for Android.
For information about the now official cucumber-android module for Cucumber-JVM and the installation, have a look here. You can also find API-documentation and examples about Cucumber-JVM here: http://cukes.info/platforms.html.
In your test-module for your app, simply add the Robotium Solo jar-file as a dependency (Scope: Compile).
One of my test-classes looks like this:
public class CucumberSteps extends ActivityInstrumentationTestCase2<YourActivity> {
private Solo solo;
public CucumberSteps() {
super(YourActivity.class);
}
#Override
protected void setUp() throws Exception {
super.setUp();
}
#Before
public void before() {
solo = new Solo(getInstrumentation(), getActivity());
}
#After
public void after() throws Throwable {
//clean up
solo.finalize();
}
#Override
protected void tearDown() throws Exception {
solo.finishOpenedActivities();
super.tearDown();
}
#Given("^step_given_description$")
public void step_given_description() throws Throwable {
final View testView = solo.getView(R.id.testView);
solo.waitForView(testView);
solo.clickOnView(testView);
// and so on
}
}
I hope this is enough information for anyone to get started. When this question was asked, cucumber-android didn't exist yet. Keep in mind though, GUI tests are very often somewhat unstable! I managed to get a stable set of tests locally, but e.g. in Jenkins, usually some tests fail for unknown reasons.
I know it's a very old questions but the documentation on this subject is very limited so I'll post another piece of information.
This article helped me a lot: http://www.opencredo.com/2014/01/28/cucumber-android-vs-cucumber-appium/
And I used the documentation here as well (I know it's deprecated but the main project has no docs about android at all): https://github.com/mfellner/cucumber-android.
I got it to work with IntelliJ 13 Community Edition and Maven using bits from Android
Bootstrap - http://www.androidbootstrap.com/ (Mostly the Maven integration test project configuration)
Hope it helps.
I got working robotium with cucumber-jvm and androidstudio using this runner:
import android.os.Bundle;
import cucumber.api.CucumberOptions; import
cucumber.api.android.CucumberInstrumentation;
#CucumberOptions(features = {"features"}, tags = {"#smoke",
"~#pending", "~#manual", "~#reboot"})
public class Instrumentation extends CucumberInstrumentation {
private final CucumberInstrumentation instrumentationCore = new CucumberInstrumentation();
#Override
public void onCreate(final Bundle bundle) {
super.onCreate(bundle);
instrumentationCore.onCreate(bundle);
start();
}
#Override
public void onStart() {
waitForIdleSync();
instrumentationCore.start();
}
}

selenium - jUnit4

I am trying to run the below code but in vain.
Code is not compiling and giving error as "selenium cannot be resolved".
Can anyone look into the below code -
package com.example.tests;
import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.regex.Pattern;
public class prashantk {
#Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://book.theautomatedtester.co.uk/");
selenium.start();
}
#Test
public void testAuto_1() throws Exception {
selenium.open("/chapter2");
verifyEquals("Button with name", selenium.getValue("name=but2"));
verifyEquals("chocolate", selenium.getValue("xpath=(//input[#name='verifybutton'])[2]"));
selenium.click("link=Index");
selenium.waitForPageToLoad("60000");
verifyTrue(selenium.isTextPresent("Chapter4"));
}
#After
public void tearDown() throws Exception {
selenium.stop();
}
}
You have references to a field named selenium, but there is no such field defined.
Somewhere in your class, probably on a line just before the #Before, you want to add this field:
Selenium selenium;
Apparently from comments you also don't have the methods verifyEquals and verifyTrue referenced in your code. Those methods are defined in a base class SeleneseTestCase which your test should extend:
public class prashantk extends SeleneseTestCase {
Replace the below line in your code as shown below
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://book.theautomatedtester.co.uk/");
to
DefaultSelenium selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://book.theautomatedtester.co.uk/");
Looking at code gives an impression it was done in Selenium IDE first the exported to a tool like eclipse.
I really don't think 'verifyEquals' exist in WebDriver instead use JUnit assertions or Hamcrest assertions. Try to write your code from scratch in Java its a lot less hassle.