diff --git a/RELEASENOTE.md b/RELEASENOTE.md index ee48945ba..a3477b3e0 100644 --- a/RELEASENOTE.md +++ b/RELEASENOTE.md @@ -1,6 +1,14 @@ Selenese Runner Java Relase Note ================================ +### 3.34.0 + +* Fix the following commands not working propperly. (#327) + * `answer on next prompt` + * `choose cancel on next prompt` + * `choose ok on next confirmation` + * `choose cancel on next confirmation` + ### 3.33.0 * Fix not to replace undefined variables. (#324) diff --git a/src/main/java/jp/vmi/selenium/selenese/command/AnswerOnNextPrompt.java b/src/main/java/jp/vmi/selenium/selenese/command/AnswerOnNextPrompt.java index 44cb7a6f2..e49a299cd 100644 --- a/src/main/java/jp/vmi/selenium/selenese/command/AnswerOnNextPrompt.java +++ b/src/main/java/jp/vmi/selenium/selenese/command/AnswerOnNextPrompt.java @@ -1,6 +1,9 @@ package jp.vmi.selenium.selenese.command; +import org.openqa.selenium.WebDriver; + import jp.vmi.selenium.selenese.Context; +import jp.vmi.selenium.selenese.javascript.JSLibrary; import jp.vmi.selenium.selenese.result.Result; import static jp.vmi.selenium.selenese.command.ArgumentType.*; @@ -24,7 +27,10 @@ public boolean mayUpdateScreen() { @Override protected Result executeImpl(Context context, String... curArgs) { - context.getJSLibrary().answerOnNextPrompt(context.getWrappedDriver(), curArgs[ARG_ANSWER]); + WebDriver driver = context.getWrappedDriver(); + JSLibrary jsLibrary = context.getJSLibrary(); + jsLibrary.replaceAlertMethod(driver, null); + jsLibrary.answerOnNextPrompt(driver, curArgs[ARG_ANSWER]); return SUCCESS; } } diff --git a/src/main/java/jp/vmi/selenium/selenese/command/ChooseCancelOnNextConfirmation.java b/src/main/java/jp/vmi/selenium/selenese/command/ChooseCancelOnNextConfirmation.java index 741ed5f52..116648f46 100644 --- a/src/main/java/jp/vmi/selenium/selenese/command/ChooseCancelOnNextConfirmation.java +++ b/src/main/java/jp/vmi/selenium/selenese/command/ChooseCancelOnNextConfirmation.java @@ -1,6 +1,9 @@ package jp.vmi.selenium.selenese.command; +import org.openqa.selenium.WebDriver; + import jp.vmi.selenium.selenese.Context; +import jp.vmi.selenium.selenese.javascript.JSLibrary; import jp.vmi.selenium.selenese.result.Result; import static jp.vmi.selenium.selenese.result.Success.*; @@ -16,7 +19,10 @@ public class ChooseCancelOnNextConfirmation extends AbstractCommand { @Override protected Result executeImpl(Context context, String... curArgs) { - context.getJSLibrary().setNextConfirmationState(context.getWrappedDriver(), false); + WebDriver driver = context.getWrappedDriver(); + JSLibrary jsLibrary = context.getJSLibrary(); + jsLibrary.replaceAlertMethod(driver, null); + jsLibrary.setNextConfirmationState(driver, false); return SUCCESS; } } diff --git a/src/main/java/jp/vmi/selenium/selenese/command/ChooseCancelOnNextPrompt.java b/src/main/java/jp/vmi/selenium/selenese/command/ChooseCancelOnNextPrompt.java index fe1537abc..3a3292dac 100644 --- a/src/main/java/jp/vmi/selenium/selenese/command/ChooseCancelOnNextPrompt.java +++ b/src/main/java/jp/vmi/selenium/selenese/command/ChooseCancelOnNextPrompt.java @@ -1,6 +1,9 @@ package jp.vmi.selenium.selenese.command; +import org.openqa.selenium.WebDriver; + import jp.vmi.selenium.selenese.Context; +import jp.vmi.selenium.selenese.javascript.JSLibrary; import jp.vmi.selenium.selenese.result.Result; import static jp.vmi.selenium.selenese.result.Success.*; @@ -21,7 +24,10 @@ public boolean mayUpdateScreen() { @Override protected Result executeImpl(Context context, String... curArgs) { - context.getJSLibrary().answerOnNextPrompt(context.getWrappedDriver(), null); + WebDriver driver = context.getWrappedDriver(); + JSLibrary jsLibrary = context.getJSLibrary(); + jsLibrary.replaceAlertMethod(driver, null); + jsLibrary.answerOnNextPrompt(driver, null); return SUCCESS; } } diff --git a/src/main/java/jp/vmi/selenium/selenese/command/ChooseOkOnNextConfirmation.java b/src/main/java/jp/vmi/selenium/selenese/command/ChooseOkOnNextConfirmation.java index 446fefe18..ae6de208b 100644 --- a/src/main/java/jp/vmi/selenium/selenese/command/ChooseOkOnNextConfirmation.java +++ b/src/main/java/jp/vmi/selenium/selenese/command/ChooseOkOnNextConfirmation.java @@ -1,6 +1,9 @@ package jp.vmi.selenium.selenese.command; +import org.openqa.selenium.WebDriver; + import jp.vmi.selenium.selenese.Context; +import jp.vmi.selenium.selenese.javascript.JSLibrary; import jp.vmi.selenium.selenese.result.Result; import static jp.vmi.selenium.selenese.result.Success.*; @@ -16,7 +19,10 @@ public class ChooseOkOnNextConfirmation extends AbstractCommand { @Override protected Result executeImpl(Context context, String... curArgs) { - context.getJSLibrary().setNextConfirmationState(context.getWrappedDriver(), true); + WebDriver driver = context.getWrappedDriver(); + JSLibrary jsLibrary = context.getJSLibrary(); + jsLibrary.replaceAlertMethod(driver, null); + jsLibrary.setNextConfirmationState(driver, true); return SUCCESS; } } diff --git a/src/main/resources/jp/vmi/selenium/selenese/javascript/JSLibrary.js b/src/main/resources/jp/vmi/selenium/selenese/javascript/JSLibrary.js index c508a1994..abc053567 100644 --- a/src/main/resources/jp/vmi/selenium/selenese/javascript/JSLibrary.js +++ b/src/main/resources/jp/vmi/selenium/selenese/javascript/JSLibrary.js @@ -27,60 +27,63 @@ // function replaceAlertMethod(element) { - var canUseLocalStorage = false; - try { canUseLocalStorage = !!window.localStorage; } catch(ex) { /* probe failed */ } - var canUseJSON = false; - try { canUseJSON = !!JSON; } catch(ex) { /* probe failed */ } - if (canUseLocalStorage && canUseJSON) { - window.localStorage.setItem('__webdriverAlerts', JSON.stringify([])); - window.alert = function(msg) { - var alerts = JSON.parse(window.localStorage.getItem('__webdriverAlerts')); - alerts.push(msg); - window.localStorage.setItem('__webdriverAlerts', JSON.stringify(alerts)); - }; - window.localStorage.setItem('__webdriverConfirms', JSON.stringify([])); - if (!('__webdriverNextConfirm' in window.localStorage)) - window.localStorage.setItem('__webdriverNextConfirm', JSON.stringify(true)); - window.confirm = function(msg) { - var confirms = JSON.parse(window.localStorage.getItem('__webdriverConfirms')); - confirms.push(msg); - window.localStorage.setItem('__webdriverConfirms', JSON.stringify(confirms)); - var res = JSON.parse(window.localStorage.getItem('__webdriverNextConfirm')); - window.localStorage.setItem('__webdriverNextConfirm', JSON.stringify(true)); - return res; - }; - window.localStorage.setItem('__webdriverPrompts', JSON.stringify([])); - if (!('__webdriverNextPrompt' in window.localStorage)) - window.localStorage.setItem('__webdriverNextPrompt', JSON.stringify("")); - window.prompt = function(msg) { - var prompts = JSON.parse(window.localStorage.getItem('__webdriverPrompts')); - prompts.push(msg); - window.localStorage.setItem('__webdriverPrompts', JSON.stringify(prompts)); - var res = JSON.parse(window.localStorage.getItem('__webdriverNextPrompt')); - window.localStorage.setItem('__webdriverNextPrompt', JSON.stringify("")); - return res; - }; - } else { - window.__webdriverAlerts = []; - window.alert = function(msg) { window.__webdriverAlerts.push(msg); }; - window.__webdriverConfirms = []; - if (typeof window.__webdriverNextConfirm === 'undefined') - window.__webdriverNextConfirm = true; - window.confirm = function(msg) { - window.__webdriverConfirms.push(msg); - var res = window.__webdriverNextConfirm; - window.__webdriverNextConfirm = true; - return res; - }; - window.__webdriverPrompts = []; - if (typeof window.__webdriverNextPrompt === 'undefined') - window.__webdriverNextPrompt = true; - window.prompt = function(msg, def) { - window.__webdriverPrompts.push(msg || def); - var res = window.__webdriverNextPrompt; - window.__webdriverNextPrompt = true; - return res; - }; + if (!window.__isReplacedAlertMethod) { + window.__isReplacedAlertMethod = true; + var canUseLocalStorage = false; + try { canUseLocalStorage = !!window.localStorage; } catch(ex) { /* probe failed */ } + var canUseJSON = false; + try { canUseJSON = !!JSON; } catch(ex) { /* probe failed */ } + if (canUseLocalStorage && canUseJSON) { + window.localStorage.setItem('__webdriverAlerts', JSON.stringify([])); + window.alert = function(msg) { + var alerts = JSON.parse(window.localStorage.getItem('__webdriverAlerts')); + alerts.push(msg); + window.localStorage.setItem('__webdriverAlerts', JSON.stringify(alerts)); + }; + window.localStorage.setItem('__webdriverConfirms', JSON.stringify([])); + if (!('__webdriverNextConfirm' in window.localStorage)) + window.localStorage.setItem('__webdriverNextConfirm', JSON.stringify(true)); + window.confirm = function(msg) { + var confirms = JSON.parse(window.localStorage.getItem('__webdriverConfirms')); + confirms.push(msg); + window.localStorage.setItem('__webdriverConfirms', JSON.stringify(confirms)); + var res = JSON.parse(window.localStorage.getItem('__webdriverNextConfirm')); + window.localStorage.setItem('__webdriverNextConfirm', JSON.stringify(true)); + return res; + }; + window.localStorage.setItem('__webdriverPrompts', JSON.stringify([])); + if (!('__webdriverNextPrompt' in window.localStorage)) + window.localStorage.setItem('__webdriverNextPrompt', JSON.stringify("")); + window.prompt = function(msg) { + var prompts = JSON.parse(window.localStorage.getItem('__webdriverPrompts')); + prompts.push(msg); + window.localStorage.setItem('__webdriverPrompts', JSON.stringify(prompts)); + var res = JSON.parse(window.localStorage.getItem('__webdriverNextPrompt')); + window.localStorage.setItem('__webdriverNextPrompt', JSON.stringify("")); + return res; + }; + } else { + window.__webdriverAlerts = []; + window.alert = function(msg) { window.__webdriverAlerts.push(msg); }; + window.__webdriverConfirms = []; + if (typeof window.__webdriverNextConfirm === 'undefined') + window.__webdriverNextConfirm = true; + window.confirm = function(msg) { + window.__webdriverConfirms.push(msg); + var res = window.__webdriverNextConfirm; + window.__webdriverNextConfirm = true; + return res; + }; + window.__webdriverPrompts = []; + if (typeof window.__webdriverNextPrompt === 'undefined') + window.__webdriverNextPrompt = true; + window.prompt = function(msg, def) { + window.__webdriverPrompts.push(msg || def); + var res = window.__webdriverNextPrompt; + window.__webdriverNextPrompt = true; + return res; + }; + } } var fw; if (element && (fw = element.ownerDocument.defaultView) && fw != window) { diff --git a/src/test/java/jp/vmi/selenium/selenese/DriverDependentTest.java b/src/test/java/jp/vmi/selenium/selenese/DriverDependentTest.java index fcc3a6519..c0b7e9d6c 100644 --- a/src/test/java/jp/vmi/selenium/selenese/DriverDependentTest.java +++ b/src/test/java/jp/vmi/selenium/selenese/DriverDependentTest.java @@ -512,4 +512,10 @@ public void issue324() { assertThat(result, is(instanceOf(Success.class))); } + @Test + public void alerts() { + assumeNot(HTMLUNIT); + execute("testcase_alerts.side"); + assertThat(result, is(instanceOf(Success.class))); + } } diff --git a/src/test/resources/htdocs/alerts.html b/src/test/resources/htdocs/alerts.html new file mode 100644 index 000000000..ca6e2fbd2 --- /dev/null +++ b/src/test/resources/htdocs/alerts.html @@ -0,0 +1,19 @@ + + + + +Alerts + + + +

