I've got a simple setup with Cucumber feature files and Java step definition files.
feature.feature -> StepDefinition.java -> PageObject.java
Specifically:
STEP: Step definition file:
Given I am logged in as .... -> LoginSteps.java
And I am at the workspace -> WorkspaceSteps.java
And I start a new application -> WorkspaceSteps.java
And I accept -> AcceptPage.java - does not work.
- BUT:
And I accept -> AcceptPage.java - DOES work
As shown above, I'm using three step defintion files here. And cucumber recognizes the step definitions in both files. But it doesn't even attempt to run the "And I accept" step when it's defined in the AcceptPage.java file. If I move it to the WorkspaceSteps.java file, it runs fine.
There are no complaints about missing step definitions. In fact, there are no error messages, just orange coloring of the test results.
IntelliJ Run window:
Test results 4s462ms
- Feature: Complete application 4s462ms
- - Scenario: Budget 4s462ms
- - - And I accept 0ms
The .feature file:
Scenario: Budget
# Login/workspace
Given I am logged in as "12048746711"
And I am on the Workspace screen
And I start a new application
# Accept
And I accept
LoginSteps.java:
#Given("^I am logged in as \"([^\"]*)\"$")
public void iLogInWithId(String id) {
login(createPersonInfo(id));
}
WorkspaceSteps.java
#Given("^And I am on the Workspace screen$")
public void iAmOnScreenWorkspace() {
iAmOnWorkspace();
}
#Given("^I start a new application$")
public void startNewApplication() {
workSpacePage.applyForLoan.click();
}
AcceptPage.java
#Given"^I accept$")
public void iAccept() {
System.out.print("");
acceptPage.iAccept.click();
}
The step pointing to the iAccept() method on the AcceptPage.java doesn't run. Even when I put a break point on the System.out.print() line and debug, it doesn't stop or even get there.
But If I move the entire iAccept() method into the WorkSpace.java file, everything works.
Any ideas?
Of course, I've invalidated IntelliJ's caches and restarted. I've even tried creating a brand new Steps file, containing a new step with a new name. It's the same: The cucumber test refuses to go anywhere except to the WorkspaceSteps.java file. This is starting to look completely idiotic, hillarious and absurd.
It turns out the solution was really silly. For some reason, IntelliJ had removed the top package from the Glue in the configuration, and instead added specific packages. So, when a file not belonging to those packages was used, it was just ignored without further explanation. Deleting the specific packages and entering the top package solved the problem.
Related
Currently working on Selenium WebDriver and using Java. I have a project called*Test*.
In that Project i have many Java Programs such as Login.java, Testing1.java etc.,.
The scenario is i want to run all my scripts daily morning at 12.00 a.m. Is there any possibility to create a scheduler to run my scripts automatically.
Create an testng.xml file say name as testsuite.xml.
Now follow below 2 steps:
Step 1: Create an batch file for scheduler:
use below code - modify it and paste in notepad. save the notepad in working directory as"run.bat"
set ProjectPath=C:\Selenium\Selenium_tests\DemoProject
echo %ProjectPath%
set classpath=%ProjectPath%\bin;%ProjectPath%\Lib\*
echo %classpath%
java org.testng.TestNG %ProjectPath%\testsuite.xml
a) First line is for setting project path .
b) second line is for verifying that path is set or not.
c) third
line is for setting classpath - lib folder contain all the jar
file added to project build path
d) fourth line is for verifying
whether classpath is set or not
e) fifth line is for executing
xml file having details of all test.
Step 2:
Go to control panel.
Administrative tool.
Task scheduler and create a task which will trigger run.bat file at the time you want.
It will work.
check with quartz scheduler.. http://quartz-scheduler.org/
I am currently working on a similar project where I have to check different web applications for their availability every ~5 minutes and report any errors via mail. I am also using TestNG ans the WebDriver together. I solved my "scheduling problem" by using the TimerTask class.
Here's a short code example: (Find more code examples here)
import java.util.Timer;
import java.util.TimerTask;
public class KeepMeAwake {
*
* #param args
*/
public static void main(String[] args) {
TimerTask action = new TimerTask() {
public void run() {
Beep b = Beep.getInstance();
b.beep();
}
};
Timer caretaker = new Timer();
caretaker.schedule(action, 1000, 5000);
}
}
Since it implements Runnable, you can run multiple threads with it.
Hope that helps.
If you have questions how to integrate it with your TestNG set up, just shoot.
Follow the above steps and in windows scheduler do the steps :
Creating .bat file steps
Task Scheduler in Windows > Create new Task>
'Action' settings - "Start in (Optional)" option.
Go the task properties --> Action tab --> Edit --> Fill up as below:
Action: Start a program
Program/script: path to your batch script e.g. C:\Users\beruk\bodo.bat
Add arguments (optional): <if necessary - depending on your script>
Start in (optional): Put the full path to your batch script location e.g. C:\Users\beruk\(Do not put quotes around Start In)
Then Click OK
It works for me. Good Luck!
I understand how to run my application with command line arguments using the run configuration menu.
The problem I have is that no matter what I update these command line arguments to, eclipse does not reflect these updates when I execute the code.
so far I have set the arguments to:
test1.txt test2.txt dfs
and this will print:
args[0] = test1.txt
args[1] = test2.txt
args[2] = dfs
but if I update the arguments and re-run it, the arguments won't update
How can I "reset" the arguments and re-run the application using the updated arguments.
The above and below both function correctly and it was in fact eclipse that was causing me issues. The problem was resolved with a simple restart of eclipse.
Thanks all.
Click on Run -> Run Configurations
Click on Arguments tab
In Program Arguments section , Enter your arguments.
Click Apply
It is sure to work cause I tried it in mine right before I wrote this answer
There is a situation (bug) where modifying the Run -> Run Configurations arguments does not work, since the actual run configuration being executed is actually hidden from you.
So updating the visible one will not be reflected in your actual run.
Example:
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class EclipseRunConfigurationTest {
#Test
public void test() {
assertEquals("foo", System.getProperty("runProperty"));
}
}
Run it - it will fail.
Modify the run configuration using the method specified by Little Child. add "-DrunProperty=foo" VM parameter
Run it again - it will pass
Debug it, then switch to the debug view,
RClick the Junit launch -> Edit Rerun EclipseRunConfigurationTest...
Change the VM parameter to "-DrunProperty=bar"
Apply and Debug - it will fail
Open the Run/Debug manager again
Note that 'Rerun EclipseRunConfigurationTest' is not listed.
Note that the VM parameter is still "-DrunProperty=foo"
No amount of changing it makes the slightest bit of difference.
I shall file a bug report.
The above was run on Eclipse Kepler running on Fedora 20.
A small update in the solution given by Little Child above, to make it work with arguments having spaces in them.
e.g. first argument - abc def
second argument - ghi
third argument - jkl mno pqrs
In Program Arguments, give them like this using double quotes
"abc def"
"ghi"
"jkl mno pqrs"
If you don't give spaces it will take abc as first argument and def as second argument and ghi as thrid argument and so on..
For Eclipse Neon Users
Step 1: Click Run -> Run Configurations
Step 2: Click on arguments Tab.
Step 3: insert required arguments in VM Arguments.
Step 4: Click Apply
Step 5: Click Run.
I have tried some of the existing solutions but it does not work.
I have two files/classes First.java (in which main is defined) and Second.java where simple functions are defined.
**First.java:**
import java.util.*;
public class First
{
public static void main(String[] args)
{
Second s1 = new Second();
s1.Hello();
}
}
When I debug the above code in eclipse, it gives me error "source not found" on the line Second s1 = new Second();
However, this error occurs, if I click "step into". If I click "step over" on the aforementioned line, the error does not occur; and if click on "step into" in subsequent steps, the error does not occur again, and the execution successfully enters into the second file "Second.java".
So my question is, Is there a way that I can enter the constructor of the "Second.java" without stepping over it?
How to set the source path.
Second.java class:
public class Second
{
int a;
public Second()
{
this.a=100;
}
public void Hello()
{
System.out.println("hello how are you");
}
public int GetResult()
{
return a;
}
}
The problem is when you 'step into' the line when the Second object is created, it is asking the classloader to load the Second class. Since you probably do not have eclipse setup to point to the location of the java sources, eclipse does not know where the java source code is on your machine for all the files the vm uses to load the class, including java.lang.ClassLoader, and eclipse shows you the 'Source not found' page.
You can:
Move the break point from the line Second s1 = new Second(); in First.java to public Second() in Second.java. Then when you debug, you should hit the break point after the Second object has been loaded by the VM and you should be able to debug the constructor as it is being instanciated.
When you 'step into' the break point at the line Second s1 = new Second(); and get the source not found page, immediately 'step return' and then press 'step into' again, which should take you to the constructor of the Second class.
Click on attach source, and browse to the directory of the java source files. They are usually included with the JDK download and are located in a file called src.zip in the installation folder of your VM (for the Sun VM).
Source not found usually means that Eclipse can't find the required files. Is Second.java in your project? The best thing is to make a package such as me.russjr08.projects. That way Eclipse can search through the package (Assuming all of the correct files are there) and find the class / java files you want to use.
IIRC Another solution is to include Second.java in your src folder
I am new to java and to the eclipse IDE.
I am running Eclipse
Eclipse SDK
Version: 3.7.1
Build id: M20110909-1335
On a windows Vista machine.
I am trying to learn from the book Thinking in Java vol4.
The author uses his own packages to reduce typing. However the author did not use Eclipse and this is where the problem commes in..
This is an example of the code in the book.
import java.util.*;
import static net.mindview.util.print.*;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("hello world");
print("this does not work");
}
this is the contents of print.Java
//: net/mindview/util/Print.java
// Print methods that can be used without
// qualifiers, using Java SE5 static imports:
package net.mindview.util;
import java.io.*;
public class Print {
// Print with a newline:
public static void print(Object obj) {
System.out.println(obj);
}
// Print a newline by itself:
public static void print() {
System.out.println();
}
// Print with no line break:
public static void printnb(Object obj) {
System.out.print(obj);
}
// The new Java SE5 printf() (from C):
public static PrintStream
printf(String format, Object... args) {
return System.out.printf(format, args);
}
} ///:~
The error I get the most is in the statement.
Import static net.mindview.util.print.*;
On this staement the Eclipse IDE says it cannot resolve net
also on the
print("this does not work");
The Eclipse IDE says that the class print() does not exist for the class HelloWorld.
I have been trying to get these to work, but with only limited success, The autor uses another 32 of these packages through the rest of the book.
I have tried to add the directory to the classpath, but that seems to only work if you are using the JDK compiler. I have tried to add them as libraries and i have tried importing them into a package in a source file in the project. I have tried a few other things but cant remember them all now.
I have been able to make one of the files work, the print.java file I gave the listing for in this message. I did that by creating a new source folder then making a new package in that foldeer then importing the print.java file into the package.
But the next time I try the same thing it does not work for me.
What I need is a way to have eclipse load all these .java files at start up so when I need them for the exercises in the book they will be there and work for me, or just an easy way to make them work everytime.
I know I am not the only one that has had this problem I have seen other questions about it on google searches and they were also asking about the Thinking In Java book.
I have searched this site and others and am just not having any luck.
Any help with this or sugestions are welcome and very appreciated.
thank you
Ok I have tried to get this working as you said, I have started a new project and I removed the static from the import statement, I then created a new source folder, then I created a new package in the source folder. Then I imported the file system and selected the the net.mindview.util folder.
Now the immport statement no longer gives me an error. But the the print statement does, the only way to make the print statement work is to use its fully qualified name. Here is the code.
import net.mindview.util.*;
public class Hello2 {
public static void main(String[] args) {
Hello2 test = new Hello2();
System.out.println();
print("this dooes not work");
net.mindview.util.Print.print("this stinks");
}
}
The Error on the print statement is:
The method print(String) is undefined for the type Hello2
and if I try to run it the error I get is:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method print(String) is undefined for the type Hello2
at Hello2.main(Hello2.java:6)
The Statement::::: net.mindview.util.Print.print("this stinks") is the fully qualified print statement and it does not throw an error but it does totally defeat the purpose of the print.java file..
If you have any questions please ask Ill get back to you as soon as I can.
I've had similar issues. I solved it by following the steps below:
Click File->New->Java Project. Fill in UtilBuild for the ProjectName. Chose the option "Use project folder as root and click 'Finish'.
Right-click on UtilBuild in the PackageExplorer window and click New->package. For the Package Name, fill in net.mindview.util
Navigate within the unzipped Thinking In Java (TIJ) folder to TIJ->net\mindview\util. Here you will find all the source code (.java) files for util.
Select all the files in the net\mindview\util folder and drag them to the net.mindview.util package under UtilBuild in Eclipse. Chose the 'Copy Files' option and hit 'OK'.
You will probably already have the 'Build Automatically' option checked. If not, go to Project and click 'Build Automatically'. This will create the .class files from the .java source files.
In Eclipse, right-click on the project you were working on (the one where you couldn't get that blasted print() method to work!) Click Properties and Java Build Path->Libraries. Click 'Add Class Folder...' check the box for UtilBuild (the default location for the .class files).
I think the confusion here arises due to CLASSPATH. If you use Eclipse to build and run your code then Eclipse manages your CLASSPATH. (You don't have to manually edit CLASSPATH in the 'Environment Variables' part of your computer properties, and doing so changes nothing as far as Eclipse Build and Run are concerned.)
In order to call code that exists outside your current project (I will name this 'outside code' for convenience) you need to satisfy three things:
A. You need to have the .class files for that code (as .class files or inside a JAR)
B. You need to indicate in your source code where to look for the 'outside code'
C. You need to indicate where to start looking for the 'outside code'
In order to satisfy these requirements, in this example we:
A. Build the project UtilBuild which creates the .class files we need.
B. Add the statement import static net.mindview.util.Print.*; in our code
C. Add the Class Folder library in Eclipse (Java Build Path->Libraries).
You can investigate the effect of Step C by examining the .classpath file that lives directly in your project folder. If you open it in notepad you will see a line similar to the following:
<classpathentry kind="lib" path="/UtilBuild>
You should combine this with your import statement to understand where the compiler will look for the .class file. Combining path="/UtilBuild" and import static net.mindview.util.Print.*; tells us that the compiler will look for the class file in:
UtilBuild/net/mindview/util
and that it will take every class that we built from the Print.java file (Print.*).
NOTE:
There is no problem with the keyword static in the statement
import static net.mindview.util.Print.*;
static here just means that you don't have to give specify the class name from Print.java, just the methods that you want to call. If we omit the keyword static from the import statement, then we would need to qualify that print() method with the class it belongs to:
import net.mindview.util.Print.*;
//...
Print.print("Hello");
which is slightly more verbose than what is achieved with the static import.
OPINION:
I think most people new to Java will use Eclipse at least initially. The Thinking in Java book seems to assume you will do things via command line (hence it's guidance to edit environment variables in order to update CLASSPATH). This combined with using the util folder code from very early in the book I think is a source of confusion to new learners of the language. I would love to see all the source code organised into an Eclipse project and available for download. Short of that, it would be a nice touch to include the .class files in just the 'net/mindview/util' folder so that things would be a little easier.
U should import package static net.mindview.util not static net.mindview.util.Print
and you should extend the class Print to use its method.......
You should remove the static keyword from your import decleration, this: import static net.mindview.util.print.*; becomes this: import net.mindview.util.print.*;
If that also does not work, I am assuming you did the following:
Create your own project;
Start copying code directly from the book.
The problem seems to be that this: package net.mindview.util; must match your folder structure in your src folder. So, if your src folder you create a new package and name it net.mindview.util and in it you place your Print class, you should be able to get it working.
For future reference, you should always make sure that your package decleration, which is at the top of your Java class, matches the package in which it resides.
EDIT:
I have seen your edit, and the problem seems to have a simple solution. You declare a static method named print(). In java, static methods are accessed through the use of ClassName.methodName(). This: print("this dooes not work"); will not work because you do not have a method named print which takes a string argument in your Hello2 class. In java, when you write something of the sort methodName(arg1...), the JVM will look for methods with that signature (method name + parameters) in the class in which you are making the call and any other classes that your calling class might extend.
However, as you correctly noted, this will work net.mindview.util.Print.print("this stinks");. This is because you are accessing the static method in the proper way, meaning ClassName.methodName();.
So in short, to solve your problem, you need to either:
Create a method named print which takes a string argument in your Hello2 class;
Call your print method like so: Print.print("this stinks");
Either of these two solutions should work for you.
In my case I've dowloaded and decompressed the file TIJ4Example-master.zip. in eclipse workspace folder. The three packages : net.mindview.atunit, net.mindview.simple and net.mindview.util are in this point of the project :
and java programs runs with no problems (on the right an example of /TIJ4Example/src/exercises/E07_CoinFlipping.java)
Dunno why this happens... Ok here is the situation: I have a nb project on my laptop. I have the same project on my desktop. I copy the sources (not the entire project) on the desktop, overwriting the desktop sources. Everything cleans and builds ok. Then I start the debugger. On the main class I can debug step by step. If it goes into an internal method here is what happens:
Listening on 37574
User program running
LineBreakpoint test.java : 45 successfully submitted.
Breakpoint hit at line 45 in class test by thread main.
Thread main stopped at test.java:45.
User program running
Not able to submit breakpoint LineBreakpoint baseControllerManager.java : 41, reason: No executable location available at line 41 in class baseClasses.JNW.baseControllerManager.
Invalid LineBreakpoint baseControllerManager.java : 41
Debugger stopped on uncompilable source code.
User program finished
As you can see until I'm in static method main it works (line 45) as I jump inside a non static method (that is an override) it comes out with that... I tried to:
clean and build = no effect
manually delete build and dist = no effect
What do you suggest?
For the sake of completeness I'm attaching the sources of the main class:
import baseClasses.JNW.baseAction;
import baseClasses.JNW.baseContResult;
import baseClasses.JNW.baseController;
import baseClasses.JNW.baseControllerManager;
public class test {
public static class starter extends baseController {
public static final String ACTION_START = "ACTION_START";
#Override
public baseContResult doAction(baseAction action) {
if (ACTION_START.equals(action.action)) {
manager.log("action start...");
return new baseContResult(RESULT_OK, baseContResult.resultType.RESULT_OK);
}
return super.doAction(action);
}
#Override
public void init() {
super.init();
}
}
public void startMe() {
baseControllerManager manager;
try {
manager = new baseControllerManager();
} catch (Exception e) {
e.printStackTrace();
return;
}
starter st = new starter();
manager.setMainController(st);
manager.doAction(new baseAction(starter.ACTION_START));
}
public static void main(String args[]) {
test te = new test();
te.startMe();
}
}
Look in the file nbproject/project.properties for the property javac.debug and make sure that it's "true". If it is, grep for that property elsewhere in the nbproject directory and any local ant settings.
On a semi-related note: when I'm creating a project in NetBeans, even if the sources already exist elsewhere, I always create a new "Java Application", and let it populate the project directory as it wants. Then I can move in my sources and update the project, and NetBeans stays happy.
Edit after you tried setting javac.debug:
Looking at your question again, I see that you were able to set a breakpoint on test.java, but not able to set one on baseControllerManager.java. That indicates to me that you're getting the latter class from a JAR somewhere, not from your project directory.
So the first step is to make sure that you haven't defined a CLASSPATH environment variable. This is never a good thing to do, regardless of whether you're using an IDE or a manual build.
Next step is to look at the libraries that you've specified for the NetBeans project. You can use grep on a JARfile; the file directory is in plaintext. It should be sufficient to look for the unqualified classname.
And the final thing is to verify that you are indeed compiling the class, by looking for it in the build directory.
Generally, an uncompilable error means a symbol could not be resolved.
If you have dependencies on other projects, libraries, or jars, make sure they have built successfully / are present.
Checking "Build Projects on Classpath" (in project properties > Build > Compiling) will often fix this. If you don't have this checked, you are responsible for ensuring dependencies are already built.
The property was not present in project.properties. So I added it (at a point where there where many javac.* properties...). Then I grepped like this:
dario#dario-desktop:~/Scrivania/JNW$ grep -r javac.debug *
nbproject/project.properties:javac.debug=true
nbproject/build-impl.xml: <property name="javac.debug" value="true"/>
nbproject/build-impl.xml: <attribute default="${javac.debug}" name="debug"/>
nbproject/build-impl.xml: <javac debug="#{debug}" deprecation="${javac.deprecation}" destdir="#{destdir}" encoding="${source.encoding}" excludes="#{excludes}" fork="${javac.fork}" includeantruntime="false" includes="#{includes}" source="${javac.source}" sourcepath="#{sourcepath}" srcdir="#{srcdir}" target="${javac.target}" tempdir="${java.io.tmpdir}">
nbproject/private/private.properties:javac.debug=true
nbproject/project.properties~:javac.debug=true
In fact it sees my addition on the last line.
I cleaned and rebuilt the project. The debugger is not working again........... I think I'll kick my boss ass until he agrees to use eclipse or he fires me. I'll be unemployed but happy. Apart from joking, #kdgrgory, do you have any more ideas???
I had the same problem and found that if I run clean from project tab it solves the problem.