Skip to main content

playwright_rs/protocol/
select_option.rs

1// Select option variants for dropdown selection
2//
3// Provides different ways to select options: by value, label, or index.
4
5/// Select option variant
6///
7/// Represents different ways to select an option in a `<select>` element.
8///
9/// # Example
10///
11/// ```ignore
12/// use playwright_rs::protocol::SelectOption;
13///
14/// // Select by value
15/// let opt = SelectOption::Value("option1".to_string());
16///
17/// // Select by label (visible text)
18/// let opt = SelectOption::Label("First Option".to_string());
19///
20/// // Select by index (0-based)
21/// let opt = SelectOption::Index(0);
22/// ```
23///
24/// See: <https://playwright.dev/docs/api/class-locator#locator-select-option>
25#[derive(Debug, Clone, PartialEq)]
26pub enum SelectOption {
27    /// Select by option value attribute
28    Value(String),
29    /// Select by option label (visible text)
30    Label(String),
31    /// Select by option index (0-based)
32    Index(usize),
33}
34
35impl SelectOption {
36    /// Convert SelectOption to JSON format for protocol
37    ///
38    /// The JSON format matches Playwright's protocol:
39    /// - Value: `{"value": "..."}`
40    /// - Label: `{"label": "..."}`
41    /// - Index: `{"index": 0}`
42    pub(crate) fn to_json(&self) -> serde_json::Value {
43        match self {
44            SelectOption::Value(v) => serde_json::json!({"value": v}),
45            SelectOption::Label(l) => serde_json::json!({"label": l}),
46            SelectOption::Index(i) => serde_json::json!({"index": i}),
47        }
48    }
49}
50
51// Implement From<&str> for convenience - treats string as value
52impl From<&str> for SelectOption {
53    fn from(value: &str) -> Self {
54        SelectOption::Value(value.to_string())
55    }
56}
57
58// Implement From<String> for convenience - treats string as value
59impl From<String> for SelectOption {
60    fn from(value: String) -> Self {
61        SelectOption::Value(value)
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68
69    #[test]
70    fn test_select_option_value() {
71        let opt = SelectOption::Value("test-value".to_string());
72        let json = opt.to_json();
73
74        assert_eq!(json["value"], "test-value");
75        assert!(json["label"].is_null());
76        assert!(json["index"].is_null());
77    }
78
79    #[test]
80    fn test_select_option_label() {
81        let opt = SelectOption::Label("Test Label".to_string());
82        let json = opt.to_json();
83
84        assert_eq!(json["label"], "Test Label");
85        assert!(json["value"].is_null());
86        assert!(json["index"].is_null());
87    }
88
89    #[test]
90    fn test_select_option_index() {
91        let opt = SelectOption::Index(2);
92        let json = opt.to_json();
93
94        assert_eq!(json["index"], 2);
95        assert!(json["value"].is_null());
96        assert!(json["label"].is_null());
97    }
98
99    #[test]
100    fn test_from_str() {
101        let opt: SelectOption = "my-value".into();
102        assert_eq!(opt, SelectOption::Value("my-value".to_string()));
103    }
104
105    #[test]
106    fn test_from_string() {
107        let opt: SelectOption = String::from("my-value").into();
108        assert_eq!(opt, SelectOption::Value("my-value".to_string()));
109    }
110}