Alerts

+
+ + + + diff --git a/src/test/resources/selenese/testcase_alerts.side b/src/test/resources/selenese/testcase_alerts.side new file mode 100644 index 000000000..f4211a147 --- /dev/null +++ b/src/test/resources/selenese/testcase_alerts.side @@ -0,0 +1,133 @@ +{ + "id": "abda1cd0-efe0-4319-a401-933d5bd358d8", + "version": "2.0", + "name": "alerts", + "url": "http://localhost/", + "tests": [{ + "id": "99bf0adf-7311-44a0-91f6-19e56d0d2770", + "name": "alerts", + "commands": [{ + "id": "4cc50d7d-0f41-43ee-aeaf-7bee50473acf", + "comment": "", + "command": "open", + "target": "about:blank", + "targets": [], + "value": "" + }, { + "id": "2c14fec8-807d-4ad2-bd05-b7471fcd654b", + "comment": "", + "command": "answerOnNextPrompt", + "target": "Good morning world", + "targets": [], + "value": "" + }, { + "id": "67f8c24d-09d4-4850-8c1a-d4d3c88f4a71", + "comment": "", + "command": "executeScript", + "target": "return prompt(\"run prompt without command\")", + "targets": [], + "value": "result01" + }, { + "id": "bf332e2a-7ddb-4efb-bd38-c6b6abecc378", + "comment": "", + "command": "echo", + "target": "result01=[${result01}]", + "targets": [], + "value": "" + }, { + "id": "62864c29-c611-4697-8a26-ba607f370cfc", + "comment": "", + "command": "assert", + "target": "result01", + "targets": [], + "value": "Good morning world" + }, { + "id": "3a1efe5f-1baa-4c89-8af9-e9fd20c75499", + "comment": "", + "command": "open", + "target": "/alerts.html", + "targets": [], + "value": "" + }, { + "id": "d79858d7-8d95-489d-9f7f-a5a8a3a8e3d3", + "comment": "", + "command": "click", + "target": "id=alert", + "targets": [], + "value": "" + }, { + "id": "d1f887c1-82f9-4b27-8614-83fdc910f5cb", + "comment": "", + "command": "assertAlert", + "target": "alert clicked", + "targets": [], + "value": "" + }, { + "id": "31c95953-113e-47fb-8ef7-62ffa1ddfe20", + "comment": "", + "command": "chooseOkOnNextConfirmation", + "target": "", + "targets": [], + "value": "" + }, { + "id": "ae91c2f3-e425-4e79-82e5-973f9579f0eb", + "comment": "", + "command": "click", + "target": "id=confirm", + "targets": [], + "value": "" + }, { + "id": "3078d010-3ca4-4345-944a-8b454d09aa9c", + "comment": "", + "command": "assertConfirmation", + "target": "confirm clicked", + "targets": [], + "value": "" + }, { + "id": "64f9fac8-7af4-4138-a6ab-e71a92770872", + "comment": "", + "command": "answerOnNextPrompt", + "target": "Hello world", + "targets": [], + "value": "" + }, { + "id": "efa30520-c9de-48f4-a940-846f81be98c0", + "comment": "", + "command": "click", + "target": "id=prompt", + "targets": [], + "value": "" + }, { + "id": "4beae019-6714-4cc1-80d1-fd18559d0431", + "comment": "", + "command": "executeScript", + "target": "return document.getElementById(\"result\").value", + "targets": [], + "value": "result02" + }, { + "id": "7dcfe0d8-31b5-4fa4-95ad-53854395bdd3", + "comment": "", + "command": "echo", + "target": "result02=[${result02}]", + "targets": [], + "value": "" + }, { + "id": "19de9927-6e83-4e9b-bdfa-2aa51ea7d054", + "comment": "", + "command": "assert", + "target": "result02", + "targets": [], + "value": "Hello world" + }] + }], + "suites": [{ + "id": "1dc0acf8-e24f-434f-a60c-dd5198f5b37a", + "name": "alerts", + "persistSession": false, + "parallel": false, + "timeout": 300, + "tests": ["99bf0adf-7311-44a0-91f6-19e56d0d2770"] + }], + "urls": ["https://google.com/", "http://localhost/"], + "plugins": [] +} \ No newline at end of file