use anyhow::{Context, Result};
use super::finite;
use crate::{TestDriver, TestElement};
impl TestDriver {
pub async fn set_style(
&self,
element: &TestElement,
property: &str,
value: &str,
) -> Result<()> {
tracing::trace!(property, value, "Setting an inline style property");
self.inner
.execute(
"arguments[0].style.setProperty(arguments[1], arguments[2]);",
vec![
element.inner.to_json()?,
serde_json::json!(property),
serde_json::json!(value),
],
)
.await?;
Ok(())
}
pub async fn set_class(&self, element: &TestElement, class: &str, on: bool) -> Result<()> {
tracing::trace!(class, on, "Forcing a class");
self.inner
.execute(
"arguments[0].classList.toggle(arguments[1], arguments[2]);",
vec![
element.inner.to_json()?,
serde_json::json!(class),
serde_json::json!(on),
],
)
.await?;
Ok(())
}
pub async fn overflowing(&self, element: &TestElement) -> Result<Vec<String>> {
tracing::trace!("Finding what overflows inside an element");
let script = "const out = []; \
for (const el of [arguments[0], ...arguments[0].querySelectorAll('*')]) { \
if (el.clientHeight <= 1 || el.clientWidth <= 1) continue; \
const cs = getComputedStyle(el); \
const y = el.scrollHeight - el.clientHeight; \
const x = el.scrollWidth - el.clientWidth; \
const shows = v => v === 'auto' || v === 'scroll' || v === 'hidden' || v === 'clip'; \
if ((y > 1 && shows(cs.overflowY)) || (x > 1 && shows(cs.overflowX))) { \
const test = el.getAttribute('data-test'); \
out.push(el.localName + (el.className ? '.' + String(el.className).trim().split(/\\s+/).join('.') : '') \
+ (test ? `[data-test=${test}]` : '') \
+ ` ${cs.overflowX}/${cs.overflowY} over by ${x}x${y}`); \
} \
} \
return out;";
let found = self
.inner
.execute(script, vec![element.inner.to_json()?])
.await?;
Ok(found
.json()
.as_array()
.into_iter()
.flatten()
.filter_map(|line| line.as_str().map(str::to_owned))
.collect())
}
pub async fn small_text(
&self,
element: &TestElement,
floor: f64,
marks: &str,
) -> Result<Vec<String>> {
tracing::trace!(floor, marks, "Finding text set below a size");
let floor = finite("a text size floor", floor)?;
let script = "const out = []; \
for (const el of [arguments[0], ...arguments[0].querySelectorAll('*')]) { \
if (el.closest(arguments[1])) continue; \
const words = Array.from(el.childNodes) \
.filter(n => n.nodeType === Node.TEXT_NODE) \
.map(n => n.textContent.trim()).join(' ').trim(); \
if (!words) continue; \
const rect = el.getBoundingClientRect(); \
if (rect.width <= 1 || rect.height <= 1) continue; \
const cs = getComputedStyle(el); \
if (cs.visibility !== 'visible') continue; \
const size = parseFloat(cs.fontSize); \
if (size < arguments[2]) { \
const test = el.getAttribute('data-test'); \
out.push(el.localName + (el.className ? '.' + String(el.className).trim().split(/\\s+/).join('.') : '') \
+ (test ? `[data-test=${test}]` : '') \
+ ` at ${cs.fontSize}: ${JSON.stringify(words.slice(0, 32))}`); \
} \
} \
return out;";
let found = self
.inner
.execute(
script,
vec![element.inner.to_json()?, serde_json::json!(marks), floor],
)
.await?;
Ok(found
.json()
.as_array()
.into_iter()
.flatten()
.filter_map(|line| line.as_str().map(str::to_owned))
.collect())
}
pub async fn computed(
&self,
element: &TestElement,
pseudo: &str,
property: &str,
) -> Result<String> {
tracing::trace!(pseudo, property, "Asking what a property computed to");
let script = "return getComputedStyle(arguments[0], arguments[1] || null) \
.getPropertyValue(arguments[2]);";
let value = self
.inner
.execute(
script,
vec![
element.inner.to_json()?,
serde_json::json!(pseudo),
serde_json::json!(property),
],
)
.await?;
Ok(value.json().as_str().unwrap_or_default().trim().to_string())
}
pub async fn reduced_motion(&self) -> Result<bool> {
let value = self
.inner
.execute(
"return matchMedia('(prefers-reduced-motion: reduce)').matches;",
Vec::new(),
)
.await?;
let reduced = value
.json()
.as_bool()
.context("reduced-motion media query")?;
if let Ok(requested) = std::env::var("E2E_MOTION") {
anyhow::ensure!(
reduced == (requested == "reduce"),
"browser did not honor E2E_MOTION={requested}"
);
}
Ok(reduced)
}
pub async fn animation_time(
&self,
element: &TestElement,
name: &str,
millis: f64,
) -> Result<()> {
self.inner.execute(
"const animation = arguments[0].getAnimations().find(a => a.animationName === arguments[1]); \
if (!animation) throw new Error('Missing animation: ' + arguments[1]); \
animation.pause(); animation.currentTime = arguments[2];",
vec![element.inner.to_json()?, serde_json::json!(name), serde_json::json!(millis)],
).await?;
Ok(())
}
pub async fn color_alpha(&self, element: &TestElement, property: &str) -> Result<f64> {
let value = self.inner.execute(
"const canvas = document.createElement('canvas'); canvas.width = canvas.height = 1; \
const context = canvas.getContext('2d'); context.fillStyle = 'transparent'; \
context.fillStyle = getComputedStyle(arguments[0]).getPropertyValue(arguments[1]); \
context.fillRect(0, 0, 1, 1); return context.getImageData(0, 0, 1, 1).data[3] / 255;",
vec![element.inner.to_json()?, serde_json::json!(property)],
).await?;
value.json().as_f64().context("computed color alpha")
}
pub async fn color_shift(&self, element: &TestElement, property: &str) -> Result<f64> {
let [red, green, blue] = self.painted(element, property).await?;
Ok((red + green + blue) / 3.0 - 128.0)
}
pub async fn painted(&self, element: &TestElement, property: &str) -> Result<[f64; 3]> {
let value = self.inner.execute(
"const canvas = document.createElement('canvas'); canvas.width = canvas.height = 1; \
const context = canvas.getContext('2d'); \
context.fillStyle = '#808080'; context.fillRect(0, 0, 1, 1); \
context.fillStyle = 'transparent'; \
context.fillStyle = getComputedStyle(arguments[0]).getPropertyValue(arguments[1]); \
context.fillRect(0, 0, 1, 1); \
const painted = context.getImageData(0, 0, 1, 1).data; \
return [painted[0], painted[1], painted[2]];",
vec![element.inner.to_json()?, serde_json::json!(property)],
).await?;
let read = value.json();
let read = read.as_array().context("what a computed colour paints")?;
let mut rgb = [0.0; 3];
for (channel, value) in rgb.iter_mut().zip(read) {
*channel = value.as_f64().context("a painted channel")?;
}
Ok(rgb)
}
}