|
| 1 | +package org.example; |
| 2 | + |
| 3 | +import io.github.bonigarcia.wdm.WebDriverManager; |
| 4 | +import org.openqa.selenium.By; |
| 5 | +import org.openqa.selenium.NoSuchElementException; |
| 6 | +import org.openqa.selenium.WebDriver; |
| 7 | +import org.openqa.selenium.WebElement; |
| 8 | +import org.openqa.selenium.chrome.ChromeDriver; |
| 9 | +import org.openqa.selenium.chrome.ChromeOptions; |
| 10 | +import org.openqa.selenium.support.ui.Select; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Scanner; |
| 13 | + |
| 14 | +public class SelectHiddenOptionsTest { |
| 15 | + public static void main(String[] args) { |
| 16 | + WebDriverManager.chromedriver().setup(); |
| 17 | + ChromeOptions options = new ChromeOptions(); |
| 18 | + options.addArguments("--remote-allow-origins=*"); |
| 19 | + WebDriver driver = new ChromeDriver(options); |
| 20 | + try { |
| 21 | + driver.get("file:///C:/Users/dell/OneDrive/Desktop/test_select.html"); |
| 22 | + WebElement selectElement = driver.findElement(By.id("mySelect")); |
| 23 | + Select select = new Select(selectElement); |
| 24 | + |
| 25 | + // Run test cases |
| 26 | + testOption(select, "Visible Option", true); |
| 27 | + testOption(select, "Hidden Option (display:none)", false); |
| 28 | + testOption(select, "Hidden Option (visibility:hidden)", false); |
| 29 | + testOption(select, "Hidden Option (opacity:0)", false); |
| 30 | + |
| 31 | + System.out.println("✅ Test execution completed. Press Enter to close the browser..."); |
| 32 | + new Scanner(System.in).nextLine(); |
| 33 | + } finally { |
| 34 | + driver.quit(); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + private static void testOption(Select select, String visibleText, boolean shouldSucceed) { |
| 39 | + try { |
| 40 | + WebElement option = findOptionByText(select, visibleText); |
| 41 | + if (option != null && isVisible(option)) { |
| 42 | + select.selectByVisibleText(visibleText); |
| 43 | + if (shouldSucceed) { |
| 44 | + System.out.println("✅ PASSED: Successfully selected: " + visibleText); |
| 45 | + } else { |
| 46 | + System.out.println("❌ FAILED: Should NOT have been able to select: " + visibleText); |
| 47 | + } |
| 48 | + } else { |
| 49 | + if (shouldSucceed) { |
| 50 | + System.out.println("❌ FAILED: Could NOT select: " + visibleText + " (option not visible)"); |
| 51 | + } else { |
| 52 | + System.out.println("✅ PASSED: Correctly failed to select: " + visibleText + " (option not visible)"); |
| 53 | + } |
| 54 | + } |
| 55 | + } catch (NoSuchElementException e) { |
| 56 | + if (shouldSucceed) { |
| 57 | + System.out.println("❌ FAILED: Could NOT select: " + visibleText + " → " + e.getMessage()); |
| 58 | + } else { |
| 59 | + System.out.println("✅ PASSED: Correctly failed to select: " + visibleText + " → " + e.getMessage()); |
| 60 | + } |
| 61 | + } catch (Exception e) { |
| 62 | + System.out.println("⚠️ ERROR: Unexpected error while selecting: " + visibleText + " → " + e.getMessage()); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private static WebElement findOptionByText(Select select, String text) { |
| 67 | + List<WebElement> options = select.getOptions(); |
| 68 | + for (WebElement option : options) { |
| 69 | + if (text.equals(option.getText())) { |
| 70 | + return option; |
| 71 | + } |
| 72 | + } |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + private static boolean isVisible(WebElement element) { |
| 77 | + try { |
| 78 | + String visibility = element.getCssValue("visibility"); |
| 79 | + String display = element.getCssValue("display"); |
| 80 | + String opacity = element.getCssValue("opacity"); |
| 81 | + return !visibility.equals("hidden") && !display.equals("none") && !opacity.equals("0") && !opacity.equals("0.0"); |
| 82 | + } catch (Exception e) { |
| 83 | + return false; |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments