[Java – Webdriver 10] – Kiểm tra phần tử đã được chọn (selected)


Yêu cầu:

  • Kiểm tra phần tử như checkbox, radiobutton, dropdown,.. đã được select hay chưa. Áp dụng khi check về UI cho 1 page hoặc khi viết kịch bản tự động hóa, bạn muốn kiểm tra chắc chắn rằng một element nào đó đã được chọn hay chưa trước khi chuyển qua bước tiếp theo

Ví dụ như hình dưới khi check vào checkbox “Contingent Rent” thì 4 textbox “Period End/ Contingent Rent Description/ Days to Report/ Breakover” sẽ được phép chỉnh sửa (nhập dữ liệu) => vậy thì chúng ta phải kiểm tra rằng checkbox “Contingent Rent” đã được select và sau đó 4 textbox kia phải nhập được dữ liệu vào -> Nếu checkbox “Contingent Rent” đã được chọn nhưng không thể nhập dữ liệu vào thì đó là Bug

Giải pháp:

  • import thư viện: org.openqa.selenium.WebElement;
  • Khởi tạo: WebElement locator;
  • Kiểm tra hiển thị: locator.isSelected();
  • Xem chi tiết hàm isElementSelected() trong phần Source demo phía dưới nhé

Site demo:     Link

  • Như hình dưới, mình sẽ check các element này đã hoặc chưa được select:
    • Age radio button
    • Job Role 02 dropdown list
    • Interest checkbox

Source demo:

package seleniumWebDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class WebDriver10_ElementIsSelectedOnPage {
 WebDriver driver;

 @BeforeClass
 public void setUp() {
 driver = new FirefoxDriver();
 }

 @Test
 public void Test01_DevelopmentCheckboxIsSelected() throws Exception {
 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
 driver.get("http://daominhdam.890m.com");
 driver.manage().window().maximize();
 String element = "//*[@id='development']";
 driver.findElement(By.xpath(element)).click();
 Thread.sleep(2000);
 if(isElementSelected(driver, element)){
 System.out.println("'Development' checkbox is selected");
 } else{
 System.out.println("'Development' checkbox isn't selected");
 }
 }
 
 @Test
 public void Test02_DesignCheckboxIsNotSelected() throws Exception {
 String element = "//*[@id='design']";
 if(isElementSelected(driver, element)){
 System.out.println("'Design' checkbox is selected");
 } else{
 System.out.println("'Design' checkbox isn't selected");
 }
 }
 
 @Test
 public void Test03_Under18RadioButtonIsSelected() throws Exception {
 String element = "//*[@id='under_18']";
 driver.findElement(By.xpath(element)).click();
 Thread.sleep(2000);
 if(isElementSelected(driver, element)){
 System.out.println("'Under 18' radio button is selected");
 } else{
 System.out.println("'Under 18' radio button isn't selected");
 }
 }
 
 @Test
 public void Test04_18OrOrderRadioButtonIsNotSelected() throws Exception {
 String element = "//*[@id='over_18']";
 if(isElementSelected(driver, element)){
 System.out.println("'Over 18' radio button is selected");
 } else{
 System.out.println("'Over 18' radio button isn't selected");
 }
 }
 
 @Test
 public void Test05_JobRoleDropdownListIsNotSelected() throws Exception {
 String element = "//*[@id='job1']";
 if(isElementSelected(driver, element)){
 System.out.println("'Job Role 01' dropdown list is selected");
 } else{
 System.out.println("'Job Role 01' dropdown list isn't selected \n");
 }
 }
 
 public boolean isElementSelected(WebDriver driver, String yourLocator) {
 try {
 WebElement locator;
 locator = driver.findElement(By.xpath(yourLocator));
 return locator.isSelected();
 } catch (NoSuchElementException e) {
 return false;
 }
 }
 
 @AfterClass
 public void tearDown() {
 driver.quit();
 }
}

Video demo: