How can I determine if the current scenario is N-th? - cucumber

I am using Scenario Outline with Examples. Examples have many rows. E.g. 10 rows. I have an After hook where I check the outcome of scenario passsed or failed and write it to a file. Now I need to save and close that file after executing the final row. How can I determine if the current scenario is the last one?

One option would be to tag the last example explicitly and have an After hook for that tag, as follows:
#others
Examples:
|input|
| abc |
| xyz |
#last
Examples:
|input|
| pqr |
#After("#last")
public void afterScenario() {
}

You would need something like an #AfterAll Hook, this is currently not supported in cucumber, but you can use this workaround which uses a jvm shutdown hook. :
static void addShutdownHook(WebDriver driver) {
Runtime.getRuntime().addShutdownHook(new Thread() {
#Override
public void run() {
// DO STUFF LIKE SAVING YOUR FILE
driver.quit();
}
});
}
Full example included in the shared driver example: https://github.com/cucumber/cucumber-jvm/blob/master/examples/java-webbit-websockets-selenium/src/test/java/cucumber/examples/java/websockets/SharedDriver.java

Related

rolling back transactions but one

I've got a method annotated as follows:
#Transactional(propagation=Propagation.REQUIRES_NEW, rollbackFor=Exception.class)
public void x() {
updateA();
updateB();
updateC();
}
I want all the transactions rolled back, except for the ones in updateB(). How can that be accomplished?
I found PROPAGATION_NOT_SUPPORTED, whose documentation says:
Do not support a current transaction; rather always execute non-transactionally..
#Transactional(propagation=PROPAGATION_NOT_SUPPORTED)
public void updateB() {
...
}

JADE AchieveREInitiator not working properly. prepareRequests is not called

