-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
193 lines (168 loc) · 5.54 KB
/
test.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
183
184
185
186
187
188
189
190
191
192
193
/*global document */
const fs = require('fs');
const expect = require('chai').expect;
const describeURL = require('./index').describeURL;
const puppeteer = require('puppeteer');
const randomWords = require('random-words');
describe('describe-url module', function() {
it('gets an Amazon description', async function() {
const url =
'https://www.amazon.com/OWN-LLC-Watch/dp/B077V4H3VJ/ref=sr_1_1?ie=UTF8&qid=1517963849&sr=8-1&keywords=own';
const result = await describeURL(url);
expect(result).to.exist;
expect(result).to.have.property('url');
expect(result).to.have.property('title');
expect(result).to.have.property('img');
expect(result).to.have.property('description');
});
it('gets an Amazon Video description', async function() {
const url =
'https://www.amazon.com/The-New-World/dp/B00RSI4WY2/ref=sr_1_3?ie=UTF8&qid=1517965273&sr=8-3&keywords=man';
const result = await describeURL(url);
expect(result).to.exist;
expect(result).to.have.property('url');
expect(result).to.have.property('title');
expect(result).to.have.property('img');
expect(result).to.have.property('description');
});
it('gets an Ebay description', async function() {
const url =
'https://www.ebay.com/itm/ROGER-WATERS-THE-WALL-NEW-BLU-RAY/381642029447?epid=218421157&hash=item58dba33987:m:mPI5QUfOHzRISTUqBO9Bd0A';
const result = await describeURL(url);
expect(result).to.exist;
expect(result).to.have.property('url');
expect(result).to.have.property('title');
expect(result).to.have.property('img');
expect(result).to.have.property('description');
});
it('returns error when URL does not exist', async function() {
const url = 'https://pfernandom.githubio';
const result = await describeURL(url);
expect(result).to.have.property('error');
expect(result).to.have.property('stack');
});
it('encodes the URL', async function() {
const url =
'https://pfernandom.github.io?something=guest<script>alert("attacked")</script>';
const result = await describeURL(url);
expect(result.url).to.not.match(/[<>'"]/);
expect(result).to.not.have.property('img');
});
});
describe('training describe-url module', function() {
let browser;
let page;
before(async function() {
browser = await puppeteer.launch({
// headless: false,
// slowMo: 250
});
page = await browser.newPage();
});
after(async function() {
await browser.close();
});
it('tests with random Amazon URLs', async function() {
try {
const term = randomWords();
console.log(`Search Amazon links for the term '${term}'`);
const links = await amazonLinks(term, page);
const results = [];
for (let i = 0; i < links.length; i++){
console.log('Describe the url:', links[i]);
results.push(await describeURL(links[i]));
}
results.forEach(async function(result){
expect(result).to.exist;
expect(result).to.have.property('url');
expect(result).to.have.property('title');
expect(result).to.have.property('img');
expect(result).to.have.property('description');
return result;
});
toHTML(results);
} catch (err){
console.error('Failed test', err);
}
}).timeout(30000);
xit('tests with random Google URLs', async function() {
const links = await googleLinks('GTX 1060', page);
links.forEach(async function(link){
const result = await describeURL(link);
expect(result).to.exist;
expect(result).to.have.property('url');
expect(result).to.have.property('title');
expect(result).to.have.property('img');
expect(result).to.have.property('description');
});
}).timeout(30000);
});
async function googleLinks(term, page){
console.log('Opening page');
await page.goto('https://google.com', { waitUntil: 'networkidle0' });
console.log('type');
await page.type('input[name=q]', term, {delay: 10});
await page.click('input[type="submit"]');
await page.waitForSelector('h3 a');
const links = await page.evaluate(() => {
const links = document.querySelectorAll('h3 a');
console.log('links', links);
return Array.from(links).map(a => {
return a.href;
});
});
return links;
}
async function amazonLinks(term, page){
await page.setViewport({ width: 1280, height: 800 });
await page.goto('https://www.amazon.com', { waitUntil: 'networkidle0' });
await page.type('#twotabsearchtextbox', term);
await page.click('input.nav-input');
await page.waitForSelector('.s-access-detail-page');
return await page.evaluate(() => {
const links = document.querySelectorAll('a.a-link-normal.a-text-normal.s-access-detail-page');
return Array.from(links)
.map(a => a.href)
.filter(a => a.indexOf('redirect') < 0);
});
}
function toHTML(json){
const pageHTML = `<style>
body {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
}
.preview {
margin: 20px;
border-style: solid;
border-width: 1px;
border-color: #dadada;
padding: 10px;
border-radius: 2px;
}
.preview img{
width: 15%;
display: inline-block;
}
.preview p{
max-height: 100px;
display: inline-block;
width: 80%;
}
</style> `;
const html = json.map(thumb => `
<div class="preview">
<div><a href="${thumb.url}">${thumb.title}</a></div>
<img src="${thumb.img}" alt="thumbnail" />
<p>${thumb.description}</p>
</div>
`)
.reduce((acc, curr) => `${acc} <br/> ${curr}`, pageHTML);
fs.writeFile('amazon.html', html, (err) => {
if (err) throw err;
console.log('The file amazon.html has been saved!');
});
fs.writeFile('amazon.json', JSON.stringify(json, null, '\t'), (err) => {
if (err) throw err;
console.log('The file amazon.json has been saved!');
});
}