Class BaseTestCase

  • Direct Known Subclasses:
    SingletonWebDriverTestCase, WebDriverTestCase

    public abstract class BaseTestCase
    extends java.lang.Object
    A 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> _local  
      protected org.openqa.selenium.WebDriver driver  
      static java.lang.String PACKAGE  
      protected static int static_port  
    • Constructor Summary

      Constructors 
      Constructor Description
      BaseTestCase()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void assertNoAnyError()
      Asserts no ZK and JS error
      protected void assertNoJSError()
      Asserts no JavaScript errors occur.
      protected void assertNoZKError()
      Asserts no ZK error
      protected void blur​(ClientWidget locator)
      Sets the blur state to the given locator.
      protected void check​(ClientWidget locator)
      Checks the given locator.
      protected void click​(ClientWidget locator)
      Clicks upon the given locator.
      protected void clickAt​(ClientWidget locator, int offsetX, int offsetY)  
      protected void closeZKLog()
      Closes the zk.log console and removes it.
      org.openqa.selenium.WebDriver connect()  
      org.openqa.selenium.WebDriver connect​(java.lang.String location)  
      protected void copy()
      Press hot key (e.g.
      protected void cut()
      Press hot key (e.g.
      protected void dblClick​(ClientWidget locator)
      Double clicks upon the given locator.
      protected void destroyServer​(org.eclipse.jetty.server.Server currentServer)  
      protected void disableConnectionWait()
      Disables the connection waiting.
      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.
      protected void dropUploadFile​(JQuery element, java.nio.file.Path file)
      Simulates drag and drop a file to a Dropupload element.
      protected void dropUploadFiles​(JQuery element, java.util.List<java.nio.file.Path> files, int offsetX, int offsetY)
      Simulates drag and drop files to a Dropupload element.
      protected void enableConnectionWait()
      Enables the connection waiting.
      static void eval​(java.lang.String script)  
      protected void focus​(ClientWidget locator)
      Sets the focus state to the given locator.
      protected org.openqa.selenium.interactions.Actions getActions()
      Returns the browser actions.
      protected org.openqa.selenium.interactions.Actions getActions​(java.time.Duration pause)
      Returns the browser actions.
      protected static java.lang.String getAddress()  
      protected static java.lang.String getBaseResource()  
      protected static java.lang.String getContextPath()  
      static java.lang.String getEval​(java.lang.String script)  
      protected java.lang.String getFileExtension()  
      protected java.lang.String getFileLocation()  
      protected static java.lang.String getHost()  
      protected java.lang.String getMessageBoxContent()
      Returns the text content of zk messagebox from client side.
      protected static int getPort()  
      protected java.lang.String getRemoteWebDriverUrl()  
      protected int getRetryCount​(boolean includingAnimation)
      Returns the retry count for waiting for Ajax response.
      protected static java.lang.String getServerPort()  
      protected int getSpeed()
      Returns the wait response speed.
      static int getStatusCode​(java.lang.String url)
      Gets the HTTP response status code.
      protected java.lang.String getTestURL​(java.lang.String file)  
      protected int getTimeout()  
      protected org.openqa.selenium.WebDriver getWebDriver()  
      protected org.openqa.selenium.chrome.ChromeOptions getWebDriverOptions()
      Gets the WebDriver options.
      protected java.lang.String getZKLog()
      Returns the text of zk.log from client side.
      boolean hasError()  
      protected void initServer​(org.eclipse.jetty.server.Server currentServer)  
      protected org.openqa.selenium.WebDriver initWebDriver()  
      protected boolean isHeadless()  
      protected static boolean isMac()  
      protected static boolean isSolaris()  
      protected static boolean isUnix()  
      protected boolean isUseDocker()  
      protected boolean isUsingRemoteWebDriver​(org.openqa.selenium.chrome.ChromeOptions driverOptions)  
      protected boolean isUsingRemoteWebDriver​(org.openqa.selenium.firefox.FirefoxOptions driverOptions)  
      protected static boolean isWindows()  
      protected boolean isZKLogAvailable()
      Returns the text of zk.log from client side.
      protected JQuery jq​(java.lang.String selector)
      Returns the Jquery object of the selector
      protected JQuery jq​(ClientWidget el)
      Returns the Jquery object of the ZKClientObject.
      protected void mouseOver​(ClientWidget locator)
      Hover.
      static int parseInt​(java.lang.String number)
      Returns the int value from the given string number.
      protected void paste()
      Press hot key (e.g.
      protected void rightClick​(ClientWidget locator)
      Right clicks upon the given locator.
      protected void selectAll()
      Trigger a select all action.
      protected Widget selectComboitem​(Widget combobox, int index)
      Selects an comboitem from the given combobox.
      protected void sendKeys​(ClientWidget locator, java.lang.CharSequence... keysToSend)
      Use this method to simulate typing into an element, which may set its value.
      protected void setCursorPosition​(ClientWidget locator, int position)
      Moves the text cursor to the specified position in the given input element or textarea.
      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.
      void stop()  
      protected java.lang.String stripJsessionid​(java.lang.String url)
      Strips ;jsessionid= in URL because it is annoying when comparing URLs.
      static org.openqa.selenium.WebElement toElement​(ClientWidget locator)  
      protected void trigger​(ClientWidget widget, java.lang.String event)
      Execute all handlers and behaviors attached to the matched elements for the given event type
      static java.lang.String trim​(java.lang.String text)
      Trims the multiline string into one line string.
      protected void type​(ClientWidget locator, java.lang.String text)
      Types the text into the given locator.
      protected void waitResponse()
      Waits for Ajax response.
      protected void waitResponse​(boolean includingAnimation)
      Waits for Ajax response.
      protected void waitResponse​(int timeout)
      Waits for Ajax response according to the timeout attribute.
      protected void waitResponse​(int timeout, boolean includingAnimation)
      Waits for Ajax response according to the timeout attribute.
      protected Widget widget​(java.lang.String uuid)
      Returns the Widget object of the UUID.
      protected Widget widget​(Element element)
      Returns the Widget object of the given element.
      protected Widget widget​(JQuery jQuery)
      Returns the Widget object from the JQuery object.
      protected ZK zk​(java.lang.String selector)
      Returns the ZK object of the selector
      protected ZK zk​(ClientWidget el)
      Returns the ZK object of the ZKClientObject.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • PACKAGE

        public static final java.lang.String PACKAGE
      • static_port

        protected static int static_port
      • _local

        protected static final java.lang.ThreadLocal<org.openqa.selenium.WebDriver> _local
      • driver

        protected org.openqa.selenium.WebDriver driver
    • Constructor Detail

      • BaseTestCase

        public BaseTestCase()
    • 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 selector

        Default: 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 -
      • 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, an Assertions.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. Starting from 1.4.39.0.1, pauses for 100 milliseconds after Actions.clickAndHold(org.openqa.selenium.WebElement).
      • getActions

        protected org.openqa.selenium.interactions.Actions getActions​(java.time.Duration pause)
        Returns the browser actions.
        Parameters:
        pause - the pause duration after Actions.clickAndHold(org.openqa.selenium.WebElement)
        Since:
        1.4.39.0.1
      • 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 element
        file - 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 element
        files - file paths
        offsetX - 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 textarea
        position - 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 - element
        fromX - 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()