I want to do an integration test on my SpringMVC application using Selenium, with something like:
@Test
public void mytest() {
WebDriver driver = new FirefoxDriver();
driver.get("localhost:8080/myapp/mycontroller");
List<WebElement> elements = driver.findElements(By.cssSelector(".oi"));
assertThat(elements, hasSize(1));
}
Is it possible to "run" my app, the same way that my mvn tomcat7:run
would do, and then perform my selenium tests?
I'm using Spring 4.0.x. I already have Unit Tests for every classes on my webapp, but I need an Integration Test. My controllers are already being tested with MockMVC and spring-test-framework... but I would like to do a selenium integration test.
After a lot o tries, i ended up with a base class for my Selenium Integration tests:
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.openqa.selenium.firefox.FirefoxDriver;
import static io.github.seleniumquery.SeleniumQuery.$;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
public class SeleniumAcceptanceTest {
private static final String WEBAPP_FOLDER = "../../src/main/webapp";
private static final String APP_CONTEXT = "/myapp";
private static final int ANY_RANDOM_PORT_AVAIABLE = 0;
static Server server;
protected static String urlBase;
@BeforeClass
public static void prepareTests() throws Exception {
startWebAppServer();
$.browser.setDefaultDriver(new FirefoxDriver());
}
@AfterClass
public static void finalizaTests() {
$.browser.quitDefaultBrowser();
}
private static void levantarServidorDeAplicacao() throws Exception {
server = new Server(QUALQUER_PORTA_DISPONIVEL);
String rootPath = SeleniumAcceptanceTest.class.getClassLoader().getResource(".").toString();
WebAppContext webapp = new WebAppContext(rootPath + WEBAPP_FOLDER , "");
webapp.setContextPath(APP_CONTEXT );
server.setHandler(webapp);
server.start();
while (true) {
if (server != null && server.isStarted()) {
break;
}
}
int port = server.getConnectors()[0].getLocalPort();
urlBase = "http://localhost:" + port + APP_CONTEXT ;
}
}
And then, a test class extends this class:
public class AlertasAcceptanceTest extends SeleniumAcceptanceTest {
@Test
public void alertas_index__must_show_table_with_4_lines() {
//given
doLogin();
//when
$.browser.openUrl(urlBase + "/alertas/" );
int linesInTheTable = $("table tr").size();
//then
assertThat(linesInTheTable , is(4));
}
}
I hate answering a question with a question, but... 1) do you have to use Selenium or could it be a jUnit test? 2) which version of Spring MVC do you use? Spring 3.2 added a VERY neat feature that lets you spin up mock MVC and execute HTTP requests against it. This way you don't have to worry about starting/stopping your server and all your tests are done from "within" the app itself. Here is a snippet:
@Test
public void addAsUser() throws Exception {
TodoDTO added = TodoTestUtil.createDTO(null, "description", "title");
mockMvc.perform(post("/api/todo")
.contentType(IntegrationTestUtil.APPLICATION_JSON_UTF8)
.content(IntegrationTestUtil.convertObjectToJsonBytes(added))
.with(userDetailsService("user"))
)
.andExpect(status().isOk())
.andExpect(content().contentType(IntegrationTestUtil.APPLICATION_JSON_UTF8))
.andExpect(content().string("{\"id\":3,\"description\":\"description\",\"title\":\"title\"}"));
}
Full article can be found here .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.