-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.js
53 lines (43 loc) · 1.41 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const faker = require('faker');
const waitForPageChange = async (page) => {
await page.waitForNavigation({ timeout: 120000 });
}
const click = async (page, selector) => {
await page.waitForSelector(selector);
await page.click(selector);
};
const open = async (page, url) => {
await page.goto(url);
};
const generateRandomTweet = () => {
return faker.lorem.paragraph().substring(0, 280);
};
const typeText = async (page, selector, text) => {
await page.waitForSelector(selector);
await page.type(selector, text);
};
const focus = async (page, selector) => {
await page.waitForSelector(selector);
await page.focus(selector);
};
const loginToTwitter = async (page, email, password) => {
await open(page, 'https://twitter.com');
await typeText(page, 'input[name=session\\5busername_or_email\\5d]', email);
await typeText(page, 'input[name=session\\5bpassword\\5d]', password);
await click(page, 'input[type=submit]');
await waitForPageChange(page);
console.log(`Logged in successfully to: ${email}`);
};
const tweetAfterLogin = async (page, tweet) => {
await open(page, 'https://twitter.com/compose/tweet');
await focus(page, '.public-DraftEditor-content');
await page.keyboard.type(tweet);
await click(page, 'div[data-testid=tweetButton]');
await waitForPageChange(page);
console.log(`Tweeted successfully`);
};
module.exports = {
loginToTwitter,
tweetAfterLogin,
generateRandomTweet,
};