Skip to content

Commit

Permalink
decaffeinate: Convert screenshots_spec.coffee to JS
Browse files Browse the repository at this point in the history
  • Loading branch information
jennifer-shehane authored and kuceb committed Sep 23, 2019
1 parent d1bbe07 commit a8aa7d0
Showing 1 changed file with 143 additions and 108 deletions.
Original file line number Diff line number Diff line change
@@ -1,73 +1,86 @@
{ devicePixelRatio } = window
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const { devicePixelRatio } = window;

describe "taking screenshots", ->
failureTestRan = false
onAfterScreenshotResults = []
describe("taking screenshots", function() {
let failureTestRan = false;
const onAfterScreenshotResults = [];

Cypress.Screenshot.defaults({
onAfterScreenshot: ($el, results) ->
onAfterScreenshotResults.push(results)
})
onAfterScreenshot($el, results) {
return onAfterScreenshotResults.push(results);
}
});

it "manually generates pngs", ->
it("manually generates pngs", () =>
cy
.visit("http://localhost:3322/color/black")
.screenshot("black", { capture: "runner" })
.wait(1500)
.visit("http://localhost:3322/color/red")
.screenshot("red", { capture: "runner" })
);

it "can nest screenshots in folders", ->
it("can nest screenshots in folders", () =>
cy
.visit("http://localhost:3322/color/white")
.screenshot("foo/bar/baz", { capture: "runner" })
);

it "generates pngs on failure", ->
failureTestRan = true
it("generates pngs on failure", function() {
failureTestRan = true;

cy
return cy
.visit("http://localhost:3322/color/yellow")
.wait(1500)
.then ->
## failure 1
throw new Error("fail whale")

it "calls onAfterScreenshot with results of failed tests", ->
## this test will only pass if the previous test ran
if not failureTestRan
throw new Error("this test can only pass if the previous test ran")

testFailure = Cypress._.find(onAfterScreenshotResults, {
.then(function() {
//# failure 1
throw new Error("fail whale");
});
});

it("calls onAfterScreenshot with results of failed tests", function() {
//# this test will only pass if the previous test ran
if (!failureTestRan) {
throw new Error("this test can only pass if the previous test ran");
}

const testFailure = Cypress._.find(onAfterScreenshotResults, {
testFailure: true
})
});

expect(testFailure).to.exist
expect(testFailure).to.exist;

expect(Cypress._.map(onAfterScreenshotResults, "name")).to.deep.eq([
return expect(Cypress._.map(onAfterScreenshotResults, "name")).to.deep.eq([
"black", "red", "foo/bar/baz", undefined
])

it "handles devicePixelRatio correctly on headless electron", ->
## this checks to see if the topLeftRight pixel (1, 0) is
## currently white. when electron runs offscreen it upscales
## images incorrectly on retina screens and the algorithm
## blurs this pixel into gray.
]);
});

it("handles devicePixelRatio correctly on headless electron", () =>
//# this checks to see if the topLeftRight pixel (1, 0) is
//# currently white. when electron runs offscreen it upscales
//# images incorrectly on retina screens and the algorithm
//# blurs this pixel into gray.
cy
.screenshot("color-check", { capture: "runner" })
.task("ensure:pixel:color", {
devicePixelRatio
coords: [1, 0]
color: [255, 255, 255] ## white
devicePixelRatio,
coords: [1, 0],
color: [255, 255, 255], //# white
name: "screenshots_spec.coffee/color-check"
})
.task("ensure:pixel:color", {
devicePixelRatio
coords: [0, 1]
color: [255, 255, 255] ## white
devicePixelRatio,
coords: [0, 1],
color: [255, 255, 255], //# white
name: "screenshots_spec.coffee/color-check"
})
);

it "crops app captures to just app size", ->
it("crops app captures to just app size", () =>
cy
.viewport(600, 400)
.visit("http://localhost:3322/color/yellow")
Expand All @@ -78,8 +91,9 @@ describe "taking screenshots", ->
height: 400,
devicePixelRatio
})
);

it "can capture fullPage screenshots", ->
it("can capture fullPage screenshots", () =>
cy
.viewport(600, 200)
.visit("http://localhost:3322/fullPage")
Expand All @@ -90,8 +104,9 @@ describe "taking screenshots", ->
height: 500,
devicePixelRatio
})
);

it "accepts subsequent same captures after multiple tries", ->
it("accepts subsequent same captures after multiple tries", () =>
cy
.viewport(600, 200)
.visit("http://localhost:3322/fullPage-same")
Expand All @@ -102,14 +117,16 @@ describe "taking screenshots", ->
height: 500,
devicePixelRatio
})
);

it "accepts screenshot after multiple tries if somehow app has pixels that match helper pixels", ->
it("accepts screenshot after multiple tries if somehow app has pixels that match helper pixels", () =>
cy
.viewport(1280, 720)
.visit("http://localhost:3322/pathological")
.screenshot("pathological", { capture: "viewport" })
);

it "can capture element screenshots", ->
it("can capture element screenshots", () =>
cy
.viewport(600, 200)
.visit("http://localhost:3322/element")
Expand All @@ -121,48 +138,55 @@ describe "taking screenshots", ->
height: 300,
devicePixelRatio
})
);

