-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpuppeteerCustomizedFunctions.js
182 lines (163 loc) · 5.12 KB
/
puppeteerCustomizedFunctions.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
const puppeteer = require("puppeteer");
let postTweetPuppeteer = function postTweetPuppeteer(config, advice) {
async function asyncCall(config, advice) {
const browser = await puppeteer.launch({
headless: true,
args: [
"--disable-dev-shm-usage",
"--shm-size=1gb", // --shm-size=1gb to fix Protocol error (Runtime.callFunctionOn)
"--no-sandbox", // configuration for heroku deployment
"--disable-setuid-sandbox" // configuration for heroku deployment
]
});
const page = await browser.newPage();
// open twitter
await page.goto("https://twitter.com/login", {
timeout: 0,
waitUntil: "domcontentloaded"
});
//Login
// wait for page dom content to load
await page.waitForNavigation({ waitUntil: "domcontentloaded" }).catch(e => {
console.log("Error: " + e);
});
// await page
// .waitForSelector(".js-username-field.email-input.js-initial-focus")
// .then(() => console.log("got Username Field"))
// .catch(e => {
// console.log("Error: " + e);
// });
// await page
// .waitForSelector(
// ".submit.EdgeButton.EdgeButton--primary.EdgeButtom--medium"
// )
// .then(() => console.log("got login button"))
// .catch(e => {
// console.log("Error: " + e);
// });
await page.waitFor(2000)
.then(() => console.log("waited for 2000"))
.catch(e => {
console.log("Error: " + e);
});
// await page
// .click(".js-username-field.email-input.js-initial-focus")
// .then(() => console.log("clicked it username_or_email"))
// .catch(e => {
// console.log("Error: " + e);
// });
//page.$('select[name="busca_grupo_estado"]');
await page
.waitForSelector('[name="session[username_or_email]"]')
.then(() => console.log("got Username Field"))
.catch(e => {
console.log("Error: " + e);
});
await page
.click('[name="session[username_or_email]"]')
.then(() => console.log("clicked it username_or_email"))
.catch(e => {
console.log("Error: " + e);
});
await page.keyboard.type(config.username)
.then(() => console.log("typed username"))
.catch(e => {
console.log("Error: " + e);
});
await page.waitFor(2000).catch(e => {
console.log("Error: " + e);
});
// await page
// .click(".js-password-field")
// .then(() => console.log("clicked it session[password]"))
// .catch(e => {
// console.log("Error: " + e);
// });
//session[password]
await page
.click('[name="session[password]"')
.then(() => console.log("clicked it session[password]"))
.catch(e => {
console.log("Error: " + e);
});
await page.keyboard.type(config.password).catch(e => {
console.log("Error: " + e);
});
//data-testid="LoginForm_Login_Button"
await page
.click('[data-testid="LoginForm_Login_Button"')
.then(() => console.log("Clicked LoginForm_Login_Button"))
.catch(e => {
console.log("Error: " + e);
});
// wait till page load
await page
.waitForNavigation({ waitUntil: "domcontentloaded" })
.then(() => {
console.log("Waited for page navigation");
})
.catch(e => {
console.log("Error: " + e);
});
await page
.waitFor(3000)
.then(() => {
console.log("Waited for 3000");
})
.catch(e => {
console.log("Error: " + e);
});
await page
.waitForSelector('[data-testid="SideNav_NewTweet_Button"]')
.then(() => console.log("got it"))
.catch(e => {
console.log("Error: " + e);
});
//same thing
//await page.click('.css-1dbjc4n.r-1awozwy.r-jw8lkh.r-e7q0ms')
await page.click('[data-testid="SideNav_NewTweet_Button"]').catch(e => {
console.log("Error: " + e);
});
// Typing Tweet
await page
.waitForSelector('[data-testid="tweetTextarea_0"]')
.then(() => console.log("got it tweetTextarea_0"))
.catch(e => {
console.log("Error: " + e);
});
await page
.click('[data-testid="tweetTextarea_0"]')
.then(() => console.log("clicked it tweetTextarea_0"))
.catch(e => {
console.log("Error: " + e);
});
await page.keyboard.type(advice.data).catch(e => {
console.log("Error: " + e);
});
await page.waitFor(2000).catch(e => {
console.log("Error: " + e);
});
//Clicking Tweet Button to post Tweet
await page
.waitForSelector('[data-testid="tweetButton"]')
.then(() => console.log("got it tweetButton"))
.catch(e => {
console.log("Error: " + e);
});
await page
.click('[data-testid="tweetButton"]')
.then(() => console.log("clicked it tweetButton"))
.catch(e => {
console.log("Error: " + e);
});
await page.waitFor(2000).catch(e => {
console.log("Error: " + e);
});
// Closing browser
await browser.close().catch(e => {
console.log("Error: " + e);
});
}
asyncCall(config, advice);
};
module.exports = { postTweetPuppeteer };