public class JavaScriptFunctions
extends java.lang.Object
StringBuffer scriptCommand = new StringBuffer(); scriptCommand.append(JavaScriptFunctions.getSAFSgetElementFromXpathFunction(false)); scriptCommand.append(JavaScriptFunctions.getSAFSgetAttributeFunction(false)); scriptCommand.append(" var xpath = \""+compObject.getLocator()+"\";"); scriptCommand.append(" var prop = \""+prop+"\";"); scriptCommand.append(" SAFSgetAttribute(xpath,prop);"); rval = selenium.getEval(scriptCommand.toString());
StringBuffer scriptCommand = new StringBuffer(); scriptCommand.append(JavaScriptFunctions.getDojoClassNameByCSSSelector(true)); scriptCommand.append(" return getDojoClassNameByCSSSelector(arguments[0]);"); Object obj = ((JavascriptExecutor) webdriver).executeScript(scriptCommand.toString(), css);
StringBuffer scriptCommand = new StringBuffer(); scriptCommand.append(JavaScriptFunctions.getDojoClassNameByCSSSelector(true)); scriptCommand.append(" return getDojoClassNameByCSSSelector(arguments[0]);"); Object obj = ((JavascriptExecutor) webdriver).executeScript(scriptCommand.toString(), css);It is better to set this parameter as false to avoid dead including loop.
StringBuffer scriptCommand = new StringBuffer(); scriptCommand.append(JavaScriptFunctions.getDojoClassNameByCSSSelector(false)); //getDojoClassNameByCSSSelector() depends on getDomNodesByCSSSelector(), include it manually. scriptCommand.append(JavaScriptFunctions.getDomNodesByCSSSelector(false)); scriptCommand.append(" return getDojoClassNameByCSSSelector(arguments[0]);"); Object obj = ((JavascriptExecutor) webdriver).executeScript(scriptCommand.toString(), css);About method comments?
Modifier and Type | Class and Description |
---|---|
static class |
JavaScriptFunctions.DOJO
Define javascript functions related to DOJO.
History: Jan 21, 2014 (Lei Wang) Initial release. |
static class |
JavaScriptFunctions.GENERIC |
static class |
JavaScriptFunctions.SAP |
Modifier and Type | Field and Description |
---|---|
static boolean |
DEBUG_OUTPUT_JAVASCRIPT_FUNCTIONS
Used to turn on/off the output of javascript functions to debug log.
|
static int |
ERROR_CODE_A |
static int |
ERROR_CODE_B |
static int |
ERROR_CODE_EXCEPTION |
static int |
ERROR_CODE_NOT_SET |
static int |
ERROR_CODE_RESERVED |
static boolean |
jsDebugLogEnable
Used to enable/disable debug message output of execution of javascript.
The default value is false. |
static int |
LOG_LEVEL_DEBUG |
static int |
LOG_LEVEL_DEFAULT |
static int |
LOG_LEVEL_INFO |
static boolean |
logToBrowserConsole
Normally the debug message will be wrote to a file.
|
static java.lang.String |
PREFIX_GLOBAL |
static java.lang.String |
SAFS_JAVASCRIPT_GLOBAL_DEBUG_MESSAGES_VAR |
static java.lang.String |
SAFS_JAVASCRIPT_GLOBAL_ERROR_CODE_VAR |
static java.lang.String |
SAFS_JAVASCRIPT_GLOBAL_ERROR_OBJECT_VAR |
static java.lang.String |
SAFS_JAVASCRIPT_GLOBAL_PREVIOUS_HIGHLIGHT_ELEMENT_STYLE_VAR |
static java.lang.String |
SAFS_JAVASCRIPT_GLOBAL_PREVIOUS_HIGHLIGHT_ELEMENT_VAR |
static java.lang.String |
SUFFIX_DOJO_HANDLE |
static java.lang.String |
SUFFIX_FUNCTION |
Constructor and Description |
---|
JavaScriptFunctions() |
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
addEventListener()
Attach an event callback to an event for an object.
depending on: nothing depending level: 0 |
static java.lang.String |
addGenericEventListener(boolean includeDependency,
java.lang.String variable)
Attach a generic event callback to a javasript event for an object.
depending on: addEventListener() defineGenericEventCallBack(String) depending level: 1 |
static java.lang.String |
cleanJSError()
Clean the error code global variable, reset it to
ERROR_CODE_NOT_SET . |
static java.lang.String |
clearHighlight()
Remove the previous highlight of a DOM object.
|
static java.lang.String |
compareObject(java.util.List<java.lang.String> properties)
Compare 2 javascript objects.
|
static java.lang.String |
debug()
Used to add debug messages the global array, so that
in the java code, we can get the javascript debug messages write them to debug-Log. |
static java.lang.String |
defineObject(java.util.Hashtable<java.lang.String,java.lang.Object> properties)
Define a javascript object according to an Hashtable object.
|
static java.lang.String |
fireMouseClick()
Click on an html element, this will ONLY fire an click event, the mouse point will not be
moved to the element and click on it. depending level: 0 |
static java.lang.String |
fireMouseClickById(boolean includeDependency) |
static java.lang.String |
fireMouseEvent(MouseEvent event)
Perform a Mouse Event on an html element.
|
static java.lang.String |
fireMouseHover() |
static java.lang.String |
getAllDojoFunctions() |
static java.lang.String |
getAllFunctions() |
static java.lang.String |
getAllSAPFunctions() |
static java.lang.String |
getAttributes()
Get all attributes of a dom element.
depending level: 0 |
static java.lang.String |
getBrowserInformation()
Get the browser's information, such like height, width, clientHeight, clientWidth etc.
depending level: 0 |
static java.lang.String |
getDomElementBy()
Find a DOM element according to the parameter By.
|
protected static java.lang.String |
getElementAbsoluteXPath()
!!! UNTESTED AND LIKELY NOT WORKING AT THIS TIME !!!
|
static java.lang.String |
getGlobalVariable(java.lang.String variable)
Get the value of global variable.
|
static java.lang.String |
getHighlightFunction()
Support highlight by id, name or xpath.
|
static java.lang.String |
getHighlightFunction(boolean includeDependency) |
static java.lang.String |
getHtmlProperties()
Get standard Html properties of a dom element.
depending level: 0 |
static java.lang.String |
getJSDebugArray()
Get the global variable 'debug-message-array'.
|
static java.lang.String |
getJSErrorCode()
Get the value of error code global variable.
|
static java.lang.String |
getJSErrorObject()
Get the value of error Object global variable.
|
static java.lang.String |
getSAFSgetAttributeFunction() |
static java.lang.String |
getSAFSgetAttributeFunction(boolean includeDependency) |
static java.lang.String |
getSAFSgetElementFromXpathFunction() |
static java.lang.String |
getSAFSgetElementFromXpathFunction(boolean includeDependency) |
static java.lang.String |
highlight2()
Highlight a DOM object with a red rectangle.
|
static java.lang.String |
initializeJSArray(java.lang.StringBuffer scriptCommand,
boolean needquoted,
java.lang.String tab,
int numberOfTab,
java.lang.String... arrayValues)
Generate a javascript array filled with the values provided by Java String array.
|
static java.lang.String |
initializeJSMap(java.lang.StringBuffer scriptCommand,
boolean needquoted,
java.lang.String tab,
int numberOfTab,
java.util.Map<java.lang.String,java.lang.String> keyValueMap)
Generate a javascript map filled with the pairs (key, value) provided by Java Map
|
static java.lang.String |
initJSDebugArray()
Initialize the global variable 'debug-message-array', set it to new Array().
|
static java.lang.String |
initJSError()
Initialize the error code global variable, set it value to
ERROR_CODE_NOT_SET . |
static java.lang.String |
initPreviousHighlightElement()
Initialize the variables related to highlight element, initialize it value to undefined.
|
static void |
main(java.lang.String[] args) |
static java.lang.String |
objectIsInstanceof()
Test if object is an instance of certain classes.
depending level: 0 |
static java.lang.String |
removeEventListener()
Detach an event callback to an event from an object.
depending on: nothing depending level: 0 |
static java.lang.String |
removeGenericEventListener(boolean includeDependency,
java.lang.String variable)
Detach a generic event callback to a javasript event from an object.
depending on: removeEventListener() removeGlobalVariable(String) depending level: 1 |
static java.lang.String |
removeGlobalVariable(java.lang.String variable)
Remove the global variable, set it to undefined.
|
static java.lang.String |
scrollBy()
Scroll the browser page to a location by increment.
|
static java.lang.String |
scrollByLines()
Scroll the browser scroll up/down by lines.
|
static java.lang.String |
scrollByPages()
Scroll the browser page up/down by pages.
|
static java.lang.String |
scrollIntoView()
Scroll to show the dom element in the browser's viewport.
depending on: nothing. depending level: 0 |
static java.lang.String |
scrollTo()
Scroll the browser page to a location.
|
static java.lang.String |
sendHttpRequest(java.util.Map<java.lang.String,java.lang.String> requestHeaders)
Perform a HttpRequest by AJAX XMLHttpRequest.
NOTE: As this is performed by "AJAX XMLHttpRequest", it is normally not permitted to request an URL of domain other than the Application under test. This is the common cross-domain problem. depending on: initializeJSMap(StringBuffer, boolean, String, int, Map)
depending level: 1 |
static java.lang.String |
setGlobalVariable(java.lang.String variable,
java.lang.String value)
Set the value of global variable.
|
static java.lang.String |
setJavaScriptLogLevel(int logLevel)
Set javascript's program log level
|
static void |
setJsDebugLogEnable(boolean enable) |
static java.lang.String |
setJSErrorCode(int errorCode)
Set the value of the error code global variable.
|
static java.lang.String |
setJSErrorObject(java.lang.String errorObject)
Set the value of the error Object global variable.
|
static java.lang.String |
throw_error()
Used to set the global "error code" and "error object", so that
in the java code, we can detect the global errorcode and errorobject to do some necessary work. |
public static boolean DEBUG_OUTPUT_JAVASCRIPT_FUNCTIONS
public static boolean jsDebugLogEnable
public static boolean logToBrowserConsole
public static final java.lang.String SAFS_JAVASCRIPT_GLOBAL_ERROR_OBJECT_VAR
public static final java.lang.String SAFS_JAVASCRIPT_GLOBAL_ERROR_CODE_VAR
public static final java.lang.String SAFS_JAVASCRIPT_GLOBAL_DEBUG_MESSAGES_VAR
public static final int ERROR_CODE_NOT_SET
public static final int ERROR_CODE_RESERVED
public static final int ERROR_CODE_EXCEPTION
public static final int ERROR_CODE_A
public static final int ERROR_CODE_B
public static final java.lang.String SAFS_JAVASCRIPT_GLOBAL_PREVIOUS_HIGHLIGHT_ELEMENT_VAR
public static final java.lang.String SAFS_JAVASCRIPT_GLOBAL_PREVIOUS_HIGHLIGHT_ELEMENT_STYLE_VAR
public static final java.lang.String PREFIX_GLOBAL
public static final java.lang.String SUFFIX_FUNCTION
public static final java.lang.String SUFFIX_DOJO_HANDLE
public static final int LOG_LEVEL_INFO
public static final int LOG_LEVEL_DEBUG
public static final int LOG_LEVEL_DEFAULT
public static void setJsDebugLogEnable(boolean enable)
public static java.lang.String getSAFSgetElementFromXpathFunction()
public static java.lang.String getSAFSgetElementFromXpathFunction(boolean includeDependency)
includeDependency
- boolean, if true the output string will contain the depending javascript functions.public static java.lang.String getHighlightFunction()
public static java.lang.String getHighlightFunction(boolean includeDependency)
includeDependency
- boolean, if true the output string will contain the depending javascript functions.xpathOrIdOrName
- (Javascript) String, used to search a DOM object.public static java.lang.String highlight2()
element
- (Javascript) DOM OBJECTpublic static java.lang.String scrollTo()
x
- (Javascript) int, the x coordinates to scroll to.y
- (Javascript) int, the y coordinates to scroll to.public static java.lang.String scrollBy()
x
- (Javascript) int, the increment of x coordinates to scroll.y
- (Javascript) int, the increment of y coordinates to scroll.public static java.lang.String scrollByLines()
lines
- (Javascript) int, the number of lines to scroll.public static java.lang.String scrollByPages()
pages
- (Javascript) int, the number of pages to scroll.public static java.lang.String scrollIntoView()
domobject
- (Javascript) DOM object, the dom object to show.alignToTop
- (Javascript) boolean, true the top of the element will align the top of viewport of browser.
false the bottom of the element will align the bottom of viewport of browser.public static java.lang.String clearHighlight()
public static java.lang.String getSAFSgetAttributeFunction()
public static java.lang.String getSAFSgetAttributeFunction(boolean includeDependency)
includeDependency
- boolean, if true the output string will contain the depending javascript functions.public static java.lang.String initPreviousHighlightElement()
public static java.lang.String initJSError()
ERROR_CODE_NOT_SET
. public static java.lang.String initJSDebugArray()
public static java.lang.String getJSDebugArray()
initJSDebugArray()
MUST have been executed.
initJSDebugArray()
public static java.lang.String cleanJSError()
ERROR_CODE_NOT_SET
. public static java.lang.String getJSErrorCode()
public static java.lang.String setJSErrorCode(int errorCode)
public static java.lang.String getJSErrorObject()
public static java.lang.String setJSErrorObject(java.lang.String errorObject)
errorObject
- String, the javascript variable containing the error Object.
//"error" is a javascript variable containing an error object.
String errorObjectJSVarName = "error";
JavaScriptFunctions.setJSErrorObject(errorObjectJSVarName);
String errorMessage = "The real error message";
JavaScriptFunctions.setJSErrorObject(StringUtils.quote(errorMessage));
public static java.lang.String throw_error()
How to use it in javascript: try{ //do some javascript actions }catch(err){ throw_error(err); } An actual example, refer toJavaScriptFunctions.SAP.sap_m_List_getItems(boolean)
How to catch this error in java: //Reset the global error (code and message) SearchObject.js_cleanError(); //Append the method throw_error() to the existing javascript code script = JavaScriptFunctions.throw_error()+script; //Execute javascript code ... //Detect the error code/object int errorcode = js_getErrorCode(); if(errorcode!=JavaScriptFunctions.ERROR_CODE_NOT_SET){ Object error = SearchObject.js_getErrorObject(); throw JSException.instance(error, errorcode); } An actual example, refer toSearchObject.executeScript(boolean, String, Object...)
error
- (Javascript) Object, the javascript exception Error object.public static java.lang.String debug()
How to use it in javascript:
//do some javascript actions
debug('the parameter is XXX.');
//do some other javascript actions
debug('something is wrong.');
How to get these debug messages in java:
//Reset the global debug message array, and include the debug() method.
SearchObject.js_initJSDebugArray();
try{
//Append the method debug() to the existing javascript code
script = JavaScriptFunctions.debug()+script;
//Then execute the javascript code
...
}catch(Exception e){
}finally{
List> messages = js_getJSDebugArray();
for(Object message: messages){
IndependantLog.debug(message);
}
}
An actual example, refer to SearchObject.executeScript(boolean, String, Object...)
message
- (Javascript) String, the javascript exception Error object.public static java.lang.String removeGlobalVariable(java.lang.String variable)
variable
- String, the name of the global variable.genGlobalVariableName(String)
public static java.lang.String getGlobalVariable(java.lang.String variable)
variable
- String, the name of the global variable.genGlobalVariableName(String)
public static java.lang.String setGlobalVariable(java.lang.String variable, java.lang.String value)
variable
- String, the name of the global variable.value
- String, the direct-value to set; or the name of an other javascript object.genGlobalVariableName(String)
public static java.lang.String defineObject(java.util.Hashtable<java.lang.String,java.lang.Object> properties)
StringBuffer jsScript = new StringBuffer(); Hashtablehash = new Hashtable (); hash.put("value", "AR"); hash.put("attribute1", "Hello"); hash.put("attribute2", new Date()); jsScript.append(JavaScriptFunctions.defineObject(hash)); jsScript.append("var hash = defineObject();"); jsScript.append("//write javascript code to handle hash object."); WDLibrary.executeScript(jsScript.toString());
properties
- Hashtable, contains public static java.lang.String compareObject(java.util.List<java.lang.String> properties)
StringBuffer jsScript = new StringBuffer(); Listproperties = new List (); properties.add("value"); properties.add("id"); properties.add("name"); jsScript.append(JavaScriptFunctions.compareObject(properties)); //object1 and object2 are javascript objects. jsScript.append("return compareObject(object1, object2);"); boolean equaled = WDLibrary.executeScript(jsScript.toString());
properties
- List, the property names to get value to compare between 2 objects.object1
- (Javascript) Object, the javascript object to compareobject2
- (Javascript) Object, the javascript object to comparepublic static java.lang.String objectIsInstanceof()
object
- (Javascript) Object, the javascript object.clazzes
- (Javascript) Arraypublic static java.lang.String getDomElementBy()
By
- (Javascript) Object, the javascript object to be clicked.public static java.lang.String fireMouseClick()
element
- (Javascript) Object, the javascript object to be clicked.public static java.lang.String fireMouseEvent(MouseEvent event)
element
- (Javascript) Object, the javascript object (WebElement -- event.EVENT_TARGET) to be clicked.pview
- (Javascript) Object, the javascript object (event.EVENT_VIEW) that is the parent view or window of the target.public static java.lang.String fireMouseHover()
public static java.lang.String fireMouseClickById(boolean includeDependency)
public static java.lang.String addEventListener()
domElement
- (Javascript) Object, the DOM object.event
- (Javascript) String, the event name, like 'click', 'blur' or 'mousedown' etc.callback
- (Javascript) Object, the 'event callback' function.useCapture
- (Javascript) boolean, If true, useCapture indicates that the user wishes to initiate capture.public static java.lang.String addGenericEventListener(boolean includeDependency, java.lang.String variable)
addEventListener()
defineGenericEventCallBack(String)
includeDependency
- boolean, if true, will return the depended js as part of result.variable
- String, the javascript global variable name, used by the 'generic event callback'.domNode
- (Javascript) Object (WebElement), used to find a SAP object.eventname
- (Javascript) String, the event name, like 'click', 'blur' or 'mousedown' etc.useCapture
- (Javascript) boolean, If true, useCapture indicates that the user wishes to initiate capture.public static java.lang.String removeEventListener()
domElement
- (Javascript) Object, the DOM object.event
- (Javascript) String, the event name, like 'click', 'blur' or 'mousedown' etc.callback
- (Javascript) Object, the 'event callback' function.useCapture
- (Javascript) boolean, Specifies whether the EventListener being removed was registered as a capturing listener or not.public static java.lang.String removeGenericEventListener(boolean includeDependency, java.lang.String variable)
removeEventListener()
removeGlobalVariable(String)
includeDependency
- boolean, if true, will return the depended js as part of result.variable
- String, the javascript global variable name, used by the 'generic event callback'.domNode
- (Javascript) Object (WebElement), used to find a SAP object.eventname
- (Javascript) String, the event name, like 'click', 'blur' or 'mousedown' etc.useCapture
- (Javascript) boolean, Specifies whether the EventListener being removed was registered as a capturing listener or not.public static java.lang.String getBrowserInformation()
browserObject
- (Javascript) Object, the javascript object representing a browser object.SearchObject.BrowserWindow.PROPERTY_CLIENT_HEIGHT
SearchObject.BrowserWindow.PROPERTY_CLIENT_LOCATION_Y
SearchObject.BrowserWindow.PROPERTY_CLIENT_LOCATION_X
SearchObject.BrowserWindow.PROPERTY_CLIENT_WIDTH
SearchObject.BrowserWindow.PROPERTY_HEIGHT
SearchObject.BrowserWindow.PROPERTY_LOCATION_X
SearchObject.BrowserWindow.PROPERTY_LOCATION_Y
SearchObject.BrowserWindow.PROPERTY_WIDTH
SearchObject.BrowserWindow.PROPERTY_PAGE_X_OFFSET
SearchObject.BrowserWindow.PROPERTY_PAGE_Y_OFFSET
SearchObject.BrowserWindow.PROPERTY_BORDER_WIDTH
SearchObject.BrowserWindow.PROPERTY_HEADER_HEIGHT
SearchObject.BrowserWindow.PROPERTY_MAXIMIZED
public static java.lang.String getAttributes()
public static java.lang.String getHtmlProperties()
public static java.lang.String sendHttpRequest(java.util.Map<java.lang.String,java.lang.String> requestHeaders)
initializeJSMap(StringBuffer, boolean, String, int, Map)
requestHeaders
- Mappublic static java.lang.String setJavaScriptLogLevel(int logLevel)
logLevel
- public static java.lang.String initializeJSArray(java.lang.StringBuffer scriptCommand, boolean needquoted, java.lang.String tab, int numberOfTab, java.lang.String... arrayValues)
scriptCommand
- StringBuffer, the string buffer to store the javascript commandneedquoted
- boolean, if the value needs to be quoted before setting to the arraytab
- String, the tab string to calculate the indent before each command in the string buffernumberOfTab
- int, the number of tab to calculate the indent before each command in the string bufferarrayValues
- String..., an array of value#sap_ComboBox_getItems(boolean)
public static java.lang.String initializeJSMap(java.lang.StringBuffer scriptCommand, boolean needquoted, java.lang.String tab, int numberOfTab, java.util.Map<java.lang.String,java.lang.String> keyValueMap)
needquoted
- boolean, if the pair(key,value) needs to be quoted before setting to the maptab
- String, the tab string to calculate the indent before each command in the string buffernumberOfTab
- int, the number of tab to calculate the indent before each command in the string bufferkeyValueMap
- Mappublic static java.lang.String getAllFunctions()
public static java.lang.String getAllDojoFunctions()
public static java.lang.String getAllSAPFunctions()
protected static java.lang.String getElementAbsoluteXPath()
public static void main(java.lang.String[] args)
Copyright © SAS Institute. All Rights Reserved.