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 @@ + + +
+ +