Skip to content

Instantly share code, notes, and snippets.

@diegomarino
Last active March 29, 2025 13:33
Show Gist options
  • Select an option

  • Save diegomarino/8f887028ac35fafc1d10b253bbc246b1 to your computer and use it in GitHub Desktop.

Select an option

Save diegomarino/8f887028ac35fafc1d10b253bbc246b1 to your computer and use it in GitHub Desktop.
bugbug.io check element values
async function(element, selector, variables) {
/**
* πŸ‘‡ USAGE EXAMPLES (can be commented out):
*
* await checkColor(element, "rgb(0, 128, 128)");
* await checkFontWeight(element, "600");
* await checkVisibility(element);
* await checkFontSize(element, "16px");
* await checkTextContent(element, "Trello Clone");
*/
console.log("πŸ” Selector received:", selector);
console.log("πŸ“¦ Variables received:", variables);
console.log("πŸ“Œ Element received:", element);
const computedStyle = window.getComputedStyle(element);
console.log("πŸ§ͺ Full computed style:", computedStyle);
// Example: check color using a variable or fallback
const expectedColor = variables.expectedColor || "rgb(0, 128, 128)";
await checkColor(element, expectedColor);
// Add more checks as needed
// await checkFontWeight(element, "600");
// await checkVisibility(element);
return true; // If all checks pass
/**
* πŸ“£ checkColor: Checks if the computed text color matches the expected one.
*/
async function checkColor(el, expectedColor) {
console.log("πŸ“£ [checkColor] Running color check...");
const color = getComputedStyle(el).color;
console.log("🎨 [checkColor] Computed color:", color);
console.log("βœ… [checkColor] Expected color:", expectedColor);
if (color === expectedColor) {
console.log("🟒 [checkColor] Color matches.");
} else {
throw new Error(`πŸ”΄ [checkColor] Color mismatch. Expected: ${expectedColor}, got: ${color}`);
}
}
/**
* πŸ“£ checkFontWeight: Checks if the font weight matches the expected value.
*/
async function checkFontWeight(el, expectedWeight) {
console.log("πŸ“£ [checkFontWeight] Running font-weight check...");
const weight = getComputedStyle(el).fontWeight;
console.log("πŸ”€ [checkFontWeight] Computed font-weight:", weight);
console.log("βœ… [checkFontWeight] Expected font-weight:", expectedWeight);
if (weight === expectedWeight) {
console.log("🟒 [checkFontWeight] Font-weight matches.");
} else {
throw new Error(`πŸ”΄ [checkFontWeight] Font-weight mismatch. Expected: ${expectedWeight}, got: ${weight}`);
}
}
/**
* πŸ“£ checkVisibility: Checks if the element is visible (not hidden or display:none).
*/
async function checkVisibility(el) {
console.log("πŸ“£ [checkVisibility] Running visibility check...");
const style = getComputedStyle(el);
const visible = style.display !== "none" && style.visibility !== "hidden" && style.opacity !== "0";
console.log("πŸ‘οΈ [checkVisibility] Display:", style.display, "| Visibility:", style.visibility, "| Opacity:", style.opacity);
if (visible) {
console.log("🟒 [checkVisibility] Element is visible.");
} else {
throw new Error(`πŸ”΄ [checkVisibility] Element is not visible.`);
}
}
/**
* πŸ“£ checkFontSize: Checks the font size.
*/
async function checkFontSize(el, expectedSize) {
console.log("πŸ“£ [checkFontSize] Running font-size check...");
const size = getComputedStyle(el).fontSize;
console.log("πŸ”  [checkFontSize] Computed font-size:", size);
console.log("βœ… [checkFontSize] Expected font-size:", expectedSize);
if (size === expectedSize) {
console.log("🟒 [checkFontSize] Font-size matches.");
} else {
throw new Error(`πŸ”΄ [checkFontSize] Font-size mismatch. Expected: ${expectedSize}, got: ${size}`);
}
}
/**
* πŸ“£ checkTextContent: Checks the inner text content of the element.
*/
async function checkTextContent(el, expectedText) {
console.log("πŸ“£ [checkTextContent] Running text content check...");
const actualText = el.textContent.trim();
console.log("πŸ“ [checkTextContent] Actual text content:", actualText);
console.log("βœ… [checkTextContent] Expected text content:", expectedText);
if (actualText === expectedText) {
console.log("🟒 [checkTextContent] Text content matches.");
} else {
throw new Error(`πŸ”΄ [checkTextContent] Text content mismatch. Expected: "${expectedText}", got: "${actualText}"`);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment