Cucumber-JVM Step definitions - java

After creating my feature file in eclipse, i run it as a Cucumber feature. i use the step definition the console gives me to create the first base of the test file
#Given("^the input is <(\\d+)> <(\\d+)>$")
these should be outputted by the console however currently it is showing the feature without the step definitions.
Feature: this is a test
this test is to test if this test works right
Scenario: test runs # src/test/resources/Test.feature:4
Given: i have a test
When: i run the test
Then: i have a working test
0 Scenarios
0 Steps
0m0,000s
this feature is just to check if cucumber is working properly.
the runner:
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
#RunWith(Cucumber.class)
#CucumberOptions(
monochrome = true,
dryRun = false,
format = "pretty",
features = "src/test/resources/"
)
public class RunCukes {
}
what can be the cause of the console not showing all the info?
TL:DR console does not show the step regex for missing steps
EDIT: added feature file
Feature: this is a test
this test is to test if this test works right
Scenario: test runs
Given: i have a test
When: i run the test
Then: i have a working test

The problem is in the feature file. Using : after Given, When and Then is the problem. I was able to reproduce your issue with your feature file. But when I removed the : and ran the feature file, with the same Runner options provided above, I got the regex to implement missing step definitions.
P.S I am using IntelliJ, but don't think it would make a difference.
Feature: this is a test
this test is to test if this test works right
Scenario: test runs # src/test/resources/Test.feature:4
Given i have a test
When i run the test
Then i have a working test
Below is what I got:
Testing started at 19:12 ...
Undefined step: Given i have a test
1 Scenarios (1 undefined)
3 Steps (3 undefined)
0m0.000s
Undefined step: When i run the test
You can implement missing steps with the snippets below:
#Given("^i have a test$")
public void i_have_a_test() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
#When("^i run the test$")
public void i_run_the_test() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
#Then("^i have a working test$")
public void i_have_a_working_test() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
Undefined step: Then i have a working test
1 scenario (0 passed)
3 steps (0 passed)
Process finished with exit code 0

it can happen if your .feature file is invalid somehow. I once had it happen just because I had two || together in the examples table of my Scenario Outline

Related

How I can attach screenshot of failed Cucumber step to Allure report exactly in step, not in Tear Down section?

I can't add screenshot of failed cucumber step under the step in Allure report.
I use allure-cucumber4-jvm:2.12.1, cucumber-java:4.3.1 and try to add screenshot of tested web app to allure report EXACTLY under failed cucumber step. So I tried to use cucumber's #AfterStep and #After annotations and add screenshot in annotated methods under scenario.isFailed() conditions. But the problem is that screenshots are added after all steps in Tear Down section. The only difference is that #After method is called once and #AfterStep methods is called by every step.
#AfterStep
public void addScreenshotOnFailed(Scenario scenario) throws IOException {
if (scenario.isFailed()) {
try {
Allure.addAttachment("Failed step", new FileInputStream(Screenshots.takeScreenShotAsFile()));
} catch (FileNotFoundException ignored) {}
}
}
I expect screenshot of tested web app exactly under failed cucumber step? but actually I have it in Tear Down section after all scenario
Idea is using the events to capture the step status and embed the screenshot. If I am correct, As per current design AfterStep does not execute when the step is failed. So we have to use AfterConfiguration in hooks.
Ruby:
Here is the ruby implementation.
AfterConfiguration do |scenario, config|
config.on_event :test_step_finished do |event|
if event.result.failed?
# get screenshot
end
end
end
Check this thread regarding the allure implementation.

How to debug the selenium java code in eclipse when scripting testcases with large number of test steps

