Skip to content

Commit

Permalink
all tests passing, legacy preload works
Browse files Browse the repository at this point in the history
  • Loading branch information
toddtarsi committed Dec 23, 2023
1 parent e37429c commit 106e6fd
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ preload({
},
})
window.addEventListener('DOMContentLoaded', async () => {
const plugins = await api.plugins.listPreloadPaths()
for (const plugin of plugins) {
__non_webpack_require__(plugin)
}
webFrame.executeJavaScript(`
Object.defineProperty(navigator, 'webdriver', {
get () {
Expand Down
7 changes: 1 addition & 6 deletions packages/selenium-ide/src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,7 @@ app.on('ready', async () => {
connectSessionLogging(session)
await session.system.startup()

process.on('SIGINT', async () => {
await session.system.shutdown()
if (session.system.isDown) {
await session.system.quit()
}
})
process.on('SIGINT', () => app.quit())
app.on('open-file', async (_e, path) => {
// Instantiate the session
await session.projects.load(path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ export default class DriverController extends BaseController {
}

async stopProcess(): Promise<null | string> {
await this.session.recorder.stop()
await Promise.all(
this.session.playback.playbacks.map((playback) => playback.cleanup())
)
await this.session.windows.closeAllPlaybackWindows()
await this.session.driver.executor?.cleanup()
if (this.driverProcess) {
Expand Down
107 changes: 71 additions & 36 deletions packages/selenium-ide/src/main/session/controllers/Playback/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,32 @@ export default class PlaybackController extends BaseController {
if (useBidi) {
const handles = await driver.getAllWindowHandles()
if (!handles.length) {
const playbackPos = await this.session.store.get('windowPositionPlayback')
const playbackPos = await this.session.store.get(
'windowPositionPlayback'
)
const playbackSize = await this.session.store.get('windowSizePlayback')
await driver.switchTo().newWindow('tab')
await driver.manage().window().setPosition(...playbackPos)
await driver.manage().window().setSize(...playbackSize)
await driver
.manage()
.window()
.setPosition(...playbackPos)
await driver
.manage()
.window()
.setSize(...playbackSize)
const windowDimensionCache = setInterval(async () => {
const handles = await driver.getAllWindowHandles()
if (!handles.length) {
clearInterval(windowDimensionCache)
}
const pos = await driver.manage().window().getPosition()
const size = await driver.manage().window().getSize()
await this.session.store.set('windowPositionPlayback', [pos.x, pos.y])
await this.session.store.set('windowSizePlayback', [size.width, size.height])
}, 1000);
await this.session.store.set('windowPositionPlayback', [pos.x, pos.y])
await this.session.store.set('windowSizePlayback', [
size.width,
size.height,
])
}, 1000)
}
return
}
Expand Down Expand Up @@ -151,37 +162,48 @@ export default class PlaybackController extends BaseController {
}
}

async getPlayback(testID?: string) {
const browserInfo = this.session.store.get('browserInfo')
const allowMultiplePlaybacks =
(await this.isParallel()) && this.testQueue.length
const makeNewPlayback = allowMultiplePlaybacks || !this.playbacks.length
if (makeNewPlayback) {
const playback = new Playback({
baseUrl: this.session.projects.project.url,
executor: await this.session.driver.build({
browser: browserInfo.browser,
}),
getTestByName: (name: string) => this.session.tests.getByName(name),
logger: console,
variables: new Variables(),
options: {
delay: this.session.projects.project.delay || 0,
},
})
await playback.init()
const EE = playback['event-emitter']
EE.addListener(
PlaybackEvents.PLAYBACK_STATE_CHANGED,
this.handlePlaybackStateChanged(
playback,
allowMultiplePlaybacks ? testID : null
)
)
EE.addListener(
PlaybackEvents.COMMAND_STATE_CHANGED,
this.handleCommandStateChanged
)
this.playbacks.push(playback)
return playback
}
return this.playbacks[0]
}

async play(testID: string, playRange = PlaybackController.defaultPlayRange) {
this.playingTest = testID
this.playRange = playRange
this.isPlaying = true
/**
* Create playback if none exists
*/
const browserInfo = this.session.store.get('browserInfo')
const playback = new Playback({
baseUrl: this.session.projects.project.url,
executor: await this.session.driver.build({
browser: browserInfo.browser,
}),
getTestByName: (name: string) => this.session.tests.getByName(name),
logger: console,
variables: new Variables(),
options: {
delay: this.session.projects.project.delay || 0,
},
})
await playback.init()
const EE = playback['event-emitter']
EE.addListener(
PlaybackEvents.PLAYBACK_STATE_CHANGED,
this.handlePlaybackStateChanged(playback, testID)
)
EE.addListener(
PlaybackEvents.COMMAND_STATE_CHANGED,
this.handleCommandStateChanged
)
this.playbacks.push(playback)
const playback = await this.getPlayback(testID)
/**
* If not ending at end of test, use playTo command
* or playSingleCommand if just one command specified.
Expand All @@ -190,7 +212,7 @@ export default class PlaybackController extends BaseController {
if (playRange[1] !== -1) {
const test = this.session.tests.getByID(testID)
if (playRange[0] === playRange[1]) {
await playback.playSingleCommand(test.commands[playRange[0]])
await playback.playSingleCommand(test.commands[playRange[0]])
} else {
await playback.playTo(test, playRange[1], playRange[0])
}
Expand All @@ -201,6 +223,17 @@ export default class PlaybackController extends BaseController {
}
}

async isParallel() {
const {
project: { suites },
state: { activeSuiteID },
} = await this.session.state.get()
this.playingSuite = activeSuiteID
const suite = suites.find(hasID(activeSuiteID))
this.testQueue = suite?.tests ?? []
return suite?.parallel ?? false
}

async playSuite() {
const {
project: { suites },
Expand Down Expand Up @@ -248,9 +281,11 @@ export default class PlaybackController extends BaseController {
}

handlePlaybackStateChanged =
(playback: Playback, testID: string) =>
(playback: Playback, testID: string | null = null) =>
async (e: PlaybackEventShapes['PLAYBACK_STATE_CHANGED']) => {
const testName = this.session.tests.getByID(testID)?.name
const testName = this.session.tests.getByID(
testID || this.session.state.state.activeTestID
)?.name
console.debug(
`Playing state changed ${e.state} for test ${testName}`,
this.playingSuite
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import {
LocatorFields,
RecordNewCommandInput,
} from '@seleniumhq/side-api'
import { randomInt, randomUUID } from 'crypto'
import { randomUUID } from 'crypto'
import { relative } from 'node:path'
import BaseController from '../Base'
import { BrowserWindow } from 'electron'
import { WebDriverExecutor } from '@seleniumhq/side-runtime'

const uninitializedWindows = ['data:,', 'about:blank']

Expand Down Expand Up @@ -54,7 +53,6 @@ const getFrameTraversalCommands = (
}

export default class RecorderController extends BaseController {
driver!: WebDriverExecutor
windowIDs: number[] = []

async recordNewCommand(
Expand All @@ -65,6 +63,7 @@ export default class RecorderController extends BaseController {
return null
}
const activeWindowHandleID = getActiveWindowHandleID(session) || 'root'
console.log(activeWindowHandleID, session.project.tests[0].commands)
const commands = []
if (activeWindowHandleID != cmd.winHandleId) {
const selectWindowCommand: CommandShape = {
Expand Down Expand Up @@ -102,16 +101,6 @@ export default class RecorderController extends BaseController {
)
}

async handleNewWindow() {
const session = await this.session.state.get()
if (session.state.status !== 'recording') return
const newWindowID = `win${randomInt(1, 9999)}`
this.session.api.recorder.onNewWindow.dispatchEvent(
newWindowID,
randomUUID()
)
}

async requestSelectElement(activate: boolean, fieldName: LocatorFields) {
this.session.windows.getLastPlaybackWindow().focus()
this.session.api.recorder.onRequestSelectElement.dispatchEvent(
Expand Down Expand Up @@ -159,15 +148,17 @@ export default class RecorderController extends BaseController {
newStepID: string
windowHandle: string | null
} | null> {
const playback = await this.session.playback.getPlayback()
const executor = playback.executor
const driver = executor.driver
const useBidi = this.session.store.get('browserInfo.useBidi')
const newStepID = randomUUID()
this.driver = await this.session.driver.build()
if (useBidi) {
const firstWindowURL = await this.driver.driver.getCurrentUrl()
const firstWindowURL = await driver.getCurrentUrl()
if (uninitializedWindows.includes(firstWindowURL)) {
await this.driver.doOpen(this.session.projects.project.url)
await executor.doOpen(this.session.projects.project.url)
}
const windowHandle = await this.driver.driver.getWindowHandle()
const windowHandle = await driver.getWindowHandle()
return { newStepID, windowHandle }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,6 @@ export default class WindowsController extends BaseController {
this.playbackWindows.push(window)
window.webContents.insertCSS(playbackCSS)
window.webContents.setWindowOpenHandler(() => {
this.session.recorder.handleNewWindow()
return {
action: 'allow',
overrideBrowserWindowOptions: playbackWindowOptions,
Expand Down
1 change: 1 addition & 0 deletions packages/side-runtime/src/__tests__/playback.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ describe('Playback', () => {
value: '',
})
await playback.resume()
await psetTimeout(10)
expect(executor.doOpen).toHaveBeenCalledTimes(4)
})

Expand Down
2 changes: 1 addition & 1 deletion packages/side-runtime/src/webdriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ export default class WebDriverExecutor {
}

async cleanup() {
if (this.initialized) {
if (this.driver) {
await this.driver.quit()
// @ts-expect-error
this.driver = undefined
Expand Down

0 comments on commit 106e6fd

Please sign in to comment.