05. Highlighting element in selenium webdriver & Handling Unexpected alert

package Others;
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.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class HighlightTest
{
            WebDriver driver;
            @BeforeTest
            public void setup() throws Exception
            {
                        System.setProperty("webdriver.chrome.driver", "D:\\Softwares\\chromedriver_win32\\chromedriver.exe");   
                        driver = new ChromeDriver();
                       
                        driver.manage().window().maximize();
                       
                        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
                        driver.get("http://only-testing-blog.blogspot.in/2014/06/alert_6.html");
            }
            @AfterTest
            public void tearDown() throws Exception
            {
                        driver.quit();
            }
            @Test
            public void Text() throws InterruptedException
            {
                        //To handle unexpected alert on page load.
                        try
                        {
                                    driver.switchTo().alert().dismiss();
                        }
                        catch(Exception e)
                        {
                                    System.out.println("unexpected alert not present");
                        }
                        HighlightMyElement(driver.findElement(By.xpath("//input[@name='fname']")));
                        driver.findElement(By.xpath("//input[@name='fname']")).sendKeys("fname");
                        HighlightMyElement(driver.findElement(By.xpath("//input[@name='lname']")));
                        driver.findElement(By.xpath("//input[@name='lname']")).sendKeys("lname");
                        driver.findElement(By.xpath("//input[@type='submit']")).click();
            }
           
            public void HighlightMyElement(WebElement element) {
                        for (int i = 0; i < 10; i++) {
                                    JavascriptExecutor js = (JavascriptExecutor) driver;
                                    js.executeScript(
                                                            "arguments[0].setAttribute('style', arguments[1]);",
                                                            element, "color: red; border: 3px solid yellow;");
                                    js.executeScript(
                                                            "arguments[0].setAttribute('style', arguments[1]);",
                                                            element, "");
                        }
            }

}


2 comments:

  1. can you please explain what HighlightMyElement method functionality is .. and how it is working

    ReplyDelete
    Replies
    1. In Automation, testing sometime element highlighter plays very important role. It help us to track our execution flow which step is being processed.

      Delete