it "retries each screenshot for up to 1500ms", ->
it("retries each screenshot for up to 1500ms", () =>
cy
.viewport(400, 400)
.visit("http://localhost:3322/identical")
.get("div:first").should("have.css", "height", "1300px")
.screenshot({
onAfterScreenshot: ($el, results) ->
expect($el).to.match("div")

{ duration } = results

## there should be 4 screenshots taken
## because the height is 1700px.
## the 1st will resolve super fast since it
## won't match any other screenshots.
## the 2nd/3rd will take up to their 1500ms
## because they will be identical to the first.
## the 4th will also go quickly because it will not
## match the 3rd
first = fourth = 250
second = third = 1500
total = first + second + third + fourth
padding = 2000 ## account for slower machines

expect(duration).to.be.within(total, total + padding)
onAfterScreenshot($el, results) {
let fourth, third;
expect($el).to.match("div");

const { duration } = results;

//# there should be 4 screenshots taken
//# because the height is 1700px.
//# the 1st will resolve super fast since it
//# won't match any other screenshots.
//# the 2nd/3rd will take up to their 1500ms
//# because they will be identical to the first.
//# the 4th will also go quickly because it will not
//# match the 3rd
const first = (fourth = 250);
const second = (third = 1500);
const total = first + second + third + fourth;
const padding = 2000; //# account for slower machines

return expect(duration).to.be.within(total, total + padding);
}
})
);

it("ensures unique paths for non-named screenshots", function() {
cy.screenshot({ capture: "runner" });
cy.screenshot({ capture: "runner" });
cy.screenshot({ capture: "runner" });
cy.readFile("cypress/screenshots/screenshots_spec.coffee/taking screenshots -- ensures unique paths for non-named screenshots.png", "base64");
cy.readFile("cypress/screenshots/screenshots_spec.coffee/taking screenshots -- ensures unique paths for non-named screenshots (1).png", "base64");
return cy.readFile("cypress/screenshots/screenshots_spec.coffee/taking screenshots -- ensures unique paths for non-named screenshots (2).png", "base64");
});

it("ensures unique paths when there's a non-named screenshot and a failure", () =>
cy.screenshot({ capture: "viewport" }).then(function() {
throw new Error("failing on purpose");
})
);

it "ensures unique paths for non-named screenshots", ->
cy.screenshot({ capture: "runner" })
cy.screenshot({ capture: "runner" })
cy.screenshot({ capture: "runner" })
cy.readFile("cypress/screenshots/screenshots_spec.coffee/taking screenshots -- ensures unique paths for non-named screenshots.png", "base64")
cy.readFile("cypress/screenshots/screenshots_spec.coffee/taking screenshots -- ensures unique paths for non-named screenshots (1).png", "base64")
cy.readFile("cypress/screenshots/screenshots_spec.coffee/taking screenshots -- ensures unique paths for non-named screenshots (2).png", "base64")

it "ensures unique paths when there's a non-named screenshot and a failure", ->
cy.screenshot({ capture: "viewport" }).then ->
throw new Error("failing on purpose")

describe "clipping", ->
it "can clip app screenshots", ->
describe("clipping", function() {
it("can clip app screenshots", () =>
cy
.viewport(600, 200)
.visit("http://localhost:3322/color/yellow")
Expand All @@ -175,8 +199,9 @@ describe "taking screenshots", ->
height: 50,
devicePixelRatio
})
);

it "can clip runner screenshots", ->
it("can clip runner screenshots", () =>
cy
.viewport(600, 200)
.visit("http://localhost:3322/color/yellow")
Expand All @@ -189,8 +214,9 @@ describe "taking screenshots", ->
height: 60,
devicePixelRatio
})
);

it "can clip fullPage screenshots", ->
it("can clip fullPage screenshots", () =>
cy
.viewport(600, 200)
.visit("http://localhost:3322/fullPage")
Expand All @@ -203,8 +229,9 @@ describe "taking screenshots", ->
height: 70,
devicePixelRatio
})
);

it "can clip element screenshots", ->
return it("can clip element screenshots", () =>
cy
.viewport(600, 200)
.visit("http://localhost:3322/element")
Expand All @@ -218,31 +245,39 @@ describe "taking screenshots", ->
height: 80,
devicePixelRatio
})

it "doesn't take a screenshot for a pending test", ->
@skip()

context "before hooks", ->
before ->
## failure 2
throw new Error("before hook failing")

it "empty test 1", ->

context "each hooks", ->
beforeEach ->
## failure 3
throw new Error("before each hook failed")

afterEach ->
## failure 3 still (since associated only to a single test)
throw new Error("after each hook failed")

it "empty test 2", ->

context "really long test title #{Cypress._.repeat('a', 255)}", ->
it "takes a screenshot", ->
cy.screenshot()

it "takes another screenshot", ->
cy.screenshot()
);
});

it("doesn't take a screenshot for a pending test", function() {
return this.skip();
});

context("before hooks", function() {
before(function() {
//# failure 2
throw new Error("before hook failing");
});

return it("empty test 1", function() {});
});

context("each hooks", function() {
beforeEach(function() {
//# failure 3
throw new Error("before each hook failed");
});

afterEach(function() {
//# failure 3 still (since associated only to a single test)
throw new Error("after each hook failed");
});

return it("empty test 2", function() {});
});

return context(`really long test title ${Cypress._.repeat('a', 255)}`, function() {
it("takes a screenshot", () => cy.screenshot());

return it("takes another screenshot", () => cy.screenshot());
});
});

0 comments on commit a8aa7d0

Please sign in to comment.