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

Commit b151471

Browse files
committed
Improve Selenium plugin.
1 parent 9f0075c commit b151471

5 files changed

Lines changed: 68 additions & 40 deletions

File tree

jwebunit-core/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
<dependency>
1616
<groupId>junit</groupId>
1717
<artifactId>junit</artifactId>
18-
<scope>test</scope>
1918
</dependency>
2019
<dependency>
2120
<groupId>regexp</groupId>

jwebunit-core/src/main/java/net/sourceforge/jwebunit/junit/WebTester.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1786,6 +1786,7 @@ public void setWorkingForm(int index) {
17861786
* @param nameOrId name or id of the form to work with.
17871787
*/
17881788
public void setWorkingForm(String nameOrId) {
1789+
assertFormPresent(nameOrId);
17891790
getTestingEngine().setWorkingForm(nameOrId, 0);
17901791
}
17911792

jwebunit-htmlunit-plugin/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
<scope>test</scope>
3535
</dependency>
3636
<dependency>
37-
<groupId>htmlunit</groupId>
37+
<groupId>net.sourceforge.htmlunit</groupId>
3838
<artifactId>htmlunit</artifactId>
3939
</dependency>
4040
<dependency>

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

Lines changed: 60 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public void clickLinkWithExactText(String linkText, int index) {
107107
}
108108

109109
public void clickLinkWithImage(String imageFileName, int index) {
110-
selenium.click("xpath=//a[contains(img/@src,'" + imageFileName + "')]");
110+
selenium.click("xpath=//a[contains(img/@src,'" + imageFileName + "')][" + index + 1 + "]");
111111
selenium.waitForPageToLoad(timeout);
112112
}
113113

@@ -118,7 +118,7 @@ public void clickLinkWithText(String linkText, int index) {
118118
}
119119

120120
public void clickRadioOption(String radioGroup, String radioOptionValue) {
121-
selenium.click("xpath=" + formSelector() + "//input[@name='" + radioGroup + "' and @value='"
121+
selenium.click("xpath=" + formSelector() + "//input[@type='radio' and @name='" + radioGroup + "' and @value='"
122122
+ radioOptionValue + "']");
123123
}
124124

@@ -260,7 +260,7 @@ public boolean hasLinkWithExactText(String linkText, int index) {
260260

261261
public boolean hasLinkWithImage(String imageFileName, int index) {
262262
return selenium.isElementPresent("xpath=//a[contains(img/@src,'"
263-
+ imageFileName + "')]");
263+
+ imageFileName + "')][" + index + 1 + "]");
264264
}
265265

266266
public boolean hasLinkWithText(String linkText, int index) {
@@ -292,26 +292,32 @@ public boolean hasSelectOptionValue(String selectName, String optionValue) {
292292
}
293293

294294
public boolean hasSubmitButton() {
295+
String xpath1 = formSelector() + "//input[@type='submit' or @type='image']";
296+
String xpath2 = formSelector() + "//button[@type='submit']";
295297
return selenium
296-
.isElementPresent("xpath=" + formSelector() + "(//input[@type='submit' or @type='image']|//button[@type='submit'])");
298+
.isElementPresent("xpath=" + xpath1 + "|" + xpath2);
297299
}
298300

299301
public boolean hasSubmitButton(String nameOrID, String value) {
302+
String xpath1 = formSelector() + "//input[(@type='submit' or @type='image') and (@id='"
303+
+ nameOrID + "' or @name='" + nameOrID
304+
+ "') and @value='" + value + "']";
305+
String xpath2 = formSelector() + "//button[@type='submit' and (@id='"
306+
+ nameOrID + "' or @name='" + nameOrID
307+
+ "') and @value='" + value + "']";
300308
return selenium
301-
.isElementPresent("xpath=" + formSelector() + "(//input[(@type='submit' or @type='image') and (@id='"
302-
+ nameOrID + "' or @name='" + nameOrID
303-
+ "') and @value='" + value + "']|//button[@type='submit' and (@id='"
304-
+ nameOrID + "' or @name='" + nameOrID
305-
+ "') and @value='" + value + "'])");
309+
.isElementPresent("xpath=" + xpath1 + "|" + xpath2);
306310
}
307311

308312
public boolean hasSubmitButton(String nameOrID) {
313+
String xpath1 = formSelector() + "//input[(@type='submit' or @type='image') and (@id='"
314+
+ nameOrID + "' or @name='" + nameOrID
315+
+ "')]";
316+
String xpath2 = formSelector() + "//button[@type='submit' and (@id='"
317+
+ nameOrID + "' or @name='" + nameOrID
318+
+ "')]";
309319
return selenium
310-
.isElementPresent("xpath=" + formSelector() + "(//input[(@type='submit' or @type='image') and (@id='"
311-
+ nameOrID + "' or @name='" + nameOrID
312-
+ "')]|//button[@type='submit' and (@id='"
313-
+ nameOrID + "' or @name='" + nameOrID
314-
+ "')])");
320+
.isElementPresent("xpath=" + xpath1 + "|" + xpath2);
315321
}
316322

317323
public boolean hasResetButton() {
@@ -385,36 +391,44 @@ public void setScriptingEnabled(boolean value) {
385391
}
386392

387393
public void setTextField(String inputName, String text) {
388-
selenium.type("xpath=" + formSelector() + "(//input[@name='"+inputName+"' and (@type='text' or @type='password' or @type='file')]|//textarea[@name='"+inputName+"'])", text);
394+
String xpath1 = formSelector() + "//input[@name='"+inputName+"' and (@type='text' or @type='password' or @type='file')]";
395+
String xpath2 = formSelector() + "//textarea[@name='"+inputName+"']";
396+
selenium.type("xpath=" + xpath1 + "|" + xpath2, text);
389397
}
390398

391399
public void setWorkingForm(String nameOrId, int index) {
392400
if (nameOrId != null)
393-
formIdent="(@name='"+nameOrId+"' or @id='"+nameOrId+"') and position()="+index;
401+
formIdent="(@name='"+nameOrId+"' or @id='"+nameOrId+"')][ position()="+(index+1);
394402
else
395403
formIdent=null;
396404
}
397405

398406
public void submit() {
399-
selenium.click("xpath=" + formSelector() + "(//input[@type='submit' or @type='image']|//button[@type='submit'])");
407+
String xpath1 = formSelector() + "//input[@type='submit' or @type='image']";
408+
String xpath2 = formSelector() + "//button[@type='submit']";
409+
selenium.click("xpath=" + xpath1 + "|" + xpath2);
400410
selenium.waitForPageToLoad(timeout);
401411
}
402412

403-
public void submit(String buttonName, String buttonValue) {
404-
selenium.click("xpath=" + formSelector() + "(//input[(@type='submit' or @type='image') and (@id='"
405-
+ buttonName + "' or @name='" + buttonName
406-
+ "') and @value='" + buttonValue + "']|//button[@type='submit' and (@id='"
407-
+ buttonName + "' or @name='" + buttonName
408-
+ "') and @value='" + buttonValue + "'])");
413+
public void submit(String nameOrID, String value) {
414+
String xpath1 = formSelector() + "//input[(@type='submit' or @type='image') and (@id='"
415+
+ nameOrID + "' or @name='" + nameOrID
416+
+ "') and @value='" + value + "']";
417+
String xpath2 = formSelector() + "//button[@type='submit' and (@id='"
418+
+ nameOrID + "' or @name='" + nameOrID
419+
+ "') and @value='" + value + "']";
420+
selenium.click("xpath=" + xpath1 + "|" + xpath2);
409421
selenium.waitForPageToLoad(timeout);
410422
}
411423

412-
public void submit(String buttonName) {
413-
selenium.click("xpath=" + formSelector() + "(//input[(@type='submit' or @type='image') and (@id='"
414-
+ buttonName + "' or @name='" + buttonName
415-
+ "')]|//button[@type='submit' and (@id='"
416-
+ buttonName + "' or @name='" + buttonName
417-
+ "')])");
424+
public void submit(String nameOrID) {
425+
String xpath1 = formSelector() + "//input[(@type='submit' or @type='image') and (@id='"
426+
+ nameOrID + "' or @name='" + nameOrID
427+
+ "')]";
428+
String xpath2 = formSelector() + "//button[@type='submit' and (@id='"
429+
+ nameOrID + "' or @name='" + nameOrID
430+
+ "')]";
431+
selenium.click("xpath=" + xpath1 + "|" + xpath2);
418432
selenium.waitForPageToLoad(timeout);
419433
}
420434

@@ -525,7 +539,22 @@ public URL getPageURL() {
525539
* @see net.sourceforge.jwebunit.api.ITestingEngine#getSelectedRadio(java.lang.String)
526540
*/
527541
public String getSelectedRadio(String radioGroup) {
528-
throw new UnsupportedOperationException("getSelectedRadio");
542+
int count = 0;
543+
while (selenium.isElementPresent("xpath=" + formSelector() + "//input[@type='radio' and @name='"+radioGroup+"']["+(count+1)+"]")) {
544+
if ("on".equals(selenium.getValue("xpath=" + formSelector() + "//input[@type='radio' and @name='"+radioGroup+"']["+(count+1)+"]"))) {
545+
return selenium.getAttribute("xpath=" + formSelector() + "//input[@type='radio' and @name='"+radioGroup+"']["+(count+1)+"]@value");
546+
}
547+
count++;
548+
}
549+
return null;
550+
}
551+
552+
protected int getRadioCount(String radioGroup) {
553+
int count = 0;
554+
while (selenium.isElementPresent("xpath=" + formSelector() + "//input[@type='radio' and @name='"+radioGroup+"']["+(count+1)+"]")) {
555+
count++;
556+
}
557+
return count;
529558
}
530559

531560
/* (non-Javadoc)
@@ -558,8 +587,7 @@ public void setExpectedJavaScriptPrompt(JavascriptPrompt[] prompts) throws Expec
558587
* @see net.sourceforge.jwebunit.api.ITestingEngine#setWorkingForm(int)
559588
*/
560589
public void setWorkingForm(int index) {
561-
// TODO Auto-generated method stub
562-
throw new UnsupportedOperationException("setWorkingForm");
590+
formIdent="position()="+(index+1);
563591
}
564592

565593
}

pom.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@
162162
<repository>
163163
<id>Codehaus</id>
164164
<name>Mojo Codehaus Repository</name>
165-
<url>http://repository.codehaus.org/org/codehaus/mojo/</url>
165+
<url>http://repository.codehaus.org/</url>
166166
</repository>
167167
</repositories>
168168
<build>
@@ -269,7 +269,7 @@
269269
<dependency>
270270
<groupId>org.mortbay.jetty</groupId>
271271
<artifactId>jetty</artifactId>
272-
<version>6.1.2rc2</version>
272+
<version>6.1.3</version>
273273
</dependency>
274274
<dependency>
275275
<groupId>commons-fileupload</groupId>
@@ -282,9 +282,9 @@
282282
<version>1.3.1</version>
283283
</dependency>
284284
<dependency>
285-
<groupId>htmlunit</groupId>
285+
<groupId>net.sourceforge.htmlunit</groupId>
286286
<artifactId>htmlunit</artifactId>
287-
<version>1.11</version>
287+
<version>1.12-SNAPSHOT</version>
288288
</dependency>
289289
<dependency>
290290
<groupId>regexp</groupId>
@@ -305,7 +305,7 @@
305305
<groupId>org.slf4j</groupId>
306306
<artifactId>slf4j-log4j12</artifactId>
307307
<version>1.3.0</version>
308-
<scope>test</scope>
308+
<scope>optional</scope>
309309
</dependency>
310310
</dependencies>
311311
</dependencyManagement>
@@ -447,7 +447,7 @@
447447
<site>
448448
<id>jwebunit-website</id>
449449
<name>JWebUnit WebSite - Sourceforge</name>
450-
<url>scp://shell.sourceforge.net/home/groups/j/jw/jwebunit/htdocs</url>
450+
<url>file:///home/julien/site-jwebunit</url>
451451
</site>
452452
<repository>
453453
<id>jwebunit-m2-repo</id>

0 commit comments

Comments
 (0)