diff --git a/.github/workflows/build-docker.yml b/.github/workflows/build-docker.yml index 7c5090750b6..f4c97ebc7a2 100644 --- a/.github/workflows/build-docker.yml +++ b/.github/workflows/build-docker.yml @@ -176,13 +176,13 @@ jobs: ${{ github.workspace }}/HPCC-Platform/.github/workflows/timeoutcmd if-no-files-found: error - - name: Upload UI Test Files + - name: Upload ECL Watch UI Test Files if: ${{ inputs.upload-package == true }} uses: actions/upload-artifact@v3 with: - name: ${{ inputs.asset-name }}-ui_test-files + name: ${{ inputs.asset-name }}-ecl_watch_ui_tests path: | - ${{ github.workspace }}/HPCC-Platform/esp/src/test-ui/**/* + ${{ github.workspace }}/HPCC-Platform/esp/src/test-ui/tests/**/* if-no-files-found: error - name: Upload Error Logs diff --git a/.github/workflows/test-ui-gh_runner.yml b/.github/workflows/test-ui-gh_runner.yml index ed1fad9bc21..ef58ffb92e5 100644 --- a/.github/workflows/test-ui-gh_runner.yml +++ b/.github/workflows/test-ui-gh_runner.yml @@ -17,7 +17,10 @@ on: type: string description: 'Dependencies' required: false - default: 'bison flex build-essential binutils-dev curl lsb-release libcppunit-dev python3-dev default-jdk r-base-dev r-cran-rcpp r-cran-rinside r-cran-inline libtool autotools-dev automake git cmake xmlstarlet' + default: 'bison flex build-essential binutils-dev curl lsb-release libcppunit-dev python3-dev r-base-dev r-cran-rcpp r-cran-rinside r-cran-inline libtool autotools-dev automake git cmake xmlstarlet' + +env: + uploadArtifact: false jobs: @@ -33,17 +36,17 @@ jobs: - name: Download UI Test Files uses: actions/download-artifact@v3 with: - name: ${{ inputs.asset-name }}-ui_test-files - path: ${{ inputs.asset-name }}-ui_test-files + name: ${{ inputs.asset-name }}-ecl_watch_ui_tests + path: ${{ inputs.asset-name }}-ecl_watch_ui_tests - name: Check ECLWatch UI Test Directory id: check run: | - if [[ ! -d ${{ inputs.asset-name }}-ui_test-files ]] + if [[ ! -d ${{ inputs.asset-name }}-ecl_watch_ui_tests ]] then - echo "ECLWatch UI ${{ inputs.asset-name }}-ui_test-files directory missing." + echo "ECLWatch UI ${{ inputs.asset-name }}-ecl_watch_ui_tests directory missing." else - javaFilesCount=$(find ${{ inputs.asset-name }}-ui_test-files/ -iname '*.java' -type f -print | wc -l ) + javaFilesCount=$(find ${{ inputs.asset-name }}-ecl_watch_ui_tests/ -iname '*.java' -type f -print | wc -l ) echo "Number of test java files is $javaFilesCount" if [[ ${javaFilesCount} -eq 0 ]] then @@ -66,7 +69,6 @@ jobs: unzip \ xvfb \ libxi6 \ - default-jdk \ gdb \ ${{ inputs.dependencies }} @@ -95,7 +97,7 @@ jobs: chmod +x ./${{ inputs.asset-name }}-support-files/* sudo cp ./${{ inputs.asset-name }}-support-files/* /opt/HPCCSystems/bin - chmod +x ./${{ inputs.asset-name }}-ui_test-files/* + chmod +x ./${{ inputs.asset-name }}-ecl_watch_ui_tests/* - name: Start HPCC-Platform shell: "bash" @@ -134,36 +136,57 @@ jobs: if: steps.check.outputs.runtests shell: "bash" run: | + sudo apt remove -y openjdk-11-jdk + sudo apt autoremove -y + sudo apt update + sudo apt install -y openjdk-21-jdk + sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-21-openjdk-amd64/bin/java 1 + sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/java-21-openjdk-amd64/bin/javac 1 + sudo update-alternatives --set java /usr/lib/jvm/java-21-openjdk-amd64/bin/java + sudo update-alternatives --set javac /usr/lib/jvm/java-21-openjdk-amd64/bin/javac + export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64/bin/java + export PATH=$PATH:$JAVA_HOME/bin wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb sudo apt-get install -y ./google-chrome-stable_current_amd64.deb - wget https://chromedriver.storage.googleapis.com/2.41/chromedriver_linux64.zip - unzip chromedriver_linux64.zip - sudo mv chromedriver /usr/bin/chromedriver + wget https://storage.googleapis.com/chrome-for-testing-public/126.0.6478.126/linux64/chromedriver-linux64.zip + unzip chromedriver-linux64.zip -d chromedriver + sudo mv chromedriver/chromedriver-linux64/chromedriver /usr/bin/chromedriver sudo chown root:root /usr/bin/chromedriver sudo chmod +x /usr/bin/chromedriver - wget https://selenium-release.storage.googleapis.com/3.141/selenium-server-standalone-3.141.59.jar - wget http://www.java2s.com/Code/JarDownload/testng/testng-6.8.7.jar.zip - unzip testng-6.8.7.jar.zip - - - name: Run Tests - timeout-minutes: 10 # generous, current runtime is ~1min, this should be increased if new tests are added + wget https://repo1.maven.org/maven2/org/testng/testng/7.7.1/testng-7.7.1.jar + wget https://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-annotations/2.17.0/jackson-annotations-2.17.0.jar + wget https://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-core/2.17.0/jackson-core-2.17.0.jar + wget https://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-databind/2.17.0/jackson-databind-2.17.0.jar + wget https://repo1.maven.org/maven2/com/beust/jcommander/1.82/jcommander-1.82.jar + wget https://github.com/SeleniumHQ/selenium/releases/download/selenium-4.22.0/selenium-java-4.22.0.zip + wget https://repo1.maven.org/maven2/org/slf4j/slf4j-api/1.7.30/slf4j-api-1.7.30.jar + wget https://repo1.maven.org/maven2/org/slf4j/slf4j-simple/1.7.30/slf4j-simple-1.7.30.jar + unzip selenium-java-4.22.0.zip -d selenium-libs + + - name: Run ECL Watch UI Tests + timeout-minutes: 80 # generous, current runtime is ~38 minutes, this should be increased if new tests are added if: steps.check.outputs.runtests shell: "bash" run: | - export CLASSPATH=".:${{ github.workspace }}/selenium-server-standalone-3.141.59.jar:${{ github.workspace }}/testng-6.8.7.jar" - pushd ${{ inputs.asset-name }}-ui_test-files - ./run.sh tests http://localhost:8010 > eclWatchUiTest.log 2>&1 - retCode=$? - echo "UI test done" - [[ $retCode -ne 0 ]] && exit 1 + export CLASSPATH=".:${{ inputs.asset-name }}-ecl_watch_ui_tests:${{ github.workspace }}/selenium-libs/*:${{ github.workspace }}/testng-7.7.1.jar:${{ github.workspace }}/jackson-annotations-2.17.0.jar:${{ github.workspace }}/jackson-core-2.17.0.jar:${{ github.workspace }}/jackson-databind-2.17.0.jar:${{ github.workspace }}/jcommander-1.82.jar:${{ github.workspace }}/slf4j-api-1.7.30.jar:${{ github.workspace }}/slf4j-simple-1.7.30.jar" + pushd ${{ inputs.asset-name }}-ecl_watch_ui_tests + find . -iname '*.java' -type f -print -exec javac -Xlint:none {} \; + java framework.TestRunner -l detail -p /home/runner/HPCCSystems-regression/log/ + echo "ECL Watch UI test done" + lines=$(wc -l < error_ecl_test.log) + [[ $lines -ne 0 ]] && exit 1 + if [[ -f debug_ecl_test.log || -f detail_ecl_test.log || -f exception_ecl_test.log ]] + then + echo "uploadArtifact=true" >> $GITHUB_ENV + fi popd - - name: eclwatch-ui-test-logs-artifact - if: ${{ failure() || cancelled() }} + - name: Upload ECL Watch UI Test Logs To Artifact + if: ${{ failure() || cancelled() || env.uploadArtifact == 'true' }} uses: actions/upload-artifact@v3 with: - name: ${{ inputs.asset-name }}-ui_test-logs + name: ${{ inputs.asset-name }}-ecl_watch_ui_tests path: | - ${{ inputs.asset-name }}-ui_test-files/eclWatchUiTest.log + ${{ inputs.asset-name }}-ecl_watch_ui_tests/*.log /home/runner/HPCCSystems-regression/log/*.json if-no-files-found: error diff --git a/esp/src/test-ui/tests/Activities.java b/esp/src/test-ui/tests/Activities.java index d160ae8f60c..35686098d3e 100644 --- a/esp/src/test-ui/tests/Activities.java +++ b/esp/src/test-ui/tests/Activities.java @@ -24,7 +24,7 @@ public static void main(String[] args) throws IOException, InterruptedException Capabilities caps = ((RemoteWebDriver) driver).getCapabilities(); String browserName = caps.getBrowserName(); - String browserVersion = caps.getVersion(); + //String browserVersion = caps.getVersion(); // System.out.println(browserName+" "+browserVersion); driver.get(args[0]); diff --git a/esp/src/test-ui/tests/framework/README.md b/esp/src/test-ui/tests/framework/README.md new file mode 100644 index 00000000000..c90ae71b7d6 --- /dev/null +++ b/esp/src/test-ui/tests/framework/README.md @@ -0,0 +1,65 @@ +### Project: An Automated ECL Watch Test Suite + +This project's code begins with the TestRunner.java file. The main method in this class loads all the Java classes +created for writing test cases for specific web pages of the ECL Watch UI and then runs the tests in those classes +sequentially. + +The names of the Java classes that the TestRunner class needs to load should be listed in the config/TestClasses.java +file. ActivitiesTest class should always be the first class to load in TestClasses.java, as it gets URLs for all other web pages. + +Each Java class created to write tests for specific web pages should have at least one method annotated with @Test. The +code for each class starts to run from this method. + +#### Important Note: ChromeDriver Version Compatibility + +If the Chrome browser version updates in the future, it's crucial to ensure that the corresponding ChromeDriver version is also updated. Failure to do so may cause tests to fail due to compatibility issues between the browser and driver. Always verify and update ChromeDriver to the latest version whenever running tests to maintain compatibility and ensure smooth test execution. + +#### CLI Arguments for TestRunner.java + +While running the test suite, you can pass arguments in this way -> "-l log_level -p path". +- "log_level" is of two types "debug" and "detail" +- "debug" means generate error and exception log file with a debug log file. +- "detail" means generate error and exception log file with a detailed debug file. +- If no -l and log_level is passed in the argument, only error and exception log will be generated +- "path" is the path of the folder where the json files are +- The code will log an error if the '-p' and 'path' arguments are not provided, as the JSON folder path is required for the test suite. +- -h in the CLI arguments prints the details of parameter usage to the console + +path could be something like: + +for GitHub Actions -> /home/runner/HPCCSystems-regression/log/ + +for local machine -> C:/Users/{your_working_directory_of_json_files}/ + +So an example of complete CLI arguments would look like this: + +-l detail -p /home/runner/HPCCSystems-regression/log/ + +#### Implementation Steps for URL Management + +- A HashMap (urlMap) is created to store URL mappings in config/URLConfig.java file. This map will use the page name as the key and a URLMapping object as the value. The URLMapping object contains the page name, its URL, and another HashMap for nested pages and tabs. +- A static block is used to initialize the urlMap with the initial URL mapping for the Activities navigation. The URL is retrieved using a method from the Common utility class, which handles the dynamic retrieval of the IP address based on the environment whether it is local or GitHub Actions. +- For each main navigation section, a URLMapping object is created. This object includes the page name and its corresponding URL. Additionally, it contains another HashMap to store URLs for nested tabs and pages. +- Each URLMapping object is stored in the urlMap with the main navigation name as the key. This initial setup in the Activities.java class ensures that each navigation section has its base URL stored and accessible. +- For instance, for any navigation page, each page has multiple tabs, and within those tabs, there are multiple pages and tabs. This structure facilitates easy access to the URL of a particular page. +- Starting from the Activities page, for each main navigation section, the code iterates over its associated tabs. For each tab, a new URLMapping object is created and added to the HashMap within the corresponding URLMapping object of the main navigation section. This creates a tree-like structure, allowing easy access to URLs for both navigation sections and their nested tabs. +- By following these implementation steps, the URLConfig class ensures that all URLs within the application are well-organized and easily accessible through a hierarchical structure. This setup simplifies navigation and URL management within the application, making it easier to handle complex page structures and dynamic URL retrievals. + + +Below are the dependencies used in the project: + +- https://repo1.maven.org/maven2/org/testng/testng/7.7.1/testng-7.7.1.jar +- https://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-annotations/2.17.0/jackson-annotations-2.17.0.jar +- https://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-core/2.17.0/jackson-core-2.17.0.jar +- https://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-databind/2.17.0/jackson-databind-2.17.0.jar +- https://repo1.maven.org/maven2/com/beust/jcommander/1.82/jcommander-1.82.jar +- https://github.com/SeleniumHQ/selenium/releases/download/selenium-4.22.0/selenium-java-4.22.0.zip +- https://repo1.maven.org/maven2/org/slf4j/slf4j-api/1.7.30/slf4j-api-1.7.30.jar +- https://repo1.maven.org/maven2/org/slf4j/slf4j-simple/1.7.30/slf4j-simple-1.7.30.jar + +Notes: +1. Users need to run these tests with regression test suite only. +2. Code should be updated accordingly if selenium server jar updates. +3. ActivitiesTest class should always be the first class to load in TestClasses.java, as it gets URLs for all other pages. +4. For future testing developers, custom class names or attributes defined by UI developers can change frequently during updates or redesigns. However, standard attributes that are part of the HTML specifications (such as id, type, value, href, aria-sort, aria-disabled, etc.) are much more stable. Therefore, it is advisable to use only standard HTML attributes to access web elements. This approach ensures that test cases remain consistent and are less likely to break due to UI changes. +5. Ignore the compiler warnings/errors before the beginning of the test logs. They are because of guava-33.2.1-jre-sources.jar, it seems it is not fully compatible with JRE 21, that is installed on GH Actions. But that does not impact our code in any way, so it is better to just ignore it. \ No newline at end of file diff --git a/esp/src/test-ui/tests/framework/SetupForDev.md b/esp/src/test-ui/tests/framework/SetupForDev.md new file mode 100644 index 00000000000..19b1ca119fe --- /dev/null +++ b/esp/src/test-ui/tests/framework/SetupForDev.md @@ -0,0 +1,139 @@ +This documentation provides a comprehensive guide to setting up an Ubuntu VM on Oracle VirtualBox, installing the HPCC-Platform, and preparing the environment for testing and development. + +#### Setting Up an Ubuntu VM on Oracle VirtualBox + +**Overview of the installation process on a Windows machine:** + +1. **Download and install Oracle VirtualBox:** + - Visit the Oracle VirtualBox [website](https://www.virtualbox.org/) and download the latest version for Windows. + - Follow the installation instructions to install VirtualBox on your system. + +2. **Download the Ubuntu 22.04 Desktop ISO file:** + - Go to the Ubuntu [downloads page](https://ubuntu.com/download/desktop) and download the Ubuntu 22.04 LTS ISO file. + +3. **Set up a new VM in VirtualBox:** + - Open VirtualBox and click on `New` to create a new virtual machine. + - Name the VM and select the type and version (Linux, Ubuntu 64-bit). + - Configure system settings such as memory size and hard disk (create a virtual hard disk now). + - Assign multiple CPUs to your VM if your host machine has a multi-core processor. A good starting point is to allocate 2-4 CPUs to the VM. + - Allocate memory based on the number of CPUs assigned. A good rule of thumb is to assign at least 1 GB of RAM per CPU. If you allocate 2 CPUs, assign at least 2 GB of RAM. For 4 CPUs, assign at least 4 GB of RAM, and so on. Depending on the tasks you'll be performing on the VM, you might need to allocate more memory. + - Link the ISO file by going to the `Settings` of the VM, navigating to `Storage`, and attaching the ISO file to the optical drive. + - Boot the VM and follow the installation wizard to complete the Ubuntu setup. + +#### Installing HPCC-Platform on the VM + +**After successfully installing the VM, proceed with installing the HPCC-Platform:** + +1. **Download the HPCC-Platform package:** + + Use the below command in your VM terminal to download the latest release: + ```sh + wget {url of the latest hpcc release} + ``` + For example: + ```sh + wget https://cdn.hpccsystems.com/releases/CE-Candidate-9.8.x/bin/platform/hpccsystems-platform-community_9.8.x-1jammy_amd64_withsymbols.deb + ``` + +2. **Install the package:** + + Use the below command in your VM terminal to install the version you downloaded above: + ```sh + sudo dpkg -i {the version that you downloaded above} + ``` + For example: + ```sh + sudo dpkg -i hpccsystems-platform-community_9.8.x-1jammy_amd64_withsymbols.deb + ``` + +3. **Fix missing dependencies:** + ```sh + sudo apt-get install -f + ``` + +4. **Check if the installation is successful:** + ```sh + sudo dpkg -l | grep 'hpccsystems-pl' + ``` +5. **Start HPCC-Platform:** + ```sh + sudo /etc/init.d/hpcc-init start + ``` + +6. **Verify access to ECL Watch:** + - Open a browser on your local machine and go to ```http://{ip_of_your_vm}:8010/``` to check if you can access ECL Watch. + +#### Cloning and Checking Out the HPCC-Platform Repository on VM + +1. **Clone the HPCC-Platform GitHub repository:** + ```sh + git clone https://github.com/hpcc-systems/HPCC-Platform.git + ``` + +2. **Navigate to the repository directory:** + ```sh + cd HPCC-Platform + ``` + +3. **Check out the specific version of HPCC-Platform:** + ```sh + git checkout candidate-9.8.x + ``` + +#### Running Regression Test Setup + +**Navigate to the testing directory and set up regression tests:** + ```sh + cd testing/regress + ./ecl-test setup --preAbort '/opt/HPCCSystems/bin/smoketest-preabort.sh' + ``` + +#### Running Spray Tests + +**Execute the spray tests:** + ```sh + ./ecl-test query --preAbort /opt/HPCCSystems/bin/smoketest-preabort.sh --excludeclass python2,embedded-r,embedded-js,3rdpartyservice,mongodb *spray* + ``` + +#### Generating JSON Files + +**Generate JSON files for workunits, files, and DFU workunits:** + ```sh + curl localhost:8010/WsWorkunits/WUQuery.json | python3 -m json.tool > workunits.json + curl localhost:8010/WsDfu/DFUQuery.json?PageSize=250 | python3 -m json.tool > files.json + curl localhost:8010/FileSpray/GetDFUWorkunits.json | python3 -m json.tool > dfu-workunits.json + ``` + +#### Transferring Files Using WinSCP + +Find your json files in the VM and use the WinSCP tool to transfer files from the VM to your local machine. + +#### Downloading Dependencies + +1. **Download the following dependencies:** + - [TestNG 7.7.1](https://repo1.maven.org/maven2/org/testng/testng/7.7.1/testng-7.7.1.jar) + - [Jackson Annotations 2.17.0](https://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-annotations/2.17.0/jackson-annotations-2.17.0.jar) + - [Jackson Core 2.17.0](https://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-core/2.17.0/jackson-core-2.17.0.jar) + - [Jackson Databind 2.17.0](https://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-databind/2.17.0/jackson-databind-2.17.0.jar) + - [JCommander 1.82](https://repo1.maven.org/maven2/com/beust/jcommander/1.82/jcommander-1.82.jar) + - [Selenium Java 4.22.0](https://github.com/SeleniumHQ/selenium/releases/download/selenium-4.22.0/selenium-java-4.22.0.zip) + - [SLF4J API 1.7.30](https://repo1.maven.org/maven2/org/slf4j/slf4j-api/1.7.30/slf4j-api-1.7.30.jar) + - [SLF4J Simple 1.7.30](https://repo1.maven.org/maven2/org/slf4j/slf4j-simple/1.7.30/slf4j-simple-1.7.30.jar) + +#### Adding Dependencies to Your Java Code + +1. Include the downloaded dependencies in your Java project. +2. Specify the path to your ChromeDriver when creating a `ChromeDriver` object in your code. + +#### Writing Test Cases for ECL Watch + +You are now ready to start writing your test cases for ECL Watch running at ```http://{ip_of_your_vm}:8010/```. + +#### Uninstall HPCC-Platform on VM + +Use below commands + +```sh + cd /opt/HPCCSystems/sbin + sudo ./complete-uninstall.sh -p + ``` diff --git a/esp/src/test-ui/tests/framework/TestRunner.java b/esp/src/test-ui/tests/framework/TestRunner.java new file mode 100644 index 00000000000..b4513a02dc5 --- /dev/null +++ b/esp/src/test-ui/tests/framework/TestRunner.java @@ -0,0 +1,101 @@ +package framework; + +import framework.config.Config; +import framework.config.TestClasses; +import framework.model.TestClass; +import framework.utility.Common; +import org.testng.TestNG; + +import java.util.ArrayList; +import java.util.List; + + +public class TestRunner { + public static void main(String[] args) { + + try { + getCommandLineParameters(args); + Common.initializeLoggerAndDriver(); + + if (Common.driver != null) { + TestNG testng = new TestNG(); + testng.setTestClasses(loadClasses()); + testng.run(); + Common.driver.quit(); + } + + Common.printNumOfErrorsAndExceptions(); + + } catch (Exception e) { + Common.logException("Exception occurred in TestRunner class: " + e.getMessage(), e); + } + } + + // Parses the command-line arguments to set the log level and JSON folder path. + // The method checks for the presence of '-l' (log_level) and '-p' (path) arguments. + // Logs an error if the '-p' argument is not provided, as the JSON path is required. + // path is the path of the folder where the json files are + // log level is of two types "debug" and "detail" + // "debug" means generate error log file with a debug log file. + // "detail" means generate error log file with a detailed debug file. + // if no -l and log level is passed in the argument, only error log will be generated + // -h in the CLI arguments prints the details of parameter usage to the console + + public static void getCommandLineParameters(String[] args) { // -l -p + + String log_level = null; + String path = null; + boolean help = false; + + for (int i = 0; i < args.length; i++) { + if ("-l".equals(args[i]) && i + 1 < args.length) { + log_level = args[++i]; + } else if ("-p".equals(args[i]) && i + 1 < args.length) { + path = args[++i]; + } else if ("-h".equals(args[i])) { + help = true; + } + } + + if (log_level != null) { + Config.LOG_LEVEL = log_level; + } + + if (path != null) { + Config.PATH_FOLDER_JSON = path; + } else { + Common.logError("Error: JSON folder path is required. Use -p to specify the path."); + } + + if (help) { + printParameterUsage(); + } + } + + private static void printParameterUsage() { + System.out.println(""" + Requires CLI arguments: -l -p + + is of two types "debug" and "detail" + "debug" means generate error log file with a debug log file. + "detail" means generate error log file with a detailed debug file. + if no -l and log level is passed in the argument, only error log will be generated + + is the path of the folder where the json files are + -p is a mandatory argument, code logs an error if the '-p' argument is not provided, as the JSON path is required for tests."""); + } + + private static Class[] loadClasses() { + + List> classes = new ArrayList<>(); + for (TestClass testClass : TestClasses.testClassesList) { + try { + classes.add(Class.forName(testClass.getPath())); + } catch (Exception e) { + Common.logException("Failure: Error in loading classes: " + e.getMessage(), e); + } + } + + return classes.toArray(new Class[0]); + } +} diff --git a/esp/src/test-ui/tests/framework/config/Config.java b/esp/src/test-ui/tests/framework/config/Config.java new file mode 100644 index 00000000000..4967875c1cf --- /dev/null +++ b/esp/src/test-ui/tests/framework/config/Config.java @@ -0,0 +1,29 @@ +package framework.config; + +public class Config { + + public static final String LOG_FILE_ERROR = "error_ecl_test.log"; // name of the error log file generated after code run is completed + public static final String LOG_FILE_EXCEPTION = "exception_ecl_test.log"; // name of the exception log file generated after code run is completed + public static final String LOG_FILE_DEBUG = "debug_ecl_test.log"; // name of the debug log file generated after code run is completed + public static final String LOG_FILE_DETAIL = "detail_ecl_test.log"; // name of the detail log file generated after code run is completed + public static final String LOCAL_OS = "Windows"; // name of your local OS, it is used to identify whether the code is running on a local machine or on GitHub Actions. + public static final String LOCAL_USER_PROFILE = "C:\\Users\\{your_username}"; // name of your local user profile, it is used to identify whether the code is running on a local machine or on GitHub Actions. + public static final String PATH_LOCAL_CHROME_DRIVER = "C:/Users/{your_working_directory_for_chromedriver}/chromedriver.exe"; // path of chrome driver on your local machine + public static final String PATH_GH_ACTION_CHROME_DRIVER = "/usr/bin/chromedriver"; // path of chrome driver on GitHub Actions + public static final int MALFORMED_TIME_STRING = -1; // this integer is used to denote any malformed time string that we can get from the UI or JSON file. + public static final int WAIT_TIME_IN_SECONDS = 1; // this is the default wait time that code uses to load any web element on UI. + public static final int WAIT_TIME_THRESHOLD_IN_SECONDS = 20; // This time is used to stop the code from waiting infinitely. If it is unable to find a web element on the UI, the code stops the search after this time logs an error if the element is not found. + public static final String TEST_DESCRIPTION_TEXT = "Testing Description"; // This is the test description that is used to test the description textbox functionality + public static final boolean TEST_DETAIL_PAGE_FIELD_NAMES_ALL = true; // true means the tests for field names on details page will run for all items (whether it is workunits or logical files) and false means it will only run for the first item + public static final boolean TEST_WU_DETAIL_PAGE_DESCRIPTION_ALL = true; // true means the tests for checking the description textbox functionality on details page will run for all workunits and false means it will only run for the first workunit + public static final boolean TEST_WU_DETAIL_PAGE_PROTECTED_ALL = true; // true means the tests for checking the protected checkbox functionality on details page will run for all workunits and false means it will only run for the first workunit + public static final boolean TEST_DETAIL_PAGE_TAB_CLICK_ALL = true; // true means the tests for tab click validity on details page will run for all items (whether it is workunits or logical files) and false means it will only run for the first item + + // these values are set in the beginning in the TestRunner.java file + public static String PATH_FOLDER_JSON = ""; // path of the folder of JSON files, it is passed in the CLI arguments. + public static String LOG_LEVEL = ""; // log level is also passed in the CLI arguments, it could be "debug" or "detail" + + public static final String WORKUNITS_JSON_FILE_NAME = "workunits.json"; // name of the workunits JSON file stored in the above JSON folder path + public static final String DFU_WORKUNITS_JSON_FILE_NAME = "dfu-workunits.json"; // name of the dfu-workunits JSON file stored in the above JSON folder path + public static final String FILES_JSON_FILE_NAME = "files.json"; // name of the files JSON file stored in the above JSON folder path +} diff --git a/esp/src/test-ui/tests/framework/config/TestClasses.java b/esp/src/test-ui/tests/framework/config/TestClasses.java new file mode 100644 index 00000000000..9d24257d7fd --- /dev/null +++ b/esp/src/test-ui/tests/framework/config/TestClasses.java @@ -0,0 +1,22 @@ +package framework.config; + +import framework.model.TestClass; + +import java.util.List; + +public class TestClasses { + + // ActivitiesTest class should always be the first class to load, as it gets URLs for all other pages. + + public static final List testClassesList = List.of( + new TestClass("ActivitiesTest", "framework.pages.ActivitiesTest"), + new TestClass("ECLWorkUnitsTest", "framework.pages.ECLWorkUnitsTest") + // The test class for FilesLogicalFilesTest is commented out because of an existing bug. + // In ECL Watch UI on Logical Files tab, all items do not render on UI even after + // selecting a higher dropdown, and it shows + // empty rows. Because of this issue code is unable to get all the file items from UI + // to go ahead for further testing. Until that issue is fixed, tests cannot check the + // validity of the content. For testing this class, wait for the JIRA to be resolved: HPCC-32297 + //new TestClass("FilesLogicalFilesTest", "framework.pages.FilesLogicalFilesTest") + ); +} diff --git a/esp/src/test-ui/tests/framework/config/URLConfig.java b/esp/src/test-ui/tests/framework/config/URLConfig.java new file mode 100644 index 00000000000..e526b0d65c4 --- /dev/null +++ b/esp/src/test-ui/tests/framework/config/URLConfig.java @@ -0,0 +1,53 @@ +package framework.config; + +import framework.model.URLMapping; +import framework.utility.Common; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class URLConfig { + + public static final String LOCAL_IP = "http://192.168.0.221:8010/"; + public static final String GITHUB_ACTION_IP = "http://127.0.0.1:8010/"; + + public static final String NAV_ACTIVITIES = "Activities"; + public static final String NAV_ECL = "ECL"; + public static final String NAV_FILES = "Files"; + public static final String NAV_PUBLISHED_QUERIES = "Published Queries"; + public static final String NAV_OPERATIONS = "Operations"; + public static final String TAB_ACTIVITIES_ACTIVITIES = "Activities"; + public static final String TAB_ACTIVITIES_EVENT_SCHEDULER = "Event Scheduler"; + public static final String TAB_ECL_WORKUNITS = "Workunits"; + public static final String TAB_ECL_PLAYGROUND = "Playground"; + public static final String TAB_FILES_LOGICAL_FILES = "Logical Files"; + public static final String TAB_FILES_LANDING_ZONES = "Landing Zones"; + public static final String TAB_FILES_WORKUNITS = "Workunits"; + public static final String TAB_FILES_XREF = "XRef (L)"; + public static final String TAB_PUBLISHED_QUERIES_QUERIES = "Queries"; + public static final String TAB_PUBLISHED_QUERIES_PACKAGE_MAPS = "Package Maps"; + public static final String TAB_OPERATIONS_TOPOLOGY = "Topology (L)"; + public static final String TAB_OPERATIONS_DISK_USAGE = "Disk Usage (L)"; + public static final String TAB_OPERATIONS_TARGET_CLUSTERS = "Target Clusters (L)"; + public static final String TAB_OPERATIONS_CLUSTER_PROCESSES = "Cluster Processes (L)"; + public static final String TAB_OPERATIONS_SYSTEM_SERVERS = "System Servers (L)"; + public static final String TAB_OPERATIONS_SECURITY = "Security (L)"; + public static final String TAB_OPERATIONS_DYNAMIC_ESDL = "Dynamic ESDL (L)"; + + public static final String[] navNamesArray = {NAV_ACTIVITIES, NAV_ECL, NAV_FILES, NAV_PUBLISHED_QUERIES, NAV_OPERATIONS}; + public static final Map> tabsListMap = Map.of( + NAV_ACTIVITIES, List.of(TAB_ACTIVITIES_ACTIVITIES, TAB_ACTIVITIES_EVENT_SCHEDULER), + NAV_ECL, List.of(TAB_ECL_WORKUNITS, TAB_ECL_PLAYGROUND), + NAV_FILES, List.of(TAB_FILES_LOGICAL_FILES, TAB_FILES_LANDING_ZONES, TAB_FILES_WORKUNITS, TAB_FILES_XREF), + NAV_PUBLISHED_QUERIES, List.of(TAB_PUBLISHED_QUERIES_QUERIES, TAB_PUBLISHED_QUERIES_PACKAGE_MAPS), + NAV_OPERATIONS, List.of(TAB_OPERATIONS_TOPOLOGY, TAB_OPERATIONS_DISK_USAGE, TAB_OPERATIONS_TARGET_CLUSTERS, + TAB_OPERATIONS_CLUSTER_PROCESSES, TAB_OPERATIONS_SYSTEM_SERVERS, TAB_OPERATIONS_SECURITY, TAB_OPERATIONS_DYNAMIC_ESDL) + ); + + public static final HashMap urlMap = new HashMap<>(); + + static { + urlMap.put(NAV_ACTIVITIES, new URLMapping(NAV_ACTIVITIES, Common.getIP())); + } +} diff --git a/esp/src/test-ui/tests/framework/documentation/ActivitiesTest.png b/esp/src/test-ui/tests/framework/documentation/ActivitiesTest.png new file mode 100644 index 00000000000..21131f104bc Binary files /dev/null and b/esp/src/test-ui/tests/framework/documentation/ActivitiesTest.png differ diff --git a/esp/src/test-ui/tests/framework/documentation/BaseTableTest1.png b/esp/src/test-ui/tests/framework/documentation/BaseTableTest1.png new file mode 100644 index 00000000000..e2d9d1f6111 Binary files /dev/null and b/esp/src/test-ui/tests/framework/documentation/BaseTableTest1.png differ diff --git a/esp/src/test-ui/tests/framework/documentation/BaseTableTest2.png b/esp/src/test-ui/tests/framework/documentation/BaseTableTest2.png new file mode 100644 index 00000000000..fb247a55573 Binary files /dev/null and b/esp/src/test-ui/tests/framework/documentation/BaseTableTest2.png differ diff --git a/esp/src/test-ui/tests/framework/documentation/Common1.png b/esp/src/test-ui/tests/framework/documentation/Common1.png new file mode 100644 index 00000000000..6615ae3ab00 Binary files /dev/null and b/esp/src/test-ui/tests/framework/documentation/Common1.png differ diff --git a/esp/src/test-ui/tests/framework/documentation/Common2.png b/esp/src/test-ui/tests/framework/documentation/Common2.png new file mode 100644 index 00000000000..665896f727c Binary files /dev/null and b/esp/src/test-ui/tests/framework/documentation/Common2.png differ diff --git a/esp/src/test-ui/tests/framework/documentation/Config.png b/esp/src/test-ui/tests/framework/documentation/Config.png new file mode 100644 index 00000000000..019311a89aa Binary files /dev/null and b/esp/src/test-ui/tests/framework/documentation/Config.png differ diff --git a/esp/src/test-ui/tests/framework/documentation/CustomFormatter.png b/esp/src/test-ui/tests/framework/documentation/CustomFormatter.png new file mode 100644 index 00000000000..bd5f7b8c3b6 Binary files /dev/null and b/esp/src/test-ui/tests/framework/documentation/CustomFormatter.png differ diff --git a/esp/src/test-ui/tests/framework/documentation/DFULogicalFile.png b/esp/src/test-ui/tests/framework/documentation/DFULogicalFile.png new file mode 100644 index 00000000000..5d4819a1f07 Binary files /dev/null and b/esp/src/test-ui/tests/framework/documentation/DFULogicalFile.png differ diff --git a/esp/src/test-ui/tests/framework/documentation/DFULogicalFiles.png b/esp/src/test-ui/tests/framework/documentation/DFULogicalFiles.png new file mode 100644 index 00000000000..7c642af861a Binary files /dev/null and b/esp/src/test-ui/tests/framework/documentation/DFULogicalFiles.png differ diff --git a/esp/src/test-ui/tests/framework/documentation/DFUQueryResponse.png b/esp/src/test-ui/tests/framework/documentation/DFUQueryResponse.png new file mode 100644 index 00000000000..71d67f7c51a Binary files /dev/null and b/esp/src/test-ui/tests/framework/documentation/DFUQueryResponse.png differ diff --git a/esp/src/test-ui/tests/framework/documentation/DFUQueryRoot.png b/esp/src/test-ui/tests/framework/documentation/DFUQueryRoot.png new file mode 100644 index 00000000000..649af7da0e8 Binary files /dev/null and b/esp/src/test-ui/tests/framework/documentation/DFUQueryRoot.png differ diff --git a/esp/src/test-ui/tests/framework/documentation/DesignDocument.md b/esp/src/test-ui/tests/framework/documentation/DesignDocument.md new file mode 100644 index 00000000000..d893354de68 --- /dev/null +++ b/esp/src/test-ui/tests/framework/documentation/DesignDocument.md @@ -0,0 +1,1276 @@ +## The Class Design and Code Flow for ECL Watch Test Suite + +### TestRunner.java + +![img.png](TestRunner.png) + +The TestRunner class is responsible for setting up and executing automated tests using the TestNG framework in a +Selenium-based testing environment. It handles the initialization of logging and WebDriver, dynamically loads the +test classes to be executed, and runs the tests. + +Variables: +args: Command line arguments passed to the main method. While running the test suite, you can pass arguments in this way -> "-l log_level -p path". +- "log_level" is of two types "debug" and "detail" +- "debug" means generate error and exception log file with a debug log file. +- "detail" means generate error and exception log file with a detailed debug file. +- If no -l and log_level is passed in the argument, only error and exception log will be generated +- "path" is the path of the folder where the json files are +- The code will log an error if the '-p' and 'path' arguments are not provided, as the JSON folder path is required for the test suite. +- -h in the CLI arguments prints the details of parameter usage to the console + +path could be something like: + +for GitHub Actions -> /home/runner/HPCCSystems-regression/log/ + +for local machine -> C:/Users/{your_working_directory_of_json_files}/ + +So an example of complete CLI arguments would look like this: + +-l detail -p /home/runner/HPCCSystems-regression/log/ + +**Methods:** + +1. main Method + +The entry point of the application. It performs the following steps: + +- Calls getCommandLineParameters method to get the log level and the path of JSON files from CLI arguments and configure them in the Config file. +- Calls Common.initializeLoggerAndDriver to configure the logging system and web driver. +- If the drives sets up properly and is not null, it creates an instance of TestNG. +- Sets the test classes to be run by calling loadClasses. +- Runs the tests using TestNG. +- Quits the WebDriver session after the tests have completed. +- Calls Common.printNumOfErrorsAndExceptions to print total number of errors and exceptions to the console, so that we get an idea of the count after the tests are finished. + +2. loadClasses Method + +Loads the test classes specified in TestClasses.testClassesList: + +- Iterates over the testClassesList and loads each class by its fully qualified name. +- Adds the loaded class to the list classes. +- Catches and prints any ClassNotFoundException. + +3. getCommandLineParameters Method + +This method is designed to parse command-line arguments to set the log level and the path to a folder containing JSON files. It validates the presence of these arguments and sets configuration values accordingly. + +- It takes a single parameter, args, which is an array of strings representing the command-line arguments passed to the program. +- Three local variables, log_level, path and help, are initialized to null. These will hold the values extracted from the command-line arguments. +- A for loop iterates through the args array to examine each argument. +- The loop uses i as the index variable to traverse the array. +- Inside the loop, check if the current argument equals "-l". +- If "-l" is found and there is another argument following it (i + 1 < args.length), assign the value of the next argument to log_level and increment the index i to skip the processed argument. +- Similarly, check if the current argument equals "-p". +- If "-p" is found and there is another argument following it (i + 1 < args.length), assign the value of the next argument to path and increment the index i to skip the processed argument. +- If "-h" is found, assign true to help. +- After the loop, check if log_level is not null. If it is not null, assign its value to Config.LOG_LEVEL. +- Check if path is not null. If it is not null, assign its value to Config.PATH_FOLDER_JSON. +- If path is still null after the loop, log an error message indicating that the JSON folder path is required. Use Common.logError to log this error. +- Check if help is true. If help is true call printParameterUsage method to print the details of parameter usage to the console. + +4. printParameterUsage Method + +This method just print the details of parameter usage to the console for better user interaction. + +### TestClasses.java + +![img_4.png](TestClasses.png) + +The TestClasses class in the framework.config package is responsible for maintaining +a list of test classes used in the framework. This class provides a centralized and +immutable collection of test class metadata, which includes the name of each test +class and its fully qualified class name. This setup helps organize and reference +the test classes easily throughout the testing framework. By using the TestClasses +class, the framework can dynamically load and execute tests, enhancing modularity +and maintainability. + +### TestClass.java + +![img_3.png](TestClass.png) + +The TestClass class in the framework.model package is a simple model class designed +to encapsulate metadata about a test class within the testing framework. It contains +the name of the test class and its fully qualified class name (path). This class +provides a structured way to store and retrieve information about each test class, +which can be utilized by other components in the framework for dynamically loading and +executing tests. + +### URLMapping.java + +![img.png](URLMapping.png) + +The URLMapping class in the framework.model package is a model that represents a URL +and its associated metadata. It provides a structured way to store URLs and their hierarchical +relationships, facilitating easy navigation and retrieval of URLs in a web application. + +#### Variables: +- name: A String representing the name of the navigation element or tab. +- url: A String representing the URL associated with the navigation element or tab. +- urlMappings: A HashMap that maps the names of nested navigation elements or tabs to their corresponding URLMapping objects. + +### Config.java + +![img.png](Config.png) + +The Config class in the framework.config package serves as a centralized configuration +repository for the application. It contains various constants that are used throughout +the framework, providing a single point of reference for configuration settings such as +file paths, file names, flags, and other constants. This approach enhances the maintainability and +readability of the code by avoiding hard-coded values scattered across different classes. + +### URLConfig.java + +![img.png](URLConfig1.png) +![img.png](URLConfig2.png) + +The URLConfig class is designed to manage the URL configurations for different navigation +and tab pages within the test cases. It provides a structured way to store and retrieve +URLs associated with various sections of the ECL Watch UI. This class uses constants to define +URLs and page names, and it leverages a HashMap to store URL mappings, facilitating easy access +to the URLs of different pages and tabs. + +- NAV_ACTIVITIES, NAV_ECL, NAV_FILES, NAV_PUBLISHED_QUERIES, NAV_OPERATIONS: Constants representing main navigation sections. +- TAB_ACTIVITIES_ACTIVITIES, TAB_ACTIVITIES_EVENT_SCHEDULER: Tab names under the Activities navigation. +- TAB_ECL_WORKUNITS, TAB_ECL_PLAYGROUND: Tab names under the ECL navigation. +- TAB_FILES_LOGICAL_FILES, TAB_FILES_LANDING_ZONES, TAB_FILES_WORKUNITS, TAB_FILES_XREF: Tab names under the Files navigation. +- TAB_PUBLISHED_QUERIES_QUERIES, TAB_PUBLISHED_QUERIES_PACKAGE_MAPS: Tab names under the Published Queries navigation. +- TAB_OPERATIONS_TOPOLOGY, TAB_OPERATIONS_DISK_USAGE, TAB_OPERATIONS_TARGET_CLUSTERS, TAB_OPERATIONS_CLUSTER_PROCESSES, TAB_OPERATIONS_SYSTEM_SERVERS, TAB_OPERATIONS_SECURITY, TAB_OPERATIONS_DYNAMIC_ESDL: Tab names under the Operations navigation. +- navNamesArray: Array containing all the main navigation names. +- tabsListMap: Map containing lists of tab names associated with each main navigation section. +- urlMap: A HashMap (urlMap) is created to store URL mappings. This map will use the page name as the key and a URLMapping object as the value. The URLMapping object contains the page name, its URL, and another HashMap for nested pages and tabs. +- A static block is used to initialize the urlMap with the initial URL mapping for the Activities navigation. The URL is retrieved using a method from the Common utility class, which handles the dynamic retrieval of the IP address based on the environment whether it is local or GitHub Actions. + +#### Implementation Steps for URL Management + +- For each main navigation section, a URLMapping object is created. This object includes the page name and its corresponding URL. Additionally, it contains another HashMap to store URLs for nested tabs and pages. +- Each URLMapping object is stored in the urlMap with the main navigation name as the key. This initial setup ensures that each navigation section has its base URL stored and accessible. +- For instance, for any navigation page, each page has multiple tabs, and within those tabs, there are multiple pages and tabs. This structure facilitates easy access to the URL of a particular page. +- Starting from the Activities page, for each main navigation section, the code iterates over its associated tabs (as defined in tabsListMap). For each tab, a new URLMapping object is created and added to the HashMap within the corresponding URLMapping object of the main navigation section. This creates a tree-like structure, allowing easy access to URLs for both navigation sections and their nested tabs. +- By following these implementation steps, the URLConfig class ensures that all URLs within the application are well-organized and easily accessible through a hierarchical structure. This setup simplifies navigation and URL management within the application, making it easier to handle complex page structures and dynamic URL retrievals. + +### Common.java + +![img.png](Common1.png) +![img.png](Common2.png) + +The Common class in the framework.utility package provides a set of utility +methods that are frequently used throughout the testing framework. These +methods handle common tasks and leverages constants from the Config class to maintain consistency +and facilitate configuration management. + +Variables: + +- driver: This public static variable stores the WebDriver instance used for interacting with the web browser. +- errorLogger: This public static variable stores a logger instance used for logging error messages. +- specificLogger: This public static variable stores a logger instance used for logging specific messages +based on the provided level (debug or detail). +- num_errors: A counter to keep a count of total numbers of errors generated in the error log file. +- num_exceptions: A counter to keep a count of total numbers of exceptions generated in the exceptions log file. + +**Methods:** + +1. checkTextPresent Method + +Checks if the specified text is present on the current webpage and logs the result. + +- Retrieves the page source using driver.getPageSource(). +- Checks if the page source contains the specified text. +- Logs a success message if the text is found; otherwise, logs an error message and records it using the provided + logger. + +2. openWebPage Method + +Opens the specified URL in the browser and maximizes the window. + +- Navigates to the specified URL using driver.get(url). +- Maximizes the browser window using driver.manage().window().maximize(). +- Calls the sleep method to pause the execution for a short period to allow the page to load completely. + +3. sleep Method + +The sleep method pauses the execution of the program for a specified duration +(4 seconds in this case). This can be useful in scenarios where a delay is required, +such as waiting for a webpage to load completely before proceeding with further actions. + +4. isRunningOnLocal Method + +Determines if the code is running on a local environment. + +- Checks if the operating system name starts with the value of Config.LOCAL_OS. +- Checks if the user profile path starts with the value of Config.LOCAL_USER_PROFILE. +- Returns true if both conditions are met, indicating a local environment; otherwise, returns false. + +5. getIP Method + +The getIP method determines the appropriate IP address to use based on whether the application +is running in a local environment or in a GitHub Actions environment. +This helps in dynamically adjusting the base URL of the application depending on the +execution context. + +- Calls isRunningOnLocal to check the environment. +- If running locally, returns the URLConfig.LOCAL_IP. +- If running in GitHub Actions, returns the URLConfig.GITHUB_ACTION_IP. + +6. waitForElement Method + +- This method waits for a web element to be present in the DOM. +- Uses WebDriverWait to wait up to 10 seconds for the presence of the specified web element. + +7. logError Method + +- This method logs error messages. +- Prints the error message to the standard error stream. +- Logs the message using errorLogger. +- Increments the variable num_errors by 1, to maintain the count of number of errors generated. + +8. logException Method + +- This method logs exception messages with complete stack trace. +- Prints the exception message with complete stack trace to the standard error stream. +- Logs the message using errorLogger. +- Increments the variable num_exceptions by 1, to maintain the count of number of num_exceptions generated. + +9. logDebug Method + +- This method logs debug or detailed messages. +- Prints the message to the standard output stream. +- Logs the message using specificLogger if the logging level is INFO or FINE. + +10. logDetail method + +- This method logs detailed messages. +- Prints the message to the standard output stream. +- Logs the message using specificLogger if the logging level is FINE. + +11. initializeLoggerAndDriver Method + +- This method initializes the logger and WebDriver instances. +- Sets up the specificLogger based on the provided argument. +- Sets up the WebDriver instance using setupWebDriver() method. + +12. setupWebDriver Method + +- This method sets up the WebDriver instance based on the environment. +- Configures ChromeOptions for headless mode, no sandbox, and suppressed log output. +- Sets up the WebDriver based on the environment (local or GitHub Actions). +- Logs an error message if an exception occurs during setup. + +13. setupLogger Method + +- This method sets up a logger instance based on the provided log level (error, exception, debug and detail). +- It creates a CustomFormatter instance. This formatter will be used to format log messages according to a specified pattern. +- Configures the logger to disable console logging and set up file handlers for different log levels (error, exception, debug, detail). +- For "error" and "exception" levels, it sets up a FileHandler with a corresponding file path from Config.LOG_FILE_ERROR or Config.LOG_FILE_EXCEPTION, respectively. The log level is set to Level.SEVERE. +- For "debug", it sets up a FileHandler with Config.LOG_FILE_DEBUG and sets the log level to Level.INFO. +- For "detail", it sets up a FileHandler with Config.LOG_FILE_DETAIL and sets the log level to Level.FINE. +- Turns off all logging from Selenium WebDriver. +- Logs an error message if an exception occurs during logger setup. +- Finally, the method returns the configured Logger instance. + +14. sleepWithTime Method + +- The sleepWithTime method pauses the execution of the current thread for a specified number of seconds. +- It is a simple utility method used to introduce a delay in the execution flow. +- Used to wait for a certain condition or state before proceeding with further execution. + +15. waitForElementToBeClickable Method + +- This method waits until the specified web element is clickable. +- A WebDriverWait instance is created with a timeout duration specified by Config.WAIT_TIME_THRESHOLD_IN_SECONDS. +- The method waits until the element is clickable, using Selenium's ExpectedConditions.elementToBeClickable condition. +- It is useful in scenarios where an element needs to be interacted with, but it might not be immediately clickable due to loading times or other conditions. + +16. waitForElementToBeDisabled Method + +The waitForElementToBeDisabled method waits until the specified web element is disabled by checking its aria-disabled attribute. +The attribute "aria-disabled" is part of the Accessible Rich Internet Applications (ARIA) specification, which is used to improve the accessibility of web pages. +aria-disabled is a standard attribute and is widely used in HTML to indicate whether an element is disabled. It is system-defined, meaning it is part of the standard +HTML specifications and not a custom class or attribute that might change frequently. By using aria-disabled, you ensure that the check for the disabled state is +consistent and less likely to break due to UI changes. Custom class names or attributes defined by developers can change frequently during updates or redesigns, +but standard attributes like aria-disabled are much more stable. + +- A WebDriverWait instance is created with a timeout duration specified by Config.WAIT_TIME_THRESHOLD_IN_SECONDS. +- The method waits until the aria-disabled attribute of the element contains the value "true", indicating that the element is disabled. +- Ensures that an element has transitioned to a disabled state before proceeding with further actions. + +### CustomFormatter.java + +![img.png](CustomFormatter.png) + +The CustomFormatter class in the framework.utility package extends Formatter and uses a SimpleDateFormat instance to define the format of the timestamps in the log messages. The format specified is "yyyy-MM-dd HH:mm:ss", which outputs timestamps in a year-month-day hour:minute:seconds +format. + +1. format Method + +- The format method overrides the abstract method from the Formatter class to define how log records should be formatted. +- It takes a LogRecord object as a parameter, which contains information about the log message, including the timestamp and the message itself. +- The method converts the timestamp from milliseconds since the epoch (obtained from record.getMillis()) into a Date object. +- It then formats this Date object using the SimpleDateFormat instance to get a readable date-time string. +- Finally, it returns a formatted string consisting of the formatted date-time followed by the log message and a newline character. This ensures that each log entry starts with a timestamp and is followed by the log message. + +### TimeUtils.java + +![img.png](TimeUtils.png) + +The TimeUtils class in the framework.utility package provides utility functions to handle +time strings and convert them into milliseconds. This is useful for standardizing time +representations and performing time-based calculations in a consistent manner. + +TIME_PATTERN: A regular expression pattern used to match various time formats. The supported formats are: + +- d days h:m:s.s +- h:m:s.s +- m:s.s +- s.s + +**Methods:** + +1. convertToMilliseconds Method + +This method converts a time string into milliseconds based on the matched pattern. If the +time string does not match any recognized format, it returns a predefined constant for +malformed time strings. + +- The method first attempts to match the input time string against the TIME_PATTERN. +- If the string matches the pattern, it initializes the time components (days, hours, minutes, seconds, milliseconds) to + zero. +- The method then extracts values based on the matching groups. +- If any parsing errors occur (e.g., NumberFormatException) or if the string does not match the pattern, the method + returns Config.MALFORMED_TIME_STRING. +- If the parsing is successful, the method calculates the total duration in milliseconds + +### NavigationWebElement + +![img.png](NavigationWebElement.png) + +This NavigationWebElement class in the framework.model package, is a record class used to represent a navigation element +within a web application framework. It offers a concise way to store and manage information about such +elements. + +- The class is defined as a record which is a recent addition to Java that simplifies creating immutable data classes. +- It has three properties: + - name: A String representing the name or identifier of the navigation element in the menu bar(e.g., "Activities", " + ECL", "Files). + - hrefValue: A String representing the href attribute value of the element, which typically specifies the URL linked + to by the element. + - webElement: A WebElement object from the Selenium library. This holds the actual WebElement instance representing + the element in the web page. + +- Due to the record nature, a constructor is not explicitly defined. The compiler generates a constructor that takes + arguments for each property and initializes them. +- The NavigationWebElement class offers a structured way to manage data related to navigation elements in a web + application framework. + +### Java Classes for Representing Workunit JSON Data + +This section details the class structure used to map JSON data file of list of "Workunit" +entities into Java objects. These classes provide a clear representation of the data and +allow for easy access to its values throughout the codebase. This structure is particularly +beneficial for writing test cases, as it simplifies working with the data in a well-defined +format. Below are the UML diagram of the classes used for JSON mapping to java objects for +workunits JSON file. + +So the first java object created to map the json is WUQueryRoot (equivalent to the root object of JSON) +which contains the WUQueryResponse object (equivalent to the WUQueryResponse key in json file) inside it +and WUQueryResponse contains the WUWorkunits object (equivalent to the Workunits key in json file) +inside it, and then finally a list of WUECLWorkunit object (equivalent to the ECLWorkunit key in json file) is inside the WUWorkunits object. +Inside each WUECLWorkunit object, there is WUApplicationValues object (equivalent to the ApplicationValues key in json), which further have a list of +WUApplicationValue objects (equivalent to the ApplicationValue key in json file) inside it. +This is how the structure of Java objects created with respect to the JSON structure of workunits. + +![img.png](WUQueryRoot.png) + +![img.png](WUQueryResponse.png) + +![img.png](WUWorkunits.png) + +![img.png](WUECLWorkunit.png) + +![img.png](WUApplicationValues.png) + +![img.png](WUApplicationValue.png) + +### ActivitiesTest + +![img.png](ActivitiesTest.png) + +This ActivitiesTest class in the framework.pages package, implements a TestNG test (@Test) +for the Activities page of ECL Watch UI. It focuses on verifying the following aspects of the +Activities page: + +- Presence of specific text elements +- Functionality of navigation links and their corresponding sub-tabs + +**Class Variables:** + +- textArray: A static final String array containing expected text elements to be present on the Activities page (e.g., " + Target/Wuid", "Graph"). + +**Methods:** + +1. testActivitiesPage (Test Method) + +- This is the main test method annotated with @Test to be run as a test case. +- Initializes the WebDriver instance. +- Use the Common.openWebPage method to navigate to the URL of the Activities page, retrieved from the urlMap. +- Logs the start of the tests for the "Activities" page. +- Calls testForAllText(driver) to check for the presence of predefined texts. +- Retrieves the navigation web elements by calling getNavWebElements(driver). +- Calls testForNavigationLinks(driver, navWebElements) to test the navigation links. +- Logs the completion of the tests for the "Activities" page. + +2. testForAllText Method + +- This method checks if specific texts are present on the "Activities" page. +- Logs the start of text presence tests. +- Iterates over each text in textArray. +- Calls Common.checkTextPresent(driver, text, "Activities Page") to verify the presence of each text on the page. + +3. testForNavigationLinks Method + +- This method tests each navigation link to ensure they direct to the correct pages with the expected tabs. +- Logs the start of navigation link tests. +- Iterates over each NavigationWebElement in navWebElements. +- Clicks on each navigation element and verifies the presence of corresponding tabs by calling testTabsForNavigationLinks(driver, element). +- Logs success if all tabs are present; otherwise, logs an error with the current page details. +- Catches and logs any exceptions that occur during the process. + +4. getCurrentPage Method + +- This method determines the current page by checking the presence of specific tabs. +- Iterates over each entry in tabsListMap. +- Checks if all tabs for each page are present in the page source. +- Returns the page name if all tabs are present; otherwise, returns "Invalid Page". + +5. testTabsForNavigationLinks Method: + +- This method verifies the presence of tabs for a given navigation element and updates the URL map with the tab URLs. +- Get the list of tabs associated with the navigation element from URLConfig.tabsListMap. +- Loop through each tab in the list. +- Use Common.waitForElement to wait for the tab element to be present in the DOM. +- Add the tab's URL to the urlMap under the corresponding navigation element. +- If a timeout exception occurs, return false. Otherwise, return true. + +6. getNavWebElements Method: + +- This method retrieves the web elements for the main navigation links on the Activities page. +- Creates an empty list to store NavigationWebElement objects. +- Iterates through the URLConfig.navNamesArray. +- For each navigation link name: + - Finds the WebElement using driver.findElement with By.name strategy. + - Extracts the href attribute value. + - Creates a new NavigationWebElement object with the name, href value, and WebElement reference. + - Adds the NavigationWebElement to the list. + - Add the navigation element's URL to the urlMap. + - Log any errors that occur during the process. +- Returns the list of NavigationWebElement objects. + +### BaseTableTest + +![img.png](BaseTableTest1.png) +![img.png](BaseTableTest2.png) + +The BaseTableTest class is designed as a superclass for testing web pages containing tabular data. It is intended for use in automated tests, +particularly for pages like workunits, files, and queries, and includes functionality for testing their respective details pages. +It defines methods for common functionalities like: + +- Verifying the presence of expected text elements on the page. +- Comparing the content displayed in the table with corresponding data from a JSON file. +- Testing the sorting functionality of the table columns. +- Verifying links within the table cells and their navigation behavior. +- Testing common details functionality, such as tab clicks. + +**Abstract Methods:** + +These methods must be implemented by subclasses to provide specific information and functionality for the page being tested: + +1. getPageName(): Returns the name of the page. +2. getPageUrl(): Returns the URL of the page. +3. getJsonFilePath(): Returns the file path for the JSON data file. +4. getSaveButtonDetailsPage(): Returns the text for the save button on the details page. +5. getColumnNames(): Returns an array of column names displayed in the table. +6. getDetailNames(): Returns an array of detail names for the details page. +7. getColumnKeys(): Returns an array of column keys for accessing data. +8. getDetailKeys(): Returns an array of detail keys for accessing data on the details page. +9. getCheckboxTypeForDetailsPage(): Returns the attribute type for checkboxes on the details page. +10. getAttributeTypeForDetailsPage(): Returns the attribute type for elements on the details page. +11. getAttributeValueForDetailsPage(): Returns the attribute value for elements on the details page. +12. getDetailKeysForPageLoad(): Returns an array of detail keys to check when the details page loads. +13. getUniqueKeyName(): Returns the unique key name for identifying data. +14. getUniqueKey(): Returns the unique key used for identifying data in the table. +15. getColumnKeysWithLinks(): Returns an array of column keys that contain links. +16. parseDataUIValue(dataUIValue, dataJSONValue, columnName, dataIDUIValue): Parses the UI data value. +17. parseDataJSONValue(dataJSONValue, columnName, dataIDUIValue): Parses the JSON data value. +18. parseJson(filePath): Parses the JSON file and returns a list of objects. +19. getColumnDataFromJson(object, columnKey): Gets column data from a JSON object. +20. sortJsonUsingSortOrder(currentSortOrder, columnKey): Sorts the JSON data using the specified order and column key. +21. getCurrentPage(): Returns the current page. +22. getJsonMap(): Returns a map of JSON data. +23. getColumnNamesForTabsDetailsPage(): Returns a map of column names for tabs on the details page. +24. getTabValuesForDetailsPage(): Returns an array of tab values for the details page. +25. testDetailSpecificFunctionality(name, i): Tests specific functionality on the details page. + +**Non-Abstract Methods:** + +#### Method: `testPage()` + +This method initiates and executes a series of tests on a web page. + +1. **Open the Web Page**: The method begins by opening the specified web page using `Common.openWebPage(getPageUrl())`. + +2. **Logging**: It logs the start of tests for the page with `Common.logDebug`. + +3. **Test for All Text**: The method `testForAllText()` is called to verify the presence of expected text elements on the page. + +4. **Retrieve JSON Objects**: It retrieves all JSON objects using `getAllObjectsFromJson()` and stores them in `jsonObjects`. + +5. **Dropdown Selection**: If `jsonObjects` is not null, it determines the number of items in JSON (`numOfItemsJSON`) and uses `clickDropdown(numOfItemsJSON)` to select the appropriate dropdown value for displaying items on the page. + +6. **Content and Sorting Tests**: The method `testContentAndSortingOrder()` is called to verify the content and sorting order of items on the page. + +7. **Link Tests**: It calls `testLinksInTable()` to test all the links in the table present on the page. + +8. **Logging**: Logs the completion of tests with `Common.logDebug`. + +9. **Exception Handling**: Catches any exceptions that occur during the test and logs the error with `Common.logError`. + +#### Method: `testDetailsPage(String name, int i)` + +This method tests various aspects of a details page for a specific item. + +1. **Test Detail Page Field Names**: Depending on the configuration `Config.TEST_DETAIL_PAGE_FIELD_NAMES_ALL`, it calls `testForAllTextInDetailsPage(name)` either for all items or just the first item. + +2. **Details Content Page Test**: It calls `testDetailsContentPage(name)` to verify the content of the details page. + +3. **Detail Specific Functionality Test**: The method `testDetailSpecificFunctionality(name, i)` is called to test specific functionality on the details page. + +4. **Test Tab Clicks**: Depending on the configuration `Config.TEST_DETAIL_PAGE_TAB_CLICK_ALL`, it calls `testTabClickOnDetailsPage()` either for all items or just the first item. + +#### Method: `testTabClickOnDetailsPage()` + +This method tests the functionality of clicking through different tabs on the details page. + +1. **Logging**: Logs the start of the tab click test with `Common.logDebug`. + +2. **Wait for Page Load**: Calls `waitToLoadDetailsPage()` to ensure the details page is fully loaded. + +3. **Iterate Through Tabs**: For each tab value retrieved from `getTabValuesForDetailsPage()`: + - It tries to find the tab button element and clicks it using `javaScriptElementClick`. + - It then calls `testPresenceOfColumnNames(getColumnNamesForTabsDetailsPage().get(tabValue), tabValue)` to verify the presence of expected column names for the selected tab. + - If an error occurs, it logs the error with `Common.logError`. + +#### Method: `javaScriptElementClick(WebElement element)` + +This method clicks a web element using JavaScript. + +1. **Execute Script**: Uses a `JavascriptExecutor` to click the specified web element by executing the script `arguments[0].click();`. + +#### Method: `testPresenceOfColumnNames(List columnNames, String tabValue)` + +This method verifies the presence of specific column names within a tab on the details page. + +1. **Check Column Names**: For each column name in the provided list: + - It waits for the element containing the column name using `Common.waitForElement(By.xpath("//*[text()='" + columnName + "']"))`. + - If a `TimeoutException` occurs, it logs an error and sets a flag `allPresent` to false. + +2. **Logging**: If all column names are present, it logs success with `Common.logDetail`. If any column name is missing, it logs an error. + +#### Method: `clickOnSaveButton()` + +This method handles the action of clicking the save button on a details page. + +1. **Wait for Save Button**: It retrieves the save button element using `getSaveButtonWebElementDetailsPage()` and waits for it to be clickable with `Common.waitForElementToBeClickable`. + +2. **Click Save Button**: Clicks the save button. + +3. **Wait for Save Completion**: Waits for the save button to become disabled using `Common.waitForElementToBeDisabled`. + +#### Method: `testLinksInTable()` + +This method tests the functionality and navigation of links present in a table on the page. + +1. **Logging**: Logs the start of the link tests with `Common.logDebug`. + +2. **Page refresh**: refreshing page as page has scrolled to right for testing the sorting functionality of column headers, so bringing it back to normal view by refreshing it + +3. **Iterate Through Columns with Links**: For each column key with links retrieved from `getColumnKeysWithLinks()`: + - It retrieves the data values from the UI using `getDataFromUIUsingColumnKey(columnKey)`. + - For each value, it tries to find and click the corresponding link element. + - After clicking, it checks if the page source contains the name to confirm successful navigation. + - Logs success or failure based on the result. + - It then navigates back to the original page and refreshes it. + - It verifies if the dropdown value remains unchanged after navigation. + +3. **Exception Handling**: Catches any exceptions during the link tests and logs the errors. + +#### Method: `waitToLoadDetailsPage()` + +This method ensures the details page is fully loaded by waiting for specific elements to be visible. + +1. **Initialize Wait Time**: Starts with an initial wait time specified by `Config.WAIT_TIME_IN_SECONDS`. + +2. **Check Element Visibility**: Repeatedly checks the visibility of detail keys retrieved from `getDetailKeysForPageLoad()`: + - Waits for each element by its ID using `Common.waitForElement`. + - If any element's attribute is empty, it continues waiting and increments the wait time. + +3. **Logging**: Logs the total wait time used to load the details page with `Common.logDebug`. + +#### Method: `testDetailsContentPage(String name)` + +This method tests the content of a details page for a specific item. + +1. **Logging**: Logs the start of the content tests with `Common.logDebug`. + +2. **Wait for Page Load**: Calls `waitToLoadDetailsPage()` to ensure the page is fully loaded. + +3. **Compare Content**: For each detail key: + - It retrieves the corresponding web element and its value. + - Compares the value with the expected JSON value. + - Logs errors if there are discrepancies. + +4. **Logging**: Logs success if all values match the expected values. + +#### Method: `testForAllTextInDetailsPage(String name)` + +This method tests the presence of specific text elements on a details page. + +1. **Logging**: Logs the start of the text tests with `Common.logDebug`. + +2. **Check Text Presence**: For each expected text element retrieved from `getDetailNames()`: + - Calls `Common.checkTextPresent` to verify the text presence on the page. + +#### Method: `testContentAndSortingOrder()` + +This method tests the content and sorting order of items on a page. + +1. **Logging**: Logs the start of content tests with `Common.logDebug`. + +2. **Test Table Content**: Calls `testTableContent()` to verify the content of the table. + +3. **Test Sorting Order**: If the content test passes, it logs the start of sorting order tests and: + - Iterates through each column key and name. + - Calls `testTheSortingOrderForOneColumn(columnKey, columnName)` to verify the sorting order. + +#### Method: `testTheSortingOrderForOneColumn(String columnKey, String columnName)` + +This method tests the sorting order of a specific column. + +1. **Retrieve Sorting Order**: Calls `getCurrentSortingOrder(columnKey)` to retrieve the current sorting order for the column. + +2. **Compare Data**: If a sorting order is retrieved, it: + - Retrieves data from the UI and JSON. + - Sorts JSON data using the retrieved sorting order. + - Compares the UI and JSON data. + - Logs success if the data is correctly sorted; otherwise, logs an error. + +#### Method: `getCurrentSortingOrder(String columnKey)` + +This method retrieves the current sorting order of a column. + +1. **Find Column Header**: Locates the column header element for the specified column key. + +2. **Scrolls To Find Header**: Uses JavascriptExecutor to scroll and bring the column header into view. + +3. **Retrieve Sort Order**: Retrieves the current sorting order from the column header's attribute `aria-sort`. + +4. **Click to Change Sort Order**: Clicks the column header to change the sort order and waits for the sorting order to change using `waitToLoadChangedSortOrder`. + +5. **Return New Sort Order**: Returns the new sorting order. + +#### Method: `waitToLoadChangedSortOrder(String oldSortOrder, String columnKey)` + +This method waits for the sorting order to change after clicking a column header. + +1. **Initialize Wait Time**: Starts with an initial wait time specified by `Config.WAIT_TIME_IN_SECONDS`. + +2. **Check Sort Order Change**: Repeatedly checks the sorting order of the column header until it changes from the old sort order. + +3. **Return New Sort Order**: Returns the new sorting order once it has changed. + +#### Method: `getDataFromJSONUsingColumnKey(String columnKey)` + +This method retrieves data from JSON objects for a specified column key. + +1. **Iterate Through JSON Objects**: For each JSON object: + - Retrieves the column data using `getColumnDataFromJson`. + +2. **Return Data**: Returns the list of column data. + +#### Method: `getDataFromUIUsingColumnKey(String columnKey)` + +This method retrieves data from the UI based on a specified column key. + +1. **Initialize List**: Starts by creating an empty list to store the data. +2. **Load UI Elements**: Calls the `waitToLoadListOfAllUIObjects` method to get all web elements corresponding to the column key. +3. **Extract Text**: Iterates through the list of web elements (excluding the header) and adds the text content of each element to the list. +4. **Error Handling**: If any exception occurs, it logs an error message. +5. **Return Data**: Finally, returns the list of extracted data. + +#### Method: `waitToLoadListOfAllUIObjects(String columnKey)` + +This method waits until all UI elements corresponding to a column key are loaded. + +1. **Initialize Wait Time**: Starts with an initial wait time. +2. **Find Elements**: Uses a loop to repeatedly find web elements in a grid cell matching the column key's XPath. +3. **Check Element Count**: Checks if the number of elements (excluding the header) matches the number of JSON objects. +4. **Wait and Retry**: If the elements are not fully loaded, it sleeps for the current wait time, increments the wait time, and retries. +5. **Return Elements**: Once the elements are fully loaded, returns the list of web elements. + +#### Method: `ascendingSortJson(String columnKey)` + +This method sorts the JSON objects in ascending order based on a column key. + +1. **Sort JSON Objects**: Uses a comparator to sort the list of JSON objects by comparing the values of the specified column key. +2. **Error Handling**: If any exception occurs during sorting, logs an error message. + +#### Method: `descendingSortJson(String columnKey)` + +This method sorts the JSON objects in descending order based on a column key. + +1. **Sort JSON Objects**: Similar to `ascendingSortJson`, but uses a reversed comparator to sort the list in descending order. +2. **Error Handling**: If any exception occurs during sorting, logs an error message. + +#### Method: `testTableContent()` + +This method tests the content of a table by comparing the UI data with JSON data. + +1. **Log Number of Objects**: Logs the number of objects retrieved from JSON. +2. **Retrieve UI Data**: Calls `getDataFromUIUsingColumnKey` to get the data from the UI for the unique key. +3. **Compare Counts**: Compares the number of objects from JSON and UI. Logs an error if they are not equal and returns false. +4. **Initialize Pass Flag**: Initializes a flag to track if all comparisons pass. +5. **Iterate Through Columns**: For each column key: + - Retrieves data from UI and JSON. + - Calls `compareData` to compare the data and updates the pass flag based on the result. +6. **Return Result**: Returns the pass flag indicating whether all comparisons passed. + +#### Method: `getAllObjectsFromJson()` + +This method retrieves all objects from a JSON file. + +1. **Get File Path**: Retrieves the path of the JSON file. +2. **Parse JSON**: Calls `parseJson` to parse the JSON file and return a list of objects. +3. **Error Handling**: If any exception occurs during parsing, logs an error message. +4. **Return Data**: Returns the list of parsed objects. + +#### Method: `compareData(List dataUI, List dataJSON, List dataIDUI, String columnName)` + +This method compares the data from UI and JSON for a specific column. + +1. **Initialize Pass Flag**: Initializes a flag to track if all comparisons pass. +2. **Iterate Through Data**: For each item in the data lists: + - Parses the UI and JSON values. + - Calls `checkValues` to compare the values and updates the pass flag based on the result. +3. **Log Success**: If all comparisons pass, logs a success message. +4. **Return Result**: Returns the pass flag indicating whether all comparisons passed. + +#### Method: `checkValues(Object dataUIValue, Object dataJSONValue, Object dataIDUIValue, String columnName)` + +This method checks if the UI value matches the JSON value for a specific column. + +1. **Compare Values**: Compares the UI value with the JSON value. +2. **Log Error**: If the values do not match, logs an error message. +3. **Return Result**: Returns a boolean indicating whether the values match. + +#### Method: `clickDropdown(int numOfItemsJSON)` + +This method selects a dropdown value based on the number of JSON items. + +1. **Find Dropdown**: Locates and clicks the dropdown element. +2. **Wait for Dropdown List**: Waits for the dropdown list to become visible. +3. **Determine Selected Value**: Determines the smallest dropdown value greater than the number of JSON items. +4. **Select Option**: Iterates through the options and clicks the one matching the selected value. +5. **Wait for Invisibility**: Waits for the dropdown list to become invisible. +6. **Log and Refresh**: Logs the selected value and refreshes the page. +7. **Error Handling**: If any exception occurs, logs an error message. + +#### Method: `getSelectedDropdownValue()` + +This method retrieves the currently selected value of a dropdown. + +1. **Find Dropdown**: Locates the dropdown element. +2. **Retrieve Text**: Gets and trims the text of the dropdown element. +3. **Error Handling**: If any exception occurs, logs an error message. +4. **Return Value**: Returns the retrieved text. + +#### Method: `getSaveButtonWebElementDetailsPage()` + +This method retrieves the web element for the save button on the details page. + +1. **Locate Save Button**: Uses an XPath to find the save button element. +2. **Return Element**: Returns the located web element. + +#### Method: `testForAllText()` + +This method tests the presence of specific text elements on a page. + +1. **Log Start**: Logs the start of the text tests. +2. **Check Text**: For each expected text element: + - Calls `Common.checkTextPresent` to verify the text presence on the page. + + + +### ECLWorkUnitsTest + +![img.png](ECLWorkUnitsTest.png) + +This class, `ECLWorkUnitsTest`, in the framework.pages package, extends the `BaseTableTest` +class and specifically implements test cases for the ECL Workunits page within the ECL Watch UI. +It inherits functionalities for common table testing procedures and specializes them for the +ECL Workunits data and behavior. It includes test cases for both the main page and the details page of the workunits. + +#### Class Variables: +- badStates: A list containing states considered as 'bad', such as "compiled" and "failed". +- sortByColumnKeyWhenSortedByNone: A string representing the column key used for sorting columns when no other sorting order is specified, or the sorting order is "none". +- costColumns: A list containing column names related to cost, used for specific parsing operations. +- jsonMap: A map storing ECLWorkunit objects keyed by their unique identifiers (WUID). + +#### Methods: + +#### Method: `testingECLWorkUnitsPage` +This method is a test method annotated with `@Test`, which indicates that it is a test case to be run using a testing framework like JUnit or TestNG. The method calls `testPage()` to execute the test logic for the ECL Work Units page. + +1. Annotate the method with `@Test` to indicate that it is a test case. +2. Within the method, call the `testPage()` method to perform the necessary testing actions on the ECL Work Units page. + +#### Method: `getPageName` +This method returns the name of the page being tested, which in this case is "ECL Workunits". + +1. Override the method to return the string "ECL Workunits". + +#### Method: `getPageUrl` +The getPageUrl method is designed to retrieve the URL of a specific page within the application. It overrides a method from a superclass, BaseTableTest, and provides the URL for the "Workunits" tab under the "ECL" navigation section. This URL is fetched from a pre-configured map of URLs. + +1. The method accesses the urlMap from the URLConfig class, which stores URL mappings for different navigation sections and their tabs. +2. The method fetches the URLMapping object for the "ECL" navigation section from the urlMap using URLConfig.NAV_ECL as the key. +3. From the URLMapping object of the "ECL" navigation section, the method retrieves the nested URL mapping for the "Workunits" tab using URLConfig.TAB_ECL_WORKUNITS as the key. +4. Finally, the method returns the URL associated with the "Workunits" tab. + +#### Method: `getJsonFilePath` +This method returns the file path of the JSON file containing Workunits test data. +Combine the directory path (Config.PATH_FOLDER_JSON) with the filename (Config.WORKUNITS_JSON_FILE_NAME) to form the complete file path. + +1. Override the method to return the local path `Config.PATH_LOCAL_WORKUNITS_JSON` if the test is running locally. +2. Return the CI/CD path `Config.PATH_GH_ACTION_WORKUNITS_JSON` otherwise. + +#### Method: `getColumnNames` +This method returns an array of column names to be displayed on the ECL Work Units page. + +1. Override the method to return an array of column names: "WUID", "Owner", "Job Name", "Cluster", "State", "Total Cluster Time", "Compile Cost", "Execution Cost", "File Access Cost". + +#### Method: `getColumnKeys` +This method returns an array of column keys corresponding to the column names used in the JSON data. + +1. Override the method to return an array of column keys: "Wuid", "Owner", "Jobname", "Cluster", "State", "TotalClusterTime", "Compile Cost", "Execution Cost", "File Access Cost". + +#### Method: `getSaveButtonDetailsPage` +This method returns the label of the save button on the details page. + +1. Override the method to return the string "Save". + +#### Method: `getDetailNames` +This method returns an array of detail names to be displayed on the work unit details page. + +1. Override the method to return an array of detail names: "WUID", "Action", "State", "Owner", "Job Name", "Description", "Potential Savings", "Compile Cost", "Execution Cost", "File Access Cost", "Protected", "Cluster", "Total Cluster Time", "Aborted by", "Aborted time", "Services". + +#### Method: `getDetailKeys` +This method returns an array of detail keys corresponding to the detail names used in the JSON data. + +1. Override the method to wait for the element with ID "state" to load. +2. Check if the state value is in the list of bad states (`compiled`, `failed`). +3. If the state is in the bad states list, return a subset of detail keys: "wuid", "action", "state", "owner", "jobname", "cluster". +4. Otherwise, return the full array of detail keys: "wuid", "action", "state", "owner", "jobname", "compileCost", "executeCost", "fileAccessCost", "protected", "cluster", "totalClusterTime". + +#### Method: `getDetailKeysForPageLoad` +This method returns an array of keys to be used for loading the details page. + +1. Override the method to return an array of keys: "wuid", "state", "jobname", "cluster". + +#### Method: `getCheckboxTypeForDetailsPage` +This method returns the type of the checkbox used on the details page. + +1. Override the method to return the string "checkbox". + +#### Method: `getAttributeTypeForDetailsPage` +This method returns the attribute type used to identify elements on the details page. + +1. Override the method to return the string "type". + +#### Method: `getAttributeValueForDetailsPage` +This method returns the attribute value used to identify elements on the details page. + +1. Override the method to return the string "value". + +#### Method: `getColumnKeysWithLinks` +This method returns an array of column keys that contain links. + +1. Override the method to return an array with a single key: "Wuid". + +#### Method: `getUniqueKeyName` +This method returns the name of the unique key used to identify work units. + +1. Override the method to return the string "WUID". + +#### Method: `getUniqueKey` +This method returns the key used to uniquely identify work units. + +1. Override the method to return the string "Wuid". + +#### Method: `getTabValuesForDetailsPage` +This method returns an array of tab values for the details page. + +1. Define a method to return an array of tab values: "variables", "outputs", "inputs", "metrics", "workflows", "queries", "resources", "helpers", "xml". + +#### Method: `getColumnNamesForTabsDetailsPage` +This method returns a map where each key is the value attribute of an HTML element of a tab, and the value is a list of column names for that tab. + +1. Override the method to return a map of tab values and their corresponding column names: + - "variables": ["Type", "Name", "Value"] + - "outputs": ["Name", "File Name", "Value", "Views"] + - "inputs": ["Name", "File Cluster", "Usage"] + - "metrics": ["Refresh", "Hot spots", "Timeline", "Options"] - there is no column in this tab, so checking the presence of these buttons + - "workflows": ["Name", "Subtype", "Count", "Remaining"] + - "queries": ["ID", "Priority", "Name", "Target", "WUID", "Dll", "Published By", "Status"] + - "resources": ["Name", "Refresh", "Open", "Preview"] - Preview is not a column, it is a button, keeping it for additional check for Resources tab + - "helpers": ["Type", "Description", "File Size"] + - "xml": [" dfuLogicalFile; + + public List getDFULogicalFile() { + return dfuLogicalFile; + } + + public void setDFULogicalFile(List dfuLogicalFile) { + this.dfuLogicalFile = dfuLogicalFile; + } +} diff --git a/esp/src/test-ui/tests/framework/model/DFUQueryResponse.java b/esp/src/test-ui/tests/framework/model/DFUQueryResponse.java new file mode 100644 index 00000000000..8f7165cd450 --- /dev/null +++ b/esp/src/test-ui/tests/framework/model/DFUQueryResponse.java @@ -0,0 +1,95 @@ +package framework.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class DFUQueryResponse { + + @JsonProperty("DFULogicalFiles") + private DFULogicalFiles dfuLogicalFiles; + + @JsonProperty("Prefix") + private String prefix; + + @JsonProperty("NodeGroup") + private String nodeGroup; + + @JsonProperty("LogicalName") + private String logicalName; + + @JsonProperty("Description") + private String description; + + @JsonProperty("Owner") + private String owner; + + @JsonProperty("StartDate") + private String startDate; + + @JsonProperty("EndDate") + private String endDate; + + @JsonProperty("FileType") + private String fileType; + + @JsonProperty("FileSizeFrom") + private int fileSizeFrom; + + @JsonProperty("FileSizeTo") + private int fileSizeTo; + + @JsonProperty("FirstN") + private int firstN; + + @JsonProperty("PageSize") + private int pageSize; + + @JsonProperty("PageStartFrom") + private int pageStartFrom; + + @JsonProperty("LastPageFrom") + private int lastPageFrom; + + @JsonProperty("PageEndAt") + private int pageEndAt; + + @JsonProperty("PrevPageFrom") + private int prevPageFrom; + + @JsonProperty("NextPageFrom") + private int nextPageFrom; + + @JsonProperty("NumFiles") + private int numFiles; + + @JsonProperty("Sortby") + private String sortby; + + @JsonProperty("Descending") + private boolean descending; + + @JsonProperty("BasicQuery") + private String basicQuery; + + @JsonProperty("ParametersForPaging") + private String parametersForPaging; + + @JsonProperty("Filters") + private String filters; + + @JsonProperty("CacheHint") + private long cacheHint; + + @JsonProperty("IsSubsetOfFiles") + private String isSubsetOfFiles; + + @JsonProperty("Warning") + private String warning; + + public DFULogicalFiles getDFULogicalFiles() { + return dfuLogicalFiles; + } + + public void setDFULogicalFiles(DFULogicalFiles dfuLogicalFiles) { + this.dfuLogicalFiles = dfuLogicalFiles; + } +} diff --git a/esp/src/test-ui/tests/framework/model/DFUQueryRoot.java b/esp/src/test-ui/tests/framework/model/DFUQueryRoot.java new file mode 100644 index 00000000000..aae671c680f --- /dev/null +++ b/esp/src/test-ui/tests/framework/model/DFUQueryRoot.java @@ -0,0 +1,17 @@ +package framework.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class DFUQueryRoot { + + @JsonProperty("DFUQueryResponse") + private DFUQueryResponse dfuQueryResponse; + + public DFUQueryResponse getDFUQueryResponse() { + return dfuQueryResponse; + } + + public void setDFUQueryResponse(DFUQueryResponse dfuQueryResponse) { + this.dfuQueryResponse = dfuQueryResponse; + } +} diff --git a/esp/src/test-ui/tests/framework/model/NavigationWebElement.java b/esp/src/test-ui/tests/framework/model/NavigationWebElement.java new file mode 100644 index 00000000000..cf095ad554f --- /dev/null +++ b/esp/src/test-ui/tests/framework/model/NavigationWebElement.java @@ -0,0 +1,14 @@ +package framework.model; + +import org.openqa.selenium.WebElement; + +public record NavigationWebElement(String name, String hrefValue, WebElement webElement) { + + @Override + public String toString() { + return "NavigationWebElement{" + + "name='" + name + '\'' + + ", hrefValue='" + hrefValue + '\'' + + '}'; + } +} diff --git a/esp/src/test-ui/tests/framework/model/TestClass.java b/esp/src/test-ui/tests/framework/model/TestClass.java new file mode 100644 index 00000000000..240cdd98a90 --- /dev/null +++ b/esp/src/test-ui/tests/framework/model/TestClass.java @@ -0,0 +1,16 @@ +package framework.model; + +public class TestClass { + + String name; + String path; + + public TestClass(String name, String path) { + this.name = name; + this.path = path; + } + + public String getPath() { + return path; + } +} diff --git a/esp/src/test-ui/tests/framework/model/URLMapping.java b/esp/src/test-ui/tests/framework/model/URLMapping.java new file mode 100644 index 00000000000..bd68f686444 --- /dev/null +++ b/esp/src/test-ui/tests/framework/model/URLMapping.java @@ -0,0 +1,33 @@ +package framework.model; + +import java.util.HashMap; + +public class URLMapping { + + String name; + String url; + HashMap urlMappings; + + public URLMapping(String name, String url) { + this.name = name; + this.url = url; + urlMappings = new HashMap<>(); + } + + public String getUrl() { + return url; + } + + public HashMap getUrlMappings() { + return urlMappings; + } + + @Override + public String toString() { + return "URLMapping{" + + "name='" + name + '\'' + + ", url='" + url + '\'' + + ", urlMappings=" + urlMappings + + '}'; + } +} diff --git a/esp/src/test-ui/tests/framework/model/WUApplicationValue.java b/esp/src/test-ui/tests/framework/model/WUApplicationValue.java new file mode 100644 index 00000000000..9c6949887a1 --- /dev/null +++ b/esp/src/test-ui/tests/framework/model/WUApplicationValue.java @@ -0,0 +1,15 @@ +package framework.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class WUApplicationValue { + + @JsonProperty("Application") + private String application; + + @JsonProperty("Name") + private String name; + + @JsonProperty("Value") + private String value; +} diff --git a/esp/src/test-ui/tests/framework/model/WUApplicationValues.java b/esp/src/test-ui/tests/framework/model/WUApplicationValues.java new file mode 100644 index 00000000000..57afee6d752 --- /dev/null +++ b/esp/src/test-ui/tests/framework/model/WUApplicationValues.java @@ -0,0 +1,11 @@ +package framework.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.List; + +public class WUApplicationValues { + + @JsonProperty("ApplicationValue") + private List WUApplicationValue; +} diff --git a/esp/src/test-ui/tests/framework/model/WUECLWorkunit.java b/esp/src/test-ui/tests/framework/model/WUECLWorkunit.java new file mode 100644 index 00000000000..3db4ffc9f3a --- /dev/null +++ b/esp/src/test-ui/tests/framework/model/WUECLWorkunit.java @@ -0,0 +1,111 @@ +package framework.model; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonSetter; +import framework.utility.TimeUtils; + +public class WUECLWorkunit { + + @JsonProperty("Wuid") + private String wuid; + @JsonProperty("Owner") + private String owner; + + @JsonProperty("Cluster") + private String cluster; + + @JsonProperty("Jobname") + private String jobname; + + @JsonProperty("StateID") + private int stateID; + + @JsonProperty("State") + private String state; + + @JsonProperty("Protected") + private boolean isProtected; + + @JsonProperty("Action") + private int action; + + @JsonProperty("ActionEx") + private String actionEx; + + @JsonProperty("DateTimeScheduled") + private String dateTimeScheduled; + + @JsonProperty("IsPausing") + private boolean isPausing; + + @JsonProperty("ThorLCR") + private boolean thorLCR; + + @JsonProperty("TotalClusterTime") + private long totalClusterTime; + + @JsonProperty("ApplicationValues") + private WUApplicationValues WUApplicationValues; + + @JsonProperty("ExecuteCost") + private double executeCost; + + @JsonProperty("FileAccessCost") + private double fileAccessCost; + + @JsonProperty("CompileCost") + private double compileCost; + + @JsonProperty("NoAccess") + private boolean noAccess; + + public String getWuid() { + return wuid; + } + + public String getOwner() { + return owner; + } + + public String getCluster() { + return cluster; + } + + public String getJobname() { + return jobname; + } + + public String getState() { + return state; + } + + public long getTotalClusterTime() { + return totalClusterTime; + } + + public String getActionEx() { + return actionEx; + } + + public boolean isProtected() { + return isProtected; + } + + @JsonSetter("TotalClusterTime") + public void setTotalClusterTime(String totalClusterTime) { + this.totalClusterTime = TimeUtils.convertToMilliseconds(totalClusterTime); + } + + public double getExecuteCost() { + return executeCost; + } + + public double getFileAccessCost() { + return fileAccessCost; + } + + public double getCompileCost() { + return compileCost; + } + +} diff --git a/esp/src/test-ui/tests/framework/model/WUQueryResponse.java b/esp/src/test-ui/tests/framework/model/WUQueryResponse.java new file mode 100644 index 00000000000..a387e516f0f --- /dev/null +++ b/esp/src/test-ui/tests/framework/model/WUQueryResponse.java @@ -0,0 +1,55 @@ +package framework.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class WUQueryResponse { + + @JsonProperty("Type") + private String type; + + @JsonProperty("LogicalFileSearchType") + private String logicalFileSearchType; + + @JsonProperty("Count") + private int count; + + @JsonProperty("PageSize") + private int pageSize; + + @JsonProperty("NextPage") + private int nextPage; + + @JsonProperty("LastPage") + private int lastPage; + + @JsonProperty("NumWUs") + private int numWUs; + + @JsonProperty("First") + private boolean first; + + @JsonProperty("PageStartFrom") + private int pageStartFrom; + + @JsonProperty("PageEndAt") + private int pageEndAt; + + @JsonProperty("Descending") + private boolean descending; + + @JsonProperty("BasicQuery") + private String basicQuery; + + @JsonProperty("Filters") + private String filters; + + @JsonProperty("CacheHint") + private long cacheHint; + + @JsonProperty("Workunits") + private WUWorkunits WUWorkunits; + + public WUWorkunits getWorkunits() { + return WUWorkunits; + } +} diff --git a/esp/src/test-ui/tests/framework/model/WUQueryRoot.java b/esp/src/test-ui/tests/framework/model/WUQueryRoot.java new file mode 100644 index 00000000000..c5a1c54993d --- /dev/null +++ b/esp/src/test-ui/tests/framework/model/WUQueryRoot.java @@ -0,0 +1,12 @@ +package framework.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class WUQueryRoot { + @JsonProperty("WUQueryResponse") + private WUQueryResponse wuQueryResponse; + + public WUQueryResponse getWUQueryResponse() { + return wuQueryResponse; + } +} diff --git a/esp/src/test-ui/tests/framework/model/WUWorkunits.java b/esp/src/test-ui/tests/framework/model/WUWorkunits.java new file mode 100644 index 00000000000..136770b2234 --- /dev/null +++ b/esp/src/test-ui/tests/framework/model/WUWorkunits.java @@ -0,0 +1,15 @@ +package framework.model; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.List; + +public class WUWorkunits { + + @JsonProperty("ECLWorkunit") + private List WUECLWorkunit; + + public List getECLWorkunit() { + return WUECLWorkunit; + } +} diff --git a/esp/src/test-ui/tests/framework/pages/ActivitiesTest.java b/esp/src/test-ui/tests/framework/pages/ActivitiesTest.java new file mode 100644 index 00000000000..9229481f79a --- /dev/null +++ b/esp/src/test-ui/tests/framework/pages/ActivitiesTest.java @@ -0,0 +1,123 @@ +package framework.pages; + +import framework.config.URLConfig; +import framework.model.NavigationWebElement; +import framework.model.URLMapping; +import framework.utility.Common; +import org.openqa.selenium.By; +import org.openqa.selenium.TimeoutException; +import org.openqa.selenium.WebElement; +import org.testng.annotations.Test; + +import java.util.ArrayList; +import java.util.List; + +import static framework.config.URLConfig.urlMap; + +public class ActivitiesTest { + + static final String[] textArray = {"Target/Wuid", "Graph", "State", "Owner", "Job Name"}; + + @Test + public void testActivitiesPage() { + if(!Common.openWebPage(urlMap.get(URLConfig.NAV_ACTIVITIES).getUrl())){ + return; + } + + Common.logDebug("Tests started for: Activities page."); + + testForAllText(); + + List navWebElements = getNavWebElements(); + + testForNavigationLinks(navWebElements); + + Common.logDebug("Tests finished for: Activities page."); + Common.logDebug("URL Map Generated: " + urlMap); + } + + private void testForNavigationLinks(List navWebElements) { + + Common.logDebug("Tests started for: Activities page: Testing Navigation Links"); + + for (NavigationWebElement element : navWebElements) { + + try { + element.webElement().click(); + + if (testTabsForNavigationLinks(element)) { + String msg = "Success: Navigation Menu Link for " + element.name() + ". URL : " + element.hrefValue(); + Common.logDetail(msg); + } else { + String currentPage = getCurrentPage(); + String errorMsg = "Failure: Navigation Menu Link for " + element.name() + " page failed. The current navigation page that we landed on is " + currentPage + ". Current URL : " + element.hrefValue(); + Common.logError(errorMsg); + } + } catch (Exception ex) { + Common.logException("Failure: Exception in Navigation Link for " + element.name() + ". URL : " + element.hrefValue() + " Error: " + ex.getMessage(), ex); + } + } + } + + private String getCurrentPage() { + + for (var entry : URLConfig.tabsListMap.entrySet()) { + + List tabs = entry.getValue(); + boolean allTabsPresent = true; + for (String tab : tabs) { + if (!Common.driver.getPageSource().contains(tab)) { + allTabsPresent = false; + break; + } + } + + if (allTabsPresent) { + return entry.getKey(); + } + } + + return "Invalid Page"; + } + + private boolean testTabsForNavigationLinks(NavigationWebElement element) { + List tabsList = URLConfig.tabsListMap.get(element.name()); + + for (String tab : tabsList) { + try { + WebElement webElement = Common.waitForElement(By.xpath("//a[text()='" + tab + "']")); + urlMap.get(element.name()).getUrlMappings().put(tab, new URLMapping(tab, webElement.getAttribute("href"))); + } catch (TimeoutException ex) { + return false; + } + } + + return true; + } + + private List getNavWebElements() { + + List navWebElements = new ArrayList<>(); + + for (String navName : URLConfig.navNamesArray) { + try { + WebElement webElement = Common.driver.findElement(By.name(navName)).findElement(By.tagName("a")); + String hrefValue = webElement.getAttribute("href"); + navWebElements.add(new NavigationWebElement(navName, hrefValue, webElement)); + + urlMap.put(navName, new URLMapping(navName, hrefValue)); + } catch (Exception ex) { + Common.logException("Failure: Activities Page for Navigation Element: " + navName + ": Error: " + ex.getMessage(), ex); + } + } + + return navWebElements; + } + + private void testForAllText() { + Common.logDebug("Tests started for: Activities page: Testing Text"); + for (String text : textArray) { + Common.checkTextPresent(text, "Activities Page"); + } + } +} diff --git a/esp/src/test-ui/tests/framework/pages/BaseTableTest.java b/esp/src/test-ui/tests/framework/pages/BaseTableTest.java new file mode 100644 index 00000000000..e53088c9153 --- /dev/null +++ b/esp/src/test-ui/tests/framework/pages/BaseTableTest.java @@ -0,0 +1,587 @@ +package framework.pages; + +import framework.config.Config; +import framework.utility.Common; +import org.openqa.selenium.By; +import org.openqa.selenium.JavascriptExecutor; +import org.openqa.selenium.TimeoutException; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.ui.ExpectedConditions; +import org.openqa.selenium.support.ui.WebDriverWait; + +import java.time.Duration; +import java.util.*; +import java.util.function.Function; + + +// This class is the super class for all the those web pages that contains a tabular data for e.g. workunits, files, queries etc. It also contains the tests for their respective details page including testing text, content and tabs. + +public abstract class BaseTableTest { + + protected abstract String getPageName(); + + protected abstract String getPageUrl(); + + protected abstract String getJsonFilePath(); + + protected abstract String getSaveButtonDetailsPage(); + + protected abstract String[] getColumnNames(); + + protected abstract String[] getDetailNames(); + + protected abstract String[] getColumnKeys(); + + protected abstract String[] getDetailKeys(); + + protected abstract String getCheckboxTypeForDetailsPage(); + + protected abstract String getAttributeTypeForDetailsPage(); + + protected abstract String getAttributeValueForDetailsPage(); + + protected abstract String[] getDetailKeysForPageLoad(); + + protected abstract String getUniqueKeyName(); + + protected abstract String getUniqueKey(); + + protected abstract String[] getColumnKeysWithLinks(); + + protected abstract Object parseDataUIValue(Object dataUIValue, Object dataJSONValue, String columnName, Object dataIDUIValue); + + protected abstract Object parseDataJSONValue(Object dataJSONValue, String columnName, Object dataIDUIValue); + + protected abstract List parseJson(String filePath) throws Exception; + + protected abstract Object getColumnDataFromJson(T object, String columnKey); + + protected abstract void sortJsonUsingSortOrder(String currentSortOrder, String columnKey); + + protected abstract String getCurrentPage(); + + protected abstract Map getJsonMap(); + + protected abstract Map> getColumnNamesForTabsDetailsPage(); + + protected abstract String[] getTabValuesForDetailsPage(); + + protected abstract void testDetailSpecificFunctionality(String name, int i); + + protected List jsonObjects; + + protected void testPage() { + if(!Common.openWebPage(getPageUrl())){ + return; + } + + try { + Common.logDebug("Tests started for: " + getPageName() + " page."); + + testForAllText(); + + jsonObjects = getAllObjectsFromJson(); + if (jsonObjects != null) { + + int numOfItemsJSON = jsonObjects.size(); + clickDropdown(numOfItemsJSON); + + testContentAndSortingOrder(); + } + + testLinksInTable(); + + Common.logDebug("Tests finished for: " + getPageName() + " page."); + + } catch (Exception ex) { + Common.logException("Error: " + getPageName() + ": Exception: " + ex.getMessage(), ex); + } + } + + private void testDetailsPage(String name, int i) { + + if (Config.TEST_DETAIL_PAGE_FIELD_NAMES_ALL) { // TEST_DETAIL_PAGE_FIELD_NAMES_ALL = true means the test will run for all items and false means it will only run for the first item + testForAllTextInDetailsPage(name); + } else if (i == 0) { + testForAllTextInDetailsPage(name); + } + + testDetailsContentPage(name); + testDetailSpecificFunctionality(name, i); + + if (Config.TEST_DETAIL_PAGE_TAB_CLICK_ALL) { // TEST_DETAIL_PAGE_TAB_CLICK_ALL = true means the test will run for all items and false means it will only run for the first item + testTabClickOnDetailsPage(name); + } else if (i == 0) { + testTabClickOnDetailsPage(name); + } + } + + private void testTabClickOnDetailsPage(String name) { + + Common.logDebug("Test started for: Tab Click on " + getPageName() + " Details Page. For: " + name); + + waitToLoadDetailsPage(); + + for (var tabValue : getTabValuesForDetailsPage()) { + + try { + WebElement element = Common.driver.findElement(By.xpath("//button[@value='" + tabValue + "']")); + javaScriptElementClick(element); + testPresenceOfColumnNames(getColumnNamesForTabsDetailsPage().get(tabValue), tabValue); + } catch (Exception ex) { + Common.logException("Error: " + getPageName() + " Details Page. Testing tab click on: " + tabValue + " Error: " + ex.getMessage(), ex); + } + } + + } + + protected void javaScriptElementClick(WebElement element) { + + JavascriptExecutor js = (JavascriptExecutor) Common.driver; + js.executeScript("arguments[0].click();", element); + } + + private void testPresenceOfColumnNames(List columnNames, String tabValue) { + + boolean allPresent = true; + String currentURL = Common.driver.getCurrentUrl(); + for (String columnName : columnNames) { + + try { + Common.waitForElement(By.xpath("//*[text()='" + columnName + "']")); + } catch (TimeoutException ex) { + Common.logError("Failure: " + getPageName() + " Details Page. Text : " + columnName + " not present for tab: " + tabValue + ". Current URL: " + currentURL); + allPresent = false; + } + } + + if (allPresent) { + Common.logDetail("Success: " + getPageName() + " Details Page. Tab click on: " + tabValue); + } + } + + protected void clickOnSaveButton() { + + WebElement saveButton = getSaveButtonWebElementDetailsPage(); + Common.waitForElementToBeClickable(saveButton); + saveButton.click(); + + saveButton = getSaveButtonWebElementDetailsPage(); + Common.waitForElementToBeDisabled(saveButton); + } + + private void testLinksInTable() { + + Common.logDebug("Tests started for: " + getPageName() + " page: Testing Links"); + + Common.driver.navigate().refresh(); // refreshing page as page has scrolled to right for testing the sorting functionality of column headers, so bringing it back to normal view by refreshing it + + for (String columnKey : getColumnKeysWithLinks()) { + + List values = getDataFromUIUsingColumnKey(columnKey); + + for (int i = 0; i < values.size(); i++) { + + try { + String name = values.get(i).toString().trim(); + + WebElement element = Common.waitForElement(By.xpath("//div[text()='" + name + "']/..")); + String href = Common.driver.getCurrentUrl(); + + String dropdownValueBefore = getSelectedDropdownValue(); + element.click(); + + if (Common.driver.getPageSource().contains(name)) { + String msg = "Success: " + getPageName() + ": Link Test Pass for " + (i + 1) + ". " + name + ". URL : " + href; + Common.logDetail(msg); + // after the link test has passed, the code tests the details page(including, the text, content, checkbox, description and tabs) + testDetailsPage(name, i); + + } else { + String currentPage = getCurrentPage(); + String errorMsg = "Failure: " + getPageName() + ": Link Test Fail for " + (i + 1) + ". " + name + " page failed. The current navigation page that we landed on is " + currentPage + ". Current URL : " + href; + Common.logError(errorMsg); + } + + Common.driver.navigate().to(getPageUrl()); + Common.driver.navigate().refresh(); + + String dropdownValueAfter = getSelectedDropdownValue(); + + // Log error if the dropdown value has changed + if (!dropdownValueBefore.equals(dropdownValueAfter)) { + String dropdownErrorMsg = "Failure: " + getPageName() + ": Dropdown value changed after navigating back. Before: " + dropdownValueBefore + ", After: " + dropdownValueAfter; + Common.logError(dropdownErrorMsg); + } + } catch (Exception ex) { + Common.logException("Failure: " + getPageName() + ": Exception in testing links for column: " + columnKey + " value: " + values.get(i) + " Error: " + ex.getMessage(), ex); + } + } + } + } + + protected void waitToLoadDetailsPage() { + + int waitTimeInSecs = Config.WAIT_TIME_IN_SECONDS; + + boolean allVisible; + + do { + Common.sleepWithTime(waitTimeInSecs); + allVisible = true; + + for (String detailKey : getDetailKeysForPageLoad()) { + WebElement element = Common.waitForElement(By.id(detailKey)); + if ("".equals(element.getAttribute(getAttributeValueForDetailsPage()))) { + allVisible = false; + break; + } + } + + if (allVisible) { + break; + } + + waitTimeInSecs++; + + } while (waitTimeInSecs < Config.WAIT_TIME_THRESHOLD_IN_SECONDS); + + Common.logDebug("Sleep Time In Seconds To Load Details Page: " + waitTimeInSecs); + } + + private void testDetailsContentPage(String name) { + Common.logDebug("Tests started for : " + getPageName() + " Details page: Testing Content: " + getUniqueKeyName() + " : " + name); + try { + waitToLoadDetailsPage(); // sleep until the specific detail fields have loaded + } catch (Exception ex) { + Common.logException("Error: " + getPageName() + ": Exception in waitToLoadDetailsPage: " + getUniqueKeyName() + " : " + name + " Exception: " + ex.getMessage(), ex); + } + + boolean pass = true; + + T object = getJsonMap().get(name); + + for (String detailKey : getDetailKeys()) { + try { + WebElement element = Common.waitForElement(By.id(detailKey)); + + Object dataUIValue, dataJSONValue; + + if (element.getAttribute(getAttributeTypeForDetailsPage()).equals(getCheckboxTypeForDetailsPage())) { + dataUIValue = element.isSelected(); + } else { + dataUIValue = element.getAttribute(getAttributeValueForDetailsPage()); + } + + dataJSONValue = getColumnDataFromJson(object, detailKey); + + dataUIValue = parseDataUIValue(dataUIValue, dataJSONValue, detailKey, name); + dataJSONValue = parseDataJSONValue(dataJSONValue, detailKey, name); + + if (!dataUIValue.equals(dataJSONValue)) { + Common.logError("Failure: " + getPageName() + " Details page, Incorrect " + detailKey + " : " + dataUIValue + " in UI for " + getUniqueKeyName() + " : " + name + ". Correct " + detailKey + " is: " + dataJSONValue); + pass = false; + } + + } catch (Exception ex) { + Common.logException("Error: Details " + getPageName() + "Page, for: " + getUniqueKeyName() + " : " + name + " Error: " + ex.getMessage(), ex); + } + } + + if (pass) { + Common.logDetail("Success: " + getPageName() + " Details page: All content test passed for: " + getUniqueKeyName() + " : " + name); + } + } + + private void testForAllTextInDetailsPage(String name) { + Common.logDebug("Tests started for: " + getPageName() + " Details page: " + getUniqueKeyName() + ": " + name + ": Testing Text"); + for (String text : getDetailNames()) { + Common.checkTextPresent(text, getPageName() + " Details page: " + getUniqueKeyName() + ": " + name); + } + } + + private void testContentAndSortingOrder() { + + Common.logDebug("Tests started for: " + getPageName() + " page: Testing Content"); + + if (testTableContent()) { + + Common.logDebug("Tests started for: " + getPageName() + " page: Testing Sorting Order"); + + for (int i = 0; i < getColumnKeys().length; i++) { + try { + testTheSortingOrderForOneColumn(getColumnKeys()[i], getColumnNames()[i]); + } catch (Exception ex) { + Common.logException("Error: " + getPageName() + ": Exception while testing sort order for column: " + getColumnNames()[i] + ": Exception:" + ex.getMessage(), ex); + } + } + } + } + + private void testTheSortingOrderForOneColumn(String columnKey, String columnName) { + for (int i = 0; i < 3; i++) { + + String currentSortOrder = getCurrentSortingOrder(columnKey); + if (currentSortOrder != null) { + List columnDataFromUI = getDataFromUIUsingColumnKey(columnKey); + + sortJsonUsingSortOrder(currentSortOrder, columnKey); + + List columnDataFromJSON = getDataFromJSONUsingColumnKey(columnKey); + List columnDataIDFromUI = getDataFromUIUsingColumnKey(getUniqueKey()); + + if (compareData(columnDataFromUI, columnDataFromJSON, columnDataIDFromUI, columnName)) { + Common.logDebug("Success: " + getPageName() + ": Values are correctly sorted in " + currentSortOrder + " order by: " + columnName); + } else { + String errMsg = "Failure: " + getPageName() + ": Values are not correctly sorted in " + currentSortOrder + " order by: " + columnName; + Common.logError(errMsg); + } + } else { + Common.logError("Failure: " + getPageName() + " Unable to get sort order for column: " + columnKey); + } + } + } + + private String getCurrentSortingOrder(String columnKey) { + + try { + + WebElement columnHeader = Common.driver.findElement(By.xpath("//*[@role='columnheader' and @*[.='" + columnKey + "']]")); + + // Scroll to bring the column header into view + ((JavascriptExecutor) Common.driver).executeScript("arguments[0].scrollIntoView(true);", columnHeader); + + String oldSortOrder = columnHeader.getAttribute("aria-sort"); + + columnHeader.click(); + + return waitToLoadChangedSortOrder(oldSortOrder, columnKey); + + } catch (Exception ex) { + Common.logException("Failure: " + getPageName() + " Exception in getting sort order for column: " + columnKey + " Error: " + ex.getMessage(), ex); + } + + return null; + } + + private String waitToLoadChangedSortOrder(String oldSortOrder, String columnKey) { + + int waitTimeInSecs = Config.WAIT_TIME_IN_SECONDS; + String newSortOrder; + + do { + Common.sleepWithTime(waitTimeInSecs); + + WebElement columnHeaderNew = Common.driver.findElement(By.xpath("//*[@role='columnheader' and @*[.='" + columnKey + "']]")); + + newSortOrder = columnHeaderNew.getAttribute("aria-sort"); + + if (!Objects.equals(newSortOrder, oldSortOrder)) { + break; + } + + waitTimeInSecs++; + + } while (waitTimeInSecs < Config.WAIT_TIME_THRESHOLD_IN_SECONDS); + + return newSortOrder; + } + + private List getDataFromJSONUsingColumnKey(String columnKey) { + List columnDataFromJSON = new ArrayList<>(); + for (T jsonObject : jsonObjects) { + columnDataFromJSON.add(getColumnDataFromJson(jsonObject, columnKey)); + } + return columnDataFromJSON; + } + + protected List getDataFromUIUsingColumnKey(String columnKey) { + + List columnData = new ArrayList<>(); + + try { + + List elements = waitToLoadListOfAllUIObjects(columnKey); + for (WebElement element : elements) { + columnData.add(element.getText().trim()); + } + + } catch (Exception ex) { + Common.logException("Failure: " + getPageName() + ": Error in getting data from UI using column key: " + columnKey + "Error: " + ex.getMessage(), ex); + } + + return columnData; + } + + private List waitToLoadListOfAllUIObjects(String columnKey) { + + int waitTimeInSecs = Config.WAIT_TIME_IN_SECONDS; + List elements; + + do { + + elements = Common.driver.findElements(By.xpath("//*[@role='gridcell' and @*[.='" + columnKey + "']]")); + + if (elements.size() >= jsonObjects.size()) { + break; + } + + Common.sleepWithTime(waitTimeInSecs++); + + } while (waitTimeInSecs < Config.WAIT_TIME_THRESHOLD_IN_SECONDS); + + return elements; + } + + @SuppressWarnings("unchecked") + void ascendingSortJson(String columnKey) { + try { + jsonObjects.sort(Comparator.comparing(jsonObject -> (Comparable) getColumnDataFromJson(jsonObject, columnKey))); + } catch (Exception ex) { + Common.logException("Failure: " + getPageName() + ": Exception in sorting JSON in ascending order using column key: " + columnKey + " Error: " + ex.getMessage(), ex); + } + } + + @SuppressWarnings("unchecked") + void descendingSortJson(String columnKey) { + try { + jsonObjects.sort(Comparator.comparing((Function>) jsonObject -> (Comparable) getColumnDataFromJson(jsonObject, columnKey)).reversed()); + } catch (Exception ex) { + Common.logException("Failure: " + getPageName() + ": Exception in sorting JSON in descending order using column key: " + columnKey + " Error: " + ex.getMessage(), ex); + } + } + + private boolean testTableContent() { + Common.logDebug("Page: " + getPageName() + ": Number of Objects from Json: " + jsonObjects.size()); + + List columnDataIDFromUI = getDataFromUIUsingColumnKey(getUniqueKey()); + + Common.logDebug("Page: " + getPageName() + ": Number of Objects from UI: " + columnDataIDFromUI.size()); + + if (jsonObjects.size() != columnDataIDFromUI.size()) { + String errMsg = "Failure: " + getPageName() + ": Number of items on UI are not equal to the number of items in JSON" + + "\nNumber of Objects from Json: " + jsonObjects.size() + + "\nNumber of Objects from UI: " + columnDataIDFromUI.size(); + Common.logError(errMsg); + return false; + } + + boolean pass = true; + + for (int i = 0; i < getColumnKeys().length; i++) { + List columnDataFromUI = getDataFromUIUsingColumnKey(getColumnKeys()[i]); + List columnDataFromJSON = getDataFromJSONUsingColumnKey(getColumnKeys()[i]); + if (!compareData(columnDataFromUI, columnDataFromJSON, columnDataIDFromUI, getColumnNames()[i])) { + pass = false; + } + } + + return pass; + } + + private List getAllObjectsFromJson() { + String filePath = getJsonFilePath(); + try { + return parseJson(filePath); + } catch (Exception ex) { + Common.logException("Failure: " + getPageName() + " Unable to parse JSON File: Exception: " + ex.getMessage(), ex); + } + + return null; + } + + private boolean compareData(List dataUI, List dataJSON, List dataIDUI, String columnName) { + + boolean pass = true; + + for (int i = 0; i < dataUI.size(); i++) { + + Object dataUIValue = dataUI.get(i); + Object dataJSONValue = dataJSON.get(i); + Object dataIDUIValue = dataIDUI.get(i); + + dataUIValue = parseDataUIValue(dataUIValue, dataJSONValue, columnName, dataIDUIValue); + dataJSONValue = parseDataJSONValue(dataJSONValue, columnName, dataIDUIValue); + + if (!checkValues(dataUIValue, dataJSONValue, dataIDUIValue, columnName)) { + pass = false; + } + } + + if (pass) { + Common.logDetail("Success: " + getPageName() + ": Content test passed for column: " + columnName); + } + + return pass; + } + + private boolean checkValues(Object dataUIValue, Object dataJSONValue, Object dataIDUIValue, String columnName) { + if (!dataUIValue.equals(dataJSONValue)) { + String errMsg = "Failure: " + getPageName() + ": Incorrect " + columnName + " : " + dataUIValue + " in UI for " + getUniqueKeyName() + " : " + dataIDUIValue + ". Correct " + columnName + " is: " + dataJSONValue; + Common.logError(errMsg); + return false; + } + + return true; + } + + private void clickDropdown(int numOfItemsJSON) { + + try { + + WebElement dropdown = Common.waitForElement(By.id("pageSize")); + dropdown.click(); + + WebDriverWait wait = new WebDriverWait(Common.driver, Duration.ofSeconds(Config.WAIT_TIME_THRESHOLD_IN_SECONDS)); + + WebElement dropdownList = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("pageSize-list"))); + + List options = dropdownList.findElements(By.tagName("button")); + + int selectedValue = 0; + + // the smallest dropdown value greater than numOfItemsJSON + + for (WebElement option : options) { + int value = Integer.parseInt(option.getText()); + if (numOfItemsJSON < value) { + option.click(); + selectedValue = value; + break; + } + } + + wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("pageSize-list"))); + + Common.logDebug("Page: " + getPageName() + ": Dropdown selected: " + selectedValue); + + Common.driver.navigate().refresh(); + Common.sleep(); + } catch (Exception ex) { + Common.logException("Failure: " + getPageName() + ": Error in clicking dropdown: " + ex.getMessage(), ex); + } + } + + private String getSelectedDropdownValue() { + try { + WebElement dropdown = Common.waitForElement(By.id("pageSize")); + return dropdown.getText().trim(); + } catch (Exception ex) { + Common.logException("Failure: " + getPageName() + ": Error in getting dropdown value: " + ex.getMessage(), ex); + } + + return ""; + } + + protected WebElement getSaveButtonWebElementDetailsPage() { + return Common.waitForElement(By.xpath("//button//*[text()='" + getSaveButtonDetailsPage() + "']/../../..")); + } + + private void testForAllText() { + Common.logDebug("Tests started for: " + getPageName() + " page: Testing Text"); + for (String text : getColumnNames()) { + Common.checkTextPresent(text, getPageName()); + } + } +} + diff --git a/esp/src/test-ui/tests/framework/pages/ECLWorkUnitsTest.java b/esp/src/test-ui/tests/framework/pages/ECLWorkUnitsTest.java new file mode 100644 index 00000000000..ac9185a0685 --- /dev/null +++ b/esp/src/test-ui/tests/framework/pages/ECLWorkUnitsTest.java @@ -0,0 +1,424 @@ +package framework.pages; + +import com.fasterxml.jackson.databind.ObjectMapper; +import framework.config.Config; +import framework.config.URLConfig; +import framework.model.WUECLWorkunit; +import framework.model.WUQueryResponse; +import framework.model.WUQueryRoot; +import framework.utility.Common; +import framework.utility.TimeUtils; +import org.openqa.selenium.By; +import org.openqa.selenium.Keys; +import org.openqa.selenium.WebElement; +import org.testng.annotations.Test; + +import java.io.File; +import java.util.*; + +// This class is a subclass of BaseTableTest, and it includes test cases for ECL Workunits page and along with the tests of workunits details page. + +public class ECLWorkUnitsTest extends BaseTableTest { + + @Test + public void testingECLWorkUnitsPage() { + testPage(); + } + + @Override + protected String getPageName() { + return "ECL Workunits"; + } + + @Override + protected String getPageUrl() { + try { + return URLConfig.urlMap.get(URLConfig.NAV_ECL).getUrlMappings().get(URLConfig.TAB_ECL_WORKUNITS).getUrl(); + } catch (Exception ex){ + Common.logException("Error in getting page URL "+ getPageName() +": Exception: "+ ex.getMessage(), ex); + } + return ""; + } + + @Override + protected String getJsonFilePath() { + return Config.PATH_FOLDER_JSON + Config.WORKUNITS_JSON_FILE_NAME; + } + + @Override + protected String[] getColumnNames() { + return new String[]{"WUID", "Owner", "Job Name", "Cluster", "State", "Total Cluster Time", "Compile Cost", "Execution Cost", "File Access Cost"}; + } + + @Override + protected String[] getColumnKeys() { + return new String[]{"Wuid", "Owner", "Jobname", "Cluster", "State", "TotalClusterTime", "Compile Cost", "Execution Cost", "File Access Cost"}; + } + + private final List badStates = Arrays.asList("compiled", "failed"); + + @Override + protected String getSaveButtonDetailsPage() { + return "Save"; + } + + @Override + protected String[] getDetailNames() { + return new String[]{"WUID", "Action", "State", "Owner", "Job Name", "Description", "Potential Savings", "Compile Cost", "Execution Cost", "File Access Cost", "Protected", "Cluster", "Total Cluster Time", "Aborted by", "Aborted time", "Services"}; + } + + @Override + protected String[] getDetailKeys() { + WebElement element = Common.waitForElement(By.id("state")); + + if (badStates.contains(element.getAttribute(getAttributeValueForDetailsPage()))) { // compiled means it's published but not executed - no wu exists for this, check only these columns + return new String[]{"wuid", "action", "state", "owner", "jobname", "cluster"}; + } else { + return new String[]{"wuid", "action", "state", "owner", "jobname", "compileCost", "executeCost", "fileAccessCost", "protected", "cluster", "totalClusterTime"}; + } + } + + @Override + protected String[] getDetailKeysForPageLoad() { + return new String[]{"wuid", "state", "jobname", "cluster"}; + } + + @Override + protected String getCheckboxTypeForDetailsPage() { + return "checkbox"; + } + + @Override + protected String getAttributeTypeForDetailsPage() { + return "type"; + } + + @Override + protected String getAttributeValueForDetailsPage() { + return "value"; + } + + @Override + protected String[] getColumnKeysWithLinks() { + return new String[]{"Wuid"}; + } + + @Override + protected String getUniqueKeyName() { + return "WUID"; + } + + @Override + protected String getUniqueKey() { + return "Wuid"; + } + + String sortByColumnKeyWhenSortedByNone = "Wuid"; + + protected String[] getTabValuesForDetailsPage() { + return new String[]{"variables", "outputs", "inputs", "metrics", "workflows", + "queries", "resources", "helpers", "xml"}; + } + + @Override + protected Map> getColumnNamesForTabsDetailsPage() { // key in this map is the value attribute of the html element of a tab. + return Map.ofEntries( + Map.entry("variables", List.of("Type", "Name", "Value")), + Map.entry("outputs", List.of("Name", "File Name", "Value", "Views")), + Map.entry("inputs", List.of("Name", "File Cluster", "Usage")), + Map.entry("metrics", List.of("Refresh", "Hot spots", "Timeline", "Options")), // there is no column in this tab, so checking the presence of these buttons + Map.entry("workflows", List.of("Name", "Subtype", "Count", "Remaining")), + Map.entry("queries", List.of("ID", "Priority", "Name", "Target", "WUID", "Dll", "Published By", "Status")), + Map.entry("resources", List.of("Name", "Refresh", "Open", "Preview")), // Preview is not a column, it is a button, keeping it for additional check for Resources tab + Map.entry("helpers", List.of("Type", "Description", "File Size")), + Map.entry("xml", List.of(" costColumns = Arrays.asList("Compile Cost", "Execution Cost", "File Access Cost", "compileCost", "executeCost", "fileAccessCost"); + + @Override + protected Map getJsonMap() { + return jsonMap; + } + + @Override + protected void testDetailSpecificFunctionality(String wuName, int i) { + + if (Config.TEST_WU_DETAIL_PAGE_PROTECTED_ALL) { // TEST_WU_DETAIL_PAGE_PROTECTED_ALL = true means the test will run for all workunits and false means it will only run for the first workunit + testProtectedButtonFunctionality(wuName); + } else if (i == 0) { + testProtectedButtonFunctionality(wuName); + } + + if (Config.TEST_WU_DETAIL_PAGE_DESCRIPTION_ALL) { // TEST_WU_DETAIL_PAGE_DESCRIPTION_ALL = true means the test will run for all workunits and false means it will only run for the first workunit + testDescriptionUpdateFunctionality(wuName); + } else if (i == 0) { + testDescriptionUpdateFunctionality(wuName); + } + } + + private void testDescriptionUpdateFunctionality(String wuName) { + Common.logDebug("Test started for: Description " + getPageName() + " Details Page. For: " + wuName); + + try { + String newDescription = Config.TEST_DESCRIPTION_TEXT; + + String oldDescription = updateDescriptionAndSave(newDescription); + + testIfTheDescriptionUpdated(wuName, newDescription); + + Common.logDetail("Reverting Description Update To Old Value: " + getPageName() + " Details page, for WUID: " + wuName); + + updateDescriptionAndSave(oldDescription); + + } catch (Exception ex) { + Common.logException("Error: " + getPageName() + " Details page for WUID: " + wuName + ", Exception occurred while testing for description. Exception: " + ex.getMessage(), ex); + } + } + + private void testIfTheDescriptionUpdated(String wuName, String newDescription) { + + Common.driver.navigate().refresh(); + + waitToLoadDetailsPage(); + + WebElement element = Common.waitForElement(By.id("description")); + + String updatedDescription = element.getAttribute(getAttributeValueForDetailsPage()).trim(); + + if (newDescription.equals(updatedDescription)) { + Common.logDetail("Success: " + getPageName() + " Details page. Description Updated Successfully on click of save button: Description After refresh showing on UI: " + updatedDescription + ", Updated description was: " + newDescription + " for WUID: " + wuName); + } else { + Common.logError("Failure: " + getPageName() + " Details page. Description Did Not Update on click of save button : Description After refresh showing on UI: " + updatedDescription + ", Updated description should be: " + newDescription + " for WUID: " + wuName); + } + } + + private String updateDescriptionAndSave(String newDescription) { + + waitToLoadDetailsPage(); + + WebElement element = Common.waitForElement(By.id("description")); + + String oldDescription = element.getAttribute(getAttributeValueForDetailsPage()); + + element.sendKeys(Keys.CONTROL + "a"); // Select all text + element.sendKeys(Keys.DELETE); // Delete all selected text + + element.sendKeys(newDescription); + + clickOnSaveButton(); + + element = Common.waitForElement(By.id("description")); + Common.logDetail("Old Description: " + oldDescription + ", Updated Description After Save: " + element.getAttribute(getAttributeValueForDetailsPage())); + + return oldDescription; + } + + private void testProtectedButtonFunctionality(String wuName) { + Common.logDebug("Test started for: Protected checkbox " + getPageName() + " Details Page. For: " + wuName); + + try { + + boolean newCheckboxValue = clickProtectedCheckboxAndSave(wuName); + + checkProtectedStatusOnECLWorkunitsPage(wuName, newCheckboxValue); + + WebElement element = Common.waitForElement(By.xpath("//div[text()='" + wuName + "']/..")); + element.click(); + + if (checkIfNewCheckBoxValuePresentInDetailsPage(newCheckboxValue)) { + Common.logDetail(getPageName() + " Details Page: Reverting the checkbox value for WUID: " + wuName); + clickProtectedCheckboxAndSave(wuName); + } + + } catch (Exception ex) { + Common.logException("Error: ECL WorkUnits: Exception in testing the protected functionality for WUID: " + wuName + ": Error: " + ex.getMessage(), ex); + } + } + + private boolean checkIfNewCheckBoxValuePresentInDetailsPage(boolean newCheckboxValue) { + + waitToLoadDetailsPage(); + + WebElement protectedCheckbox = Common.waitForElement(By.id("protected")); + boolean currentCheckboxValue = protectedCheckbox.isSelected(); + + if (currentCheckboxValue != newCheckboxValue) { + Common.logError("Failure: " + getPageName() + " Details Page: Testing Protected checkbox value after coming back from ECL Workunits page: Updated value: " + newCheckboxValue + ": Showing: " + currentCheckboxValue + " on Workunits Details page"); + return false; + } else { + Common.logDetail("Success: " + getPageName() + " Details Page: Testing Protected checkbox value after coming back from ECL Workunits page: Updated value: " + newCheckboxValue + ": Showing: " + currentCheckboxValue + " on Workunits Details page"); + return true; + } + } + + private boolean clickProtectedCheckboxAndSave(String wuName) { + + waitToLoadDetailsPage(); + + WebElement protectedCheckbox = Common.waitForElement(By.id("protected")); + boolean oldCheckboxValue = protectedCheckbox.isSelected(); + + javaScriptElementClick(protectedCheckbox); + + waitToLoadUpdatedProtectedCheckbox(oldCheckboxValue); + + clickOnSaveButton(); + + boolean newCheckboxValue = protectedCheckbox.isSelected(); + + Common.logDetail("Protected checkbox old Value: " + oldCheckboxValue + ", current value after save: " + newCheckboxValue + " for WUID: " + wuName); + + return newCheckboxValue; + } + + private void waitToLoadUpdatedProtectedCheckbox(boolean oldCheckboxValue) { + + int waitTimeInSecs = Config.WAIT_TIME_IN_SECONDS; + boolean newCheckboxValue; + + do { + Common.sleepWithTime(waitTimeInSecs); + + WebElement protectedCheckbox = Common.waitForElement(By.id("protected")); + newCheckboxValue = protectedCheckbox.isSelected(); + + if (!Objects.equals(newCheckboxValue, oldCheckboxValue)) { + break; + } + + waitTimeInSecs++; + + } while (waitTimeInSecs < Config.WAIT_TIME_THRESHOLD_IN_SECONDS); + } + + private void checkProtectedStatusOnECLWorkunitsPage(String wuName, boolean newCheckboxValue) { + + Common.driver.navigate().to(getPageUrl()); + Common.driver.navigate().refresh(); + + List columnDataWUID = getDataFromUIUsingColumnKey(getUniqueKey()); + List columnDataProtected = getDataFromUIUsingColumnKey("Protected"); + + for (int i = 0; i < columnDataProtected.size(); i++) { + + if (wuName.equals(columnDataWUID.get(i))) { + + String lockStatus = !columnDataProtected.get(i).toString().isEmpty() ? "Locked" : "Unlocked"; + + if (newCheckboxValue && lockStatus.equals("Locked")) { + Common.logDetail("Success: " + getPageName() + " Details Page: Testing Protected checkbox for value: true : Showing Locked on ECL Workunits page"); + } else if (!newCheckboxValue && lockStatus.equals("Unlocked")) { + Common.logDetail("Success: " + getPageName() + " Details Page: Testing Protected checkbox for value: false : Showing Unlocked on ECL Workunits page"); + } else { + Common.logError("Failure: " + getPageName() + " Details Page: Testing Protected checkbox for value: " + newCheckboxValue + ": Showing: " + lockStatus + " on ECL Workunits page"); + } + + break; + } + } + } + + + Map jsonMap = new HashMap<>(); + + @Override + protected List parseJson(String filePath) throws Exception { + ObjectMapper objectMapper = new ObjectMapper(); + WUQueryRoot wuQueryRoot = objectMapper.readValue(new File(filePath), WUQueryRoot.class); + WUQueryResponse wuQueryResponse = wuQueryRoot.getWUQueryResponse(); + List WUECLWorkunits = wuQueryResponse.getWorkunits().getECLWorkunit(); + + for (WUECLWorkunit workunit : WUECLWorkunits) { + jsonMap.put(workunit.getWuid(), workunit); + } + + return WUECLWorkunits; + } + + @Override + protected Object getColumnDataFromJson(WUECLWorkunit workunit, String columnKey) { + return switch (columnKey) { + case "Wuid", "wuid" -> workunit.getWuid(); + case "Owner", "owner" -> workunit.getOwner(); + case "Jobname", "jobname" -> workunit.getJobname(); + case "Cluster", "cluster" -> workunit.getCluster(); + case "State", "state" -> workunit.getState(); + case "TotalClusterTime", "totalClusterTime" -> workunit.getTotalClusterTime(); + case "Compile Cost", "compileCost" -> workunit.getCompileCost(); + case "Execution Cost", "executeCost" -> workunit.getExecuteCost(); + case "File Access Cost", "fileAccessCost" -> workunit.getFileAccessCost(); + case "action" -> workunit.getActionEx(); + case "protected" -> workunit.isProtected(); + default -> null; + }; + } + + @Override + protected Object parseDataUIValue(Object dataUIValue, Object dataJSONValue, String columnName, Object dataIDUIValue) { + try { + if (costColumns.contains(columnName)) { + dataUIValue = Double.parseDouble(((String) dataUIValue).split(" ")[0]); + } else if (columnName.equals("Total Cluster Time") || columnName.equals("totalClusterTime")) { + + if ((long) dataJSONValue == 0 && "".equals(dataUIValue)) { + return 0L; // test ok, if Total Cluster Time is 0 in json and blank in UI - Jira raised - HPCC-32147 + } + + long timeInMilliSecs = TimeUtils.convertToMilliseconds((String) dataUIValue); + if (timeInMilliSecs == Config.MALFORMED_TIME_STRING) { + String errMsg = "Failure: " + getPageName() + ": Incorrect time format for " + columnName + " : " + dataUIValue + " in UI for " + getUniqueKeyName() + " : " + dataIDUIValue; + Common.logError(errMsg); + return dataUIValue; + } + + return timeInMilliSecs; + } else if (dataUIValue instanceof String) { + dataUIValue = ((String) dataUIValue).trim(); + } + } catch (Exception ex) { + Common.logException("Failure: " + getPageName() + " Error in parsing UI value: " + dataUIValue + " for column: " + columnName + " ID: " + dataIDUIValue + " Error: " + ex.getMessage(), ex); + } + + return dataUIValue; + } + + @Override + protected Object parseDataJSONValue(Object dataJSONValue, String columnName, Object dataIDUIValue) { + if (columnName.equals("Total Cluster Time") || columnName.equals("totalClusterTime")) { + long timeInMilliSecs = (long) dataJSONValue; + if (timeInMilliSecs == Config.MALFORMED_TIME_STRING) { + String errMsg = "Failure: " + getPageName() + ": Incorrect time format for " + columnName + " in JSON for " + getUniqueKeyName() + " : " + dataIDUIValue; + Common.logError(errMsg); + return dataJSONValue; + } + + return timeInMilliSecs; + } + + return dataJSONValue; + } + + @Override + protected void sortJsonUsingSortOrder(String currentSortOrder, String columnKey) { + switch (currentSortOrder) { + case "ascending" -> ascendingSortJson(columnKey); + case "descending" -> descendingSortJson(columnKey); + case "none" -> descendingSortJson(sortByColumnKeyWhenSortedByNone); + } + } + + @Override + protected String getCurrentPage() { + try { + WebElement element = Common.waitForElement(By.id("wuid")); + if (element != null) { + return element.getAttribute("title"); + } + } catch (Exception ex) { + Common.logException("Error: " + getPageName() + ex.getMessage(), ex); + } + return "Invalid Page"; + } +} diff --git a/esp/src/test-ui/tests/framework/pages/FilesLogicalFilesTest.java b/esp/src/test-ui/tests/framework/pages/FilesLogicalFilesTest.java new file mode 100644 index 00000000000..c16dbb3611b --- /dev/null +++ b/esp/src/test-ui/tests/framework/pages/FilesLogicalFilesTest.java @@ -0,0 +1,200 @@ +package framework.pages; + +import com.fasterxml.jackson.databind.ObjectMapper; +import framework.config.Config; +import framework.config.URLConfig; +import framework.model.DFULogicalFile; +import framework.model.DFUQueryResponse; +import framework.model.DFUQueryRoot; +import framework.utility.Common; +import org.testng.annotations.Test; + +import java.io.File; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +// This class is a subclass of BaseTableTest, and it includes test cases for Files LogicalFiles tab and along with the tests of logical files details page. + +public class FilesLogicalFilesTest extends BaseTableTest { + + @Test + public void testingECLWorkUnitsPage() { + testPage(); + } + + @Override + protected String getPageName() { + return "Files LogicalFiles"; + } + + @Override + protected String getPageUrl() { + try { + return URLConfig.urlMap.get(URLConfig.NAV_FILES).getUrlMappings().get(URLConfig.TAB_FILES_LOGICAL_FILES).getUrl(); + } catch (Exception ex) { + Common.logException("Error in getting page URL " + getPageName() + ": Exception: " + ex.getMessage(), ex); + } + return ""; + } + + @Override + protected String getJsonFilePath() { + return Config.PATH_FOLDER_JSON + Config.FILES_JSON_FILE_NAME; + } + + @Override + protected String[] getColumnNames() { // these are the names of the columns headers displayed on the UI + return new String[]{"Logical Name", "Owner", "Super Owner", "Description", "Cluster", "Records", "Size", "Compressed Size", "Parts", "Min Skew", "Max Skew", "Modified (UTC/GMT)", "Last Accessed", "File Cost At Rest", "File Access Cost"}; + } + + @Override + protected String[] getColumnKeys() { // these are the identifiers used in the HTML code for the respective columns + return new String[]{"Name", "Owner", "SuperOwners", "Description", "NodeGroup", "Records", "FileSize", "CompressedFileSizeString", "Parts", "MinSkew", "MaxSkew", "Modified", "Accessed", "AtRestCost", "AccessCost"}; + } + + @Override + protected String getSaveButtonDetailsPage() { + return "Save"; + } + + @Override + protected String[] getDetailNames() { + return new String[]{}; + } + + @Override + protected String[] getDetailKeys() { + return new String[]{}; + } + + @Override + protected String[] getDetailKeysForPageLoad() { + return new String[]{}; + } + + @Override + protected String getCheckboxTypeForDetailsPage() { + return "checkbox"; + } + + @Override + protected String getAttributeTypeForDetailsPage() { + return "type"; + } + + @Override + protected String getAttributeValueForDetailsPage() { + return "value"; + } + + @Override + protected String[] getColumnKeysWithLinks() { + return new String[]{"Name"}; + } + + @Override + protected String getUniqueKeyName() { + return "Logical Name"; + } + + @Override + protected String getUniqueKey() { + return "Name"; + } + + String sortByColumnKeyWhenSortedByNone = "Modified"; + + protected String[] getTabValuesForDetailsPage() { + return new String[]{}; + } + + @Override + protected Map> getColumnNamesForTabsDetailsPage() { // key in this map is the value attribute of the html element of a tab. + return Map.ofEntries(); + } + + private final List costColumns = Arrays.asList("File Cost At Rest", "File Access Cost", "AtRestCost", "AccessCost"); + + @Override + protected Map getJsonMap() { + return jsonMap; + } + + @Override + protected void testDetailSpecificFunctionality(String wuName, int i) { + } + + Map jsonMap = new HashMap<>(); + + @Override + protected List parseJson(String filePath) throws Exception { + ObjectMapper objectMapper = new ObjectMapper(); + DFUQueryRoot dfuQueryRoot = objectMapper.readValue(new File(filePath), DFUQueryRoot.class); + DFUQueryResponse dfuQueryResponse = dfuQueryRoot.getDFUQueryResponse(); + List dfuLogicalFiles = dfuQueryResponse.getDFULogicalFiles().getDFULogicalFile(); + + for (DFULogicalFile logicalFile : dfuLogicalFiles) { + jsonMap.put(logicalFile.getName(), logicalFile); + } + + return dfuLogicalFiles; + } + + @Override + protected Object getColumnDataFromJson(DFULogicalFile logicalFile, String columnKey) { + return switch (columnKey) { + case "Name" -> logicalFile.getName(); + case "Owner" -> logicalFile.getOwner(); + case "SuperOwners" -> logicalFile.getSuperOwners(); + case "Description" -> logicalFile.getDescription(); + case "NodeGroup" -> logicalFile.getNodeGroup(); + case "Records" -> logicalFile.getRecordCount(); + case "FileSize" -> logicalFile.getIntSize(); + case "CompressedFileSizeString" -> logicalFile.getCompressedFileSize(); + case "Parts" -> logicalFile.getParts(); + case "MinSkew" -> logicalFile.getMinSkew(); + case "MaxSkew" -> logicalFile.getMaxSkew(); + case "Modified" -> logicalFile.getModified(); + case "Accessed" -> logicalFile.getAccessed(); + case "AtRestCost" -> logicalFile.getAtRestCost(); + case "AccessCost" -> logicalFile.getAccessCost(); + default -> null; + }; + } + + @Override + protected Object parseDataUIValue(Object dataUIValue, Object dataJSONValue, String columnName, Object dataIDUIValue) { + try { + if (costColumns.contains(columnName)) { + dataUIValue = Double.parseDouble(((String) dataUIValue).split(" ")[0]); + } else if (dataUIValue instanceof String) { + dataUIValue = ((String) dataUIValue).trim(); + } + } catch (Exception ex) { + Common.logException("Failure: " + getPageName() + " Error in parsing UI value: " + dataUIValue + " for column: " + columnName + " ID: " + dataIDUIValue + " Error: " + ex.getMessage(), ex); + } + + return dataUIValue; + } + + @Override + protected Object parseDataJSONValue(Object dataJSONValue, String columnName, Object dataIDUIValue) { + return dataJSONValue; + } + + @Override + protected void sortJsonUsingSortOrder(String currentSortOrder, String columnKey) { + switch (currentSortOrder) { + case "ascending" -> ascendingSortJson(columnKey); + case "descending" -> descendingSortJson(columnKey); + case "none" -> descendingSortJson(sortByColumnKeyWhenSortedByNone); + } + } + + @Override + protected String getCurrentPage() { + return "Invalid Page"; + } +} diff --git a/esp/src/test-ui/tests/framework/utility/Common.java b/esp/src/test-ui/tests/framework/utility/Common.java new file mode 100644 index 00000000000..17b440a30ea --- /dev/null +++ b/esp/src/test-ui/tests/framework/utility/Common.java @@ -0,0 +1,206 @@ +package framework.utility; + +import framework.config.Config; +import framework.config.URLConfig; +import org.openqa.selenium.By; +import org.openqa.selenium.TimeoutException; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.chrome.ChromeDriver; +import org.openqa.selenium.chrome.ChromeOptions; +import org.openqa.selenium.support.ui.ExpectedConditions; +import org.openqa.selenium.support.ui.WebDriverWait; + +import java.io.IOException; +import java.time.Duration; +import java.util.Arrays; +import java.util.logging.FileHandler; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class Common { + + public static WebDriver driver; + public static Logger errorLogger = setupLogger("error"); + public static Logger exceptionLogger = setupLogger("exception"); + public static Logger specificLogger; + public static int num_exceptions = 0; + public static int num_errors = 0; + + public static void checkTextPresent(String text, String page) { + try { + WebElement element = Common.waitForElement(By.xpath("//*[text()='" + text + "']")); + if (element != null) { + String msg = "Success: " + page + ": Text present: " + text; + logDetail(msg); + } + } catch (TimeoutException ex) { + String errorMsg = "Failure: " + page + ": Text not present: " + text; + logError(errorMsg); + } + } + + public static boolean openWebPage(String url) { + try { + driver.get(url); + driver.manage().window().maximize(); + sleep(); + return true; + } catch (Exception ex) { + Common.logException("Error in opening web page: " + url, ex); + return false; + } + } + + public static void sleep() { + try { + Thread.sleep(Duration.ofSeconds(Config.WAIT_TIME_IN_SECONDS)); + } catch (InterruptedException e) { + Common.logException("Error in sleep: " + e.getMessage(), e); + } + } + + public static void sleepWithTime(int seconds) { + try { + Thread.sleep(Duration.ofSeconds(seconds)); + } catch (InterruptedException e) { + Common.logException("Error in sleep: " + e.getMessage(), e); + } + } + + public static boolean isRunningOnLocal() { + return System.getProperty("os.name").startsWith(Config.LOCAL_OS) && System.getenv("USERPROFILE").startsWith(Config.LOCAL_USER_PROFILE); + } + + public static String getIP() { + + return isRunningOnLocal() ? URLConfig.LOCAL_IP : URLConfig.GITHUB_ACTION_IP; + } + + public static WebElement waitForElement(By locator) { + return new WebDriverWait(driver, Duration.ofSeconds(Config.WAIT_TIME_THRESHOLD_IN_SECONDS)).until(ExpectedConditions.presenceOfElementLocated(locator)); + } + + public static void waitForElementToBeClickable(WebElement element) { + new WebDriverWait(driver, Duration.ofSeconds(Config.WAIT_TIME_THRESHOLD_IN_SECONDS)).until(ExpectedConditions.elementToBeClickable(element)); + } + + // aria-disabled is a standard attribute and is widely used in HTML to indicate whether an element is disabled. It is system-defined, meaning it is part of the standard HTML specifications and not a custom class or attribute that might change frequently. + // By using aria-disabled, you ensure that the check for the disabled state is consistent and less likely to break due to UI changes. Custom class names or attributes defined by developers can change frequently during updates or redesigns, but standard attributes like aria-disabled are much more stable. + + public static void waitForElementToBeDisabled(WebElement element) { + new WebDriverWait(driver, Duration.ofSeconds(Config.WAIT_TIME_THRESHOLD_IN_SECONDS)) + .until(ExpectedConditions.attributeContains(element, "aria-disabled", "true")); + } + + public static void logError(String message) { + System.err.println(message); + errorLogger.severe(message); + num_errors++; + } + + public static void logException(String message, Exception ex) { + + message += Arrays.toString(ex.getStackTrace()); + + System.err.println(message); + exceptionLogger.severe(message); + num_exceptions++; + } + + public static void logDebug(String message) { + System.out.println(message); + + if (specificLogger != null && specificLogger.getLevel() == Level.INFO) { + specificLogger.info(message); + } + + if (specificLogger != null && specificLogger.getLevel() == Level.FINE) { + specificLogger.fine(message); + } + } + + public static void logDetail(String message) { + System.out.println(message); + + if (specificLogger != null && specificLogger.getLevel() == Level.FINE) { + specificLogger.fine(message); + } + } + + public static void initializeLoggerAndDriver() { + specificLogger = setupLogger(Config.LOG_LEVEL); + driver = setupWebDriver(); + } + + public static void printNumOfErrorsAndExceptions() { + System.out.println("Total number of exceptions recorded: " + num_exceptions); + System.out.println("Total number of errors recorded: " + num_errors); + } + + private static WebDriver setupWebDriver() { + + try { + ChromeOptions chromeOptions = new ChromeOptions(); + chromeOptions.addArguments("--headless"); // sets the ChromeDriver to run in headless mode, meaning it runs without opening a visible browser window. + chromeOptions.addArguments("--no-sandbox"); // disables the sandbox security feature in Chrome. + chromeOptions.addArguments("--log-level=3"); // sets the log level for the ChromeDriver. Level 3 corresponds to errors only. + + System.setProperty("webdriver.chrome.silentOutput", "true"); // suppresses the logs generated by the ChromeDriver + + if (Common.isRunningOnLocal()) { + System.setProperty("webdriver.chrome.driver", Config.PATH_LOCAL_CHROME_DRIVER); // sets the system property to the path of the ChromeDriver executable. + } else { + System.setProperty("webdriver.chrome.driver", Config.PATH_GH_ACTION_CHROME_DRIVER); + } + + return new ChromeDriver(chromeOptions); + } catch (Exception ex) { + logException("Failure: Error in setting up web driver: " + ex.getMessage(), ex); + } + + return null; + } + + private static Logger setupLogger(String logLevel) { + + if (logLevel.isEmpty()) { + return null; + } + + Logger logger = Logger.getLogger(logLevel); + logger.setUseParentHandlers(false); // Disable console logging + + try { + FileHandler fileHandler; + CustomFormatter formatter = new CustomFormatter(); + + if (logLevel.equalsIgnoreCase("error")) { + fileHandler = new FileHandler(Config.LOG_FILE_ERROR); + fileHandler.setFormatter(formatter); + logger.addHandler(fileHandler); + logger.setLevel(Level.SEVERE); + } else if (logLevel.equalsIgnoreCase("exception")) { + fileHandler = new FileHandler(Config.LOG_FILE_EXCEPTION); + fileHandler.setFormatter(formatter); + logger.addHandler(fileHandler); + logger.setLevel(Level.SEVERE); + } else if (logLevel.equalsIgnoreCase("debug")) { + fileHandler = new FileHandler(Config.LOG_FILE_DEBUG); + fileHandler.setFormatter(formatter); + logger.addHandler(fileHandler); + logger.setLevel(Level.INFO); + } else if (logLevel.equalsIgnoreCase("detail")) { + fileHandler = new FileHandler(Config.LOG_FILE_DETAIL); + fileHandler.setFormatter(formatter); + logger.addHandler(fileHandler); + logger.setLevel(Level.FINE); + } + } catch (IOException e) { + System.err.println("Failure: Failed to setup logger: " + e.getMessage()); + } + + Logger.getLogger("org.openqa.selenium").setLevel(Level.OFF); // Turn off all logging from the Selenium WebDriver. + return logger; + } +} diff --git a/esp/src/test-ui/tests/framework/utility/CustomFormatter.java b/esp/src/test-ui/tests/framework/utility/CustomFormatter.java new file mode 100644 index 00000000000..2f8a62d7db2 --- /dev/null +++ b/esp/src/test-ui/tests/framework/utility/CustomFormatter.java @@ -0,0 +1,16 @@ +package framework.utility; + +import java.util.logging.Formatter; +import java.util.logging.LogRecord; +import java.text.SimpleDateFormat; +import java.util.Date; + +public class CustomFormatter extends Formatter { + private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + + @Override + public String format(LogRecord record) { + Date date = new Date(record.getMillis()); + return String.format("%s %s%n", dateFormat.format(date), record.getMessage()); + } +} diff --git a/esp/src/test-ui/tests/framework/utility/TimeUtils.java b/esp/src/test-ui/tests/framework/utility/TimeUtils.java new file mode 100644 index 00000000000..d15f72b2a13 --- /dev/null +++ b/esp/src/test-ui/tests/framework/utility/TimeUtils.java @@ -0,0 +1,63 @@ +package framework.utility; + +import framework.config.Config; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class TimeUtils { + private static final Pattern TIME_PATTERN = Pattern.compile( + "\\s*(\\d+)\\s+days\\s+(\\d+):(\\d+):(\\d+).(\\d+)|" + + "(\\d+):(\\d+):(\\d+).(\\d+)|" + + "(\\d+):(\\d+).(\\d+)|" + + "(\\d+).(\\d+)\\s*"); + + private static final long MILLISECONDS_IN_A_SECOND = 1000; + private static final long MILLISECONDS_IN_A_MINUTE = 60 * MILLISECONDS_IN_A_SECOND; + private static final long MILLISECONDS_IN_AN_HOUR = 60 * MILLISECONDS_IN_A_MINUTE; + private static final long MILLISECONDS_IN_A_DAY = 24 * MILLISECONDS_IN_AN_HOUR; + + public static long convertToMilliseconds(String time) { + Matcher matcher = TIME_PATTERN.matcher(time); + if (matcher.matches()) { + int days = 0; + int hours = 0; + int minutes = 0; + int seconds = 0; + int milliSeconds = 0; + + try { + if (matcher.group(1) != null) { // Matches d days h:m:s.s + days = Integer.parseInt(matcher.group(1)); + hours = Integer.parseInt(matcher.group(2)); + minutes = Integer.parseInt(matcher.group(3)); + seconds = Integer.parseInt(matcher.group(4)); + milliSeconds = Integer.parseInt(matcher.group(5)); + } else if (matcher.group(6) != null) { // Matches h:m:s.s + hours = Integer.parseInt(matcher.group(6)); + minutes = Integer.parseInt(matcher.group(7)); + seconds = Integer.parseInt(matcher.group(8)); + milliSeconds = Integer.parseInt(matcher.group(9)); + } else if (matcher.group(10) != null) { // Matches m:s.s + minutes = Integer.parseInt(matcher.group(10)); + seconds = Integer.parseInt(matcher.group(11)); + milliSeconds = Integer.parseInt(matcher.group(12)); + } else if (matcher.group(13) != null) { // Matches s.s + seconds = Integer.parseInt(matcher.group(13)); + milliSeconds = Integer.parseInt(matcher.group(14)); + } + } catch (NumberFormatException e) { + return Config.MALFORMED_TIME_STRING; + } + + return days * MILLISECONDS_IN_A_DAY + + hours * MILLISECONDS_IN_AN_HOUR + + minutes * MILLISECONDS_IN_A_MINUTE + + seconds * MILLISECONDS_IN_A_SECOND + + milliSeconds; + + } else { + return Config.MALFORMED_TIME_STRING; + } + } +}