I am currently using JADE to build an agent-based platform to solve scheduling problems. The case is that, a defined behaviour extends AchieveREInitiator doesn't work properly in some specific state of the programme.
This is the behaviour class:
class ACOInitiator extends AchieveREInitiator {
Agent myAgent;
List<Schedule> roundSchedules;
public ACOInitiator(Agent a, ACLMessage msg) {
super(a, msg);
this.myAgent = a;
roundSchedules = new ArrayList<Schedule>();
System.out.println(msg.getContent()); //This is properly printed.
// TODO Auto-generated constructor stub
}
#Override
protected Vector prepareRequests(ACLMessage request) {
System.out.println(request.getContent()); //In some specific state, this is not printed.
Vector v = new Vector(1);
for (int i = 1; i <= Properties.antNum; i++) {
AID aAID = new AID("Ant" + i, AID.ISLOCALNAME);
request.addReceiver(aAID);
aAID = null;
}
v.addElement(request);
return v;
}
#Override
protected void handleAgree(ACLMessage agree) {
try {
roundSchedules.add((Schedule) (agree.getContentObject()));
} catch (UnreadableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
agree = null;
}
#Override
protected void handleAllResponses(Vector responses) {
Collections.sort(roundSchedules);
Environment.iterSolution = roundSchedules.get(0);
roundSchedules = null;
Environment.setState(Environment.STATE_ITERATION_CHECKING);
responses = null;
myAgent.removeBehaviour(this);
}
}
When the programme is executed, the following results are shown:
Generate solution.
Generate solution.
Iteration #1: 788.0(0.48s) (New best solution)
Generate solution.
Generate solution.
Iteration #2: 809.0(0.12s) r = 1/2
Generate solution.
Generate solution.
Iteration #3: 793.0(0.08s) r = 2/2
......
----- Simulation starts -----
......
#120
Breakdown machines:
Repaired machines:
Incoming jobs: 2-5;
Generate solution.
----- All schemes finished -----
This is unwanted. Apparently, the two Strings to be printed is the same. That's why every iteration in the solution stage, 2 "Generate solution." are printed. However, in the later simulation stage, only 1 "Generate solution." is printed and the programmes goes to the end. It can be inferred that the "prepareRequests" method in not auto called, as in earlier stage.
I'm new to JADE and the problem has bothered me a couple of days. Please help if you are a JADE programmer.
I'm not sure I understand your question or problem but from what I can see the first printout "Generate solution" happens in the constructor:
public ACOInitiator(Agent a, ACLMessage msg) { -> System.out.println(msg.getContent())
the second one in the prepare requests method:
protected Vector prepareRequests(ACLMessage request) {-> System.out.println(msg.getContent())
.
Also remember when interacting with multiple agents the initiator behaviour will wait for a response from all its responder behaviours.
Hope this helps, if it does not, try attaching sniffer diagrams.

Cucumber does not read data from scenario outline

I have following scenario outline
Background:
Given customer is in hot or not page
Scenario Outline: Hot article
And customer enters <name>
And submits
And customer clicks thumbs up
Then ensure the TBD
Examples:
| name |
| Elliot |
| Florian |
and following step implementation -
#And("customer enters <name>")
public void customer_enters_name(String name) throws Throwable {
// Express the Regexp above with the code you wish you had
Thread.sleep(10000);
}
But when I execute test then I get following error -
You can implement missing steps with the snippets below:
#Given("^customer enters Elliot$")
public void customer_enters_Elliot() throws Throwable {
// Express the Regexp above with the code you wish you had
throw new PendingException();
}
#Given("^customer enters Florian$")
public void customer_enters_Florian() throws Throwable {
// Express the Regexp above with the code you wish you had
throw new PendingException();
}
Did I implement test steps wrong?
Instead of
Scenario Outline: ...
And customer enters <name>
...
Do this (Note the double quotes)
Scenario Outline: ...
And customer enters "<name>"
...
Just to add, The same error arise when in the runner class ,we put the wrong package of the step class
#CucumberOptions(glue = {"com.wrongpackage.step"})
public class TestRunner{
}
Step class should be present and the package should be correct.

Can anybody explain me this code?

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class SlideImage extends MIDlet{
private Display display;
public void startApp(){
display = Display.getDisplay(this);
display.setCurrent(new IconsCanvas());
}
public void pauseApp(){}
public void destroyApp(boolean unconditional){
notifyDestroyed();
}
}
class IconsCanvas extends Canvas implements Runnable{
SlideMenu menu = null;
public IconsCanvas(){
Image[] image = new Image[10];
try{
image[0] = Image.createImage("/bollywood-height.png");
image[1] = Image.createImage("/212229_1193669628.png");
image[2] = Image.createImage("/95936_50.png");
image[3] = Image.createImage("/shreya.png");
image[4] = Image.createImage("/Aishw.png");
image[5] = Image.createImage("/karishma.png");
image[6] = Image.createImage("/tmb_jiahkhannishabd.png");
image[7] = Image.createImage("/amisha.png");
image[8] = Image.createImage("/shilpashetty.png");
image[9] = Image.createImage("/priti.png");
menu = new SlideMenu(new String[]{"1", "2", "3", "4",
"5", "6", "7", "8", "9", "10"},
image, getWidth(), getHeight());
new Thread(this).start();
}catch(Exception e){
e.printStackTrace();
}
}
protected void paint(Graphics g){
menu.paint(g);
}
public void keyPressed(int key){
int gameKey = getGameAction(key);
if(gameKey == Canvas.RIGHT){
menu.slideItem(1);
}else if(gameKey == Canvas.LEFT){
menu.slideItem(- 1);
}
}
public void run(){
try{
while(true){
repaint();
synchronized(this){
wait(100L);
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}
Most of things in this code i got the reason for usage..but the rest of things like
SlideMenu,
menu.paint(g),
menu.slideItem(1);
menu.slideItem(-1);
The above things, i find difficult to know why they have been used...
It (your original snippet, the run method) looks like a thread whose sole task it is to just repaint the component every tenth of a second.
As to what component it refers to, you haven't provided enough information. It depends on the object that the run() is running for.
In terms of the synchronized bit, this is most likely a way to prevent other threads doing stuff to the component while this thread is within the sleep.
If this is the object xyz, then any other thread trying to execute a block like:
synchronized (xyz) {
// Work your magic here.
}
will not be able to run while the wait is progressing.
By the way, if you use four spaces at the start of each code line (instead of <pre>), you get the nice syntax colouring as well. If you just mark the entire bit of code in the editor and use CTRLK, it will do it for you automagicaly.
And, in terms of your update, SlideMenu (picture on this page) appears to be a menu where one item is shown on the screen and left/right arrows allow you to scroll:
+---------+
| |
/ | | \
< | image | >
\ | | /
| |
+---------+
(forgive my pitiful graphic ability, I'll leave it there in case the link below ever disappears).
No doubt the images in the IconsCanvas ate the menu items to display. With that in mind, the other things make sense.
SlideMenu, I've already described.
menu.paint(g) is called to display the new menu in response to the IconsCanvas getting a paint request.
menu.SlideItem() slides the menu left or right depending on the argument.
The 100ms makes more sense now, if the image actually scrolls smoothly to the left or right.
In the code snippet above, on the synchronized block, the author's intention is just to wait/sleep so to be cooperative and let other threads run, and wait() must always be synchronized. But there is no need to queue so many repaints as there is no true animation going on, just displaying static slides (Images) when advancing to next/prev; the Canvas won't get invalidated until a new slide is shown (or a call comes in, etc), meaning it should be Ok to only repaint when a new slide is shown. Note that if "smooth scrolling" is desired, such effect should be implemented by SlideMenu itself which must also take care of repaints() scheduling (vs. IconCanvas)
Other comments:
On SlideMenu -- need to see the implementation of it
On menu.paint(g) -- it is delegating to the slide menu itself to paint/ refresh the current slide
On menu.slideItem(1); / menu.slideItem(-1); -- without looking at SlideMenu it seems to be a mechanism for navigation next/previous
You should consider making IconCanvas and SlideMenu Singletons.

Multiple instances of j2me midlet problem

I have a j2me midlet running on a cell phone. The code works fine, but the issue that comes up is that the program seems to be running more than one instance of itself. I have code at the beginning of the application inside the appStart() method that runs twice when the application starts. During the lifetime of the program, the code can be seen running twice when text is written to the screen.
The code looks like this:
public MyClass()
{
form = new Form("MyProgram");
cmdClose = new Command("EXIT", Command.EXIT, 1);
form.addCommand(cmdClose);
form.setCommandListener(this);
display = Display.getDisplay(this);
display.setCurrent(form);
}
public void startApp()
{
form.append("App starting\n");
// Rest of program
}
I have no idea why the code is being called twice.
I'm coding on the i290.
This is definitely a JVM bug. startApp() should be called only once at startup and can't be called again until pauseApp() is called or you call notifyPaused() yourself.
What I suggest is the following code:
private boolean midletStarted = false;
public void startApp() {
if (!midletStarted) {
midletStarted = true;
//Your code
}
}
This way you can track midlet state changes. But in fact it is better that you don't use this method at all and use constructor instead.
Oh, by the way, I don't think that there are some multiple instances or something like that, this is merely a JVM error.
Maybe you did something that made the runtime call pauseApp() and then when you set the focus to the app the runtime called startApp() again.
Put logging in pauseApp() and see what happens.

Resources