From c108fc7359fafb5d846200d4470e49bfec63d30f Mon Sep 17 00:00:00 2001 From: vladimir efimov Date: Tue, 9 Jun 2020 12:40:24 +0300 Subject: [PATCH 1/3] add tests for java kernel --- autotests/README.md | 1 + autotests/ipynb/java/JavaTest.ipynb | 285 ++++++++++++++++++ .../beakerx/autotests/groovy/GroovyTest.java | 1 - .../beakerx/autotests/java/JavaTest.java | 53 ++++ 4 files changed, 339 insertions(+), 1 deletion(-) create mode 100644 autotests/ipynb/java/JavaTest.ipynb create mode 100644 autotests/src/test/java/com/twosigma/beakerx/autotests/java/JavaTest.java diff --git a/autotests/README.md b/autotests/README.md index dd18cf5..1488b53 100644 --- a/autotests/README.md +++ b/autotests/README.md @@ -20,6 +20,7 @@ Running the e2e tests from its GitHub source code requires: * chrome browser (80 or higher) * [conda](https://www.anaconda.com/download/) * java (1.8 or higher) and Gradle (5.2 or higher) +* [psutil](https://pypi.org/project/psutil/) * any beakerx kernel ## Run the tests diff --git a/autotests/ipynb/java/JavaTest.ipynb b/autotests/ipynb/java/JavaTest.ipynb new file mode 100644 index 0000000..c34a801 --- /dev/null +++ b/autotests/ipynb/java/JavaTest.ipynb @@ -0,0 +1,285 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import java.util.Date;\n", + "import java.text.SimpleDateFormat;\n", + "\n", + "public class BeakerxTest {\n", + " private Date _date;\n", + " private SimpleDateFormat sdf = new SimpleDateFormat(\"yyyy-MM-dd'T'HH:mmZ\");\n", + " \n", + " public BeakerxTest() {\n", + " _date = new Date();\n", + " }\n", + "\n", + " public String getDateTxt() {\n", + " return \"Today:\" + sdf.format(_date);\n", + " }\n", + "\n", + " public String getDateUpperCaseTxt() {\n", + " return getDateTxt().toUpperCase();\n", + " }\n", + "}\n", + "BeakerxTest.class.getSimpleName()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "BeakerxTest bt = new BeakerxTest();\n", + "bt.getDateUpperCaseTxt();" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "scrolled": true + }, + "outputs": [], + "source": [ + "import java.util.List;\n", + "import java.util.ArrayList;\n", + "import beakerx.chart.xychart.Plot;\n", + "import beakerx.chart.xychart.plotitem.*;\n", + "import beakerx.chart.Color;\n", + "\n", + "Plot p = new Plot();\n", + "\n", + "p.setTitle(\"this is a Java plot\");\n", + "\n", + "Bars b = new Bars();\n", + "\n", + "List yList = new ArrayList();\n", + "yList.add(2);\n", + "yList.add(5);\n", + "yList.add(4);\n", + "yList.add(8);\n", + "\n", + "b.setY(yList);\n", + "b.setColor(Color.blue);\n", + "b.setWidth(0.5);\n", + "\n", + "p.add(b);\n", + " \n", + "p;" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "interface DateGetter {\n", + " public String getDateTxt();\n", + "}\n", + "DateGetter.class.getSimpleName()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "public class DG2 extends BeakerTest implements DateGetter {\n", + "}\n", + "DG2.class.getSimpleName()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%classpath add jar ../../../doc/resources/jar/BeakerXClasspathTest.jar" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "System.out.println(\"The Groovy working folder is :\");\n", + "System.out.println(java.nio.file.Paths.get(\".\").toAbsolutePath().normalize().toString());\n", + "System.out.println(\"BeakerXClasspathTest.jar exists in this folder\");" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import com.beaker.BeakerXClasspathTest;\n", + "\n", + "BeakerXClasspathTest t = new BeakerXClasspathTest();\n", + "System.out.println(com.beaker.BeakerXClasspathTest.staticTest);\n", + "System.out.println(t.getObjectTest());" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%%python\n", + "from beakerx import *\n", + "pp = Plot(title=\"Title\", xLabel=\"Horizontal\", yLabel=\"Vertical\")\n", + "pp.add(Line(displayName=\"Line\", x=[1, 4, 6, 8, 10],y=[3, 6, 4, 5, 9]))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "press 'Tab' for autocomplete" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "press 'Shift' + 'Tab' for doc" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Output usage" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import beakerx.widget.Output;\n", + "Output out2 = new Output();\n", + "OutputManager.setOutput(out2);\n", + "out2;" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import static java.lang.System.out;\n", + "\n", + "Thread t = new Thread(() -> {\n", + " out.println(\"Some output init\");\n", + " try {\n", + " Thread.sleep(2000);\n", + " } catch (InterruptedException e) {\n", + " }\n", + " for (int i = 0; i <10 ; i++){\n", + " out.println(\"Some output \"+ i); \n", + " }\n", + "});\n", + "out.println(\"Start\");\n", + "t.start();\n", + "//t.join();\n", + "out.println(\"end\");\n", + "\"Thread example result\";" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "OutputManager.clearOutput();" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import static java.lang.System.out;\n", + "for (int i = 0; i <30 ; i++){\n", + " out.println(\"Done \"+ i); \n", + "}\n", + "return 3+5;" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "OutputManager.setOutput(null);" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Java11", + "language": "java", + "name": "java" + }, + "language_info": { + "codemirror_mode": "text/x-java", + "file_extension": ".java", + "mimetype": "", + "name": "Java", + "nbconverter_exporter": "", + "version": "11.0.1" + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": false, + "sideBar": false, + "skip_h1_title": false, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": {}, + "toc_section_display": false, + "toc_window_display": false + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/autotests/src/test/java/com/twosigma/beakerx/autotests/groovy/GroovyTest.java b/autotests/src/test/java/com/twosigma/beakerx/autotests/groovy/GroovyTest.java index 0b68e9b..2132c72 100644 --- a/autotests/src/test/java/com/twosigma/beakerx/autotests/groovy/GroovyTest.java +++ b/autotests/src/test/java/com/twosigma/beakerx/autotests/groovy/GroovyTest.java @@ -29,7 +29,6 @@ public class GroovyTest extends BaseTest { @BeforeClass public static void setupClass() { BaseTest.setupClass(); - System.out.println("groovy_________test"); beakerxPO.runNotebookByUrl("/autotests/ipynb/groovy/GroovyTest.ipynb"); } diff --git a/autotests/src/test/java/com/twosigma/beakerx/autotests/java/JavaTest.java b/autotests/src/test/java/com/twosigma/beakerx/autotests/java/JavaTest.java new file mode 100644 index 0000000..b98d4aa --- /dev/null +++ b/autotests/src/test/java/com/twosigma/beakerx/autotests/java/JavaTest.java @@ -0,0 +1,53 @@ +/* + * Copyright 2020 TWO SIGMA OPEN SOURCE, LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.twosigma.beakerx.autotests.java; + +import com.twosigma.beakerx.autotests.BaseTest; +import org.openqa.selenium.WebElement; +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +public class JavaTest extends BaseTest { + + int cellIndex = 0; + + @BeforeClass + public static void setupClass() { + BaseTest.setupClass(); + beakerxPO.runNotebookByUrl("/autotests/ipynb/java/JavaTest.ipynb"); + } + + @Test(priority = 1, description = "Execute Results output contains 'BeakerxTest'.") + public void defineJavaClass() { + cellIndex = 0; + WebElement codeCell = beakerxPO.runCodeCellByIndex(cellIndex); + String txt = beakerxPO.getAllOutputsOfCodeCell(codeCell, beakerxPO.getAllOutputsExecuteResultsSelector()) + .get(0).getText(); + Assert.assertEquals(txt, "BeakerxTest"); + } + + @Test(priority = 5, description = "Execute Results output contains 'TODAY:'.") + public void callJavaCla() { + cellIndex++; + WebElement codeCell = beakerxPO.runCodeCellByIndex(cellIndex); + String txt = beakerxPO.getAllOutputsOfCodeCell(codeCell, beakerxPO.getAllOutputsExecuteResultsSelector()) + .get(0).getText(); + Assert.assertTrue(txt.contains("TODAY:")); + } + +} From 1bbb1e1275054030782cf4906131c1d9f343dc3b Mon Sep 17 00:00:00 2001 From: vladimir efimov Date: Tue, 9 Jun 2020 14:32:29 +0300 Subject: [PATCH 2/3] add tests for scala kernel --- .../test/java/com/twosigma/beakerx/autotests/BasePageObject.java | 1 + 1 file changed, 1 insertion(+) diff --git a/autotests/src/test/java/com/twosigma/beakerx/autotests/BasePageObject.java b/autotests/src/test/java/com/twosigma/beakerx/autotests/BasePageObject.java index 216ead9..0381241 100644 --- a/autotests/src/test/java/com/twosigma/beakerx/autotests/BasePageObject.java +++ b/autotests/src/test/java/com/twosigma/beakerx/autotests/BasePageObject.java @@ -21,6 +21,7 @@ import org.openqa.selenium.WebElement; import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.support.ui.FluentWait; +import org.openqa.selenium.support.ui.WebDriverWait; import ru.yandex.qatools.ashot.AShot; import ru.yandex.qatools.ashot.Screenshot; import ru.yandex.qatools.ashot.comparison.ImageDiff; From 7c421462b823f2e0083ce97bdeb759e9efa2b486 Mon Sep 17 00:00:00 2001 From: vladimir efimov Date: Tue, 9 Jun 2020 14:34:36 +0300 Subject: [PATCH 3/3] add tests for java, scala kernels --- autotests/ipynb/scala/ScalaTest.ipynb | 90 +++++++++++++++++++ autotests/run_tests.py | 2 +- .../beakerx/autotests/NotebookPO.java | 7 +- .../beakerx/autotests/java/JavaTest.java | 2 +- .../beakerx/autotests/scala/ScalaTest.java | 52 +++++++++++ 5 files changed, 147 insertions(+), 6 deletions(-) create mode 100644 autotests/ipynb/scala/ScalaTest.ipynb create mode 100644 autotests/src/test/java/com/twosigma/beakerx/autotests/scala/ScalaTest.java diff --git a/autotests/ipynb/scala/ScalaTest.ipynb b/autotests/ipynb/scala/ScalaTest.ipynb new file mode 100644 index 0000000..8e6d41b --- /dev/null +++ b/autotests/ipynb/scala/ScalaTest.ipynb @@ -0,0 +1,90 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "class Greeter(prefix: String, suffix: String) {\n", + " def greet(name: String): Unit =\n", + " println(prefix + name + suffix)\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "val greeter = new Greeter(\"Hello, \", \"!\")\n", + "greeter.greet(\"Scala developer\") // Hello, Scala developer!" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%%python\n", + "from beakerx import *\n", + "pp = Plot(title=\"Title\", xLabel=\"Horizontal\", yLabel=\"Vertical\")\n", + "pp.add(Line(displayName=\"Line\", x=[1, 4, 6, 8, 10],y=[3, 6, 4, 5, 9]))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "press 'Tab' for autocomplete" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "press 'Shift' + 'Tab' for doc" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Scala", + "language": "scala", + "name": "scala" + }, + "language_info": { + "codemirror_mode": "text/x-scala", + "file_extension": ".scala", + "mimetype": "", + "name": "Scala", + "nbconverter_exporter": "", + "version": "2.12.8" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/autotests/run_tests.py b/autotests/run_tests.py index f209202..f63176e 100644 --- a/autotests/run_tests.py +++ b/autotests/run_tests.py @@ -77,7 +77,7 @@ def kill_processes(name): # kill unused processes if platform.system() == 'Windows': for proc in psutil.process_iter(): - if proc.name() in ["jupyter-lab.exe", "jupyter.exe", "python.exe", "chromedriver.exe"]: + if proc.name() in ["jupyter-lab.exe", "jupyter.exe", "jupyter-notebook.exe", "chromedriver.exe"]: print(proc) os.kill(proc.pid, signal.SIGTERM) else: diff --git a/autotests/src/test/java/com/twosigma/beakerx/autotests/NotebookPO.java b/autotests/src/test/java/com/twosigma/beakerx/autotests/NotebookPO.java index de45551..f4d49ff 100644 --- a/autotests/src/test/java/com/twosigma/beakerx/autotests/NotebookPO.java +++ b/autotests/src/test/java/com/twosigma/beakerx/autotests/NotebookPO.java @@ -24,6 +24,8 @@ public class NotebookPO extends BasePageObject { + WebDriverWait wait = new WebDriverWait(webDriver, 20); + protected NotebookPO(WebDriver webDriver) { super(webDriver); } @@ -31,6 +33,7 @@ protected NotebookPO(WebDriver webDriver) { @Override public void runNotebookByUrl(String url) { webDriver.get("http://127.0.0.1:8888/notebooks" + url); + wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@id='notification_kernel']//span[text()='Kernel ready']"))); waitKernelIdleIcon(10); } @@ -45,7 +48,6 @@ public WebElement runCodeCellByIndex(int index) { private void clickRunCell(){ By runButtonSelector = By.cssSelector("button[data-jupyter-action=\"jupyter-notebook:run-cell-and-select-next\"]"); - WebDriverWait wait = new WebDriverWait(webDriver, 10); wait.until(ExpectedConditions.visibilityOfElementLocated(runButtonSelector)); action.click(webDriver.findElement(runButtonSelector)).perform(); @@ -59,7 +61,6 @@ public WebElement getCodeCellByIndex(int index) { @Override public void waitKernelIdleIcon(int timeOutInSeconds) { - WebDriverWait wait = new WebDriverWait(webDriver, timeOutInSeconds); wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("i.kernel_idle_icon"))); } @@ -68,7 +69,6 @@ public void closeAndHaltNotebook() { this.clickCellAllOutputClear(); webDriver.findElement(By.partialLinkText("File")).click(); - WebDriverWait wait = new WebDriverWait(webDriver, 3); wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("li#close_and_halt"))); action.moveToElement(webDriver.findElement(By.cssSelector("li#close_and_halt"))).click().perform(); } @@ -76,7 +76,6 @@ public void closeAndHaltNotebook() { private void clickCellAllOutputClear(){ webDriver.findElement(By.partialLinkText("Cell")).click(); - WebDriverWait wait = new WebDriverWait(webDriver, 3); wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("li#all_outputs"))); action.moveToElement(webDriver.findElement(By.cssSelector("li#all_outputs"))).perform(); diff --git a/autotests/src/test/java/com/twosigma/beakerx/autotests/java/JavaTest.java b/autotests/src/test/java/com/twosigma/beakerx/autotests/java/JavaTest.java index b98d4aa..d508418 100644 --- a/autotests/src/test/java/com/twosigma/beakerx/autotests/java/JavaTest.java +++ b/autotests/src/test/java/com/twosigma/beakerx/autotests/java/JavaTest.java @@ -42,7 +42,7 @@ public void defineJavaClass() { } @Test(priority = 5, description = "Execute Results output contains 'TODAY:'.") - public void callJavaCla() { + public void callJavaClass() { cellIndex++; WebElement codeCell = beakerxPO.runCodeCellByIndex(cellIndex); String txt = beakerxPO.getAllOutputsOfCodeCell(codeCell, beakerxPO.getAllOutputsExecuteResultsSelector()) diff --git a/autotests/src/test/java/com/twosigma/beakerx/autotests/scala/ScalaTest.java b/autotests/src/test/java/com/twosigma/beakerx/autotests/scala/ScalaTest.java new file mode 100644 index 0000000..3f14591 --- /dev/null +++ b/autotests/src/test/java/com/twosigma/beakerx/autotests/scala/ScalaTest.java @@ -0,0 +1,52 @@ +/* + * Copyright 2020 TWO SIGMA OPEN SOURCE, LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.twosigma.beakerx.autotests.scala; + +import com.twosigma.beakerx.autotests.BaseTest; +import org.openqa.selenium.WebElement; +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +public class ScalaTest extends BaseTest { + + int cellIndex = 0; + + @BeforeClass + public static void setupClass() { + BaseTest.setupClass(); + beakerxPO.runNotebookByUrl("/autotests/ipynb/scala/ScalaTest.ipynb"); + } + + @Test(priority = 1, description = "Execute Results output contains 'class Greeter'.") + public void defineScalaClass() { + cellIndex = 0; + WebElement codeCell = beakerxPO.runCodeCellByIndex(cellIndex); + String txt = beakerxPO.getAllOutputsOfCodeCell(codeCell, beakerxPO.getAllOutputsExecuteResultsSelector()) + .get(0).getText(); + Assert.assertEquals(txt, "defined class Greeter"); + } + + @Test(priority = 5, description = "Stdout output contains 'Scala developer'.") + public void callScalaClass() { + cellIndex++; + WebElement codeCell = beakerxPO.runCodeCellByIndex(cellIndex); + String txt = beakerxPO.getAllOutputsOfCodeCell(codeCell, beakerxPO.getAllOutputsStdoutSelector()) + .get(0).getText(); + Assert.assertEquals(txt, "Hello, Scala developer!"); + } +}