Package org.zkoss.test.webdriver
Class BaseTestCase
- java.lang.Object
-
- org.zkoss.test.webdriver.BaseTestCase
-
- Direct Known Subclasses:
SingletonWebDriverTestCase,WebDriverTestCase
public abstract class BaseTestCase extends java.lang.ObjectA base class to test using WebDriver.Currently support Chromium headless
- Author:
- jumperchen
-
-
Field Summary
Fields Modifier and Type Field Description protected static java.lang.ThreadLocal<org.openqa.selenium.WebDriver>_localprotected org.openqa.selenium.WebDriverdriverstatic java.lang.StringPACKAGEprotected static intstatic_port
-
Constructor Summary
Constructors Constructor Description BaseTestCase()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidassertNoAnyError()Asserts no ZK and JS errorprotected voidassertNoJSError()Asserts no JavaScript errors occur.protected voidassertNoZKError()Asserts no ZK errorprotected voidblur(ClientWidget locator)Sets the blur state to the given locator.protected voidcheck(ClientWidget locator)Checks the given locator.protected voidclick(ClientWidget locator)Clicks upon the given locator.protected voidclickAt(ClientWidget locator, int offsetX, int offsetY)protected voidcloseZKLog()Closes the zk.log console and removes it.org.openqa.selenium.WebDriverconnect()org.openqa.selenium.WebDriverconnect(java.lang.String location)protected voidcopy()Press hot key (e.g.protected voidcut()Press hot key (e.g.protected voiddblClick(ClientWidget locator)Double clicks upon the given locator.protected voiddestroyServer(org.eclipse.jetty.server.Server currentServer)protected voiddisableConnectionWait()Disables the connection waiting.protected voiddragdropTo(ClientWidget locator, int fromX, int fromY, int offsetX, int offsetY)Drag the element from (x, y) (start from element center point), move the mouse by offset, and drop.protected voiddropUploadFile(JQuery element, java.nio.file.Path file)Simulates drag and drop a file to a Dropupload element.protected voiddropUploadFiles(JQuery element, java.util.List<java.nio.file.Path> files, int offsetX, int offsetY)Simulates drag and drop files to a Dropupload element.protected voidenableConnectionWait()Enables the connection waiting.static voideval(java.lang.String script)protected voidfocus(ClientWidget locator)Sets the focus state to the given locator.protected org.openqa.selenium.interactions.ActionsgetActions()Returns the browser actions.protected static java.lang.StringgetAddress()protected static java.lang.StringgetBaseResource()protected static java.lang.StringgetContextPath()static java.lang.StringgetEval(java.lang.String script)protected java.lang.StringgetFileExtension()protected java.lang.StringgetFileLocation()protected static java.lang.StringgetHost()protected java.lang.StringgetMessageBoxContent()Returns the text content of zk messagebox from client side.protected static intgetPort()protected java.lang.StringgetRemoteWebDriverUrl()protected intgetRetryCount(boolean includingAnimation)Returns the retry count for waiting for Ajax response.protected static java.lang.StringgetServerPort()protected intgetSpeed()Returns the wait response speed.static intgetStatusCode(java.lang.String url)Gets the HTTP response status code.protected java.lang.StringgetTestURL(java.lang.String file)protected intgetTimeout()protected org.openqa.selenium.WebDrivergetWebDriver()protected org.openqa.selenium.chrome.ChromeOptionsgetWebDriverOptions()Gets the WebDriver options.protected java.lang.StringgetZKLog()Returns the text of zk.log from client side.booleanhasError()protected voidinitServer(org.eclipse.jetty.server.Server currentServer)protected org.openqa.selenium.WebDriverinitWebDriver()protected booleanisHeadless()protected static booleanisMac()protected static booleanisSolaris()protected static booleanisUnix()protected booleanisUseDocker()protected booleanisUsingRemoteWebDriver(org.openqa.selenium.chrome.ChromeOptions driverOptions)protected booleanisUsingRemoteWebDriver(org.openqa.selenium.firefox.FirefoxOptions driverOptions)protected static booleanisWindows()protected booleanisZKLogAvailable()Returns the text of zk.log from client side.protected JQueryjq(java.lang.String selector)Returns the Jquery object of the selectorprotected JQueryjq(ClientWidget el)Returns the Jquery object of the ZKClientObject.protected voidmouseOver(ClientWidget locator)Hover.static intparseInt(java.lang.String number)Returns the int value from the given string number.protected voidpaste()Press hot key (e.g.protected voidrightClick(ClientWidget locator)Right clicks upon the given locator.protected voidselectAll()Trigger a select all action.protected WidgetselectComboitem(Widget combobox, int index)Selects an comboitem from the given combobox.protected voidsendKeys(ClientWidget locator, java.lang.CharSequence... keysToSend)Use this method to simulate typing into an element, which may set its value.protected voidsetCursorPosition(ClientWidget locator, int position)Moves the text cursor to the specified position in the given input element or textarea.protected voidsleep(long millis)Causes the currently executing thread to sleep for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers.voidstop()protected java.lang.StringstripJsessionid(java.lang.String url)Strips ;jsessionid= in URL because it is annoying when comparing URLs.static org.openqa.selenium.WebElementtoElement(ClientWidget locator)protected voidtrigger(ClientWidget widget, java.lang.String event)Execute all handlers and behaviors attached to the matched elements for the given event typestatic java.lang.Stringtrim(java.lang.String text)Trims the multiline string into one line string.protected voidtype(ClientWidget locator, java.lang.String text)Types the text into the given locator.protected voidwaitResponse()Waits for Ajax response.protected voidwaitResponse(boolean includingAnimation)Waits for Ajax response.protected voidwaitResponse(int timeout)Waits for Ajax response according to the timeout attribute.protected voidwaitResponse(int timeout, boolean includingAnimation)Waits for Ajax response according to the timeout attribute.protected Widgetwidget(java.lang.String uuid)Returns the Widget object of the UUID.protected Widgetwidget(Element element)Returns the Widget object of the given element.protected Widgetwidget(JQuery jQuery)Returns the Widget object from the JQuery object.protected ZKzk(java.lang.String selector)Returns the ZK object of the selectorprotected ZKzk(ClientWidget el)Returns the ZK object of the ZKClientObject.
-
-
-
Method Detail
-
getPort
protected static int getPort()
-
getContextPath
protected static java.lang.String getContextPath()
-
getHost
protected static java.lang.String getHost()
-
getAddress
protected static java.lang.String getAddress()
-
isHeadless
protected boolean isHeadless()
-
getWebDriver
protected org.openqa.selenium.WebDriver getWebDriver()
-
getRemoteWebDriverUrl
protected java.lang.String getRemoteWebDriverUrl()
-
isUseDocker
protected boolean isUseDocker()
-
isUsingRemoteWebDriver
protected boolean isUsingRemoteWebDriver(org.openqa.selenium.chrome.ChromeOptions driverOptions)
-
isUsingRemoteWebDriver
protected boolean isUsingRemoteWebDriver(org.openqa.selenium.firefox.FirefoxOptions driverOptions)
-
getWebDriverOptions
protected org.openqa.selenium.chrome.ChromeOptions getWebDriverOptions()
Gets the WebDriver options. You can add arguments for Chromium to change settings like locale or user-agent string. A list of available options can be found at Capabilities & ChromeOptions.- Returns:
- WebDriver options
-
connect
public org.openqa.selenium.WebDriver connect()
-
getTestURL
protected java.lang.String getTestURL(java.lang.String file)
-
getFileLocation
protected java.lang.String getFileLocation()
-
getFileExtension
protected java.lang.String getFileExtension()
-
connect
public org.openqa.selenium.WebDriver connect(java.lang.String location)
-
disableConnectionWait
protected void disableConnectionWait()
Disables the connection waiting.By default, it will wait for the connection to be established. In some cases, you may want to disable it, such as alert text at connect phase.
-
enableConnectionWait
protected void enableConnectionWait()
Enables the connection waiting.- See Also:
disableConnectionWait()
-
initWebDriver
protected org.openqa.selenium.WebDriver initWebDriver()
-
getTimeout
protected int getTimeout()
-
stop
@AfterEach public void stop()
-
getServerPort
protected static java.lang.String getServerPort()
-
getBaseResource
protected static java.lang.String getBaseResource()
-
initServer
protected void initServer(org.eclipse.jetty.server.Server currentServer) throws java.lang.Exception- Throws:
java.lang.Exception
-
destroyServer
protected void destroyServer(org.eclipse.jetty.server.Server currentServer)
-
waitResponse
protected void waitResponse()
Waits for Ajax response. (excluding animation check)- See Also:
waitResponse(int)
-
waitResponse
protected void waitResponse(boolean includingAnimation)
Waits for Ajax response.By default the timeout time is specified in config.properties
- Parameters:
includingAnimation- if true, it will include animation check.- See Also:
waitResponse(int, boolean)
-
getSpeed
protected int getSpeed()
Returns the wait response speed.Default: 500ms
- Returns:
-
waitResponse
protected void waitResponse(int timeout, boolean includingAnimation)Waits for Ajax response according to the timeout attribute.- Parameters:
timeout-includingAnimation- if true, it will include animation check.
-
getRetryCount
protected int getRetryCount(boolean includingAnimation)
Returns the retry count for waiting for Ajax response. Default: 3 and if including animation, 5.
-
getEval
public static java.lang.String getEval(java.lang.String script)
-
eval
public static void eval(java.lang.String script)
-
sleep
protected void sleep(long millis)
Causes the currently executing thread to sleep for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers. The thread does not lose ownership of any monitors.- Parameters:
millis- the length of time to sleep in milliseconds.
-
waitResponse
protected void waitResponse(int timeout)
Waits for Ajax response according to the timeout attribute.(excluding animation check)- Parameters:
timeout- the time. (millisecond).- See Also:
waitResponse(int, boolean)
-
widget
protected Widget widget(java.lang.String uuid)
Returns the Widget object of the UUID.- Parameters:
uuid- the element ID.
-
widget
protected Widget widget(Element element)
Returns the Widget object of the given element.- Parameters:
element- the element.
-
widget
protected Widget widget(JQuery jQuery)
Returns the Widget object from the JQuery object.- Parameters:
jQuery- the JQuery object.
-
jq
protected JQuery jq(java.lang.String selector)
Returns the Jquery object of the selectorDefault: without "#" sign
- Parameters:
selector- the selector
-
jq
protected JQuery jq(ClientWidget el)
Returns the Jquery object of the ZKClientObject.- Parameters:
el- the ZKClientObject
-
zk
protected ZK zk(ClientWidget el)
Returns the ZK object of the ZKClientObject.- Parameters:
el- the ZKClientObject
-
zk
protected ZK zk(java.lang.String selector)
Returns the ZK object of the selector- Parameters:
selector- the selector of the element
-
parseInt
public static int parseInt(java.lang.String number)
Returns the int value from the given string number.- Parameters:
number- the string number, if null or empty, 0 is assumed.
-
trigger
protected void trigger(ClientWidget widget, java.lang.String event)
Execute all handlers and behaviors attached to the matched elements for the given event type- Parameters:
widget-event-
-
toElement
public static org.openqa.selenium.WebElement toElement(ClientWidget locator)
-
trim
public static java.lang.String trim(java.lang.String text)
Trims the multiline string into one line string.
-
getZKLog
protected java.lang.String getZKLog()
Returns the text of zk.log from client side.
-
isZKLogAvailable
protected boolean isZKLogAvailable()
Returns the text of zk.log from client side.
-
closeZKLog
protected void closeZKLog()
Closes the zk.log console and removes it.
-
focus
protected void focus(ClientWidget locator)
Sets the focus state to the given locator.- Parameters:
locator-
-
blur
protected void blur(ClientWidget locator)
Sets the blur state to the given locator.- Parameters:
locator-
-
click
protected void click(ClientWidget locator)
Clicks upon the given locator.- Parameters:
locator-
-
clickAt
protected void clickAt(ClientWidget locator, int offsetX, int offsetY)
-
rightClick
protected void rightClick(ClientWidget locator)
Right clicks upon the given locator.- Parameters:
locator-
-
dblClick
protected void dblClick(ClientWidget locator)
Double clicks upon the given locator.- Parameters:
locator-
-
check
protected void check(ClientWidget locator)
Checks the given locator. It's the same asclick(ClientWidget)internally.- Parameters:
locator-
-
type
protected void type(ClientWidget locator, java.lang.String text)
Types the text into the given locator.By default, it will simulate a real user behavior to focus the input elemnt from the given locator, and then replace the old text with the new text and then blur the input element.
- Parameters:
locator-text-
-
sendKeys
protected void sendKeys(ClientWidget locator, java.lang.CharSequence... keysToSend)
Use this method to simulate typing into an element, which may set its value.- Parameters:
keysToSend- character sequence to send to the element
-
selectComboitem
protected Widget selectComboitem(Widget combobox, int index)
Selects an comboitem from the given combobox.- Parameters:
combobox-index-
-
getStatusCode
public static int getStatusCode(java.lang.String url)
Gets the HTTP response status code.- Parameters:
url- the URL- Returns:
- status code. -1 if the connection has any exception.
-
stripJsessionid
protected java.lang.String stripJsessionid(java.lang.String url)
Strips ;jsessionid= in URL because it is annoying when comparing URLs.- Parameters:
url- the original URL- Returns:
- stripped URL
-
hasError
public boolean hasError()
-
assertNoZKError
protected void assertNoZKError()
Asserts no ZK error
-
assertNoAnyError
protected void assertNoAnyError()
Asserts no ZK and JS error
-
assertNoJSError
protected void assertNoJSError()
Asserts no JavaScript errors occur. If there is any error on browser console, anAssertions.fail(String)would be raised.
-
getMessageBoxContent
protected java.lang.String getMessageBoxContent()
Returns the text content of zk messagebox from client side.
-
getActions
protected org.openqa.selenium.interactions.Actions getActions()
Returns the browser actions.
-
dropUploadFile
protected void dropUploadFile(JQuery element, java.nio.file.Path file) throws java.io.FileNotFoundException
Simulates drag and drop a file to a Dropupload element.- Parameters:
element- Dropupload elementfile- file path- Throws:
java.io.FileNotFoundException- file not found
-
dropUploadFiles
protected void dropUploadFiles(JQuery element, java.util.List<java.nio.file.Path> files, int offsetX, int offsetY) throws java.io.FileNotFoundException
Simulates drag and drop files to a Dropupload element.- Parameters:
element- Dropupload elementfiles- file pathsoffsetX- Drop offset x relative to the top/left corner of the drop area. Center if 0.offsetY- Drop offset y relative to the top/left corner of the drop area. Center if 0.- Throws:
java.io.FileNotFoundException- file not found- See Also:
- Selenium: Drag and Drop from file system to WebDriver?
-
cut
protected void cut()
Press hot key (e.g. Ctrl + X) to trigger a cut action. It's caller's responsibility to focus/select text before calling this method.
-
copy
protected void copy()
Press hot key (e.g. Ctrl + C) to trigger a copy action. It's caller's responsibility to focus/select text before calling this method.
-
paste
protected void paste()
Press hot key (e.g. Ctrl + V) to trigger a paste action. It's caller's responsibility to click/focus on a DOM node before calling this method.
-
selectAll
protected void selectAll()
Trigger a select all action. It's caller's responsibility to click/focus on a DOM node before calling this method.
-
setCursorPosition
protected void setCursorPosition(ClientWidget locator, int position)
Moves the text cursor to the specified position in the given input element or textarea.- Parameters:
locator- pointing to an input element or textareaposition- position (starts from 0)
-
mouseOver
protected void mouseOver(ClientWidget locator)
Hover.- Parameters:
locator- element
-
dragdropTo
protected void dragdropTo(ClientWidget locator, int fromX, int fromY, int offsetX, int offsetY)
Drag the element from (x, y) (start from element center point), move the mouse by offset, and drop.- Parameters:
locator- elementfromX- X (start from element center point)fromY- Y (start from element center point)offsetX- Offset X (negative means left)offsetY- Offset Y (negative means up)
-
isUnix
protected static boolean isUnix()
-
isWindows
protected static boolean isWindows()
-
isSolaris
protected static boolean isSolaris()
-
isMac
protected static boolean isMac()
-
-