Skip to main content

allwright/
client_locator.rs

1use super::selectors::chain_selector_for_transport;
2use super::types::{
3    ClickResult, CommandOptions, CountResult, ElementResult, FillResult, HighlightOptions,
4    HighlightResult, Locator, Page, PressOptions, PressResult, Result, TextResult,
5    WaitForSelectorOptions, WaitForSelectorResult,
6};
7
8impl Locator {
9    pub fn page(&self) -> &Page {
10        &self.page
11    }
12
13    pub fn selector(&self) -> &str {
14        &self.selector
15    }
16
17    pub fn locator(&self, css_selector: impl Into<String>) -> Locator {
18        let child_selector = css_selector.into();
19        Locator {
20            page: self.page.clone(),
21            selector: chain_selector_for_transport(&self.selector, &child_selector),
22        }
23    }
24
25    pub async fn click(&self) -> Result<ClickResult> {
26        self.page.click(self.selector.clone()).await
27    }
28
29    pub async fn click_with_options(&self, options: CommandOptions) -> Result<ClickResult> {
30        self.page
31            .click_with_options(self.selector.clone(), options)
32            .await
33    }
34
35    pub async fn count(&self) -> Result<CountResult> {
36        self.page.count(self.selector.clone()).await
37    }
38
39    pub async fn count_with_options(&self, options: CommandOptions) -> Result<CountResult> {
40        self.page
41            .count_with_options(self.selector.clone(), options)
42            .await
43    }
44
45    pub async fn highlight(&self) -> Result<HighlightResult> {
46        self.page.highlight(self.selector.clone()).await
47    }
48
49    pub async fn highlight_with_options(
50        &self,
51        options: HighlightOptions,
52    ) -> Result<HighlightResult> {
53        self.page
54            .highlight_with_options(self.selector.clone(), options)
55            .await
56    }
57
58    pub async fn focus(&self) -> Result<ElementResult> {
59        self.page.focus(self.selector.clone()).await
60    }
61
62    pub async fn focus_with_options(&self, options: CommandOptions) -> Result<ElementResult> {
63        self.page
64            .focus_with_options(self.selector.clone(), options)
65            .await
66    }
67
68    pub async fn fill(&self, value: impl Into<String>) -> Result<FillResult> {
69        self.page.fill(self.selector.clone(), value.into()).await
70    }
71
72    pub async fn fill_with_options(
73        &self,
74        value: impl Into<String>,
75        options: CommandOptions,
76    ) -> Result<FillResult> {
77        self.page
78            .fill_with_options(self.selector.clone(), value.into(), options)
79            .await
80    }
81
82    pub async fn hover(&self) -> Result<ElementResult> {
83        self.page.hover(self.selector.clone()).await
84    }
85
86    pub async fn hover_with_options(&self, options: CommandOptions) -> Result<ElementResult> {
87        self.page
88            .hover_with_options(self.selector.clone(), options)
89            .await
90    }
91
92    pub async fn press(&self, key: impl Into<String>) -> Result<PressResult> {
93        self.page.press(self.selector.clone(), key.into()).await
94    }
95
96    pub async fn press_with_options(
97        &self,
98        key: impl Into<String>,
99        options: PressOptions,
100    ) -> Result<PressResult> {
101        self.page
102            .press_with_options(self.selector.clone(), key.into(), options)
103            .await
104    }
105
106    pub async fn text_content(&self) -> Result<TextResult> {
107        self.page.text_content(self.selector.clone()).await
108    }
109
110    pub async fn text_content_with_options(&self, options: CommandOptions) -> Result<TextResult> {
111        self.page
112            .text_content_with_options(self.selector.clone(), options)
113            .await
114    }
115
116    pub async fn inner_text(&self) -> Result<TextResult> {
117        self.page.inner_text(self.selector.clone()).await
118    }
119
120    pub async fn inner_text_with_options(&self, options: CommandOptions) -> Result<TextResult> {
121        self.page
122            .inner_text_with_options(self.selector.clone(), options)
123            .await
124    }
125
126    pub async fn wait_for(&self) -> Result<WaitForSelectorResult> {
127        self.wait_for_with_options(WaitForSelectorOptions {
128            visible: Some(true),
129            ..Default::default()
130        })
131        .await
132    }
133
134    pub async fn wait_for_with_options(
135        &self,
136        options: WaitForSelectorOptions,
137    ) -> Result<WaitForSelectorResult> {
138        let mut options = options;
139        if options.visible.is_none() {
140            options.visible = Some(true);
141        }
142        self.page
143            .wait_for_selector_with_options(self.selector.clone(), options)
144            .await
145    }
146}