github.com/argoproj/argo-cd/v3@v3.2.1/ui-test/src/UiTestUtilities.ts (about)

     1  import Configuration from './Configuration';
     2  import {Builder, By, until, WebDriver, WebElement} from 'selenium-webdriver';
     3  import chrome from 'selenium-webdriver/chrome';
     4  import * as Const from './Constants';
     5  import {Navigation} from './navigation';
     6  
     7  export default class UiTestUtilities {
     8      /**
     9       * Log a message to the console.
    10       * @param message
    11       */
    12      public static async log(message: string): Promise<void> {
    13          let doLog = Const.ENABLE_CONSOLE_LOG;
    14          // Config override
    15          if (Configuration.ENABLE_CONSOLE_LOG) {
    16              if (Configuration.ENABLE_CONSOLE_LOG === 'false') {
    17                  doLog = false;
    18              } else {
    19                  doLog = true;
    20              }
    21          }
    22          if (doLog) {
    23              // tslint:disable-next-line:no-console
    24              console.log(message);
    25          }
    26      }
    27  
    28      public static async logError(message: string): Promise<void> {
    29          let doLog = Const.ENABLE_CONSOLE_LOG;
    30          // Config override
    31          if (Configuration.ENABLE_CONSOLE_LOG) {
    32              if (Configuration.ENABLE_CONSOLE_LOG === 'false') {
    33                  doLog = false;
    34              } else {
    35                  doLog = true;
    36              }
    37          }
    38          if (doLog) {
    39              // tslint:disable-next-line:no-console
    40              console.error(message);
    41          }
    42      }
    43  
    44      /**
    45       * Set up the WebDriver. Initial steps for all tests.  Returns the instance of Navigation with the WebDriver.
    46       * From there, navigate the UI.  Test cases do no need to reference the instance of WebDriver since Component/Page-specific
    47       * API methods should be called instead.
    48       *
    49       */
    50      public static async init(): Promise<Navigation> {
    51          const options = new chrome.Options();
    52          UiTestUtilities.log('Env var IS_HEADLESS = ' + process.env.IS_HEADLESS);
    53          if (process.env.IS_HEADLESS !== 'false') {
    54              UiTestUtilities.log('Adding headless option');
    55              options.addArguments('headless');
    56          }
    57          options.addArguments('window-size=1400x1200');
    58          const driver = await new Builder().forBrowser('chrome').setChromeOptions(options).build();
    59  
    60          UiTestUtilities.log('Environment variables are:');
    61          UiTestUtilities.log(require('dotenv').config({path: __dirname + '/../.env'}));
    62  
    63          // Navigate to the ArgoCD URL
    64          await driver.get(Configuration.ARGOCD_URL);
    65          UiTestUtilities.log('Navigate to Argo CD URL successful: driver.get');
    66          return new Navigation(driver);
    67      }
    68  
    69      /**
    70       * Locate the UI Element for the given locator, and wait until it is visible
    71       *
    72       * @param driver
    73       * @param locator
    74       */
    75      public static async findUiElement(driver: WebDriver, locator: By): Promise<WebElement> {
    76          try {
    77              let timeout = Const.TEST_TIMEOUT;
    78              if (Configuration.TEST_TIMEOUT) {
    79                  timeout = parseInt(Configuration.TEST_TIMEOUT, 10);
    80              }
    81              const element = await driver.wait(until.elementLocated(locator), timeout);
    82              const isDisplayed = await element.isDisplayed();
    83              if (isDisplayed) {
    84                  await driver.wait(until.elementIsVisible(element), timeout);
    85              }
    86              return element;
    87          } catch (err) {
    88              throw err;
    89          }
    90      }
    91  
    92      /**
    93       * Similar to until.methods and used in driver.wait, this will wait until
    94       * the expected attribute is the same as the actual attribute on the element
    95       *
    96       * @param attr
    97       * @param attrValue
    98       */
    99      public static async untilAttributeIs(element: WebElement, attr: string, attrValue: string): Promise<boolean> {
   100          const actual = await element.getAttribute(attr);
   101          UiTestUtilities.log('Actual = ' + actual + ', expected = ' + attrValue + ', ' + (actual === attrValue));
   102          return actual === attrValue;
   103      }
   104  
   105      /**
   106       * Similar to until.methods and used in driver.wait, this function will wait until
   107       * the element (eg. operation state) title attribute no longer is present
   108       *
   109       * @param element
   110       */
   111      public static async untilOperationStatusDisappears(element: WebElement): Promise<boolean> {
   112          try {
   113              const opState = await element.getAttribute('title');
   114              UiTestUtilities.log('Operation State = ' + opState);
   115              return false;
   116          } catch (err) {
   117              UiTestUtilities.log('Status disappeared');
   118              return true;
   119          }
   120      }
   121  
   122      /**
   123       * For clicking on elements if WebElement.click() doesn't work
   124       *
   125       * @param driver
   126       * @param element
   127       */
   128      public static async click(driver: WebDriver, element: WebElement): Promise<void> {
   129          try {
   130              // Execute synchronous script
   131              await driver.executeScript('arguments[0].click();', element);
   132          } catch (e) {
   133              throw e;
   134          }
   135      }
   136  }