WebDriver application example (java) - highlight the currently operated page element

        In the testing process, because all operations are performed automatically by the program, sometimes it is difficult to see which element is being operated, or it is necessary to check the specific operation element during the debugging process. Therefore, using the method of highlighting the operated page elements can prompt the tester which elements on the page to operate normally, and improve the efficiency in the testing process.

        Specific examples are as follows:

package cn.om.webdriverapi;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;

public class TestHighLightWebElement {

	WebDriver driver;
	String url;
	JavascriptExecutor jsExecutor;

	@Test
	public void testHighLightWebElement() {
		driver.get(url);
		jsExecutor=(JavascriptExecutor)driver;
		WebElement input = driver.findElement(By.id("query"));
		WebElement submitbut = driver.findElement(By.id("stb"));

		highlightElement(input);
		input.sendKeys("test");
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
		}
		returnNormal(input);
		
		highlightElement(submitbut);
		submitbut.click();
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
		}
		returnNormal(submitbut);
	}

	public void highlightElement(WebElement element) {
		jsExecutor.executeScript("arguments[0].setAttribute('style',arguments[1]);", element,"background:yellow;border:2px solid red");
		
	}
	
	public void returnNormal(WebElement element){
		jsExecutor.executeScript("arguments[0].removeAttribute('style');", element);

	}

	@BeforeMethod
	public void beforeMethod() {
		System.setProperty("webdriver.firefox.bin", "D:/Mozilla Firefox/firefox.exe");
		driver = new FirefoxDriver();
		url = "http://www.sogou.com";
	}

	@AfterMethod
	public void afterMethod() {
		driver.quit();
	}

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325770900&siteId=291194637