Skip to main content

ferridriver_script/bindings/
frame_locator.rs

1//! `FrameLocatorJs` -- QuickJS wrapper for
2//! [`ferridriver::locator::FrameLocator`].
3//!
4//! Mirrors Playwright's `FrameLocator` interface
5//! (`/tmp/playwright/packages/playwright-core/types/types.d.ts` —
6//! search `interface FrameLocator`). Construct via `page.frameLocator`,
7//! `frame.frameLocator`, `locator.contentFrame`, or
8//! `locator.frameLocator`.
9
10use ferridriver::locator::FrameLocator;
11use rquickjs::function::Opt;
12use rquickjs::{JsLifetime, class::Trace};
13
14use super::locator::LocatorJs;
15use super::page::{parse_role_options, parse_text_options, string_or_regex_from_js};
16
17#[derive(JsLifetime, Trace)]
18#[rquickjs::class(rename = "FrameLocator")]
19pub struct FrameLocatorJs {
20  #[qjs(skip_trace)]
21  pub(crate) inner: FrameLocator,
22}
23
24impl FrameLocatorJs {
25  #[must_use]
26  pub fn new(inner: FrameLocator) -> Self {
27    Self { inner }
28  }
29}
30
31#[rquickjs::methods]
32impl FrameLocatorJs {
33  #[qjs(rename = "locator")]
34  pub fn locator(&self, selector: String) -> LocatorJs {
35    LocatorJs::new(self.inner.locator(&selector, None))
36  }
37
38  #[qjs(rename = "getByRole")]
39  pub fn get_by_role(&self, role: String, options: Opt<rquickjs::Value<'_>>) -> rquickjs::Result<LocatorJs> {
40    let opts = parse_role_options(options)?;
41    Ok(LocatorJs::new(self.inner.get_by_role(&role, &opts)))
42  }
43
44  #[qjs(rename = "getByText")]
45  pub fn get_by_text(
46    &self,
47    text: rquickjs::Value<'_>,
48    options: Opt<rquickjs::Value<'_>>,
49  ) -> rquickjs::Result<LocatorJs> {
50    let t = string_or_regex_from_js(text)?;
51    let opts = parse_text_options(options);
52    Ok(LocatorJs::new(self.inner.get_by_text(&t, &opts)))
53  }
54
55  #[qjs(rename = "getByLabel")]
56  pub fn get_by_label(
57    &self,
58    text: rquickjs::Value<'_>,
59    options: Opt<rquickjs::Value<'_>>,
60  ) -> rquickjs::Result<LocatorJs> {
61    let t = string_or_regex_from_js(text)?;
62    let opts = parse_text_options(options);
63    Ok(LocatorJs::new(self.inner.get_by_label(&t, &opts)))
64  }
65
66  #[qjs(rename = "getByPlaceholder")]
67  pub fn get_by_placeholder(
68    &self,
69    text: rquickjs::Value<'_>,
70    options: Opt<rquickjs::Value<'_>>,
71  ) -> rquickjs::Result<LocatorJs> {
72    let t = string_or_regex_from_js(text)?;
73    let opts = parse_text_options(options);
74    Ok(LocatorJs::new(self.inner.get_by_placeholder(&t, &opts)))
75  }
76
77  #[qjs(rename = "getByAltText")]
78  pub fn get_by_alt_text(
79    &self,
80    text: rquickjs::Value<'_>,
81    options: Opt<rquickjs::Value<'_>>,
82  ) -> rquickjs::Result<LocatorJs> {
83    let t = string_or_regex_from_js(text)?;
84    let opts = parse_text_options(options);
85    Ok(LocatorJs::new(self.inner.get_by_alt_text(&t, &opts)))
86  }
87
88  #[qjs(rename = "getByTitle")]
89  pub fn get_by_title(
90    &self,
91    text: rquickjs::Value<'_>,
92    options: Opt<rquickjs::Value<'_>>,
93  ) -> rquickjs::Result<LocatorJs> {
94    let t = string_or_regex_from_js(text)?;
95    let opts = parse_text_options(options);
96    Ok(LocatorJs::new(self.inner.get_by_title(&t, &opts)))
97  }
98
99  #[qjs(rename = "getByTestId")]
100  pub fn get_by_test_id(&self, test_id: rquickjs::Value<'_>) -> rquickjs::Result<LocatorJs> {
101    let t = string_or_regex_from_js(test_id)?;
102    Ok(LocatorJs::new(self.inner.get_by_test_id(&t)))
103  }
104
105  #[qjs(rename = "owner")]
106  pub fn owner(&self) -> LocatorJs {
107    LocatorJs::new(self.inner.owner())
108  }
109
110  #[qjs(rename = "frameLocator")]
111  pub fn frame_locator(&self, selector: String) -> FrameLocatorJs {
112    FrameLocatorJs::new(self.inner.frame_locator(&selector))
113  }
114
115  #[qjs(rename = "first")]
116  pub fn first(&self) -> FrameLocatorJs {
117    FrameLocatorJs::new(self.inner.first())
118  }
119
120  #[qjs(rename = "last")]
121  pub fn last(&self) -> FrameLocatorJs {
122    FrameLocatorJs::new(self.inner.last())
123  }
124
125  #[qjs(rename = "nth")]
126  pub fn nth(&self, index: i32) -> FrameLocatorJs {
127    FrameLocatorJs::new(self.inner.nth(index))
128  }
129}