From 1ff1d8dbe68172352a76e4622637410b364f02d9 Mon Sep 17 00:00:00 2001 From: Peter Thomas Date: Mon, 1 May 2017 12:04:24 +0530 Subject: [PATCH] web wip, using floating log window from wicket --- .../src/main/java/application.properties | 1 + .../intuit/karate/web/wicket/BasePage.html | 11 +- .../intuit/karate/web/wicket/BasePage.java | 4 +- .../intuit/karate/web/wicket/FeaturePage.java | 57 ++++- .../web/wicket/FeatureSectionPanel.html | 2 +- .../web/wicket/FeatureSectionPanel.java | 4 +- .../intuit/karate/web/wicket/HomePanel.java | 3 +- .../web/wicket/KarateJsResourceReference.java | 40 +++ .../intuit/karate/web/wicket/LogPanel.html | 16 -- .../intuit/karate/web/wicket/LogPanel.java | 146 ----------- .../intuit/karate/web/wicket/StepPanel.java | 6 - .../intuit/karate/web/wicket/VarPanel.html | 2 +- .../intuit/karate/web/wicket/VarPanel.java | 6 +- .../intuit/karate/web/wicket/VarsPanel.html | 5 - .../intuit/karate/web/wicket/VarsPanel.java | 9 - .../karate/web/wicket/VarsRefreshingView.java | 2 +- .../com/intuit/karate/web/wicket/karate.js | 234 ++++++++++++++++++ karate-web/src/main/webapp/karate.css | 4 +- 18 files changed, 337 insertions(+), 215 deletions(-) create mode 100644 karate-web/src/main/java/com/intuit/karate/web/wicket/KarateJsResourceReference.java delete mode 100644 karate-web/src/main/java/com/intuit/karate/web/wicket/LogPanel.html delete mode 100644 karate-web/src/main/java/com/intuit/karate/web/wicket/LogPanel.java create mode 100644 karate-web/src/main/java/com/intuit/karate/web/wicket/karate.js diff --git a/karate-web/src/main/java/application.properties b/karate-web/src/main/java/application.properties index 37d9a2451..b4782eb12 100644 --- a/karate-web/src/main/java/application.properties +++ b/karate-web/src/main/java/application.properties @@ -1,3 +1,4 @@ +spring.jackson.default-property-inclusion=NON_NULL wicket.configuration=deployment diff --git a/karate-web/src/main/java/com/intuit/karate/web/wicket/BasePage.html b/karate-web/src/main/java/com/intuit/karate/web/wicket/BasePage.html index 440b62953..5164bc12b 100644 --- a/karate-web/src/main/java/com/intuit/karate/web/wicket/BasePage.html +++ b/karate-web/src/main/java/com/intuit/karate/web/wicket/BasePage.html @@ -12,7 +12,8 @@ -
+
+
@@ -21,13 +22,11 @@
-
-
+
-
-
+
-
+
diff --git a/karate-web/src/main/java/com/intuit/karate/web/wicket/BasePage.java b/karate-web/src/main/java/com/intuit/karate/web/wicket/BasePage.java index d7365a5f1..39f6e1f18 100644 --- a/karate-web/src/main/java/com/intuit/karate/web/wicket/BasePage.java +++ b/karate-web/src/main/java/com/intuit/karate/web/wicket/BasePage.java @@ -32,14 +32,12 @@ public class BasePage extends WebPage { public static final String HEADER_ID = "header"; public static final String CONTENT_ID = "content"; public static final String STICKY_HEADER_ID = "sticky-header"; - public static final String STICKY_FOOTER_ID = "sticky-footer"; - public BasePage() { + public BasePage() { add(new LeftNavPanel(LEFT_NAV_ID)); add(new Label(STICKY_HEADER_ID, "").setOutputMarkupId(true)); add(new HeaderPanel(HEADER_ID)); add(new Label(CONTENT_ID, "")); - add(new Label(STICKY_FOOTER_ID, "").setOutputMarkupId(true)); } } diff --git a/karate-web/src/main/java/com/intuit/karate/web/wicket/FeaturePage.java b/karate-web/src/main/java/com/intuit/karate/web/wicket/FeaturePage.java index f532ac84a..cf5b49e31 100644 --- a/karate-web/src/main/java/com/intuit/karate/web/wicket/FeaturePage.java +++ b/karate-web/src/main/java/com/intuit/karate/web/wicket/FeaturePage.java @@ -23,13 +23,23 @@ */ package com.intuit.karate.web.wicket; +import com.intuit.karate.web.config.LogAppenderTarget; import com.intuit.karate.web.config.WebSocketLogAppender; import com.intuit.karate.web.service.KarateService; import com.intuit.karate.web.service.KarateSession; +import com.jayway.jsonpath.JsonPath; +import java.util.HashMap; +import java.util.Map; +import org.apache.wicket.Application; import org.apache.wicket.markup.head.IHeaderResponse; import org.apache.wicket.markup.head.JavaScriptHeaderItem; +import org.apache.wicket.markup.html.basic.Label; +import org.apache.wicket.protocol.ws.WebSocketSettings; +import org.apache.wicket.protocol.ws.api.IWebSocketConnection; import org.apache.wicket.protocol.ws.api.WebSocketBehavior; import org.apache.wicket.protocol.ws.api.message.ConnectedMessage; +import org.apache.wicket.protocol.ws.api.registry.IKey; +import org.apache.wicket.protocol.ws.api.registry.IWebSocketConnectionRegistry; import org.apache.wicket.spring.injection.annot.SpringBean; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -38,45 +48,66 @@ * * @author pthomas3 */ -public class FeaturePage extends BasePage { +public class FeaturePage extends BasePage implements LogAppenderTarget { private static final Logger logger = LoggerFactory.getLogger(FeaturePage.class); @SpringBean(required = true) private KarateService service; - private final LogPanel logPanel; - - public LogPanel getLogPanel() { - return logPanel; - } + private String webSocketSessionId; + private IKey webSocketClientKey; public FeaturePage(String sessionId) { + replace(new Label(HEADER_ID, "")); replace(new FeaturePanel(CONTENT_ID, sessionId)); replace(new VarsPanel(LEFT_NAV_ID, sessionId)); - logPanel = new LogPanel(STICKY_FOOTER_ID, sessionId); add(new WebSocketBehavior() { @Override protected void onConnect(ConnectedMessage message) { KarateSession session = service.getSession(sessionId); + webSocketSessionId = message.getSessionId(); + webSocketClientKey = message.getKey(); WebSocketLogAppender appender = session.getAppender(); - logPanel.onConnect(message); - appender.setTarget(logPanel); + appender.setTarget(FeaturePage.this); logger.debug("websocket client connected, session: {}", message.getSessionId()); } }); } + + @Override + public void append(String text) { + Map map = new HashMap(1); + map.put("text", text); + String json = JsonPath.parse(map).jsonString(); + pushJsonWebSocketMessage(json); + } + + public void pushJsonWebSocketMessage(String json) { + Application application = Application.get(); + WebSocketSettings settings = WebSocketSettings.Holder.get(application); + IWebSocketConnectionRegistry registry = settings.getConnectionRegistry(); + IWebSocketConnection connection = registry.getConnection(application, webSocketSessionId, webSocketClientKey); + if (connection == null) { + logger.warn("websocket client lookup failed for web-socket session: {}", webSocketSessionId); + return; + } + try { + connection.sendMessage(json); + } catch (Exception e) { + logger.error("websocket push failed", e); + } + } @Override public void renderHead(IHeaderResponse response) { - super.renderHead(response); String script = "Wicket.Event.subscribe(\"/websocket/message\", function(jqEvent, message) {\n" + " message = JSON.parse(message);\n" - + " if (message.type == 'step') updateStep(message); else updateLog(message);\n" + + " if (message.type == 'step') updateStep(message); else { Karate.Ajax.DebugWindow.logInfo(message.text); }\n" + "});\n" - + "function updateStep(message){ var btn = jQuery('#' + message.buttonId); btn.addClass('btn-success'); }\n" - + logPanel.getUpdateScript(); + + "function updateStep(message){ var btn = jQuery('#' + message.buttonId); btn.addClass('btn-success'); }"; response.render(JavaScriptHeaderItem.forScript(script, "karate-ws-js")); + response.render(JavaScriptHeaderItem.forReference(KarateJsResourceReference.INSTANCE)); } } diff --git a/karate-web/src/main/java/com/intuit/karate/web/wicket/FeatureSectionPanel.html b/karate-web/src/main/java/com/intuit/karate/web/wicket/FeatureSectionPanel.html index 6364b0a54..086cc2959 100644 --- a/karate-web/src/main/java/com/intuit/karate/web/wicket/FeatureSectionPanel.html +++ b/karate-web/src/main/java/com/intuit/karate/web/wicket/FeatureSectionPanel.html @@ -2,7 +2,7 @@
-
+
diff --git a/karate-web/src/main/java/com/intuit/karate/web/wicket/FeatureSectionPanel.java b/karate-web/src/main/java/com/intuit/karate/web/wicket/FeatureSectionPanel.java index fce9469a6..9d8365486 100644 --- a/karate-web/src/main/java/com/intuit/karate/web/wicket/FeatureSectionPanel.java +++ b/karate-web/src/main/java/com/intuit/karate/web/wicket/FeatureSectionPanel.java @@ -71,7 +71,7 @@ protected void populateItem(ListItem li) { public void onClick(AjaxRequestTarget target) { KarateSession session = service.getSession(model.getSessionId()); KarateBackend backend = session.getBackend(); - LogPanel logPanel = ((FeaturePage) getPage()).getLogPanel(); + FeaturePage featurePage = (FeaturePage) getPage(); for (ListItem li : listItems) { StepWrapper step = li.getModelObject(); StepResult result = step.run(backend); @@ -79,7 +79,7 @@ public void onClick(AjaxRequestTarget target) { StepPanel stepPanel = (StepPanel) li.get("step"); String json = JsonUtils.toJsonString( "{ type: 'step', buttonId: '" + stepPanel.getRunButton().getMarkupId() + "' }"); - logPanel.pushJsonWebSocketMessage(json); + featurePage.pushJsonWebSocketMessage(json); } } }); diff --git a/karate-web/src/main/java/com/intuit/karate/web/wicket/HomePanel.java b/karate-web/src/main/java/com/intuit/karate/web/wicket/HomePanel.java index 603bd8a33..9e6493658 100644 --- a/karate-web/src/main/java/com/intuit/karate/web/wicket/HomePanel.java +++ b/karate-web/src/main/java/com/intuit/karate/web/wicket/HomePanel.java @@ -41,7 +41,7 @@ public class HomePanel extends Panel { @SpringBean(required = true) private KarateService service; - private String text; + private final String text; public HomePanel(String id) { super(id); @@ -57,6 +57,7 @@ protected void onSubmit() { form.add(new TextArea("text")); add(form); add(new FeedbackPanel("feedback")); + text = "Feature:\n\nScenario:\n"; } } \ No newline at end of file diff --git a/karate-web/src/main/java/com/intuit/karate/web/wicket/KarateJsResourceReference.java b/karate-web/src/main/java/com/intuit/karate/web/wicket/KarateJsResourceReference.java new file mode 100644 index 000000000..f1d2cafea --- /dev/null +++ b/karate-web/src/main/java/com/intuit/karate/web/wicket/KarateJsResourceReference.java @@ -0,0 +1,40 @@ +/* + * The MIT License + * + * Copyright 2017 Intuit Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.intuit.karate.web.wicket; + +import org.apache.wicket.request.resource.JavaScriptResourceReference; + +/** + * + * @author pthomas3 + */ +public class KarateJsResourceReference extends JavaScriptResourceReference { + + public static final KarateJsResourceReference INSTANCE = new KarateJsResourceReference(); + + private KarateJsResourceReference() { + super(KarateJsResourceReference.class, "karate.js"); + } + +} diff --git a/karate-web/src/main/java/com/intuit/karate/web/wicket/LogPanel.html b/karate-web/src/main/java/com/intuit/karate/web/wicket/LogPanel.html deleted file mode 100644 index df4d8e0ac..000000000 --- a/karate-web/src/main/java/com/intuit/karate/web/wicket/LogPanel.html +++ /dev/null @@ -1,16 +0,0 @@ - - - diff --git a/karate-web/src/main/java/com/intuit/karate/web/wicket/LogPanel.java b/karate-web/src/main/java/com/intuit/karate/web/wicket/LogPanel.java deleted file mode 100644 index 4a489b031..000000000 --- a/karate-web/src/main/java/com/intuit/karate/web/wicket/LogPanel.java +++ /dev/null @@ -1,146 +0,0 @@ -/* - * The MIT License - * - * Copyright 2017 Intuit Inc. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -package com.intuit.karate.web.wicket; - -import com.intuit.karate.web.config.LogAppenderTarget; -import com.intuit.karate.web.config.WebSocketLogAppender; -import com.intuit.karate.web.service.KarateService; -import com.jayway.jsonpath.JsonPath; -import java.util.HashMap; -import java.util.Map; -import org.apache.wicket.Application; -import org.apache.wicket.Component; -import org.apache.wicket.ajax.AjaxRequestTarget; -import org.apache.wicket.ajax.markup.html.AjaxLink; -import org.apache.wicket.markup.html.basic.Label; -import org.apache.wicket.markup.html.form.TextArea; -import org.apache.wicket.markup.html.panel.Panel; -import org.apache.wicket.model.AbstractReadOnlyModel; -import org.apache.wicket.model.IModel; -import org.apache.wicket.protocol.ws.WebSocketSettings; -import org.apache.wicket.protocol.ws.api.IWebSocketConnection; -import org.apache.wicket.protocol.ws.api.message.ConnectedMessage; -import org.apache.wicket.protocol.ws.api.registry.IKey; -import org.apache.wicket.protocol.ws.api.registry.IWebSocketConnectionRegistry; -import org.apache.wicket.spring.injection.annot.SpringBean; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class LogPanel extends Panel implements LogAppenderTarget { - - private static final Logger logger = LoggerFactory.getLogger(LogPanel.class); - - @SpringBean(required = true) - private KarateService service; - - private String webSocketSessionId; - private IKey webSocketClientKey; - - public void onConnect(ConnectedMessage message) { - this.webSocketSessionId = message.getSessionId(); - this.webSocketClientKey = message.getKey(); - } - - public String getUpdateScript() { - return "function updateLog(message) {\n" - + " var textArea = jQuery('#" + textArea.getMarkupId() + "');\n" - + " textArea.append(message.text);\n" - + "};"; - } - - private final TextArea textArea; - private boolean showing; - - public boolean isShowing() { - return showing; - } - - public void hide(AjaxRequestTarget target) { - showing = false; - Component hidden = replaceWith(new Label(BasePage.STICKY_FOOTER_ID, "")); - target.add(hidden); - } - - public void show(AjaxRequestTarget target) { - if (showing) { - return; - } - showing = true; - Component shown = target.getPage().get(BasePage.STICKY_FOOTER_ID); - shown = shown.replaceWith(this); - target.add(shown); - } - - public void pushJsonWebSocketMessage(String json) { - Application application = Application.get(); - WebSocketSettings settings = WebSocketSettings.Holder.get(application); - IWebSocketConnectionRegistry registry = settings.getConnectionRegistry(); - IWebSocketConnection connection = registry.getConnection(application, webSocketSessionId, webSocketClientKey); - if (connection == null) { - logger.warn("websocket client lookup failed for web-socket session: {}", webSocketSessionId); - return; - } - try { - connection.sendMessage(json); - } catch (Exception e) { - logger.error("websocket push failed", e); - } - } - - @Override - public void append(String text) { - Map map = new HashMap(1); - map.put("text", text); - String json = JsonPath.parse(map).jsonString(); - pushJsonWebSocketMessage(json); - } - - public LogPanel(String id, String sessionId) { - super(id); - setOutputMarkupId(true); - add(new AjaxLink("close") { - @Override - public void onClick(AjaxRequestTarget target) { - hide(target); - } - }); - add(new AjaxLink("clear") { - @Override - public void onClick(AjaxRequestTarget target) { - service.getSession(sessionId).getAppender().clearBuffer(); - target.add(LogPanel.this); - } - }); - IModel model = new AbstractReadOnlyModel() { - @Override - public String getObject() { - return service.getSession(sessionId).getAppender().getBuffer(); - } - }; - textArea = new TextArea("text", model); - textArea.setOutputMarkupId(true); // for js get by id - add(textArea); - } - -} diff --git a/karate-web/src/main/java/com/intuit/karate/web/wicket/StepPanel.java b/karate-web/src/main/java/com/intuit/karate/web/wicket/StepPanel.java index 80a08dc29..c547acb93 100644 --- a/karate-web/src/main/java/com/intuit/karate/web/wicket/StepPanel.java +++ b/karate-web/src/main/java/com/intuit/karate/web/wicket/StepPanel.java @@ -165,12 +165,6 @@ public void onClick(AjaxRequestTarget target) { KarateBackend backend = session.getBackend(); StepResult result = step.run(backend); step.setPassed(result.isPass()); - LogPanel logPanel = ((FeaturePage) getPage()).getLogPanel(); - if (!step.isPassed()) { - if (!logPanel.isShowing()) { - logPanel.show(target); - } - } target.add(this); } }; diff --git a/karate-web/src/main/java/com/intuit/karate/web/wicket/VarPanel.html b/karate-web/src/main/java/com/intuit/karate/web/wicket/VarPanel.html index a1dc1bd20..91acdeecf 100644 --- a/karate-web/src/main/java/com/intuit/karate/web/wicket/VarPanel.html +++ b/karate-web/src/main/java/com/intuit/karate/web/wicket/VarPanel.html @@ -1,5 +1,5 @@ -