Java tutorial
package com.conwet.silbops; /* * #%L * SilboPS Service Test * %% * Copyright (C) 2011 - 2014 CoNWeT Lab., Universidad Politcnica de Madrid * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * #L% */ import java.util.ArrayList; import java.util.List; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait; import static org.assertj.core.api.Assertions.*; /** * This class execute tests against the HTML/Javascript API. * @author sergio */ public class EndToEndHTMLIT extends TestEnvironment { private String baseUrl; private FakeClient fakeClient; private WebDriver browser; @Before public void setup() { System.out.println("*** Setting up environment..."); baseUrl = "http://localhost:" + getServerPort() + "/"; fakeClient = new FakeClient(); System.out.println("connecting " + fakeClient.getClass().getSimpleName() + " to broker..."); fakeClient.connect(); System.out.print("creating new FirefoxDriver..."); browser = new FirefoxDriver(); System.out.println("done."); } @After public void tearDown() { System.out.println("-- quitting browser..."); browser.quit(); System.out.println("done"); System.out.print("disconnecting fake client..."); fakeClient.disconnect(); System.out.println("done."); } private void cleanTextArea(String id) { browser.findElement(By.id(id)).clear(); } private String getTextAreaValue(String textAreaID) { return browser.findElement(By.id(textAreaID)).getAttribute("value"); } private void waitMessage(final String id, final String message) { new WebDriverWait(browser, 5).until(new ExpectedCondition<Boolean>() { @Override public Boolean apply(WebDriver driver) { return getTextAreaValue(id).contains(message); } }); } @Test public void shouldOpenConnection() { System.out.print("opening connection to 'test.html'..."); browser.get(baseUrl + "silbops/test.html"); System.out.println("done."); assertThat(browser.getTitle()).isEqualTo("SilboPS Manual Test Page"); browser.findElement(By.id("connectButton")).click(); System.out.println("Clicked 'connectButton'..."); waitMessage("messageTextArea", "connected"); String msg = getTextAreaValue("messageTextArea"); assertThat(msg).contains("Message").contains("PubMessage").contains("open"); System.out.println("end open connection"); } @Test public void shouldCloseConnection() { System.out.println("start close connection test..."); shouldOpenConnection(); cleanTextArea("messageTextArea"); System.out.println("selecting endpoints to close..."); // iterates through options and close endPoints WebElement disconnect = browser.findElement(By.id("disconnectButton")); List<String> endpointList = new ArrayList<>(); for (WebElement option : browser.findElements(By.cssSelector("option[id*=\"#\"]"))) { System.out.println("disconnecting " + option.getText()); endpointList.add(option.getAttribute("id")); disconnect.click(); } System.out.println("done."); System.out.print("waiting disconnect message..."); waitMessage("messageTextArea", "disconnected"); String msg = getTextAreaValue("messageTextArea"); for (String endpoint : endpointList) { assertThat(msg).contains(endpoint); } System.out.println("done."); System.out.println("end close connection test"); } @Test public void shouldReceiveAdvertise() throws Exception { System.out.println("start advertise test"); shouldOpenConnection(); browser.findElement(By.id("advertiseButton")).click(); System.out.print("waiting advertise message..."); waitMessage("messageTextArea", "advertise success"); waitMessage("endpointTextArea", "Advertise"); System.out.println("done."); System.out.println("end advertise test"); } @Test public void shouldPublish() throws Exception { System.out.println("start publish test"); shouldOpenConnection(); browser.findElement(By.id("publishButton")).click(); System.out.print("waiting 'publish success' message..."); waitMessage("messageTextArea", "publish success"); System.out.println("done."); System.out.println("end publish test."); } @Test public void shouldSelectBySelectedSubscription() { shouldOpenConnection(); // send advertise to enable subscriptions browser.findElement(By.id("advertiseButton")).click(); // send subscription browser.findElement(By.id("subscribeButton")).click(); waitMessage("messageTextArea", "subscribe success"); // subscription = number < 10, should receive only the first and the last long first = 2L; long last = 5L; fakeClient.publish(2L); fakeClient.publish(10L); fakeClient.publish(last); String prefix = "{\"number:long\":"; waitMessage("endpointTextArea", prefix + last + "}"); String msg = getTextAreaValue("endpointTextArea"); assertThat(msg).doesNotContain(prefix + 10).contains(prefix + first).contains(prefix + last); } // TODO advanced test, to complete when HTML Test Page support // configurable subscriptions // @Test // public void shouldNotAddSubscriptionWithoutMatchingAdvertise() { // // shouldOpenConnection(); // // // send subscriptions without a matching advertise // browser.findElement(By.xpath("//input[@name='subfilter' and @value='1']")).click(); // browser.findElement(By.id("subscribe")).click(); // waitMessage("subscribe success"); // // fakeClient.publish(2L); // // TODO can't verify "not present", so better to verify // // "not recv A" and "recv B" with send(A), send(B). // // browser.verifyMessageNotRecv("2"); // } }