Skip to content

Commit

Permalink
Add full page screenshot generation
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeShi42 committed Nov 21, 2021
1 parent 171d824 commit e535c77
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 50 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ feel free to open an issue or PR for what you think is missing.
| Copy Code to Clipboard |||||
| data-testid Selector Support |||||
| Text selector support |||||
| Screenshot event generation | ||||
| Screenshot event generation | ||||
| Hover event generation |||||
| Record from Chrome Stable |||||

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "deploysentinel-recorder",
"version": "0.1.0",
"version": "0.1.1",
"description": "A Chrome Extension that generates Playwright and Puppeteer scripts automatically from your browser interactions.",
"license": "Apache-2.0",
"scripts": {
Expand Down
3 changes: 3 additions & 0 deletions src/pages/Common/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@
.mr-2 {
margin-right: 0.5em;
}
.mr-4 {
margin-right: 1em;
}
.mb-2 {
margin-bottom: 0.5em;
}
Expand Down
7 changes: 6 additions & 1 deletion src/pages/Content/ActionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ function ActionListItem({
X:{action.deltaX}, Y:{action.deltaY}
</span>
</>
) : action.type === 'fullScreenshot' ? (
<>
<span className="em-text">Take full page screenshot</span>
</>
) : (
<></>
)}
Expand All @@ -89,7 +93,7 @@ export default function ActionList({ actions }: { actions: Action[] }) {
return (
<>
<style>{ActionListStyle}</style>
<div className="ActionList">
<div className="ActionList" data-testId="action-list">
{actions
.filter((action) =>
[
Expand All @@ -100,6 +104,7 @@ export default function ActionList({ actions }: { actions: Action[] }) {
'load',
'resize',
'wheel',
'fullScreenshot',
].includes(action.type)
)
.map((action, i) => (
Expand Down
12 changes: 12 additions & 0 deletions src/pages/Content/CodeGen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ ${lines}
deltaY
)});`;
}

fullScreenshot() {
return `await page.screenshot({ path: 'screenshot.png', fullPage: true });`;
}
}

class PuppeteerScriptBuilder {
Expand Down Expand Up @@ -159,6 +163,10 @@ ${lines}
wheel(deltaX: number, deltaY: number) {
return `await page.evaluate(() => window.scrollBy(${deltaX}, ${deltaY}));`;
}

fullScreenshot() {
return `await page.screenshot({ path: 'screenshot.png', fullPage: true });`;
}
}

function describeAction(action: Action) {
Expand Down Expand Up @@ -192,6 +200,8 @@ function describeAction(action: Action) {
? `Resize window to ${action.width} x ${action.height}`
: action.type === 'wheel'
? `Scroll wheel by X:${action.deltaX}, Y:${action.deltaY}`
: action.type === 'fullScreenshot'
? `Take full page screenshot`
: '';
}

Expand Down Expand Up @@ -255,6 +265,8 @@ export function genCode(
line += scriptBuilder.resize(action.width, action.height);
} else if (action.type === 'wheel') {
line += scriptBuilder.wheel(action.deltaX, action.deltaY);
} else if (action.type === 'fullScreenshot') {
line += scriptBuilder.fullScreenshot();
} else {
return null;
}
Expand Down
120 changes: 78 additions & 42 deletions src/pages/Content/ControlBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { CopyToClipboard } from 'react-copy-to-clipboard';

import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import {
faCamera,
faCopy,
faCheck,
faCheckCircle,
Expand All @@ -26,12 +27,14 @@ const ActionButton = ({
onClick,
children,
label,
testId,
}: {
onClick: () => void;
children: JSX.Element;
label: String;
testId?: String;
}) => (
<div className="ActionButton" onClick={onClick}>
<div className="ActionButton" onClick={onClick} data-testId={testId}>
<div>
<div
style={{
Expand Down Expand Up @@ -76,6 +79,8 @@ function RenderActionText({ action }: { action: Action }) {
? `Resize window to ${action.width} x ${action.height}`
: action.type === 'wheel'
? `Scroll wheel by X:${action.deltaX}, Y:${action.deltaY}`
: action.type === 'fullScreenshot'
? `Take full page screenshot`
: ''}
</>
);
Expand All @@ -102,6 +107,7 @@ export default function ControlBar({ onExit }: { onExit: () => void }) {
'actions' | 'playwright' | 'puppeteer'
>('playwright');
const [copyCodeConfirm, setCopyCodeConfirm] = useState<boolean>(false);
const [screenshotConfirm, setScreenshotConfirm] = useState<boolean>(false);

const [isFinished, setIsFinished] = useState<boolean>(false);

Expand Down Expand Up @@ -208,7 +214,10 @@ export default function ControlBar({ onExit }: { onExit: () => void }) {
<div className="p-4">
<div className="d-flex justify-between mb-2">
<div className="text-xl">
<span className="mr-2">Recording Finished!</span>🎉
<span className="mr-2" data-testId="recording-finished">
Recording Finished!
</span>
🎉
</div>
<div className="text-button" onClick={() => onClose()}>
<FontAwesomeIcon icon={faTimes} size="sm" />
Expand All @@ -233,7 +242,11 @@ export default function ControlBar({ onExit }: { onExit: () => void }) {
</div>
) : (
<div className="d-flex items-center">
<ActionButton label="End Test" onClick={() => onEndRecording()}>
<ActionButton
label="End Test"
onClick={() => onEndRecording()}
testId="end-test"
>
<FontAwesomeIcon icon={faCheckCircle} size="2x" />
</ActionButton>
<div className="w-100 p-4">
Expand All @@ -254,9 +267,12 @@ export default function ControlBar({ onExit }: { onExit: () => void }) {
</div>
<div
className="text-sm link-button"
data-testid={
showAllActions ? 'show-less-actions' : 'show-more-actions'
}
onClick={() => setShowAllActions(!showAllActions)}
>
{showAllActions ? 'Hide' : 'See'} Prior Steps{' '}
{showAllActions ? 'Show Less' : 'Show More'}{' '}
<FontAwesomeIcon
icon={showAllActions ? faChevronUp : faChevronDown}
/>
Expand All @@ -271,58 +287,78 @@ export default function ControlBar({ onExit }: { onExit: () => void }) {
<div className="mb-4">
<span
className="text-sm link-button mr-2"
data-testId={`show-${
showActionsMode === 'actions' ? 'code' : 'actions'
}`}
onClick={() => {
setShowActionsMode(
showActionsMode === 'actions' ? 'playwright' : 'actions'
);
}}
>
Show{' '}
{showActionsMode === 'actions' ? 'Generated Code' : 'Actions'}
Show {showActionsMode === 'actions' ? 'Code' : 'Actions'}
</span>
<span
className={`text-sm link-button mr-2 ${
screenshotConfirm ? 'text-green' : ''
}`}
data-testid="record-screenshot"
onClick={() => {
recorderRef.current?.onFullScreenshot();
setScreenshotConfirm(true);
setTimeout(() => {
setScreenshotConfirm(false);
}, 2000);
}}
>
<FontAwesomeIcon
icon={screenshotConfirm ? faCheck : faCamera}
size="sm"
/>{' '}
Record Screenshot
</span>
{(showActionsMode === 'playwright' ||
showActionsMode === 'puppeteer') && (
<span
className="text-sm link-button mb-4"
onClick={() => {
setShowActionsMode(
showActionsMode === 'playwright'
? 'puppeteer'
: 'playwright'
);
}}
>
Switch to{' '}
{showActionsMode === 'playwright'
? 'Puppeteer'
: 'Playwright'}
</span>
)}
</div>
<div>
{(showActionsMode === 'playwright' ||
showActionsMode === 'puppeteer') && (
<CopyToClipboard
text={genCode(actions, true, showActionsMode)}
onCopy={() => {
setCopyCodeConfirm(true);
setTimeout(() => {
setCopyCodeConfirm(false);
}, 2000);
}}
>
<>
<span
className={`text-sm link-button ${
copyCodeConfirm ? 'text-green' : ''
}`}
className="text-sm link-button mb-4 mr-4"
onClick={() => {
setShowActionsMode(
showActionsMode === 'playwright'
? 'puppeteer'
: 'playwright'
);
}}
>
<FontAwesomeIcon
icon={copyCodeConfirm ? faCheck : faCopy}
size="sm"
/>{' '}
Copy Code
Switch to{' '}
{showActionsMode === 'playwright'
? 'Puppeteer'
: 'Playwright'}
</span>
</CopyToClipboard>
<CopyToClipboard
text={genCode(actions, true, showActionsMode)}
onCopy={() => {
setCopyCodeConfirm(true);
setTimeout(() => {
setCopyCodeConfirm(false);
}, 2000);
}}
>
<span
className={`text-sm link-button ${
copyCodeConfirm ? 'text-green' : ''
}`}
>
<FontAwesomeIcon
icon={copyCodeConfirm ? faCheck : faCopy}
size="sm"
/>{' '}
Copy Code
</span>
</CopyToClipboard>
</>
)}
</div>
</div>
Expand Down
14 changes: 12 additions & 2 deletions src/pages/Content/recorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export type Action =
source: string;
}
| ResizeAction
| { type: 'wheel'; deltaY: number; deltaX: number };
| { type: 'wheel'; deltaY: number; deltaX: number }
| { type: 'fullScreenshot' };

function isEventFromOverlay(event: Event) {
return (
Expand Down Expand Up @@ -308,6 +309,7 @@ class Recorder {
timestamp: event.timeStamp,
};

// @ts-ignore
this.appendToRecording(action);
};

Expand All @@ -319,7 +321,7 @@ class Recorder {
lastResizeAction.width !== width ||
lastResizeAction.height !== height
) {
const action = {
const action: Action = {
type: 'resize',
width,
height,
Expand All @@ -341,6 +343,14 @@ class Recorder {
};

private debouncedOnResize = debounce(this.onResize, 300);

public onFullScreenshot = (): void => {
const action: Action = {
type: 'fullScreenshot',
};

this.appendToRecording(action);
};
}

export default Recorder;
Loading

0 comments on commit e535c77

Please sign in to comment.