Skip to content

Commit

Permalink
fix the following commands not working propperly. (#327)
Browse files Browse the repository at this point in the history
* `answer on next prompt`
* `choose cancel on next prompt`
* `choose ok on next confirmation`
* `choose cancel on next confirmation`
  • Loading branch information
vmi committed Jul 4, 2021
1 parent a63be03 commit ca8511e
Show file tree
Hide file tree
Showing 9 changed files with 251 additions and 58 deletions.
8 changes: 8 additions & 0 deletions RELEASENOTE.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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.*;
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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.*;
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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.*;
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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.*;
Expand All @@ -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;
}
}
111 changes: 57 additions & 54 deletions src/main/resources/jp/vmi/selenium/selenese/javascript/JSLibrary.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
}
19 changes: 19 additions & 0 deletions src/test/resources/htdocs/alerts.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Alerts</title>
<script>
</script>
</head>
<body>
<h1>Alerts</h1>
<hr>
<ul>
<li><button id="alert" onclick="alert('alert clicked')">alert clicked</button></li>
<li><button id="confirm" onclick="confirm('confirm clicked')">confirm clicked</button></li>
<li><button id="prompt" onclick="document.getElementById('result').value = prompt('prompt clicked')">prompt clicked</button></li>
</ul>
<input id="result" type="hidden">
</body>
</html>
133 changes: 133 additions & 0 deletions src/test/resources/selenese/testcase_alerts.side
Original file line number Diff line number Diff line change
@@ -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": []
}

0 comments on commit ca8511e

Please sign in to comment.