Skip to content

Commit 1883844

Browse files
committed
add selectOptionByIndex. fixes #25
1 parent da6affe commit 1883844

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

src/main/java/io/ddavison/conductor/Conductor.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,16 @@ public interface Conductor<Test> {
121121
Test selectOptionByValue(String css, String value);
122122
Test selectOptionByValue(By by, String value);
123123

124+
125+
/**
126+
* Selects an option from a dropdown ({@literal <select> tag}) based on the index.
127+
* @param css/by The element
128+
* @param i The index of which it appears (zero based)
129+
* @return The implementing class for fluency
130+
*/
131+
Test selectOptionByIndex(String css, Integer i);
132+
Test selectOptionByIndex(By by, Integer i);
133+
124134
/**
125135
* Waits for a window to appear, then switches to it.
126136
* @param regex Regex enabled. Url of the window, or title.

src/main/java/io/ddavison/conductor/Locomotive.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,20 @@ public Locomotive selectOptionByValue(By by, String value) {
395395
return this;
396396
}
397397

398+
@Override
399+
public Locomotive selectOptionByIndex(String css, Integer i) {
400+
return selectOptionByIndex(By.cssSelector(css), i);
401+
}
402+
403+
@Override
404+
public Locomotive selectOptionByIndex(By by, Integer i) {
405+
Select box = new Select(waitForElement(by));
406+
waitForCondition(ExpectedConditions.not(ExpectedConditions.invisibilityOfElementLocated(by)))
407+
.waitForCondition(ExpectedConditions.elementToBeClickable(by));
408+
box.selectByIndex(i);
409+
return this;
410+
}
411+
398412
/* Window / Frame Switching */
399413

400414
public Locomotive waitForWindow(String regex) {

src/test/java/io/ddavison/conductor/FrameworkTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,12 @@ public void testGetTextFromTextArea() throws Exception {
8787
setText("#textArea", "some text")
8888
.validateText("#textArea", "some text");
8989
}
90+
91+
@Test
92+
public void testSelectOptionByIndex() throws Exception {
93+
selectOptionByIndex("#select", 2)
94+
.validateText("#select", "3")
95+
.selectOptionByIndex(By.xpath("//*[@id='select']"), 0)
96+
.validateText("#select", "1");
97+
}
9098
}

0 commit comments

Comments
 (0)