Last active
July 13, 2021 14:36
-
-
Save upgundecha/654a173081e6b9399837612ed207c416 to your computer and use it in GitHub Desktop.
Selenium WebDriver + Java - Kendo Dropdown Example (requires Java 8)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.After; | |
import org.junit.Assert; | |
import org.junit.Before; | |
import org.junit.Test; | |
import org.openqa.selenium.By; | |
import org.openqa.selenium.WebDriver; | |
import org.openqa.selenium.WebElement; | |
import org.openqa.selenium.chrome.ChromeDriver; | |
import org.openqa.selenium.internal.WrapsDriver; | |
import java.util.List; | |
import java.util.concurrent.TimeUnit; | |
import java.util.stream.Collectors; | |
/** | |
* Created by upgundecha on 07/03/17. | |
*/ | |
public class KendoDropdownTest { | |
WebDriver driver; | |
@Before | |
public void setUp() { | |
driver = new ChromeDriver(); | |
driver.manage().window().maximize(); | |
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); | |
} | |
@After | |
public void tearDown() { | |
driver.close(); | |
driver.quit(); | |
} | |
@Test | |
public void dropDownTests() throws Exception { | |
// Example 1 | |
driver.get("http://demos.telerik.com/kendo-ui/bootstrap"); | |
// Find Kendo dropdown using aria-owns attribute | |
KendoDropdwon genderDropdown = new KendoDropdwon(driver | |
.findElement(By.cssSelector("span[aria-owns='gender_listbox']"))); | |
System.out.println("Size >> " + genderDropdown.getSize()); | |
Assert.assertEquals("Male", genderDropdown.getSelection());; | |
genderDropdown.selectItem("Female"); | |
Assert.assertEquals("Female", genderDropdown.getSelection());; | |
System.out.println(genderDropdown.getOptions()); | |
// Example 2 | |
driver.get("http://demos.telerik.com/kendo-ui/dropdownlist/index"); | |
// Find Kendo dropdown using aria-owns attribute | |
KendoDropdwon colorDropdown = new KendoDropdwon(driver | |
.findElement(By.cssSelector("span[aria-owns='color_listbox']"))); | |
System.out.println("Size >> " + colorDropdown.getSize()); | |
Assert.assertEquals("Black", colorDropdown.getSelection()); | |
colorDropdown.selectItem("Grey"); | |
Assert.assertEquals("Grey", colorDropdown.getSelection()); | |
} | |
public class KendoDropdwon { | |
WebDriver driver; | |
String name; | |
WebElement parentElem; | |
public KendoDropdwon(WebElement parentElem) { | |
this.parentElem = parentElem; | |
name = parentElem.getAttribute("aria-owns"); | |
this.driver = ((WrapsDriver) parentElem).getWrappedDriver(); | |
} | |
/** | |
* Returns selected item from the Dropdown | |
* @return | |
*/ | |
public String getSelection() { | |
return parentElem.findElement(By | |
.cssSelector("span.k-state-default span.k-input")) | |
.getText(); | |
} | |
/** | |
* Retruns all options displayed on the Dropdown | |
* @return list of options | |
*/ | |
public List<String> getOptions() { | |
expand(); | |
List<String> options = driver.findElements(By.cssSelector("ul#" + name + " li")) | |
.stream() | |
.map(WebElement::getText) | |
.collect(Collectors.toList()); | |
collapse(); | |
return options; | |
} | |
/** | |
* Returns count of options | |
* @return | |
*/ | |
public int getSize() { | |
//expand(); | |
int size = driver.findElements(By.cssSelector("ul#" + name + " li")).size(); | |
//collapse(); | |
return size; | |
} | |
/** | |
* Select item in the Dropdown | |
* @param item text | |
*/ | |
public void selectItem(String item) { | |
expand(); | |
driver.findElement(By.cssSelector("ul#" + name)) | |
.findElement(By.xpath("./li[text()='" + item + "']")).click(); | |
collapse(); | |
} | |
/** | |
* Open Dropdown | |
*/ | |
private void expand() { | |
if (!parentElem.getAttribute("aria-expanded").equals("true")) { | |
parentElem.findElement(By.cssSelector("span.k-icon")).click(); | |
} | |
} | |
/** | |
* Close Dropdown | |
*/ | |
private void collapse() { | |
if (!parentElem.getAttribute("aria-expanded").equals("false")) { | |
parentElem.findElement(By.cssSelector("span.k-icon")).click(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment