2021-02-18 02:51:57 +01:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import './snapshotTab.css';
|
|
|
|
|
import * as React from 'react';
|
2022-09-21 03:41:51 +02:00
|
|
|
import type { ActionTraceEvent } from '@trace/trace';
|
2023-03-18 04:20:35 +01:00
|
|
|
import { context, prevInList } from './modelUtil';
|
2023-02-17 20:19:53 +01:00
|
|
|
import { CodeMirrorWrapper } from '@web/components/codeMirrorWrapper';
|
|
|
|
|
import { Toolbar } from '@web/components/toolbar';
|
|
|
|
|
import { ToolbarButton } from '@web/components/toolbarButton';
|
2023-03-22 02:20:48 +01:00
|
|
|
import { copy, useMeasure } from '@web/uiUtils';
|
2023-02-17 20:19:53 +01:00
|
|
|
import { InjectedScript } from '@injected/injectedScript';
|
|
|
|
|
import { Recorder } from '@injected/recorder';
|
2023-05-23 21:17:26 +02:00
|
|
|
import ConsoleAPI from '@injected/consoleApi';
|
2023-02-17 20:19:53 +01:00
|
|
|
import { asLocator } from '@isomorphic/locatorGenerators';
|
|
|
|
|
import type { Language } from '@isomorphic/locatorGenerators';
|
|
|
|
|
import { locatorOrSelectorAsSelector } from '@isomorphic/locatorParser';
|
|
|
|
|
import { TabbedPaneTab } from '@web/components/tabbedPane';
|
2021-02-18 02:51:57 +01:00
|
|
|
|
|
|
|
|
export const SnapshotTab: React.FunctionComponent<{
|
2021-05-14 05:41:32 +02:00
|
|
|
action: ActionTraceEvent | undefined,
|
2023-02-17 20:19:53 +01:00
|
|
|
sdkLanguage: Language,
|
|
|
|
|
testIdAttributeName: string,
|
|
|
|
|
}> = ({ action, sdkLanguage, testIdAttributeName }) => {
|
2021-02-18 02:51:57 +01:00
|
|
|
const [measure, ref] = useMeasure<HTMLDivElement>();
|
2023-03-18 04:20:35 +01:00
|
|
|
const [snapshotTab, setSnapshotTab] = React.useState<'action'|'before'|'after'>('action');
|
2023-02-18 02:25:47 +01:00
|
|
|
const [isInspecting, setIsInspecting] = React.useState(false);
|
|
|
|
|
const [highlightedLocator, setHighlightedLocator] = React.useState<string>('');
|
2023-02-17 20:19:53 +01:00
|
|
|
const [pickerVisible, setPickerVisible] = React.useState(false);
|
2021-02-18 02:51:57 +01:00
|
|
|
|
2023-03-18 04:20:35 +01:00
|
|
|
const { snapshots } = React.useMemo(() => {
|
|
|
|
|
if (!action)
|
|
|
|
|
return { snapshots: {} };
|
2023-03-16 06:33:40 +01:00
|
|
|
|
2023-03-18 04:20:35 +01:00
|
|
|
// if the action has no beforeSnapshot, use the last available afterSnapshot.
|
|
|
|
|
let beforeSnapshot = action.beforeSnapshot ? { action, snapshotName: action.beforeSnapshot } : undefined;
|
|
|
|
|
let a = action;
|
|
|
|
|
while (!beforeSnapshot && a) {
|
|
|
|
|
a = prevInList(a);
|
|
|
|
|
beforeSnapshot = a?.afterSnapshot ? { action: a, snapshotName: a?.afterSnapshot } : undefined;
|
2021-08-11 02:06:14 +02:00
|
|
|
}
|
2023-03-18 04:20:35 +01:00
|
|
|
const afterSnapshot = action.afterSnapshot ? { action, snapshotName: action.afterSnapshot } : beforeSnapshot;
|
|
|
|
|
const actionSnapshot = action.inputSnapshot ? { action, snapshotName: action.inputSnapshot } : afterSnapshot;
|
|
|
|
|
return { snapshots: { action: actionSnapshot, before: beforeSnapshot, after: afterSnapshot } };
|
|
|
|
|
}, [action]);
|
2021-08-11 02:06:14 +02:00
|
|
|
|
2023-03-18 04:20:35 +01:00
|
|
|
const { snapshotInfoUrl, snapshotUrl, pointX, pointY, popoutUrl } = React.useMemo(() => {
|
|
|
|
|
const snapshot = snapshots[snapshotTab];
|
|
|
|
|
if (!snapshot)
|
|
|
|
|
return { snapshotUrl: kBlankSnapshotUrl };
|
|
|
|
|
|
|
|
|
|
const params = new URLSearchParams();
|
|
|
|
|
params.set('trace', context(snapshot.action).traceUrl);
|
|
|
|
|
params.set('name', snapshot.snapshotName);
|
|
|
|
|
const snapshotUrl = new URL(`snapshot/${snapshot.action.pageId}?${params.toString()}`, window.location.href).toString();
|
|
|
|
|
const snapshotInfoUrl = new URL(`snapshotInfo/${snapshot.action.pageId}?${params.toString()}`, window.location.href).toString();
|
|
|
|
|
|
|
|
|
|
const pointX = snapshotTab === 'action' ? snapshot.action.point?.x : undefined;
|
|
|
|
|
const pointY = snapshotTab === 'action' ? snapshot.action.point?.y : undefined;
|
|
|
|
|
const popoutParams = new URLSearchParams();
|
|
|
|
|
popoutParams.set('r', snapshotUrl);
|
|
|
|
|
popoutParams.set('trace', context(snapshot.action).traceUrl);
|
|
|
|
|
const popoutUrl = new URL(`popout.html?${popoutParams.toString()}`, window.location.href).toString();
|
|
|
|
|
return { snapshots, snapshotInfoUrl, snapshotUrl, pointX, pointY, popoutUrl };
|
|
|
|
|
}, [snapshots, snapshotTab]);
|
2021-02-18 02:51:57 +01:00
|
|
|
|
2023-03-21 15:40:54 +01:00
|
|
|
const iframeRef0 = React.useRef<HTMLIFrameElement>(null);
|
|
|
|
|
const iframeRef1 = React.useRef<HTMLIFrameElement>(null);
|
2021-11-23 20:36:18 +01:00
|
|
|
const [snapshotInfo, setSnapshotInfo] = React.useState({ viewport: kDefaultViewport, url: '' });
|
2023-03-21 15:40:54 +01:00
|
|
|
const loadingRef = React.useRef({ iteration: 0, visibleIframe: 0 });
|
|
|
|
|
|
2021-02-18 02:51:57 +01:00
|
|
|
React.useEffect(() => {
|
2021-08-11 02:06:14 +02:00
|
|
|
(async () => {
|
2023-03-21 15:40:54 +01:00
|
|
|
const thisIteration = loadingRef.current.iteration + 1;
|
|
|
|
|
const newVisibleIframe = 1 - loadingRef.current.visibleIframe;
|
|
|
|
|
loadingRef.current.iteration = thisIteration;
|
|
|
|
|
|
|
|
|
|
const newSnapshotInfo = { url: '', viewport: kDefaultViewport };
|
2021-11-03 01:35:23 +01:00
|
|
|
if (snapshotInfoUrl) {
|
|
|
|
|
const response = await fetch(snapshotInfoUrl);
|
|
|
|
|
const info = await response.json();
|
2023-03-21 15:40:54 +01:00
|
|
|
if (!info.error) {
|
|
|
|
|
newSnapshotInfo.url = info.url;
|
|
|
|
|
newSnapshotInfo.viewport = info.viewport;
|
|
|
|
|
}
|
2021-03-10 20:43:26 +01:00
|
|
|
}
|
2023-03-21 15:40:54 +01:00
|
|
|
|
|
|
|
|
// Interrupted by another load - bail out.
|
|
|
|
|
if (loadingRef.current.iteration !== thisIteration)
|
2021-08-11 02:06:14 +02:00
|
|
|
return;
|
2023-03-21 15:40:54 +01:00
|
|
|
|
|
|
|
|
const iframe = [iframeRef0, iframeRef1][newVisibleIframe].current;
|
|
|
|
|
if (iframe) {
|
|
|
|
|
let loadedCallback = () => {};
|
|
|
|
|
const loadedPromise = new Promise<void>(f => loadedCallback = f);
|
|
|
|
|
try {
|
|
|
|
|
iframe.addEventListener('load', loadedCallback);
|
|
|
|
|
iframe.addEventListener('error', loadedCallback);
|
|
|
|
|
|
|
|
|
|
const newUrl = snapshotUrl + (pointX === undefined ? '' : `&pointX=${pointX}&pointY=${pointY}`);
|
|
|
|
|
// Try preventing history entry from being created.
|
|
|
|
|
if (iframe.contentWindow)
|
|
|
|
|
iframe.contentWindow.location.replace(newUrl);
|
|
|
|
|
else
|
|
|
|
|
iframe.src = newUrl;
|
|
|
|
|
|
|
|
|
|
await loadedPromise;
|
|
|
|
|
} catch {
|
|
|
|
|
} finally {
|
|
|
|
|
iframe.removeEventListener('load', loadedCallback);
|
|
|
|
|
iframe.removeEventListener('error', loadedCallback);
|
|
|
|
|
}
|
2021-08-11 02:06:14 +02:00
|
|
|
}
|
2023-03-21 15:40:54 +01:00
|
|
|
// Interrupted by another load - bail out.
|
|
|
|
|
if (loadingRef.current.iteration !== thisIteration)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
loadingRef.current.visibleIframe = newVisibleIframe;
|
|
|
|
|
setSnapshotInfo(newSnapshotInfo);
|
2021-08-11 02:06:14 +02:00
|
|
|
})();
|
2023-03-21 15:40:54 +01:00
|
|
|
}, [snapshotUrl, snapshotInfoUrl, pointX, pointY]);
|
2021-08-06 01:04:09 +02:00
|
|
|
|
2022-11-22 17:41:52 +01:00
|
|
|
const windowHeaderHeight = 40;
|
2023-01-30 23:32:45 +01:00
|
|
|
const snapshotContainerSize = {
|
|
|
|
|
width: snapshotInfo.viewport.width,
|
|
|
|
|
height: snapshotInfo.viewport.height + windowHeaderHeight,
|
|
|
|
|
};
|
|
|
|
|
const scale = Math.min(measure.width / snapshotContainerSize.width, measure.height / snapshotContainerSize.height, 1);
|
|
|
|
|
const translate = {
|
|
|
|
|
x: (measure.width - snapshotContainerSize.width) / 2,
|
|
|
|
|
y: (measure.height - snapshotContainerSize.height) / 2,
|
2021-03-11 20:22:59 +01:00
|
|
|
};
|
2023-02-17 20:19:53 +01:00
|
|
|
|
2021-04-19 23:09:50 +02:00
|
|
|
return <div
|
|
|
|
|
className='snapshot-tab'
|
|
|
|
|
tabIndex={0}
|
|
|
|
|
onKeyDown={event => {
|
2023-02-18 02:25:47 +01:00
|
|
|
if (event.key === 'Escape') {
|
|
|
|
|
if (isInspecting)
|
|
|
|
|
setIsInspecting(false);
|
|
|
|
|
}
|
2021-04-19 23:09:50 +02:00
|
|
|
}}
|
2023-02-17 20:19:53 +01:00
|
|
|
>
|
2023-02-18 02:25:47 +01:00
|
|
|
<InspectModeController
|
|
|
|
|
isInspecting={isInspecting}
|
|
|
|
|
sdkLanguage={sdkLanguage}
|
|
|
|
|
testIdAttributeName={testIdAttributeName}
|
|
|
|
|
highlightedLocator={highlightedLocator}
|
|
|
|
|
setHighlightedLocator={setHighlightedLocator}
|
2023-06-20 20:39:21 +02:00
|
|
|
iframe={iframeRef0.current}
|
|
|
|
|
iteration={loadingRef.current.iteration} />
|
2023-03-21 15:40:54 +01:00
|
|
|
<InspectModeController
|
|
|
|
|
isInspecting={isInspecting}
|
|
|
|
|
sdkLanguage={sdkLanguage}
|
|
|
|
|
testIdAttributeName={testIdAttributeName}
|
|
|
|
|
highlightedLocator={highlightedLocator}
|
|
|
|
|
setHighlightedLocator={setHighlightedLocator}
|
2023-06-20 20:39:21 +02:00
|
|
|
iframe={iframeRef1.current}
|
|
|
|
|
iteration={loadingRef.current.iteration} />
|
2023-02-17 20:19:53 +01:00
|
|
|
<Toolbar>
|
|
|
|
|
<ToolbarButton title='Pick locator' disabled={!popoutUrl} toggled={pickerVisible} onClick={() => {
|
|
|
|
|
setPickerVisible(!pickerVisible);
|
2023-02-18 02:25:47 +01:00
|
|
|
setHighlightedLocator('');
|
|
|
|
|
setIsInspecting(!pickerVisible);
|
2023-02-17 20:19:53 +01:00
|
|
|
}}>Pick locator</ToolbarButton>
|
2023-03-18 04:20:35 +01:00
|
|
|
{['action', 'before', 'after'].map(tab => {
|
2023-02-17 20:19:53 +01:00
|
|
|
return <TabbedPaneTab
|
2023-03-18 04:20:35 +01:00
|
|
|
id={tab}
|
|
|
|
|
title={renderTitle(tab)}
|
|
|
|
|
selected={snapshotTab === tab}
|
|
|
|
|
onSelect={() => setSnapshotTab(tab as 'action' | 'before' | 'after')}
|
2023-02-17 20:19:53 +01:00
|
|
|
></TabbedPaneTab>;
|
2021-04-20 04:50:11 +02:00
|
|
|
})}
|
2023-02-17 20:19:53 +01:00
|
|
|
<div style={{ flex: 'auto' }}></div>
|
|
|
|
|
<ToolbarButton icon='link-external' title='Open snapshot in a new tab' disabled={!popoutUrl} onClick={() => {
|
2023-05-23 21:17:26 +02:00
|
|
|
const win = window.open(popoutUrl || '', '_blank');
|
|
|
|
|
win?.addEventListener('DOMContentLoaded', () => {
|
|
|
|
|
const injectedScript = new InjectedScript(win as any, false, sdkLanguage, testIdAttributeName, 1, 'chromium', []);
|
|
|
|
|
new ConsoleAPI(injectedScript);
|
|
|
|
|
});
|
2023-02-17 20:19:53 +01:00
|
|
|
}}></ToolbarButton>
|
|
|
|
|
</Toolbar>
|
2023-03-21 04:45:32 +01:00
|
|
|
{pickerVisible && <Toolbar noMinHeight={true}>
|
2023-02-18 02:25:47 +01:00
|
|
|
<ToolbarButton icon='microscope' title='Pick locator' disabled={!popoutUrl} toggled={isInspecting} onClick={() => {
|
|
|
|
|
setIsInspecting(!isInspecting);
|
2023-02-17 20:19:53 +01:00
|
|
|
}}></ToolbarButton>
|
2023-02-18 02:25:47 +01:00
|
|
|
<CodeMirrorWrapper text={highlightedLocator} language={sdkLanguage} readOnly={!popoutUrl} focusOnChange={true} wrapLines={true} onChange={text => {
|
2023-03-10 04:34:05 +01:00
|
|
|
// Updating text needs to go first - react can squeeze a render between the state updates.
|
2023-02-18 02:25:47 +01:00
|
|
|
setHighlightedLocator(text);
|
2023-03-10 04:34:05 +01:00
|
|
|
setIsInspecting(false);
|
2023-02-17 20:19:53 +01:00
|
|
|
}}></CodeMirrorWrapper>
|
|
|
|
|
<ToolbarButton icon='files' title='Copy locator' disabled={!popoutUrl} onClick={() => {
|
2023-02-18 02:25:47 +01:00
|
|
|
copy(highlightedLocator);
|
2023-02-17 20:19:53 +01:00
|
|
|
}}></ToolbarButton>
|
|
|
|
|
</Toolbar>}
|
2021-02-18 02:51:57 +01:00
|
|
|
<div ref={ref} className='snapshot-wrapper'>
|
2023-03-18 04:20:35 +01:00
|
|
|
<div className='snapshot-container' style={{
|
2023-01-30 23:32:45 +01:00
|
|
|
width: snapshotContainerSize.width + 'px',
|
|
|
|
|
height: snapshotContainerSize.height + 'px',
|
|
|
|
|
transform: `translate(${translate.x}px, ${translate.y}px) scale(${scale})`,
|
2021-02-18 02:51:57 +01:00
|
|
|
}}>
|
2022-11-22 17:41:52 +01:00
|
|
|
<div className='window-header'>
|
|
|
|
|
<div style={{ whiteSpace: 'nowrap' }}>
|
|
|
|
|
<span className='window-dot' style={{ backgroundColor: 'rgb(242, 95, 88)' }}></span>
|
|
|
|
|
<span className='window-dot' style={{ backgroundColor: 'rgb(251, 190, 60)' }}></span>
|
|
|
|
|
<span className='window-dot' style={{ backgroundColor: 'rgb(88, 203, 66)' }}></span>
|
|
|
|
|
</div>
|
2023-02-17 20:19:53 +01:00
|
|
|
<div className='window-address-bar' title={snapshotInfo.url || 'about:blank'}>{snapshotInfo.url || 'about:blank'}</div>
|
2022-11-22 17:41:52 +01:00
|
|
|
<div style={{ marginLeft: 'auto' }}>
|
|
|
|
|
<div>
|
|
|
|
|
<span className='window-menu-bar'></span>
|
|
|
|
|
<span className='window-menu-bar'></span>
|
|
|
|
|
<span className='window-menu-bar'></span>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2023-03-21 15:40:54 +01:00
|
|
|
<div className='snapshot-switcher'>
|
|
|
|
|
<iframe ref={iframeRef0} name='snapshot' className={loadingRef.current.visibleIframe === 0 ? 'snapshot-visible' : ''}></iframe>
|
|
|
|
|
<iframe ref={iframeRef1} name='snapshot' className={loadingRef.current.visibleIframe === 1 ? 'snapshot-visible' : ''}></iframe>
|
|
|
|
|
</div>
|
2023-03-18 04:20:35 +01:00
|
|
|
</div>
|
2021-02-18 02:51:57 +01:00
|
|
|
</div>
|
|
|
|
|
</div>;
|
|
|
|
|
};
|
2021-04-20 04:50:11 +02:00
|
|
|
|
|
|
|
|
function renderTitle(snapshotTitle: string): string {
|
|
|
|
|
if (snapshotTitle === 'before')
|
|
|
|
|
return 'Before';
|
|
|
|
|
if (snapshotTitle === 'after')
|
|
|
|
|
return 'After';
|
|
|
|
|
if (snapshotTitle === 'action')
|
|
|
|
|
return 'Action';
|
|
|
|
|
return snapshotTitle;
|
|
|
|
|
}
|
2021-11-23 20:36:18 +01:00
|
|
|
|
2023-02-18 02:25:47 +01:00
|
|
|
export const InspectModeController: React.FunctionComponent<{
|
|
|
|
|
iframe: HTMLIFrameElement | null,
|
|
|
|
|
isInspecting: boolean,
|
|
|
|
|
sdkLanguage: Language,
|
|
|
|
|
testIdAttributeName: string,
|
|
|
|
|
highlightedLocator: string,
|
|
|
|
|
setHighlightedLocator: (locator: string) => void,
|
2023-06-20 20:39:21 +02:00
|
|
|
iteration: number,
|
|
|
|
|
}> = ({ iframe, isInspecting, sdkLanguage, testIdAttributeName, highlightedLocator, setHighlightedLocator, iteration }) => {
|
2023-02-18 02:25:47 +01:00
|
|
|
React.useEffect(() => {
|
2023-02-23 20:57:15 +01:00
|
|
|
const win = iframe?.contentWindow as any;
|
2023-03-21 15:40:54 +01:00
|
|
|
let recorder: Recorder | undefined;
|
2023-03-18 04:20:35 +01:00
|
|
|
try {
|
2023-03-21 15:40:54 +01:00
|
|
|
if (!win)
|
|
|
|
|
return;
|
|
|
|
|
recorder = win._recorder;
|
|
|
|
|
if (!recorder && !isInspecting && !highlightedLocator)
|
2023-03-18 04:20:35 +01:00
|
|
|
return;
|
|
|
|
|
} catch {
|
2023-03-21 15:40:54 +01:00
|
|
|
// Potential cross-origin exception when accessing win._recorder.
|
2023-02-18 02:25:47 +01:00
|
|
|
return;
|
2023-03-18 04:20:35 +01:00
|
|
|
}
|
2023-02-18 02:25:47 +01:00
|
|
|
if (!recorder) {
|
|
|
|
|
const injectedScript = new InjectedScript(win, false, sdkLanguage, testIdAttributeName, 1, 'chromium', []);
|
|
|
|
|
recorder = new Recorder(injectedScript, {
|
|
|
|
|
async setSelector(selector: string) {
|
2023-03-23 02:52:04 +01:00
|
|
|
setHighlightedLocator(asLocator('javascript', selector, false /* isFrameLocator */, true /* playSafe */));
|
2023-02-18 02:25:47 +01:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
win._recorder = recorder;
|
|
|
|
|
}
|
|
|
|
|
const actionSelector = locatorOrSelectorAsSelector(sdkLanguage, highlightedLocator, testIdAttributeName);
|
|
|
|
|
recorder.setUIState({
|
|
|
|
|
mode: isInspecting ? 'inspecting' : 'none',
|
|
|
|
|
actionSelector,
|
|
|
|
|
language: sdkLanguage,
|
|
|
|
|
testIdAttributeName,
|
|
|
|
|
});
|
2023-06-20 20:39:21 +02:00
|
|
|
}, [iframe, isInspecting, highlightedLocator, setHighlightedLocator, sdkLanguage, testIdAttributeName, iteration]);
|
2023-02-18 02:25:47 +01:00
|
|
|
return <></>;
|
|
|
|
|
};
|
|
|
|
|
|
2021-11-23 20:36:18 +01:00
|
|
|
const kDefaultViewport = { width: 1280, height: 720 };
|
2023-03-18 04:20:35 +01:00
|
|
|
const kBlankSnapshotUrl = 'data:text/html,<body style="background: #ddd"></body>';
|