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

Basic input #45

Merged
merged 5 commits into from
Jun 28, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
82 changes: 82 additions & 0 deletions lib/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,88 @@ class Page extends EventEmitter {
async close() {
await this._client.dispose();
}

/**
* @param {string} selector
* @param {!Promise<number>}
*/
async _querySelector(selector) {
let documentNode = await this._client.send('DOM.getDocument', {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should query for the document only once in the very beginning

depth: 0
});
return (await this._client.send('DOM.querySelector', {
nodeId: documentNode.root.nodeId,
selector
})).nodeId;
}

/**
* @param {string} selector
* @param {!Promise}
*/
async click(selector) {
let boxModel = (await this._client.send('DOM.getBoxModel', {
nodeId: await this._querySelector(selector)
})).model.content;
let x = Math.round((boxModel[0] + boxModel[0]) / 2);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this feels like an error

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

let y = Math.round((boxModel[1] + boxModel[1]) / 2);

await this._client.send('Input.dispatchMouseEvent', {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can await only the last one due to the serial nature of the protocol

type: 'mouseMoved',
x, y
});
await this._client.send('Input.dispatchMouseEvent', {
type: 'mousePressed',
button: 'left',
x, y,
clickCount: 1
});
await this._client.send('Input.dispatchMouseEvent', {
type: 'mouseReleased',
button: 'left',
x, y,
clickCount: 1
});
}

/**
* @param {string} selector
* @param {!Promise}
*/
async focus(selector) {
await this.evaluate(selector => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's use DOM.querySelector and DOM.focus

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

let element = document.querySelector(selector);
if (element && element.focus)
element.focus();
}, selector);
}

/**
* @param {string} text
* @param {!Promise}
*/
async type(text) {
for (let i = 0; i < text.length; i++) {
let code = text.charCodeAt(i);
let char = text.charAt(i);
await this._client.send('Input.dispatchKeyEvent', {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you'd probably speed this up considerably if you were to wait only for the very last one

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

type: 'rawKeyDown',
windowsVirtualKeyCode: code,
key: char
});
await this._client.send('Input.dispatchKeyEvent', {
type: 'char',
text: char,
key: char,
unmodifiedText: char
});
await this._client.send('Input.dispatchKeyEvent', {
type: 'keyUp',
windowsVirtualKeyCode: code,
key: char
});
}
}
}

/** @enum {string} */
Expand Down
15 changes: 15 additions & 0 deletions test/assets/input/button.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<title>Button test</title>
</head>
<body>
<button onclick="clicked();">Click target</button>
<script>
window.result = "Was not clicked";
function clicked() {
result = "Clicked";
}
</script>
</body>
</html>
14 changes: 14 additions & 0 deletions test/assets/input/textarea.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<title>Button test</title>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

text area test?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

</head>
<body>
<textarea></textarea>
<script>
window.result = "";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

''

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

let textarea = document.querySelector('textarea');
textarea.addEventListener('input', () => result = textarea.value, false);
</script>
</body>
</html>
14 changes: 14 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,20 @@ describe('Puppeteer', function() {
expect(navigatedFrames.length).toBe(1);
}));
});

describe('input', function() {
it('should click the button', SX(async function() {
await page.navigate(STATIC_PREFIX + '/input/button.html');
await page.click('button');
expect(await page.evaluate(() => result)).toBe('Clicked');
}));
it('should type into the textarea', SX(async function() {
await page.navigate(STATIC_PREFIX + '/input/textarea.html');
await page.focus('textarea');
await page.type('Type in this text!');
expect(await page.evaluate(() => result)).toBe('Type in this text!');
}));
});
});

// Since Jasmine doesn't like async functions, they should be wrapped
Expand Down