Skip to content

Commit

Permalink
fix: move cursor relative to the center of body when using "resetCurs…
Browse files Browse the repository at this point in the history
…or" option
  • Loading branch information
DudaGod committed Feb 14, 2024
1 parent 33a9b15 commit 64b289c
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 25 deletions.
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -389,12 +389,14 @@ node_modules/.bin/hermione

Since Hermione is based on [WebdriverIO v8](https://webdriver.io/docs/api/), all the commands provided by WebdriverIO are available in it. But Hermione also has her own commands.

### clearSession
### Browser commands

#### clearSession

Browser command that clears session state (deletes cookies, clears local and session storages). For example:

```js
it('', async ({ browser }) => {
it('test', async ({ browser }) => {
await browser.url('https://github.com/gemini-testing/hermione');

(await browser.getCookies()).length; // 5
Expand All @@ -409,6 +411,24 @@ it('', async ({ browser }) => {
});
```

### Element commands

#### moveCursorTo

> This command is temporary and will be removed in the next major (hermione@9). Differs from the standard [moveTo](https://webdriver.io/docs/api/element/moveTo/) in that it moves the cursor relative to the top-left corner of the element (like it was in hermione@7).
Move the mouse by an offset of the specified element. If offset is not specified then mouse will be moved to the top-left corder of the element.
Usage:

```typescript
await browser.$(selector).moveCursorTo({ xOffset, yOffset });
```

Available parameters:

* **xOffset** (optional) `Number` – X offset to move to, relative to the top-left corner of the element;
* **yOffset** (optional) `Number` – Y offset to move to, relative to the top-left corner of the element.

## Tests API

### Arguments
Expand Down
1 change: 1 addition & 0 deletions src/browser/commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ module.exports = [
"scrollIntoView",
"openAndWait",
"switchToRepl",
"moveCursorTo",
];
34 changes: 34 additions & 0 deletions src/browser/commands/moveCursorTo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { Browser } from "../types";

type MoveToOptions = {
xOffset?: number;
yOffset?: number;
};

// TODO: remove after next major version
export = async (browser: Browser): Promise<void> => {
const { publicAPI: session } = browser;

session.addCommand(
"moveCursorTo",
async function (this: WebdriverIO.Element, { xOffset = 0, yOffset = 0 }: MoveToOptions = {}): Promise<void> {
if (!this.isW3C) {
return this.moveToElement(this.elementId, xOffset, yOffset);
}

const { x, y, width, height } = await this.getElementRect(this.elementId);
const { scrollX, scrollY } = await session.execute(function (this: Window) {
return { scrollX: this.scrollX, scrollY: this.scrollY };
});

const newXOffset = Math.floor(x - scrollX + (typeof xOffset === "number" ? xOffset : width / 2));
const newYOffset = Math.floor(y - scrollY + (typeof yOffset === "number" ? yOffset : height / 2));

return session
.action("pointer", { parameters: { pointerType: "mouse" } })
.move({ x: newXOffset, y: newYOffset })
.perform();
},
true,
);
};
17 changes: 12 additions & 5 deletions src/worker/runner/test-runner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,18 @@ module.exports = class TestRunner {

await body.scrollIntoView();

const { x = 0, y = 0 } = await session.execute(function () {
return document.body.getBoundingClientRect();
});
// x and y must be integer, wdio will throw error otherwise
await body.moveTo({ xOffset: -Math.floor(x), yOffset: -Math.floor(y) });
if (!session.isW3C) {
const { x = 0, y = 0 } = await session.execute(function () {
return document.body.getBoundingClientRect();
});

return session.moveToElement(body.elementId, -Math.floor(x), -Math.floor(y));
}

await session
.action("pointer", { parameters: { pointerType: "mouse" } })
.move({ x: 0, y: 0 })
.perform();
}
};

Expand Down
80 changes: 62 additions & 18 deletions test/src/worker/runner/test-runner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,31 @@ describe("worker/runner/test-runner", () => {
const mkElement_ = proto => {
return _.defaults(proto, {
scrollIntoView: sandbox.stub().named("scrollIntoView").resolves(),
moveTo: sandbox.stub().named("moveTo").resolves(),
getSize: sandbox.stub().named("getSize").resolves({ width: 100, height: 500 }),
elementId: 100500,
});
};

const mkActionAPI_ = () => {
const actionStub = {};
actionStub.move = sandbox.stub().named("move").returns(actionStub);
actionStub.perform = sandbox.stub().named("perform").resolves();

return actionStub;
};

const mkBrowser_ = ({ prototype, config, id } = {}) => {
const actionStub = {};
actionStub.move = sandbox.stub().named("move").returns(actionStub);
actionStub.perform = sandbox.stub().named("perform").resolves();

const publicAPI = _.defaults(prototype, {
$: sandbox.stub().named("$").resolves(mkElement_()),
execute: sandbox.stub().named("execute").resolves({ x: 0, y: 0 }),
assertView: sandbox.stub().named("assertView").resolves(),
moveToElement: sandbox.stub().named("moveToElement").resolves(),
action: sandbox.stub().named("getSize").returns(mkActionAPI_()),
isW3C: true,
});
config = _.defaults(config, { resetCursor: true });

Expand Down Expand Up @@ -272,7 +288,7 @@ describe("worker/runner/test-runner", () => {
it('should not move cursor to position "0,0" on body element', async () => {
await run_();

assert.notCalled(body.moveTo);
assert.notCalled(browser.publicAPI.action);
});
});

Expand Down Expand Up @@ -309,32 +325,60 @@ describe("worker/runner/test-runner", () => {
assert.calledOnceWith(body.scrollIntoView);
});

it('should move cursor to position "0,0" on body element', async () => {
await run_();
describe("in jsonwp protocol", () => {
beforeEach(() => {
browser.publicAPI.isW3C = false;
});

assert.calledOnceWith(body.moveTo, { xOffset: 0, yOffset: 0 });
});
it("should scroll before moving cursor", async () => {
await run_();

it("should move cursor correctly if body element has negative coords", async () => {
browser.publicAPI.execute.resolves({ x: -100, y: -500 });
assert.callOrder(body.scrollIntoView, browser.publicAPI.moveToElement);
});

await run_();
it('should move cursor to position "0,0"', async () => {
browser.publicAPI.execute.resolves({ x: 5, y: 5 });
body.elementId = 12345;

assert.calledOnceWith(body.moveTo, { xOffset: 100, yOffset: 500 });
});
await run_();

it("should scroll before moving cursor", async () => {
await run_();
assert.calledOnceWith(browser.publicAPI.moveToElement, 12345, -5, -5);
});

assert.callOrder(body.scrollIntoView, body.moveTo);
it("should floor coords if body element has fractional coords", async () => {
browser.publicAPI.execute.resolves({ x: 10.123, y: 15.899 });
body.elementId = 12345;

await run_();

assert.calledOnceWith(browser.publicAPI.moveToElement, 12345, -10, -15);
});
});

it("should floor coords if body element has fractional coords", async () => {
browser.publicAPI.execute.resolves({ x: 10.123, y: 15.899 });
describe("in w3c protocol", () => {
beforeEach(() => {
browser.publicAPI.isW3C = true;
});

await run_();
it("should scroll before moving cursor", async () => {
await run_();

assert.calledOnceWith(body.moveTo, { xOffset: -10, yOffset: -15 });
assert.callOrder(body.scrollIntoView, browser.publicAPI.action);
});

it('should move cursor to position "0,0"', async () => {
const actionAPI = mkActionAPI_();
browser.publicAPI.action.returns(actionAPI);

await run_();

assert.calledOnceWith(browser.publicAPI.action, "pointer", {
parameters: { pointerType: "mouse" },
});
assert.calledOnceWith(actionAPI.move, { x: 0, y: 0 });
assert.calledOnce(actionAPI.perform);
assert.callOrder(browser.publicAPI.action, actionAPI.move, actionAPI.perform);
});
});
});
});
Expand Down

0 comments on commit 64b289c

Please sign in to comment.