Skip to content

Commit

Permalink
fix(core): sinceCommandId validation
Browse files Browse the repository at this point in the history
  • Loading branch information
blakebyrnes committed Sep 1, 2021
1 parent 75af647 commit fa61cc0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
12 changes: 6 additions & 6 deletions client/lib/DomExtender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ const { getState } = StateMachine<ISuperElement, ISuperElementProperties>();
interface IBaseExtend {
$: {
click: () => Promise<void>;
type: () => Promise<void>;
type: (...typeInteractions: ITypeInteraction[]) => Promise<void>;
waitForVisible: () => Promise<void>;
}
};
}

declare module 'awaited-dom/base/interfaces/super' {
Expand All @@ -28,12 +28,12 @@ for (const Super of [SuperElement, SuperNode, SuperHTMLElement]) {
get: function $() {
const click = async (): Promise<void> => {
const { awaitedOptions } = getState(this);
const coreFrame = await awaitedOptions?.coreFrame
const coreFrame = await awaitedOptions?.coreFrame;
await Interactor.run(coreFrame, [{ click: this }]);
};
const type = async (...typeInteractions: ITypeInteraction[]): Promise<void> => {
const { awaitedOptions } = getState(this);
const coreFrame = await awaitedOptions?.coreFrame
const coreFrame = await awaitedOptions?.coreFrame;
await click();
await Interactor.run(
coreFrame,
Expand All @@ -42,11 +42,11 @@ for (const Super of [SuperElement, SuperNode, SuperHTMLElement]) {
};
const waitForVisible = async (): Promise<void> => {
const { awaitedPath, awaitedOptions } = getState(this);
const coreFrame = await awaitedOptions?.coreFrame
const coreFrame = await awaitedOptions?.coreFrame;
await coreFrame.waitForElement(awaitedPath.toJSON(), { waitForVisible: true });
};

return { click, type, waitForVisible };
}
},
});
}
5 changes: 4 additions & 1 deletion core/lib/FrameNavigationsObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ export default class FrameNavigationsObserver {
assert(LocationTrigger[status], `Invalid location status: ${status}`);

// determine if this location trigger has already been satisfied
const sinceCommandId = Number(options.sinceCommandId ?? this.defaultWaitForLocationCommandId);
const sinceCommandId = Number.isInteger(options.sinceCommandId)
? options.sinceCommandId
: this.defaultWaitForLocationCommandId;

if (this.hasLocationTrigger(status, sinceCommandId)) {
return Promise.resolve();
}
Expand Down
21 changes: 15 additions & 6 deletions core/lib/Tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,9 @@ export default class Tab extends TypedEventEmitter<ITabEventParams> {

public async waitForNewTab(options: IWaitForOptions = {}): Promise<Tab> {
// last command is the one running right now
const startCommandId = options?.sinceCommandId ?? this.lastCommandId - 1;
const startCommandId = Number.isInteger(options.sinceCommandId)
? options.sinceCommandId
: this.lastCommandId - 1;
let newTab: Tab;
const startTime = new Date();
if (startCommandId >= 0) {
Expand All @@ -468,11 +470,15 @@ export default class Tab extends TypedEventEmitter<ITabEventParams> {

public async waitForResource(
filter: IResourceFilterProperties,
opts?: IWaitForResourceOptions,
options?: IWaitForResourceOptions,
): Promise<IResourceMeta[]> {
const timer = new Timer(opts?.timeoutMs ?? 60e3, this.waitTimeouts);
const timer = new Timer(options?.timeoutMs ?? 60e3, this.waitTimeouts);
const resourceMetas: IResourceMeta[] = [];
const promise = createPromise();
const sinceCommandId =
options?.sinceCommandId && Number.isInteger(options.sinceCommandId)
? options.sinceCommandId
: -1;

const onResource = (resourceMeta: IResourceMeta) => {
if (resourceMeta.tabId !== this.id) return;
Expand All @@ -481,7 +487,7 @@ export default class Tab extends TypedEventEmitter<ITabEventParams> {
// need to set directly since passed in object is a copy
this.sessionState.getResourceMeta(resourceMeta.id).seenAtCommandId = this.lastCommandId;
}
if (resourceMeta.seenAtCommandId <= opts?.sinceCommandId ?? -1) return;
if (resourceMeta.seenAtCommandId <= sinceCommandId) return;
if (filter.type && resourceMeta.type !== filter.type) return;
if (filter.url) {
if (typeof filter.url === 'string') {
Expand All @@ -508,7 +514,7 @@ export default class Tab extends TypedEventEmitter<ITabEventParams> {
await timer.waitForPromise(promise.promise, 'Timeout waiting for DomContentLoaded');
} catch (err) {
const isTimeout = err instanceof TimeoutError;
if (isTimeout && opts?.throwIfTimeout === false) {
if (isTimeout && options?.throwIfTimeout === false) {
return resourceMetas;
}
throw err;
Expand All @@ -521,7 +527,10 @@ export default class Tab extends TypedEventEmitter<ITabEventParams> {
}

public async waitForFileChooser(options?: IWaitForOptions): Promise<IFileChooserPrompt> {
let startCommandId = options?.sinceCommandId;
let startCommandId =
options?.sinceCommandId && Number.isInteger(options.sinceCommandId)
? options.sinceCommandId
: null;

if (!startCommandId && this.sessionState.commands.length >= 2) {
startCommandId = this.sessionState.commands[this.sessionState.commands.length - 2]?.id;
Expand Down

0 comments on commit fa61cc0

Please sign in to comment.