Skip to content

Commit 7b5f77b

Browse files
committed
clean up deprecated features in Selenium.
* `CapabilityType.TAKES_SCREENSHOT`: remove its use. * `CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR`: replace with equivalent code. * `DesiredCapabilities.setJavascriptEnabled(boolean)`: replace with equivalent code.
1 parent a97bb31 commit 7b5f77b

7 files changed

Lines changed: 69 additions & 24 deletions

File tree

src/generator/OptionGenerator.groovy

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ genDir = Paths.get(sourceUri).parent
1515
baseDir = genDir.resolveSibling("main/java/jp/vmi/selenium")
1616
config = new ConfigSlurper().parse(genDir.resolve('config.groovy').toUri().toURL())
1717

18+
INDENT = " "
19+
1820
def toUpperCamelCase(s) {
1921
s.replaceAll(/(^|_)([a-z])/) { m -> m[2].toUpperCase() }
2022
}
@@ -43,8 +45,15 @@ def updateOptions(iConfig, dConfig, dOpts) {
4345
def optStr = oName.replace('_', '-')
4446
def option = "name = \"--\" + ${cName}"
4547
def fType = "String"
48+
def deprecated = false
4649
odef.forEach { name, value ->
4750
switch (name) {
51+
case "deprecated":
52+
if (value instanceof Boolean && value) {
53+
deprecated = true
54+
}
55+
break
56+
4857
case "type":
4958
fType = value
5059
break
@@ -53,7 +62,11 @@ def updateOptions(iConfig, dConfig, dOpts) {
5362
if (value instanceof Boolean && !value) {
5463
break
5564
}
56-
enumItems += " /** --${optStr} */\n ${cName}"
65+
enumItems += "${INDENT}/** --${optStr} */\n"
66+
if (deprecated) {
67+
enumItems += "${INDENT}@Deprecated\n"
68+
}
69+
enumItems += "${INDENT}${cName}"
5770
def t = (value instanceof String) ? value : fType
5871
if (t != "String") {
5972
enumItems += "(${t}.class)"
@@ -70,6 +83,9 @@ def updateOptions(iConfig, dConfig, dOpts) {
7083
value = value.replaceAll(/"/, "\\\\\"").replaceAll(/%(\w+)%/) {
7184
"\" + ${it[1]} + \""
7285
}
86+
if (deprecated) {
87+
value = "[deprecated] " + value
88+
}
7389
}
7490
option += " ${name} = \"${value}\""
7591
break
@@ -83,31 +99,59 @@ def updateOptions(iConfig, dConfig, dOpts) {
8399
mgPrefix = "is"
84100
dValue = "false"
85101
}
102+
if (deprecated) {
103+
oNames += " @Deprecated\n"
104+
}
86105
oNames += """\
87106
public static final String ${cName} = "${optStr}";
88107
"""
108+
if (deprecated) {
109+
getters += "\n @Deprecated"
110+
}
89111
getters += """
90112
${mType} ${mgPrefix}${uName}();
91113
"""
114+
if (deprecated) {
115+
fields += "\n @Deprecated\n @SuppressWarnings(\"deprecation\")"
116+
}
92117
fields += "\n @Option(${option})\n private ${fType} ${lName};\n"
93-
accessors += """
118+
if (deprecated) {
119+
accessors += """
120+
@Override
121+
@Deprecated
122+
public ${mType} ${mgPrefix}${uName}() {
123+
return ${dValue};
124+
}
125+
"""
126+
} else {
127+
accessors += """
94128
@Override
95129
public ${mType} ${mgPrefix}${uName}() {
96130
return ${lName} != null ? ${lName} : (parentOptions != null ? parentOptions.${mgPrefix}${uName}() : ${dValue});
97131
}
98132
"""
133+
}
99134
if (fType == "String[]") {
100135
accessors += """
101136
public void add${uName}(String ${lName}Item) {
102137
this.${lName} = ArrayUtils.add(this.${lName}, ${lName}Item);
103138
}
104139
"""
105140
} else {
106-
accessors += """
141+
if (deprecated) {
142+
accessors += """
143+
@Deprecated
144+
public void set${uName}(${mType} ${lName}) {
145+
log.warn("--${optStr} is deprecated.");
146+
}
147+
"""
148+
} else {
149+
accessors += """
107150
public void set${uName}(${mType} ${lName}) {
108151
this.${lName} = ${lName};
109152
}
110153
"""
154+
}
111155
}
112156
}
113157

src/generator/config.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ width {
213213
}
214214

215215
alerts_policy {
216-
usage = "The default behaviour for unexpected alerts (accept/ignore/dismiss)"
216+
usage = "The default behaviour for unexpected alerts (accept/dismiss/accept_and_notify/dismiss_and_notify/ignore)"
217217
driverOption = "UnexpectedAlertBehaviour"
218218
}
219219

src/main/java/jp/vmi/selenium/selenese/Runner.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import org.apache.commons.io.FilenameUtils;
2121
import org.apache.commons.io.output.NullOutputStream;
2222
import org.openqa.selenium.Alert;
23-
import org.openqa.selenium.HasCapabilities;
2423
import org.openqa.selenium.JavascriptExecutor;
2524
import org.openqa.selenium.OutputType;
2625
import org.openqa.selenium.TakesScreenshot;
@@ -61,7 +60,6 @@
6160
import jp.vmi.selenium.webdriver.WebDriverPreparator;
6261

6362
import static jp.vmi.selenium.selenese.result.Unexecuted.*;
64-
import static org.openqa.selenium.remote.CapabilityType.*;
6563

6664
/**
6765
* Provide Java API to run Selenese script.
@@ -205,7 +203,7 @@ public PrintStream getPrintStream() {
205203
protected TakesScreenshot getTakesScreenshot() {
206204
if (driver instanceof TakesScreenshot) {
207205
return (TakesScreenshot) driver;
208-
} else if (driver instanceof RemoteWebDriver && ((HasCapabilities) driver).getCapabilities().is(TAKES_SCREENSHOT)) {
206+
} else if (driver instanceof RemoteWebDriver) {
209207
return (TakesScreenshot) new Augmenter().augment(driver);
210208
} else {
211209
return null;

src/main/java/jp/vmi/selenium/selenese/config/DefaultConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ private static String statusListItem(Level level) {
200200
@Option(name = "--" + WIDTH, metaVar = "<width>", usage = "set initial width. (excluding mobile)")
201201
private String width;
202202

203-
@Option(name = "--" + ALERTS_POLICY, usage = "The default behaviour for unexpected alerts (accept/ignore/dismiss)")
203+
@Option(name = "--" + ALERTS_POLICY, usage = "The default behaviour for unexpected alerts (accept/dismiss/accept_and_notify/dismiss_and_notify/ignore)")
204204
private String alertsPolicy;
205205

206206
@Option(name = "--" + DEFINE, aliases = "-D", metaVar = "<key>[:<type>][+]=<value>",

src/main/java/jp/vmi/selenium/webdriver/DriverOptions.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public boolean has(DriverOption opt) {
211211
case CHROME_EXTENSION:
212212
return !chromeExtensions.isEmpty();
213213
case ALERTS_POLICY:
214-
return caps.getCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR) != null;
214+
return caps.getCapability(CapabilityType.UNHANDLED_PROMPT_BEHAVIOUR) != null;
215215
default:
216216
return map.containsKey(opt);
217217
}
@@ -241,11 +241,10 @@ private static UnexpectedAlertBehaviour parseUnexpectedAlertBehaviour(Object val
241241
if (value == null || value instanceof UnexpectedAlertBehaviour)
242242
return (UnexpectedAlertBehaviour) value;
243243
if (value instanceof String) {
244-
try {
245-
return Enum.valueOf(UnexpectedAlertBehaviour.class, ((String) value).toUpperCase());
246-
} catch (IllegalArgumentException e) {
247-
// fall through.
248-
}
244+
String canon = ((String) value).trim().replaceAll("[\\-_\t ]+", " ");
245+
UnexpectedAlertBehaviour uab = UnexpectedAlertBehaviour.fromString(canon);
246+
if (uab != null)
247+
return uab;
249248
}
250249
throw new IllegalArgumentException(String.format(
251250
"The value of %s is \"%s\". It must be one of the following: accept, dismiss, accept_and_notify, dismiss_and_notify, ignore",
@@ -292,7 +291,7 @@ public DriverOptions set(DriverOption opt, Object value) {
292291
break;
293292
case ALERTS_POLICY:
294293
UnexpectedAlertBehaviour uab = parseUnexpectedAlertBehaviour(value);
295-
caps.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, uab);
294+
caps.setCapability(CapabilityType.UNHANDLED_PROMPT_BEHAVIOUR, uab);
296295
return this;
297296
default:
298297
if (value == null)
@@ -305,8 +304,10 @@ public DriverOptions set(DriverOption opt, Object value) {
305304
opt, opt.valueType.getSimpleName(), value.getClass().getSimpleName(), value));
306305
}
307306

307+
@SuppressWarnings("deprecation")
308308
private static Object getTypedValue(String name, String type, String value, Consumer<String> errorHandler) {
309309
switch (name) {
310+
case CapabilityType.UNHANDLED_PROMPT_BEHAVIOUR:
310311
case CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR:
311312
return parseUnexpectedAlertBehaviour(value);
312313
default:

src/main/java/jp/vmi/selenium/webdriver/HtmlUnitDriverFactory.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package jp.vmi.selenium.webdriver;
22

3+
import java.lang.reflect.Field;
4+
35
import org.openqa.selenium.Capabilities;
46
import org.openqa.selenium.Dimension;
57
import org.openqa.selenium.Platform;
@@ -34,10 +36,12 @@ public String getBrowserName() {
3436
public WebDriver newInstance(DriverOptions driverOptions) {
3537
DesiredCapabilities caps = new DesiredCapabilities(Browser.HTMLUNIT.browserName(), "", Platform.ANY);
3638
setupProxy(caps, driverOptions);
37-
caps.setJavascriptEnabled(true);
3839
caps.merge(driverOptions.getCapabilities());
3940
try {
4041
Class<?> htmlUnitDriverClass = Class.forName(HTML_UNIT_DRIVER);
42+
Field javascriptEnabledField = htmlUnitDriverClass.getDeclaredField("JAVASCRIPT_ENABLED");
43+
String javascriptEnabled = (String) javascriptEnabledField.get(null);
44+
caps.setCapability(javascriptEnabled, true);
4145
WebDriver driver = (WebDriver) htmlUnitDriverClass.getConstructor(Capabilities.class).newInstance(caps);
4246
//HtmlUnitDriver driver = new HtmlUnitDriver(caps);
4347
Class.forName(HTML_UNIT_CONSOLE).getMethod("setHtmlUnitConsole", WebDriver.class).invoke(null, driver);

src/test/java/jp/vmi/selenium/webdriver/DriverOptionsTest.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package jp.vmi.selenium.webdriver;
22

33
import org.junit.Test;
4-
import org.openqa.selenium.JavascriptExecutor;
4+
import org.openqa.selenium.HasCapabilities;
55
import org.openqa.selenium.WebDriver;
66
import org.openqa.selenium.remote.DesiredCapabilities;
77

@@ -48,21 +48,19 @@ public void defineBool() {
4848
WebDriverManager wdm = WebDriverManager.newInstance();
4949
wdm.setWebDriverFactory(WebDriverManager.HTMLUNIT);
5050
DriverOptions driverOptions = new DriverOptions();
51-
driverOptions.addDefinitions("javascriptEnabled:bool=false");
51+
driverOptions.addDefinitions("acceptInsecureCerts:bool=true");
5252
wdm.setDriverOptions(driverOptions);
5353
WebDriver driver = wdm.get();
5454
WebServer ws = new WebServer();
5555
ws.start();
56-
Exception actual = null;
56+
DesiredCapabilities caps = null;
5757
try {
5858
driver.get(ws.getBaseURL());
59-
((JavascriptExecutor) driver).executeScript("true");
60-
} catch (Exception e) {
61-
actual = e;
59+
HasCapabilities hasCaps = (HasCapabilities) driver;
60+
caps = (DesiredCapabilities) hasCaps.getCapabilities();
6261
} finally {
6362
ws.stop();
6463
}
65-
assertThat(actual, is(instanceOf(UnsupportedOperationException.class)));
66-
assertThat(actual.getMessage(), equalTo("Javascript is not enabled for this HtmlUnitDriver instance"));
64+
assertThat(caps.acceptInsecureCerts(), is(true));
6765
}
6866
}

0 commit comments

Comments
 (0)