Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add types for custom commands on page object sections. #4298

Merged
merged 2 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ export type NightwatchGenericCallback<T> = (

export type Awaitable<T, V> = Omit<T, 'then'> & PromiseLike<V>;

export type KeysFilter<T, U> = {
[K in keyof T]-?: T[K] extends U ? K : never;
}[keyof T];

// tslint:disable-next-line
type VoidToNull<T> = T extends void ? null : T;

Expand Down
3 changes: 2 additions & 1 deletion types/page-object.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {
ElementCommands,
ElementFunction,
Expect,
KeysFilter,
LocateStrategy,
NamespacedApi,
NightwatchAPI,
NightwatchClient,
NightwatchComponentTestingCommands,
Expand Down Expand Up @@ -147,6 +147,7 @@ export type EnhancedSectionInstance<
Commands &
ElementCommands &
ChromiumClientCommands &
Pick<NightwatchCustomCommands, KeysFilter<NightwatchCustomCommands, Function>> & // eslint-disable-line @typescript-eslint/ban-types
Pick<
NightwatchComponentTestingCommands,
'importScript' | 'launchComponentRenderer' | 'mountComponent'
Expand Down
40 changes: 36 additions & 4 deletions types/tests/page-object.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
// Page object file
import {EnhancedPageObject, PageObjectModel} from '..';
import {expectError, expectType} from 'tsd';
import {EnhancedPageObject, PageObjectModel, Awaitable, NightwatchAPI} from '..';

// Page object file
const fileUploadPageElements = {
fileUploadInput: 'input#file-upload',
submitButton: 'input#file-submit',
uploadFiles: '#uploaded-files'
};

const menuSection = {
selector: 'nav',
elements: {
home: 'a[href="/"]',
about: 'a[href="/about"]'
}
};

const fileUploadPage = {
url(this: EnhancedPageObject) {
return `${this.api.launch_url}/upload`;
},
elements: fileUploadPageElements
elements: fileUploadPageElements,
sections: {
menu: menuSection
}
} satisfies PageObjectModel;

export interface FileUploadPage extends
EnhancedPageObject<{}, typeof fileUploadPageElements, {}, {}, () => string> {} // eslint-disable-line @typescript-eslint/ban-types
EnhancedPageObject<{}, typeof fileUploadPageElements, typeof fileUploadPage.sections, {}, () => string> {} // eslint-disable-line @typescript-eslint/ban-types

export default fileUploadPage;

Expand All @@ -26,6 +38,13 @@ declare module '..' {
interface NightwatchCustomPageObjects {
FileUpload(): FileUploadPage;
}
interface NightwatchCustomCommands {
uploadFile1(selector: string, filePath: string): NightwatchAPI;
uploadFile2(selector: string, filePath: string): Awaitable<NightwatchAPI, null>;
upload: {
file(selector: string, filePath: string): NightwatchAPI;
}
}
}


Expand All @@ -42,6 +61,19 @@ describe('File Upload', function() {
.click(fileUploadPage.elements.submitButton)
.expect.element('@uploadFiles').text.to.equal('test.txt');

// test custom commands over page object
expectType<NightwatchAPI>(fileUploadPage.uploadFile1('@fileUploadInput', 'test2.txt'));
expectType<Awaitable<NightwatchAPI, null>>(fileUploadPage.uploadFile2('@fileUploadInput', 'test2.txt'));
// should fail ideally but succeeding
expectType<NightwatchAPI>(fileUploadPage.upload.file('@fileUploadInput', 'test2.txt'));

// test custom commands over page object sections
const menuSection = fileUploadPage.section.menu;
expectType<NightwatchAPI>(menuSection.uploadFile1('@fileUploadInput', 'test2.txt'));
expectType<Awaitable<NightwatchAPI, null>>(menuSection.uploadFile2('@fileUploadInput', 'test2.txt'));
// should fail because the namespaces from custom commands are not loaded directly into the section
expectError(menuSection.upload.file('@fileUploadInput', 'test2.txt'));

browser.end();
});
});
Loading