Skip to content

Commit f505c83

Browse files
authored
#2797 +CustomLocators interface (#2798)
* #2797 +CustomLocators interface * add documentation Co-authored-by: Daniel Rentz <[email protected]>
1 parent fb3421f commit f505c83

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

docs/typescript.md

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ Example:
1919

2020
![Quick Info](/img/Quick_info.gif)
2121

22-
- Checks types - thanks to TypeScript support in CodeceptJS now allow to tests your tests. TypeScript can prevent some errors:
22+
- Checks types - thanks to TypeScript support in CodeceptJS now allow to tests your tests. TypeScript can prevent some errors:
2323
- invalid type of variables passed to function;
2424
- calls no-exist method from PageObject or `I` object;
25-
- incorrectly used CodeceptJS features;
25+
- incorrectly used CodeceptJS features;
2626

2727

2828
## Getting Started
@@ -106,7 +106,7 @@ declare namespace CodeceptJS {
106106

107107
## Types for custom helper or page object
108108

109-
If you want to get types for your [custom helper](https://codecept.io/helpers/#configuration), you can add their automatically with CodeceptJS command `npx codeceptjs def`.
109+
If you want to get types for your [custom helper](https://codecept.io/helpers/#configuration), you can add their automatically with CodeceptJS command `npx codeceptjs def`.
110110

111111
For example, if you add the new step `printMessage` for your custom helper like this:
112112
```js
@@ -121,9 +121,9 @@ export = CustomHelper
121121
```
122122

123123
Then you need to add this helper to your `codecept.conf.js` like in this [docs](https://codecept.io/helpers/#configuration).
124-
And then run the command `npx codeceptjs def`.
124+
And then run the command `npx codeceptjs def`.
125125

126-
As result our `steps.d.ts` file will be updated like this:
126+
As result our `steps.d.ts` file will be updated like this:
127127
```ts
128128
/// <reference types='codeceptjs' />
129129
type CustomHelper = import('./CustomHelper');
@@ -156,3 +156,45 @@ declare namespace CodeceptJS {
156156
}
157157
}
158158
```
159+
160+
## Types for custom strict locators
161+
162+
You can define [custom strict locators](https://codecept.io/locators/#custom-strict-locators) that can be used in all methods taking a locator (parameter type `LocatorOrString`).
163+
164+
Example: A custom strict locator with a `data` property, which can be used like this:
165+
166+
```ts
167+
I.click({ data: 'user-login' });
168+
```
169+
170+
In order to use the custom locator in TypeScript code, its type shape needs to be registered in the interface `CustomLocators` in your `steps.d.ts` file:
171+
172+
```ts
173+
/// <reference types='codeceptjs' />
174+
...
175+
176+
declare namespace CodeceptJS {
177+
...
178+
179+
interface CustomLocators {
180+
data: { data: string };
181+
}
182+
}
183+
```
184+
185+
The property keys used in the `CustomLocators` interface do not matter (only the *types* of the interface properties are used). For simplicity it is recommended to use the name that is also used in your custom locator itself.
186+
187+
You can also define more complicated custom locators with multiple (also optional) properties:
188+
189+
```ts
190+
/// <reference types='codeceptjs' />
191+
...
192+
193+
declare namespace CodeceptJS {
194+
...
195+
196+
interface CustomLocators {
197+
data: { data: string, value?: number, flag?: boolean };
198+
}
199+
}
200+
```

typings/index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ declare namespace CodeceptJS {
5454
| { android: string, ios: string }
5555
| { react: string };
5656

57-
type LocatorOrString = string | ILocator | Locator;
57+
interface CustomLocators { }
58+
type LocatorOrString = string | ILocator | Locator | CustomLocators[keyof CustomLocators];
59+
5860
type StringOrSecret = string | CodeceptJS.Secret;
5961

6062
interface HookCallback { (args: SupportObject): void; }

0 commit comments

Comments
 (0)