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    /// ```no_run
16    /// use viewpoint_core::BrowserContext;
17    ///
18    /// # async fn example(context: &BrowserContext) -> Result<(), viewpoint_core::CoreError> {
19    /// // Use data-test instead of data-testid
20    /// context.set_test_id_attribute("data-test").await;
21    ///
22    /// // Now get_by_test_id looks for data-test attribute
23    /// let page = context.new_page().await?;
24    /// let button = page.get_by_test_id("submit"); // looks for [data-test="submit"]
25    /// # Ok(())
26    /// # }
27    /// ```
28    pub async fn set_test_id_attribute(&self, name: impl Into<String>) {
29        let mut attr = self.test_id_attribute.write().await;
30        *attr = name.into();
31    }
32
33    /// Get the current test ID attribute name.
34    ///
35    /// Returns the attribute name used by `get_by_test_id()` (default: `data-testid`).
36    pub async fn test_id_attribute(&self) -> String {
37        self.test_id_attribute.read().await.clone()
38    }
39
40    /// Get the test ID attribute synchronously (for internal use).
41    pub(crate) fn test_id_attribute_blocking(&self) -> String {
42        // Use try_read to avoid blocking; fall back to default if lock is held
43        self.test_id_attribute.try_read().map_or_else(
44            |_| DEFAULT_TEST_ID_ATTRIBUTE.to_string(),
45            |guard| guard.clone(),
46        )
47    }
48}