Verifying a label and wanted to hightlight with a color

I wanted to verify a label on the web page and highlight with a color after verifying the text. For that I created a class called HighlightElement under keyword. not sure how to call this in the element.

WebUI.verifyElementPresent(findTestObject(‘ENF123 Submissions/h2_ENF-123 Book Wagering Cover Sheet Submissions’), 0)

please help

It’s usually something like:

//Assuming your keyword is declared static ...
import static your_package.your_class.*

//... Stuff here

highlightElement(element)
// Or
highlightElement(testObject)

But without seeing how you wrote it, it’s hard to say more.

sure…here is the code

public class HighlightElement {
static WebDriver driver = DriverFactory.getWebDriver()

@Keyword

public static void run(TestObject objectto) {

	try {

		WebElement element = WebUiCommonHelper.findWebElement(objectto, 20);

		for (int i = 0; i < 5; i++) {



			JavascriptExecutor js = (JavascriptExecutor) driver;

			js.executeScript("arguments[0].setAttribute('style','background: yellow; border: 5px solid red;');",

					element);
		}
	} catch (Exception e) {

		Assert.fail(e.getMessage());
	}
}

}

Try this:

public class Highlight {
  
  /**
   * Highlight an element for five seconds using JavaScript.
   * @param selector (String) The CSS selector for the element.
   */
  @Keyword
  public static void highlightElement(String selector) {
    // Triple-quoted string containing JavaScript
    String js = '''
      function highlightElement(selector) {
        $(selector).css("box-shadow", "1px 1px 4px 4px yellow, -1px -1px 4px 4px yellow");
        setTimeout(function() {
          $(selector).css("box-shadow", "none");
        }, 5000);
      }

      highlightElement(selector);
    '''
    // execute the JavaScript
    WebUI.executeJavaScript(js, Arrays.asList(selector))
  }
}

Notes:

  1. You don’t need to use @Keyword if you never use Manual View.
  2. I believe a method called run has special meaning in script classes - I changed it to highlightElement and named the class Highlight.
  3. This method does not need Webdriver. It uses JavaScript to directly target the element using a suitable CSS selector.
  4. Note the JavaScript is a multi-line string surrounded by triple quotes - ''' and '''

Example call:

import static package_name.Highlight.*

//highlight the element with id="username"
highlightElement("#username")

Thanks Russ for sharing the code…but no luck
Error:
Submissions FAILED.
Reason:
java.lang.Error: Unresolved compilation problem:

at reusableComponents.Highlight.highlightElement(HighlightElement.groovy:41)

Moreover I am trying to highlight the labels and which don’t have the ids other than the CSS class…in this case how do we need to call the method?

Thanks

Let me see more of the error.

issue got fixed now and able to highlight the labels

CustomKeywords.‘packagename.classname.run’(findTestObject(‘ObjectName’))