Skip to content
This repository was archived by the owner on Apr 7, 2022. It is now read-only.

Commit 1955218

Browse files
committed
[2007985,2009776] Some improvements to Selenium plugin. Patch from Josh Lane.
1 parent 7cfeaf3 commit 1955218

3 files changed

Lines changed: 64 additions & 15 deletions

File tree

jwebunit-selenium-plugin/src/main/java/net/sourceforge/jwebunit/selenium/SeleniumTestingEngineImpl.java

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77

88
import java.io.InputStream;
9+
import java.net.MalformedURLException;
910
import java.net.URL;
1011
import java.util.LinkedList;
1112
import java.util.List;
@@ -302,15 +303,21 @@ public boolean hasSelectOption(String selectName, String optionLabel) {
302303

303304
public boolean hasSelectOptionValue(String selectName, String optionValue) {
304305
try {
305-
getSelectOptionLabelForValue(selectName, optionValue);
306-
return true;
306+
return getSelectOptionLabelForValue(selectName, optionValue) != null;
307307
} catch (SeleniumException e) {
308308
return false;
309309
}
310310
}
311311

312312
public boolean hasSelectOption(String selectName, int index, String optionLabel) {
313-
return false;
313+
boolean equals;
314+
try {
315+
equals = getSelectedOptions(selectName)[index].equals(optionLabel);
316+
} catch (RuntimeException e) {
317+
logger.error("", e);
318+
equals = false;
319+
}
320+
return equals;
314321
}
315322

316323
public boolean hasSelectOptionValue(String selectName, int index, String optionValue) {
@@ -388,13 +395,31 @@ public boolean isCheckboxSelected(String checkBoxName, String checkBoxValue) {
388395

389396

390397
public boolean isMatchInElement(String elementID, String regexp) {
391-
//TODO Implement isMatchInElement in SeleniumDialog
392-
throw new UnsupportedOperationException("isMatchInElement");
398+
boolean match;
399+
try {
400+
String locator = "id=" + elementID;
401+
String elementText = selenium.getText(locator);
402+
if (elementText == null || elementText.equals("")) elementText = selenium.getValue(locator);
403+
match = (elementText != null ? elementText.matches(regexp) : false);
404+
} catch (RuntimeException e) {
405+
logger.error("", e);
406+
match = false;
407+
}
408+
return match;
393409
}
394410

395411
public boolean isTextInElement(String elementID, String text) {
396-
// TODO Implement isTextInElement in SeleniumDialog
397-
throw new UnsupportedOperationException("isTextInElement");
412+
boolean contains;
413+
try {
414+
String locator = "id=" + elementID;
415+
String elementText = selenium.getText(locator);
416+
if (elementText == null || elementText.equals("")) elementText = selenium.getValue(locator);
417+
contains = (elementText != null ? elementText.contains(text) : false);
418+
} catch (RuntimeException e) {
419+
logger.error("", e);
420+
contains = false;
421+
}
422+
return contains;
398423
}
399424

400425
public void refresh() {
@@ -408,11 +433,17 @@ public void reset() {
408433
}
409434

410435
public void selectOptions(String selectName, String[] optionsValue) {
411-
for (int i=0; i<optionsValue.length; i++) {
412-
selenium.addSelection("xpath=" + formSelector() + "//select[@name='"+selectName+"']","value="+optionsValue[i]);
436+
if (optionsValue.length == 1) {
437+
selenium.select("xpath=" + formSelector() + "//select[@name='" + selectName + "']",
438+
optionsValue[0]);
439+
} else {
440+
for (int i = 0; i < optionsValue.length; i++) {
441+
selenium.addSelection("xpath=" + formSelector() + "//select[@name='" + selectName + "']",
442+
"value=" + optionsValue[i]);
443+
}
413444
}
414445
}
415-
446+
416447
public void selectOptions(String selectName, int index, String[] optionsValue) {
417448
}
418449

@@ -569,12 +600,15 @@ public InputStream getInputStream(URL url) throws TestingEngineResponseException
569600
throw new UnsupportedOperationException("getInputStream");
570601
}
571602

572-
/* (non-Javadoc)
573-
* @see net.sourceforge.jwebunit.api.ITestingEngine#getPageURL()
574-
*/
575603
public URL getPageURL() {
576-
// TODO Auto-generated method stub
577-
throw new UnsupportedOperationException("getPageURL");
604+
URL url;
605+
try {
606+
url = new URL(selenium.getLocation());
607+
} catch (MalformedURLException e) {
608+
logger.error("", e);
609+
url = null;
610+
}
611+
return url;
578612
}
579613

580614
/* (non-Javadoc)

pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,16 @@
137137
<timezone />
138138
<properties />
139139
</contributor>
140+
<contributor>
141+
<name>Josh Lane</name>
142+
<email>lane.joshlane at gmail.com</email>
143+
<url />
144+
<organization />
145+
<organizationUrl />
146+
<roles />
147+
<timezone />
148+
<properties />
149+
</contributor>
140150
</contributors>
141151
<licenses>
142152
<license>

src/changes/changes.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
</properties>
99
<body>
1010
<release version="2.0" date="UNKNOW">
11+
<action type="fix" dev="joshlane" issue="2007985,2009776" due-to="Josh Lane">
12+
selectOptionByValue failed with Selenium Plugin.
13+
selectOption always failed, cannot find element.
14+
Other improvements to Selenium plugin.
15+
</action>
1116
<action type="update" dev="henryju" issue="1950248" due-to="Agnes Ro">
1217
Update to HtmlUnit 2.1 and Java 1.5.
1318
</action>

0 commit comments

Comments
 (0)