|
| 1 | +package com.anhtester.Bai8_Checkbox_Radio_Dropdown; |
| 2 | + |
| 3 | +import com.anhtester.common.BaseTest; |
| 4 | +import org.openqa.selenium.By; |
| 5 | +import org.openqa.selenium.WebElement; |
| 6 | + |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +public class HandleMultiCheckbox extends BaseTest { |
| 10 | + public static void main(String[] args) { |
| 11 | + createBrowser(); |
| 12 | + |
| 13 | + driver.get("http://demo.seleniumeasy.com/basic-checkbox-demo.html"); |
| 14 | + |
| 15 | + List<WebElement> listCheckbox = driver.findElements(By.xpath("//div[normalize-space()='Multiple Checkbox Demo']/following-sibling::div//input[@type='checkbox']")); |
| 16 | + System.out.println(listCheckbox.size()); |
| 17 | + |
| 18 | + //Cách 1: dùng duyệt List |
| 19 | + for (int i = 0; i < listCheckbox.size(); i++) { |
| 20 | + System.out.println(listCheckbox.get(i).isSelected()); |
| 21 | + } |
| 22 | + |
| 23 | + driver.findElement(By.xpath("//label[normalize-space()='Option 1']")).click(); |
| 24 | + driver.findElement(By.xpath("//label[normalize-space()='Option 2']")).click(); |
| 25 | + |
| 26 | + //Kiểm tra lại sau khi click chọn 2 ô checkbox |
| 27 | + listCheckbox = driver.findElements(By.xpath("//div[normalize-space()='Multiple Checkbox Demo']/following-sibling::div//input[@type='checkbox']")); |
| 28 | + |
| 29 | + for (int i = 0; i < listCheckbox.size(); i++) { |
| 30 | + System.out.println(listCheckbox.get(i).isSelected()); |
| 31 | + //Xác nhận nó đúng hay chưa? => TCs passes/failed |
| 32 | + } |
| 33 | + |
| 34 | + System.out.println("==================="); |
| 35 | + if (listCheckbox.get(0).isSelected() == true && |
| 36 | + listCheckbox.get(1).isSelected() == true && |
| 37 | + listCheckbox.get(2).isSelected() == true) { |
| 38 | + System.out.println("Passed"); //Sau này dùng hàm trong class Assert của TestNG để xác nhận pass/fail |
| 39 | + } else { |
| 40 | + System.out.println("Failed"); |
| 41 | + } |
| 42 | + |
| 43 | + //Cách 2 dùng doạn Xpath cụ thể để duyệt từng vị trí checkbox |
| 44 | + System.out.println("==========Cách 2==========="); |
| 45 | + for (int i = 1; i <= listCheckbox.size(); i++) { |
| 46 | + System.out.println(driver.findElement(By.xpath("(//div[normalize-space()='Multiple Checkbox Demo']/following-sibling::div//input[@type='checkbox'])[" + i + "]")).isSelected()); |
| 47 | + } |
| 48 | + |
| 49 | + closeBrowser(); |
| 50 | + } |
| 51 | +} |
0 commit comments