viewpoint_core/context/test_id/mod.rs
1//! Test ID attribute configuration for BrowserContext.
2//!
3//! This module provides methods to configure the test ID attribute used by locators.
4
5use super::{BrowserContext, DEFAULT_TEST_ID_ATTRIBUTE};
6
7impl BrowserContext {
8 /// Set the custom test ID attribute for this context.
9 ///
10 /// By default, `get_by_test_id()` uses the `data-testid` attribute.
11 /// Call this method to use a different attribute name.
12 ///
13 /// # Example
14 ///
15 /// ```ignore
16 /// // Use data-test instead of data-testid
17 /// context.set_test_id_attribute("data-test").await;
18 ///
19 /// // Now get_by_test_id looks for data-test attribute
20 /// let page = context.new_page().await?;
21 /// let button = page.get_by_test_id("submit"); // looks for [data-test="submit"]
22 /// ```
23 pub async fn set_test_id_attribute(&self, name: impl Into<String>) {
24 let mut attr = self.test_id_attribute.write().await;
25 *attr = name.into();
26 }
27
28 /// Get the current test ID attribute name.
29 ///
30 /// Returns the attribute name used by `get_by_test_id()` (default: `data-testid`).
31 pub async fn test_id_attribute(&self) -> String {
32 self.test_id_attribute.read().await.clone()
33 }
34
35 /// Get the test ID attribute synchronously (for internal use).
36 pub(crate) fn test_id_attribute_blocking(&self) -> String {
37 // Use try_read to avoid blocking; fall back to default if lock is held
38 self.test_id_attribute
39 .try_read().map_or_else(|_| DEFAULT_TEST_ID_ATTRIBUTE.to_string(), |guard| guard.clone())
40 }
41}