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

Add bar position option, experimental Cypress test browser support #23

Merged
merged 2 commits into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add bar position option, experimental Cypress test browser support
  • Loading branch information
MikeShi42 committed Apr 21, 2022
commit aad877351e576a69c33457bcc26f285ca06bf034
37 changes: 21 additions & 16 deletions src/pages/Background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,17 @@ async function onNavEvent(
details: chrome.webNavigation.WebNavigationTransitionCallbackDetails
) {
const { tabId, url, transitionType, transitionQualifiers, frameId } = details;
const { recording, recordingTabId, recordingState } = await localStorageGet([
'recording',
'recordingState',
'recordingTabId',
]);
const { recording, recordingTabId, recordingFrameId, recordingState } =
await localStorageGet([
'recording',
'recordingState',
'recordingTabId',
'recordingFrameId',
]);

// Check if it's a parent frame, we're recording, and it's the right tabid
if (
frameId !== 0 ||
frameId !== recordingFrameId ||
recordingState !== 'active' ||
recordingTabId !== tabId
) {
Expand Down Expand Up @@ -75,7 +77,7 @@ chrome.runtime.onMessage.addListener(async function (
throw new Error('New tab id not defined');
}

setStartRecordingStorage(tabId, newUrl, testEditorTabId);
setStartRecordingStorage(tabId, 0, newUrl, testEditorTabId);
} else if (request.type === 'forward-recording') {
// Focus the original deploysentinel webapp tab post-recording
chrome.tabs.update(request.tabId, { active: true });
Expand Down Expand Up @@ -103,19 +105,22 @@ chrome.webNavigation.onCommitted.addListener(onNavEvent);
chrome.webNavigation.onCompleted.addListener(async (details) => {
const { tabId, frameId } = details;

if (frameId !== 0) {
return;
}
const { recordingTabId, recordingFrameId, recordingState } =
await localStorageGet([
'recordingTabId',
'recordingFrameId',
'recordingState',
]);

const { recordingTabId, recordingState } = await localStorageGet([
'recordingTabId',
'recordingState',
]);
if (tabId != recordingTabId || recordingState != 'active') {
if (
frameId !== recordingFrameId ||
tabId != recordingTabId ||
recordingState != 'active'
) {
return;
}

executeScript(tabId, 'contentScript.bundle.js');
executeScript(tabId, recordingFrameId, 'contentScript.bundle.js');
});

chrome.contextMenus.removeAll();
Expand Down
41 changes: 41 additions & 0 deletions src/pages/Common/ScriptTypeSelect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React, { useEffect, useState } from 'react';
import { ScriptType } from '../types';

export default function ScriptTypeSelect({
value,
onChange,
color,
fontSize,
shortDescription,
}: {
value: ScriptType;
onChange: (val: ScriptType) => void;
color?: string;
fontSize?: number;
shortDescription?: boolean;
}) {
return (
<select
className="link-button mr-4"
style={{
backgroundColor: '#080a0b',
color: color ?? 'white',
border: 'none',
outline: 'none',
fontSize,
}}
onChange={(e) => onChange(e.target.value as ScriptType)}
value={value}
>
<option value={ScriptType.Playwright}>
Playwright{!shortDescription ? ' Library' : ''}
</option>
<option value={ScriptType.Puppeteer}>
Puppeteer{!shortDescription ? ' Library' : ''}
</option>
<option value={ScriptType.Cypress}>
Cypress{!shortDescription ? ' Library' : ''}
</option>
</select>
);
}
49 changes: 49 additions & 0 deletions src/pages/Common/hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import React, { useEffect, useState } from 'react';

import {
setPreferredLibraryStorage,
setPreferredBarPositionStorage,
localStorageGet,
} from './utils';
import { ScriptType, BarPosition } from '../types';

export function usePreferredLibrary() {
const [preferredLibrary, setPreferredLibrary] = useState<ScriptType | null>(
null
);

useEffect(() => {
localStorageGet(['preferredLibrary']).then(
({ preferredLibrary: storedPreferredLibrary }) => {
setPreferredLibrary(storedPreferredLibrary);
}
);
}, []);

const setPreferredLibraryWithStorage = (library: ScriptType) => {
setPreferredLibrary(library);
setPreferredLibraryStorage(library);
};

return [preferredLibrary, setPreferredLibraryWithStorage] as const;
}

export function usePreferredBarPosition(defaultPosition: BarPosition) {
const [preferredBarPosition, setPreferredBarPosition] =
useState<BarPosition | null>(defaultPosition);

useEffect(() => {
localStorageGet(['preferredBarPosition']).then(
({ preferredBarPosition: storedPreferredBarPosition }) => {
setPreferredBarPosition(storedPreferredBarPosition ?? defaultPosition);
}
);
}, []);

const setPreferredBarPositionWithStorage = (barPosition: BarPosition) => {
setPreferredBarPosition(barPosition);
setPreferredBarPositionStorage(barPosition);
};

return [preferredBarPosition, setPreferredBarPositionWithStorage] as const;
}
3 changes: 3 additions & 0 deletions src/pages/Common/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@
.justify-between {
justify-content: space-between;
}
.justify-content-center {
justify-content: center;
}
.items-center {
align-items: center;
}
Expand Down
48 changes: 43 additions & 5 deletions src/pages/Common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,29 @@ export function setEndRecordingStorage() {
chrome.storage.local.set({
recordingState: 'finished',
recordingTabId: null,
recordingFrameId: null,
returnTabId: null,
});
}

export function setPreferredLibraryStorage(library: string) {
chrome.storage.local.set({ preferredLibrary: library });
}

export function setPreferredBarPositionStorage(position: string) {
chrome.storage.local.set({ preferredBarPosition: position });
}

export function setStartRecordingStorage(
tabId: number,
frameId: number,
newUrl: string,
returnTabId?: number
) {
const storage = {
recordingState: 'active',
recordingTabId: tabId,
recordingFrameId: frameId,
recording: [
{
type: 'load',
Expand Down Expand Up @@ -72,20 +83,47 @@ export async function getCurrentTab(): Promise<chrome.tabs.Tab> {
return tab;
}

export async function executeScript(tabId: number, file: string) {
// Determins if the current tab is a Cypress test tab
export function isCypressBrowser(tabId: number) {
if (typeof browser === 'object') {
return browser.tabs
.executeScript(tabId, {
code: `document.querySelector('script[src*="/__cypress"]') != null`,
})
.then((result) => (result?.[0] ?? false) as boolean);
} else {
return new Promise((resolve, reject) => {
chrome.scripting.executeScript(
{
target: { tabId },
func: () =>
document.querySelector('script[src*="/__cypress"]') != null,
},
(executedScript) => resolve(executedScript?.[0]?.result ?? false)
);
});
}
}

export async function executeScript(
tabId: number,
frameId: number,
file: string
) {
if (typeof browser === 'object') {
await browser.tabs.executeScript(tabId, { file });
await browser.tabs.executeScript(tabId, { file, frameId });
} else {
await chrome.scripting.executeScript({
target: { tabId },
target: { tabId, frameIds: [frameId] },
files: [file],
});
}
}

export async function executeCleanUp(tabId: number) {
export async function executeCleanUp(tabId: number, frameId: number) {
if (typeof browser === 'object') {
await browser.tabs.executeScript(tabId, {
frameId,
code: `
if (typeof window?.__DEPLOYSENTINEL_CLEAN_UP === 'function') {
window.__DEPLOYSENTINEL_CLEAN_UP();
Expand All @@ -94,7 +132,7 @@ export async function executeCleanUp(tabId: number) {
});
} else {
await chrome.scripting.executeScript({
target: { tabId },
target: { tabId, frameIds: [frameId] },
func: () => {
if (typeof window?.__DEPLOYSENTINEL_CLEAN_UP === 'function') {
window.__DEPLOYSENTINEL_CLEAN_UP();
Expand Down
Loading