[Java – Webdriver 11] – Xử lí Dropdowns/ Lists

Yêu cầu:

  • Làm sao để chọn các giá trị: Automation Tester/ Manual Tester trong dropdown ở hình trên?
  • Làm sao để kiểm tra trong dropdown có bao nhiêu giá trị => như hình trên có 5 giá trị?
  • Làm sao để kiểm tra 1 giá trị trong dropdown hiển thị đúng hay chưa sau khi đã chọn thành công?
  • Làm sao để kiểm tra dropdown có phải là multi-select hay single select => multi-select: cho phép chọn nhiều giá trị trong dropdown/ list?

Giải pháp:

  • Selenim WebDriver hỗ trợ kiểm tra các phần tử Dropdown/ List bằng cách sử dụng lớp Select thay vì sử dụng lớp WebElement
    => import org.openqa.selenium.support.ui.Select;
  • Lớp Select cung cấp các phương pháp và thuộc tính khác nhau để tương tác với dropdown/ list qua phần tử HTML là thẻ <select>

Các phương thức sử dụng trong bài viết:

  • Select dropdown sử dụng thuộc tính ID:
    Select select = new Select(driver.findElement(By.id(“job1”)));
    => <select id=”job1 name=”user_job1>
  • Select một giá trị trong dropdown sử dụng visible text (thường sử dụng)
    select.selectByVisibleText(“Automation Tester”);
    => <option value=”automation>Automation Tester</option>
  • Hoặc: Select một giá trị trong dropdown sử dụng value
    select.selectByValue(“manual”);
    => <option value=”manual>Manual Tester</option>
  • Hoặc: Select một giá trị trong dropdown sử dụng index (bắt đầu từ vị trí số 0)
    select.selectByIndex(3);
    => <option value=”website>Website Tester</option>
  • Kiểm tra dropdown không hỗ trợ multi-select:
    Assert.assertFalse(select.isMultiple());
  • Kiểm tra dropdown có 5 giá trị:
    Assert.assertEquals(5, select.getOptions().size());
    => Mỗi 1 giá trị tương ứng với 1 thẻ <option>
  • Kiểm tra giá trị trong dropdown hiển thị đúng sau khi đã chọn thành công (giá trị đã chọn sẽ luôn hiển thị ở vị trí đầu tiên):
    Assert.assertEquals(“Manual Tester”, select.getFirstSelectedOption().getText());

Site demo:     Link

Source demo:

package seleniumWebDriver;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class WebDriver11_HandleDropdownAndList {
 WebDriver driver;
 @BeforeClass

 public void setUp() {
 driver = new FirefoxDriver();
 driver.get("http://daominhdam.890m.com/");
 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
 driver.manage().window().maximize();
 }

 @Test
 public void Test01_HandleDropdownList() throws Exception {

 // Select using ID attribute
 Select select = new Select(driver.findElement(By.id("job1")));

 // Verify Dropdown doesn't support multi-select
 Assert.assertFalse(select.isMultiple());

 // Verify Dropdown has five options
 Assert.assertEquals(5, select.getOptions().size());

 // Select an option in Dropdown using visible text
 select.selectByVisibleText("Automation Tester");

 Assert.assertEquals("Automation Tester", select.getFirstSelectedOption().getText());
 Thread.sleep(4000);

 // Select an option in Dropdown using value attribute
 select.selectByValue("manual");

 Assert.assertEquals("Manual Tester", select.getFirstSelectedOption().getText());
 Thread.sleep(4000);

 // Select an option in Dropdown using index
 select.selectByIndex(3);

 Assert.assertEquals("Mobile Tester", select.getFirstSelectedOption().getText());
 Thread.sleep(4000);
 }

 @AfterClass
 public void tearDown() {
 driver.quit();
 }
}

Video demo: