I am using Selenium to test my java web app\'s html pages (JSPs actually). My web app requires a flow to access each pages (it is a small online game web app), as in: to get to
Another option that may work is applying JUnit Parameterization to your test. It is my present understanding that the Parameters for the implementation are always executed in the order they are provided.
Using that concept you could have your JUnit implementation accept the URL as a constructor argument and fork the test internally based on the parameters provided.
In order to ensure that you're using the same WebDriver reference it would probably need to be static @BeforeClass/@AfterClass declarations. With that you may be able to have the parameters chain off one another, effectively testing that "From the previous test I am on page X. While here I will perform task Y. At the end of this test I will be on page Z, or in state A".
In Unit-level testing I would certainly say this solution would be bad form, but when you integrate a tool like Selenium you start acting on the integration-test level. I'm fairly new to this concept myself, but in the integration-test level it seems the rules of modularity are a bit more fuzzy since you will have conditions that are dependent.
I was curious, so I tried it out. It acts like I was thinking it would, if we assume we can treat the application as a static resource in relation to the test.
package demo.testing;
import java.util.List;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Lists;
@RunWith(Parameterized.class)
public class SequentialParams {
private static SystemState state;
@BeforeClass
public static void validateBeforeState() {
state = new SystemState();
Assert.assertFalse(state.one);
Assert.assertFalse(state.two);
Assert.assertFalse(state.three);
Assert.assertFalse(state.four);
}
@Parameters
public static Object buildParameters() {
Runnable reset = new Runnable() {
public void run() {
state.one = false;
state.two = false;
state.three = false;
state.four = false;
}
};
Runnable oneToTrue = new Runnable() {
public void run() {
state.one = true;
}
};
Runnable twoToTrue = new Runnable() {
public void run() {
state.two = true;
}
};
Runnable threeToTrue = new Runnable() {
public void run() {
state.three = true;
}
};
Runnable fourToTrue = new Runnable() {
public void run() {
state.four = true;
}
};
Predicate oneIsTrue = new Predicate() {
public boolean apply(SystemState input) {
return input.one;
}
};
Predicate twoIsTrue = new Predicate() {
public boolean apply(SystemState input) {
return input.two;
}
};
Predicate threeIsTrue = new Predicate() {
public boolean apply(SystemState input) {
return input.three;
}
};
Predicate fourIsTrue = new Predicate() {
public boolean apply(SystemState input) {
return input.four;
}
};
Predicate oneIsFalse = new Predicate() {
public boolean apply(SystemState input) {
return !input.one;
}
};
Predicate twoIsFalse = new Predicate() {
public boolean apply(SystemState input) {
return !input.two;
}
};
Predicate threeIsFalse = new Predicate() {
public boolean apply(SystemState input) {
return !input.three;
}
};
Predicate fourIsFalse = new Predicate() {
public boolean apply(SystemState input) {
return !input.four;
}
};
List