1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.Test; public class elementHighlighter { @Test public void elementHighlighterCode(){ //Creating reference variable of Webdriver WebDriver driver; // Setting up the properties for Chrome Driver System.setProperty("webdriver.chrome.driver", "C:\\Vision\\chromedriver.exe"); // Inserting Chromedriver to Webdriver refernece object driver = new ChromeDriver(); // Providing wait to load all the elements on page driver.manage().timeouts().implicitlyWait(10000, TimeUnit.MILLISECONDS); // Maximizing the browser window driver.manage().window().maximize(); // Passing the URL driver.get("http://www.facebook.com"); // Inspect element //Alloting Username field's Element locator - id to "username" veriable WebElement username= driver.findElement(By.id("email")); //Highlighting the Username Text box Field highLightElement(driver,username); //Sending the emailID to username text box username.sendKeys("123@gmail.com"); //Alloting Password field's Element locator id to "password" veriable WebElement password= driver.findElement(By.id("pass")); //Highlighting the Username Text box Field highLightElement(driver,password); //Sending the emailID to password text box password.sendKeys("123@gmail.com"); //Alloting Login_button field's Element locator id to "login_btn" veriable WebElement login_btn= driver.findElement(By.id("u_0_8")); //Highlighting the Login_button Field highLightElement(driver,login_btn); // Clicking on Login_button login_btn.click(); } // Element highlighter Method Code with two Arguments as "driver and WebElement to be highlighted" public static void highLightElement(WebDriver driver, WebElement element) { //Creating javascript reference variable JavascriptExecutor js=(JavascriptExecutor)driver; //Setting up the Style as Background Yellow and Border to be 2Pixel width in Red Colour for WebElement js.executeScript("arguments[0].setAttribute('style', 'background: yellow; border: 2px solid red;');", element); try { //Wait for JavaScript to be visible in slow Thread.sleep(1000); } catch (InterruptedException e) { //Catch if anyexception occure during runtime System.out.println(e.getMessage()); } //Changing the WebElement Properties as original Attributes //As Border Yellow to White Colour with 2Pixel Border Size js.executeScript("arguments[0].setAttribute('style','border: solid 2px white');", element); } } |