|
| 1 | +/** |
| 2 | + * Test Element interface |
| 3 | + * This is a wrapper of native element |
| 4 | + */ |
| 5 | +export interface TestElement { |
| 6 | + blur(): Promise<void>; |
| 7 | + clear(): Promise<void>; |
| 8 | + click(): Promise<void>; |
| 9 | + focus(): Promise<void>; |
| 10 | + getCssValue(property: string): Promise<string>; |
| 11 | + hover(): Promise<void>; |
| 12 | + sendKeys(keys: string): Promise<void>; |
| 13 | + text(): Promise<string>; |
| 14 | + getAttribute(name: string): Promise<string|null>; |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * Extra searching options used by searching functions |
| 19 | + * |
| 20 | + * @param allowNull Optional, whether the found element can be null. If |
| 21 | + * allowNull is set, the searching function will always try to fetch the element |
| 22 | + * at once. When the element cannot be found, the searching function should |
| 23 | + * return null if allowNull is set to true, throw an error if allowNull is set |
| 24 | + * to false. If allowNull is not set, the framework will choose the behaviors |
| 25 | + * that make more sense for each test type (e.g. for unit test, the framework |
| 26 | + * will make sure the element is not null; otherwise throw an error); however, |
| 27 | + * the internal behavior is not guaranteed and user should not rely on it. Note |
| 28 | + * that in most cases, you don't need to care about whether an element is |
| 29 | + * present when loading the element and don't need to set this parameter unless |
| 30 | + * you do want to check whether the element is present when calling the |
| 31 | + * searching function. e.g. you want to make sure some element is not there when |
| 32 | + * loading the element in order to check whether a "ngif" works well. |
| 33 | + * |
| 34 | + * @param global Optional. If global is set to true, the selector will match any |
| 35 | + * element on the page and is not limited to the root of the harness. If |
| 36 | + * global is unset or set to false, the selector will only find elements under |
| 37 | + * the current root. |
| 38 | + */ |
| 39 | +export interface Options { |
| 40 | + allowNull?: boolean; |
| 41 | + global?: boolean; |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Type narrowing of Options to allow the overloads of ComponentHarness.find to |
| 46 | + * return null only if allowNull is set to true. |
| 47 | + */ |
| 48 | +interface OptionsWithAllowNullSet extends Options { |
| 49 | + allowNull: true; |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Locator interface |
| 54 | + */ |
| 55 | +export interface Locator { |
| 56 | + /** |
| 57 | + * Get the host element of locator. |
| 58 | + */ |
| 59 | + host(): TestElement; |
| 60 | + |
| 61 | + /** |
| 62 | + * Search the first matched test element. |
| 63 | + * @param css Selector of the test elements. |
| 64 | + * @param options Optional, extra searching options |
| 65 | + */ |
| 66 | + find(css: string, options?: Options): Promise<TestElement|null>; |
| 67 | + |
| 68 | + /** |
| 69 | + * Search all matched test elements under current root by css selector. |
| 70 | + * @param css Selector of the test elements. |
| 71 | + */ |
| 72 | + findAll(css: string): Promise<TestElement[]>; |
| 73 | + |
| 74 | + /** |
| 75 | + * Load the first matched Component Harness. |
| 76 | + * @param componentHarness Type of user customized harness. |
| 77 | + * @param root Css root selector of the new component harness. |
| 78 | + * @param options Optional, extra searching options |
| 79 | + */ |
| 80 | + load<T extends ComponentHarness>( |
| 81 | + componentHarness: ComponentHarnessType<T>, root: string, |
| 82 | + options?: Options): Promise<T|null>; |
| 83 | + |
| 84 | + /** |
| 85 | + * Load all Component Harnesses under current root. |
| 86 | + * @param componentHarness Type of user customized harness. |
| 87 | + * @param root Css root selector of the new component harnesses. |
| 88 | + */ |
| 89 | + loadAll<T extends ComponentHarness>( |
| 90 | + componentHarness: ComponentHarnessType<T>, root: string): Promise<T[]>; |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * Base Component Harness |
| 95 | + * This base component harness provides the basic ability to locate element and |
| 96 | + * sub-component harness. It should be inherited when defining user's own |
| 97 | + * harness. |
| 98 | + */ |
| 99 | +export class ComponentHarness { |
| 100 | + constructor(private readonly locator: Locator) {} |
| 101 | + |
| 102 | + /** |
| 103 | + * Get the host element of component harness. |
| 104 | + */ |
| 105 | + host(): TestElement { |
| 106 | + return this.locator.host(); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Generate a function to find the first matched test element by css |
| 111 | + * selector. |
| 112 | + * @param css Css selector of the test element. |
| 113 | + */ |
| 114 | + protected find(css: string): () => Promise<TestElement>; |
| 115 | + |
| 116 | + /** |
| 117 | + * Generate a function to find the first matched test element by css |
| 118 | + * selector. |
| 119 | + * @param css Css selector of the test element. |
| 120 | + * @param options Extra searching options |
| 121 | + */ |
| 122 | + protected find(css: string, options: OptionsWithAllowNullSet): |
| 123 | + () => Promise<TestElement|null>; |
| 124 | + |
| 125 | + /** |
| 126 | + * Generate a function to find the first matched test element by css |
| 127 | + * selector. |
| 128 | + * @param css Css selector of the test element. |
| 129 | + * @param options Extra searching options |
| 130 | + */ |
| 131 | + protected find(css: string, options: Options): () => Promise<TestElement>; |
| 132 | + |
| 133 | + /** |
| 134 | + * Generate a function to find the first matched Component Harness. |
| 135 | + * @param componentHarness Type of user customized harness. |
| 136 | + * @param root Css root selector of the new component harness. |
| 137 | + */ |
| 138 | + protected find<T extends ComponentHarness>( |
| 139 | + componentHarness: ComponentHarnessType<T>, |
| 140 | + root: string): () => Promise<T>; |
| 141 | + |
| 142 | + /** |
| 143 | + * Generate a function to find the first matched Component Harness. |
| 144 | + * @param componentHarness Type of user customized harness. |
| 145 | + * @param root Css root selector of the new component harness. |
| 146 | + * @param options Extra searching options |
| 147 | + */ |
| 148 | + protected find<T extends ComponentHarness>( |
| 149 | + componentHarness: ComponentHarnessType<T>, root: string, |
| 150 | + options: OptionsWithAllowNullSet): () => Promise<T|null>; |
| 151 | + |
| 152 | + /** |
| 153 | + * Generate a function to find the first matched Component Harness. |
| 154 | + * @param componentHarness Type of user customized harness. |
| 155 | + * @param root Css root selector of the new component harness. |
| 156 | + * @param options Extra searching options |
| 157 | + */ |
| 158 | + protected find<T extends ComponentHarness>( |
| 159 | + componentHarness: ComponentHarnessType<T>, root: string, |
| 160 | + options: Options): () => Promise<T>; |
| 161 | + |
| 162 | + protected find<T extends ComponentHarness>( |
| 163 | + cssOrComponentHarness: string|ComponentHarnessType<T>, |
| 164 | + cssOrOptions?: string|Options, |
| 165 | + options?: Options): () => Promise<TestElement|T|null> { |
| 166 | + if (typeof cssOrComponentHarness === 'string') { |
| 167 | + const css = cssOrComponentHarness; |
| 168 | + const options = cssOrOptions as Options; |
| 169 | + return () => this.locator.find(css, options); |
| 170 | + } else { |
| 171 | + const componentHarness = cssOrComponentHarness; |
| 172 | + const css = cssOrOptions as string; |
| 173 | + return () => this.locator.load(componentHarness, css, options); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Generate a function to find all matched test elements by css selector. |
| 179 | + * @param css Css root selector of elements. It will locate |
| 180 | + * elements under the current root. |
| 181 | + */ |
| 182 | + protected findAll(css: string): () => Promise<TestElement[]>; |
| 183 | + |
| 184 | + /** |
| 185 | + * Generate a function to find all Component Harnesses under current |
| 186 | + * component harness. |
| 187 | + * @param componentHarness Type of user customized harness. |
| 188 | + * @param root Css root selector of the new component harnesses. It will |
| 189 | + * locate harnesses under the current root. |
| 190 | + */ |
| 191 | + protected findAll<T extends ComponentHarness>( |
| 192 | + componentHarness: ComponentHarnessType<T>, |
| 193 | + root: string): () => Promise<T[]>; |
| 194 | + |
| 195 | + protected findAll<T extends ComponentHarness>( |
| 196 | + cssOrComponentHarness: string|ComponentHarnessType<T>, |
| 197 | + root?: string): () => Promise<TestElement[]|T[]> { |
| 198 | + if (typeof cssOrComponentHarness === 'string') { |
| 199 | + const css = cssOrComponentHarness; |
| 200 | + return () => this.locator.findAll(css); |
| 201 | + } else { |
| 202 | + const componentHarness = cssOrComponentHarness; |
| 203 | + return () => this.locator.loadAll(componentHarness, root as string); |
| 204 | + } |
| 205 | + } |
| 206 | +} |
| 207 | + |
| 208 | +/** |
| 209 | + * Type of ComponentHarness. |
| 210 | + */ |
| 211 | +export interface ComponentHarnessType<T extends ComponentHarness> { |
| 212 | + new(locator: Locator): T; |
| 213 | +} |
0 commit comments