How to perform dry run or debug my selenium java code in Eclipse? If am writing script for the testcase consisting 25 steps , my script failed at 21st step. So I am adjusting my script at 21st step, for this do i need rerun from first that is all the 20 steps on each time adjusting the script ?
Please help me.
Thanks in advance!!!
Yes, if your steps are connected you will have to run from the beginning every time. That is why it is recommended to keep test cases for automation small and independent, simply because it is hard and costly to maintain and make the really stable. As a suggestion try to split it into multiple independent test cases.
For example, if your test case looks like
action 1:
check 1
action 2:
check 2
action 3:
check 3
You can have 3 automation tests looking like (pseudo code):
Test 1.
beforeTest1() {
makeSystemForTest1();
}
test1() {
action1();
check1();
}
Test 2.
beforeTest2() {
makeSystemForTest2();
}
test2() {
action2();
check2();
}
Test 3.
beforeTest3() {
makeSystemForTest3();
}
test3() {
action3();
check3();
}

Cucumber step definitions are not being located when running from the command line

Been following along the cucumber book for Java and this example I am working on is very similar to what was in chapter 2. This is on the cmd line as I am not wanting to incorporate in IDE at this point.
Feature: practice test
Scenario Outline: not CheckingOut bananas
given abc
when place holder2
then place holder3
The test steps:
public class SimpleTest {
#Given("^abc")
public void part1() throws Exception {
System.out.println("part 1");
throw new Exception("an exceptoin");
}
#When("place holder2")
public void part2(){
System.out.println("part 2");
}
#Then("place holder3")
public void part3() {
System.out.println("part 3");
}
}
I've tried the "abc", "^abc", "give abc"... on the #Given
I am driving this with the following bat file and the step definitions do compile and the class file is there. I realize on the -g (glue) option that the package name is the argument value so I made this as simple as possible
but I am getting 0 scenarios found. The bat file is simple:
echo cucumber compile and execution
javac -cp "lib/*" step_definitions/SimpleTest.java
javac -cp "lib/*;web/WEB-INF/lib/*" test/java/xxxxx/zzzz/referral/out/batch/RunCukesTest.java
java -cp "lib/*;web/WEB-INF/lib/*;.;test/java/*" cucumber.api.cli.Main -p pretty -g step_definitions features
consistently I get the following results:
Feature: practice test
Scenario Outline: not CheckingOut bananas ←[90m# bogus.feature:2←[0m
given abc
when place holder2
then place holder3
0 Scenarios
0 Steps
0m0.000s
Clearly the CLI is finding the feature file but the compiled step definitions are not being found. Doubt it is a class path issue. I've provided extra dirs and have moved the step definition file around. the CLI is not taking the feature file and matching it to the compiled definitions. Thanks ahead of time. A lot of times typing out these questions the solution is realized but on this it is not.
If the *.feature files are a requirement for cucumber to run and that they must correspond to a compiled step definition even if junit is hooks into the cucumber.class test runnner, the book could have left less room uncertainty.
The forum for the book looked thin. Probably need to go to github or wherever the source code is.
Firstly, Keep the first letter of keywords as capital ex. Given, And etc.
Secondly, Steps defined under Scenario Outline executes only and only when there are few records present in the 'Example'. Moreover, These steps will be executed 'n' number of times for 'n' records.
It can be considered as an implicit loop provided by Feature file.
Try capitalising the first letters of Given, When, Then, And & But
Cucumber allows text before each scenario and feature, as to allow more description of what is going to happen in each. Using capital letters for the Given, When, Then, And & But, is the standard that they keep to, to know there is a step there.
So for your example:
Feature: practice test
Scenario: not CheckingOut bananas ←[90m# bogus.feature:2←[0m
Given abc
When place holder2
Then place holder3
EDIT
Don't use the Scenario Outline syntax unless you are going to provide examples below, as demonstrated here:
Feature: practice test
Scenario Outline: not CheckingOut bananas ←[90m# bogus.feature:2←[0m
Given abc
When place holder<placeholder>
Then place holder<placeholder2>
Examples:
|placeholder|placeholder2|
| 1 | 2 |
| 2 | 3 |
| 3 | 4 |

How to implement TestNG Listeners in Python test framework?

I am trying to learn python to work on a test project.
Is there a way to implement TestNG Listeners like functionality in python test framework.
Listeners have method like OnTestFailure(), OnTestSuccess, OnStart(), and many more which are really helpful when you want to do certain things.
Let's say, a test case failed and you want to perform some actions like taking a screenshot. Then you can just write that in one place and not have write that in every afterTest method also.
This class will be called from the test case like this
TestStatus.mark('testName', result, 'message you want to log')
result is a boolean
class TestStatus(unittest.TestCase):
def __init__(self):
super(TestStatus, self).__init__()
def mark(self, testName, result, resultMessage):
testName = testName.lower()
try:
if result:
self.log.info("Verification successful :: " + resultMessage)
else:
# If the test fails,
# this calls screenshot method from util class
self.util.screenShot("FAIL" + mapKey)
self.log.info("Verification failed :: " + resultMessage)
except:
self.log.info("### Exception Occurred !!!")
traceback.print_stack()
This is a sample test case in the test case class:
def test_accountSignup(self):
# Generate a username and password to use in the test case
userName = self.util.getUniqueName()
password = self.util.getUniqueName()
# You can ignore this, this is calling a method
# signup from the account page (page object model)
self.accounts.signup(userName, password)
# This call is also from the page object,
# it checks if the sign up was successful
# it returns a boolean
result = isSignUpSuccessful()
# test_status object was created in the setUp method
#
self.test_status.mark("test_accountSignup", result,
"Signup was successful")
#Sunshine, Thanks for the reply. One question.
I see that, inside the test case function "def test_accountSignup(self)", towards the end of the test case, you added the line
self.test_status.mark("test_accountSignup", result,
"Signup was successful")
What if the test cases fails some where before reaching test_status.mark call in the test case. For eg: What will happen if the test case fails on the line
self.accounts.signup(userName, password)
I guess, in this case, the test result won't be captured for this particular test case. Correct?
Do you think, we should instead write the test cases as shown below?
def test_accountSignup(self):
try:
# Write all the test case specific code here
self.test_status.mark("test_accountSignup", "Passed",
"test_accountSignup passed")
except Exception as e:
self.test_status.mark("test_accountSignup", "Failed",
"test_accountSignup failed. Exception is: "+e.message)
Please let me know what you think. Thanks again!

Java - having to test many inputs manually

I am using eclipse and need to test many files for my application. This mean, I have to go to: `run -> run configurations -> arguments', change them and re-run, for about 30 different test files.
Is there a quicker way to do this?
I have googled java automated testing. Just need some guidance, I am abit confused.
thanks
daniel
You should setup a Maven project or an ant build file to perform a suite of tests in one click rather than going one by one as you currently do.
Otherwise you can simply put all the tests you want to run in a specific package or folder then select : "Run all tests in the selected project, package or source folder" in the JUnit Run/debug configuration :
Another way with Eclipse is to create a test suite :
Open the New wizard
Select Java > JUnit > JUnit Test Suite and click Next.
Enter a name for your test suite class
Select the classes that should be included in the suite.
if it's just command line variations, you can sort it out adding a simple class like this (wrote this on the fly without a javac, may have errors)
public class PropertyRunner {
private static String commands [] = {"TEST_1", "TEST_2", "TEST_3" };
public static void main(String[] args) throws FileNotFoundException, IOException
{
Properties config = new Properties();
config.load(new FileInputStream("config.props"));
// config.props contains all my tests in the format:
// TEST_1=-a|-k|ccccc
// TEST_2=-b|-k|ccccd
// TEST_3=-c|-k|FEFEF
// now run test cases:
for (String key : commands) {
String cmdLine = config.getProperty(key);
// cmdLine is in format "-a|-b|ccccc"
String childArgs[] = cmdLine.split("\\|");
// Exec your main class passing args directly or via threads
// YourApp.main(childArgs);
}
System.exit(0);
}
}
It's pretty simple, you store all your command lines in a property file, and then iterate on every key executing your real main class and passing the arguments read from the property file.

Categories

Resources