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 frame(&self) -> Result<Page> {
26        self.frame_with_options(CommandOptions::default()).await
27    }
28
29    pub async fn frame_with_options(&self, options: CommandOptions) -> Result<Page> {
30        self.page
31            .frame_with_options(self.selector.clone(), options)
32            .await
33    }
34
35    pub async fn click(&self) -> Result<ClickResult> {
36        self.page.click(self.selector.clone()).await
37    }
38
39    pub async fn click_with_options(&self, options: CommandOptions) -> Result<ClickResult> {
40        self.page
41            .click_with_options(self.selector.clone(), options)
42            .await
43    }
44
45    pub async fn count(&self) -> Result<CountResult> {
46        self.page.count(self.selector.clone()).await
47    }
48
49    pub async fn count_with_options(&self, options: CommandOptions) -> Result<CountResult> {
50        self.page
51            .count_with_options(self.selector.clone(), options)
52            .await
53    }
54
55    pub async fn highlight(&self) -> Result<HighlightResult> {
56        self.page.highlight(self.selector.clone()).await
57    }
58
59    pub async fn highlight_with_options(
60        &self,
61        options: HighlightOptions,
62    ) -> Result<HighlightResult> {
63        self.page
64            .highlight_with_options(self.selector.clone(), options)
65            .await
66    }
67
68    pub async fn focus(&self) -> Result<ElementResult> {
69        self.page.focus(self.selector.clone()).await
70    }
71
72    pub async fn focus_with_options(&self, options: CommandOptions) -> Result<ElementResult> {
73        self.page
74            .focus_with_options(self.selector.clone(), options)
75            .await
76    }
77
78    pub async fn fill(&self, value: impl Into<String>) -> Result<FillResult> {
79        self.page.fill(self.selector.clone(), value.into()).await
80    }
81
82    pub async fn fill_with_options(
83        &self,
84        value: impl Into<String>,
85        options: CommandOptions,
86    ) -> Result<FillResult> {
87        self.page
88            .fill_with_options(self.selector.clone(), value.into(), options)
89            .await
90    }
91
92    pub async fn hover(&self) -> Result<ElementResult> {
93        self.page.hover(self.selector.clone()).await
94    }
95
96    pub async fn hover_with_options(&self, options: CommandOptions) -> Result<ElementResult> {
97        self.page
98            .hover_with_options(self.selector.clone(), options)
99            .await
100    }
101
102    pub async fn press(&self, key: impl Into<String>) -> Result<PressResult> {
103        self.page.press(self.selector.clone(), key.into()).await
104    }
105
106    pub async fn press_with_options(
107        &self,
108        key: impl Into<String>,
109        options: PressOptions,
110    ) -> Result<PressResult> {
111        self.page
112            .press_with_options(self.selector.clone(), key.into(), options)
113            .await
114    }
115
116    pub async fn text_content(&self) -> Result<TextResult> {
117        self.page.text_content(self.selector.clone()).await
118    }
119
120    pub async fn text_content_with_options(&self, options: CommandOptions) -> Result<TextResult> {
121        self.page
122            .text_content_with_options(self.selector.clone(), options)
123            .await
124    }
125
126    pub async fn inner_text(&self) -> Result<TextResult> {
127        self.page.inner_text(self.selector.clone()).await
128    }
129
130    pub async fn inner_text_with_options(&self, options: CommandOptions) -> Result<TextResult> {
131        self.page
132            .inner_text_with_options(self.selector.clone(), options)
133            .await
134    }
135
136    pub async fn wait_for(&self) -> Result<WaitForSelectorResult> {
137        self.wait_for_with_options(WaitForSelectorOptions {
138            visible: Some(true),
139            ..Default::default()
140        })
141        .await
142    }
143
144    pub async fn wait_for_with_options(
145        &self,
146        options: WaitForSelectorOptions,
147    ) -> Result<WaitForSelectorResult> {
148        let mut options = options;
149        if options.visible.is_none() {
150            options.visible = Some(true);
151        }
152        self.page
153            .wait_for_selector_with_options(self.selector.clone(), options)
154            .await
155    }
156}