I stumbled upon this SO answer https://stackoverflow.com/a/10660734/2985796 and am trying to make sense of how SCRIPT_GET_ELEMENT_BORDER
and SCRIPT_UNHIGHLIGHT_ELEMENT
are instantiated.
I feel like the post would make sense to someone familiar with JS, but I sadly have never gotten my hands dirty with it. What I am trying to do is basically recreate this answer but in view of my application. What I have done is to extend Selenium's FirefoxDriver
to override the FindElement
function. In the new function a found WebElement
is highlighted and remembered. When the function is called again the remembered element has its border set to none
. Which, as the answer notes, removes the original border. I would like to keep the original border after the highlighting is completed.
Here is my class FirefoxDriverEx
so far
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FirefoxDriverEx extends FirefoxDriver
{
private WebElement LastHighlightedElement = null;
private static final String HIGHLIGHT_BOARDER_JS = "arguments[0].style.border='3px solid red'";
private static final String UNHIGHLIGHT_BOARDER_JS = "arguments[0].style.border='none'";
@Override
public WebElement findElement(By by)
{
UndoHighlight();
WebElement foundElement = super.findElement(by);
if (this instanceof JavascriptExecutor)
{
((JavascriptExecutor)this).executeScript(HIGHLIGHT_BOARDER_JS, foundElement);
LastHighlightedElement = foundElement;
}
return foundElement;
}
private void UndoHighlight()
{
if(LastHighlightedElement != null)
{
try
{
if (this instanceof JavascriptExecutor)
((JavascriptExecutor)this).executeScript(UNHIGHLIGHT_BOARDER_JS, LastHighlightedElement);
LastHighlightedElement = null;
}
catch(Exception ex) { }
}
}
}
What I would like to do is create a function GetElementBorder
that when passed a WebElement
returns a String
representing its current border. Then use it in the manner below.
public class FirefoxDriverEx extends FirefoxDriver
{
private String LastElementBorder = null;
private WebElement LastHighlightedElement = null;
private static final String HIGHLIGHT_BOARDER_JS = "arguments[0].style.border='3px solid red'";
@Override
public WebElement findElement(By by)
{
UndoHighlight();
WebElement foundElement = super.findElement(by);
if (this instanceof JavascriptExecutor)
{
((JavascriptExecutor)this).executeScript(HIGHLIGHT_BOARDER_JS, foundElement);
LastElementBorder = GetElementBorder(foundElement);
LastHighlightedElement = foundElement;
}
return foundElement;
}
private void UndoHighlight()
{
if(LastHighlightedElement != null && LastElementBorder != null)
{
try
{
String setLastBorderJS = "arguments[0].style.border='" + LastElementBorder + "'";
if (this instanceof JavascriptExecutor)
((JavascriptExecutor)this).executeScript(setLastBorderJS, LastHighlightedElement);
LastHighlightedElement = null;
LastElementBorder = null;
}
catch(Exception ex) { }
}
}
private String GetElementBorder(WebElement elem)
{
return null;
}
}
I guess under the linked answer above I do not see how the answerer goes from the JS shown under the SCRIPT_GET_ELEMENT_BORDER
to the String
representing the border?