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

Max depth #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ $ npm install --global backstop-crawl
--limit-similar[=3] Limits the number of similar URLs to a set number
Defaults to 3
e.g /blog/1, /blog/2, /blog/3
--max-depth Sets the maximum crawl depth.
Defaults to 0 (no limit)
--reference-url Allows a reference URL to be used in testing

Examples
Expand Down
11 changes: 11 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const cli = meow(
--limit-similar[=3] Limits the number of similar URLs to a set number
Defaults to 3
e.g /blog/1, /blog/2, /blog/3
--max-depth Sets the maximum crawl depth.
Defaults to 0 (no limit)
--reference-url Allows a reference URL to be used in testing

Examples
Expand All @@ -49,6 +51,15 @@ if (cli.flags.limitSimilar) {
}
}

if (cli.flags.maxDepth) {
if (!Number.isInteger(cli.flags.maxDepth)) {
console.error(
`> Error: "${cli.flags.maxDepth}" isn't a valid depth`
);
process.exit(1);
}
}

if (cli.flags.referenceUrl) {

if (!validurl(cli.flags.referenceUrl)) {
Expand Down
4 changes: 4 additions & 0 deletions lib/crawl.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ function crawl(url, flags) {
crawler.ignoreInvalidSSL = true;
}

if (flags.maxDepth) {
crawler.maxDepth = flags.maxDepth;
}

if (flags.outfile) {
outfile = flags.outfile;
}
Expand Down
34 changes: 34 additions & 0 deletions test/fixtures/max-depth-1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"viewports": [
{
"name": "Screen",
"width": 1440,
"height": 900
}
],
"scenarios": [
{
"label": "/",
"url": "http://0.0.0.0:8080/",
"selectors": [
"document"
],
"hideSelectors": [
"iframe"
]
}
],
"paths": {
"bitmaps_reference": "backstop_data/bitmaps_reference",
"bitmaps_test": "backstop_data/bitmaps_test",
"casper_scripts": "backstop_data/casper_scripts",
"html_report": "backstop_data/html_report",
"ci_report": "backstop_data/ci_report"
},
"casperFlags": [],
"engine": "phantomjs",
"report": [
"browser"
],
"debug": false
}
54 changes: 54 additions & 0 deletions test/fixtures/max-depth-2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"viewports": [
{
"name": "Screen",
"width": 1440,
"height": 900
}
],
"scenarios": [
{
"label": "/",
"url": "http://0.0.0.0:8080/",
"selectors": [
"document"
],
"hideSelectors": [
"iframe"
]
},
{
"label": "/test1.html",
"url": "http://0.0.0.0:8080/test1.html",
"selectors": [
"document"
],
"hideSelectors": [
"iframe"
]
},
{
"label": "/blog/",
"url": "http://0.0.0.0:8080/blog/",
"selectors": [
"document"
],
"hideSelectors": [
"iframe"
]
}
],
"paths": {
"bitmaps_reference": "backstop_data/bitmaps_reference",
"bitmaps_test": "backstop_data/bitmaps_test",
"casper_scripts": "backstop_data/casper_scripts",
"html_report": "backstop_data/html_report",
"ci_report": "backstop_data/ci_report"
},
"casperFlags": [],
"engine": "phantomjs",
"report": [
"browser"
],
"debug": false
}
56 changes: 56 additions & 0 deletions test/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,59 @@ test('Can limit similar urls lower than default (4)', async t => {
);
t.deepEqual(file, expected);
});

test('Default usage', async t => {
const [file, expected] = await getFiles(
'./backstop.json',
'./fixtures/default-test.json'
);
return t.deepEqual(file, expected);
});

test('Using max depth zero yields same results as default', async t => {
await execa(crawl, [
'http://0.0.0.0:8080',
'--max-depth=0'
]);
const [file, expected] = await getFiles(
'./backstop.json',
'./fixtures/default-test.json'
);
return t.deepEqual(file, expected);
});

test('Using max depth one includes only the top level.', async t => {
await execa(crawl, [
'http://0.0.0.0:8080',
'--max-depth=1'
]);
const [file, expected] = await getFiles(
'./backstop.json',
'./fixtures/max-depth-1.json'
);
return t.deepEqual(file, expected);
});

test('Using max depth two includes only two levels.', async t => {
await execa(crawl, [
'http://0.0.0.0:8080',
'--max-depth=2'
]);
const [file, expected] = await getFiles(
'./backstop.json',
'./fixtures/max-depth-2.json'
);
return t.deepEqual(file, expected);
});

test('Using max depth three yields same results as default.', async t => {
await execa(crawl, [
'http://0.0.0.0:8080',
'--max-depth=3'
]);
const [file, expected] = await getFiles(
'./backstop.json',
'./fixtures/default-test.json'
);
return t.deepEqual(file, expected);
});