github.com/argoproj/argo-cd/v2@v2.10.9/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 if (process.env.IS_HEADLESS) { 53 options.addArguments('headless'); 54 } 55 options.addArguments('window-size=1400x1200'); 56 const driver = await new Builder() 57 .forBrowser('chrome') 58 .setChromeOptions(options) 59 .build(); 60 61 UiTestUtilities.log('Environment variables are:'); 62 UiTestUtilities.log(require('dotenv').config({path: __dirname + '/../.env'})); 63 64 // Navigate to the ArgoCD URL 65 await driver.get(Configuration.ARGOCD_URL); 66 67 return new Navigation(driver); 68 } 69 70 /** 71 * Locate the UI Element for the given locator, and wait until it is visible 72 * 73 * @param driver 74 * @param locator 75 */ 76 public static async findUiElement(driver: WebDriver, locator: By): Promise<WebElement> { 77 try { 78 let timeout = Const.TEST_TIMEOUT; 79 if (Configuration.TEST_TIMEOUT) { 80 timeout = parseInt(Configuration.TEST_TIMEOUT, 10); 81 } 82 const element = await driver.wait(until.elementLocated(locator), timeout); 83 var isDisplayed = await element.isDisplayed(); 84 if (isDisplayed) { 85 await driver.wait(until.elementIsVisible(element), timeout); 86 } 87 return element; 88 } catch (err) { 89 throw err; 90 } 91 } 92 93 /** 94 * Similar to until.methods and used in driver.wait, this will wait until 95 * the expected attribute is the same as the actual attribute on the element 96 * 97 * @param attr 98 * @param attrValue 99 */ 100 public static async untilAttributeIs(element: WebElement, attr: string, attrValue: string): Promise<boolean> { 101 const actual = await element.getAttribute(attr); 102 UiTestUtilities.log('Actual = ' + actual + ', expected = ' + attrValue + ', ' + (actual === attrValue)); 103 return actual === attrValue; 104 } 105 106 /** 107 * Similar to until.methods and used in driver.wait, this function will wait until 108 * the element (eg. operation state) title attribute no longer is present 109 * 110 * @param element 111 */ 112 public static async untilOperationStatusDisappears(element: WebElement): Promise<boolean> { 113 try { 114 const opState = await element.getAttribute('title'); 115 UiTestUtilities.log('Operation State = ' + opState); 116 return false; 117 } catch (err) { 118 UiTestUtilities.log('Status disappeared'); 119 return true; 120 } 121 } 122 123 /** 124 * For clicking on elements if WebElement.click() doesn't work 125 * 126 * @param driver 127 * @param element 128 */ 129 public static async click(driver: WebDriver, element: WebElement): Promise<void> { 130 try { 131 // Execute synchronous script 132 await driver.executeScript('arguments[0].click();', element); 133 } catch (e) { 134 throw e; 135 } 136 } 137 }