Skip to content

Commit

Permalink
Merge branch '10.0-release' into UNIFY-693-component-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marktnoonan authored Jan 27, 2022
2 parents dc02e84 + 56626c6 commit 13792aa
Show file tree
Hide file tree
Showing 27 changed files with 351 additions and 38 deletions.
4 changes: 4 additions & 0 deletions packages/data-context/src/actions/BrowserActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ export class BrowserActions {
closeBrowser () {
return this.browserApi.close()
}

async focusActiveBrowserWindow () {
await this.browserApi.focusActiveBrowserWindow()
}
}
27 changes: 27 additions & 0 deletions packages/data-context/src/sources/BrowserDataSource.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
import type { FoundBrowser } from '@packages/types'
import os from 'os'
import { execSync } from 'child_process'
import type { DataContext } from '..'

let isPowerShellAvailable = false

try {
execSync(`[void] ''`, { shell: 'powershell' })
isPowerShellAvailable = true
} catch {
// Powershell is unavailable
}

const platform = os.platform()

export interface BrowserApiShape {
close(): Promise<any>
ensureAndGetByNameOrPath(nameOrPath: string): Promise<FoundBrowser | undefined>
getBrowsers(): Promise<FoundBrowser[]>
focusActiveBrowserWindow(): Promise<any>
}

export class BrowserDataSource {
Expand Down Expand Up @@ -48,4 +62,17 @@ export class BrowserDataSource {

return this.idForBrowser(this.ctx.coreData.chosenBrowser) === this.idForBrowser(obj)
}

isFocusSupported (obj: FoundBrowser) {
if (platform === 'darwin' || obj.family !== 'firefox') {
return true
}

// Only allow focusing if PowerShell is available on Windows, since that's what we use to do it
if (obj.family === 'firefox' && platform === 'win32') {
return isPowerShellAvailable
}

return false
}
}
7 changes: 1 addition & 6 deletions packages/frontend-shared/cypress/e2e/e2ePluginSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import pDefer from 'p-defer'
interface InternalOpenProjectArgs {
argv: string[]
projectName: string
browser: string
}

interface InternalAddProjectOpts {
Expand Down Expand Up @@ -228,15 +227,11 @@ async function makeE2ETasks () {
e2eServerPort: ctx.appServerPort,
}
},
async __internal_openProject ({ argv, projectName, browser }: InternalOpenProjectArgs): Promise<ResetOptionsResult> {
async __internal_openProject ({ argv, projectName }: InternalOpenProjectArgs): Promise<ResetOptionsResult> {
if (!scaffoldedProjects.has(projectName)) {
throw new Error(`${projectName} has not been scaffolded. Be sure to call cy.scaffoldProject('${projectName}') in the test, a before, or beforeEach hook`)
}

if (browser !== 'chrome') {
throw new Error(`Cypress in cypress does not support running in the ${browser} browser`)
}

const openArgv = [...argv, '--project', Fixtures.projectPath(projectName), '--port', '4455']

// Runs the launchArgs through the whole pipeline for the CLI open process,
Expand Down
8 changes: 7 additions & 1 deletion packages/frontend-shared/cypress/e2e/support/e2eSupport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ function openProject (projectName: ProjectFixture, argv: string[] = []) {
}

return logInternal({ name: 'openProject', message: argv.join(' ') }, () => {
return taskInternal('__internal_openProject', { projectName, argv, browser: Cypress.browser.name })
return taskInternal('__internal_openProject', { projectName, argv })
}).then((obj) => {
Cypress.env('e2e_serverPort', obj.e2eServerPort)

Expand All @@ -195,6 +195,12 @@ function openProject (projectName: ProjectFixture, argv: string[] = []) {
}

function startAppServer (mode: 'component' | 'e2e' = 'e2e') {
const browser = Cypress.browser.name

if (browser !== 'chrome') {
throw new Error(`Cypress in cypress does not support running in the ${browser} browser`)
}

return logInternal('startAppServer', (log) => {
return cy.window({ log: false }).then((win) => {
return cy.withCtx(async (ctx, o) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const longBrowsersList = [
{
id: '1',
name: 'electron',
displayName: 'Electron',
family: 'chromium',
Expand All @@ -8,80 +9,98 @@ export const longBrowsersList = [
path: '',
majorVersion: '73',
info: 'Info about electron browser',
isFocusSupported: true,
},
{
id: '2',
name: 'chrome',
displayName: 'Chrome',
family: 'chromium',
channel: 'stable',
version: '78.0.3904.108',
path: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
majorVersion: '78',
isFocusSupported: true,
},
{
id: '3',
name: 'chrome',
displayName: 'Chrome',
family: 'chromium',
channel: 'stable',
version: '88.0.3904.00',
path: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
majorVersion: '88',
isFocusSupported: true,
},
{
id: '4',
name: 'chrome',
displayName: 'Canary',
family: 'chromium',
channel: 'canary',
version: '80.0.3977.4',
path: '/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary',
majorVersion: '80',
isFocusSupported: true,
},
{
id: '5',
name: 'chromium',
displayName: 'Chromium',
family: 'chromium',
channel: 'stable',
version: '74.0.3729.0',
path: '/Applications/Chromium.app/Contents/MacOS/Chromium',
majorVersion: '74',
isFocusSupported: true,
},
{
id: '6',
name: 'chromium',
displayName: 'Chromium',
family: 'chromium',
channel: 'stable',
version: '85.0.3729.0',
path: '/Applications/Chromium.app/Contents/MacOS/Chromium',
majorVersion: '85',
isFocusSupported: true,
},
{
id: '7',
name: 'edge',
displayName: 'Edge Beta',
family: 'chromium',
channel: 'beta',
version: '79.0.309.71',
path: '/Applications/Microsoft Edge Beta.app/Contents/MacOS/Microsoft Edge Beta',
majorVersion: '79',
isFocusSupported: true,
},
{
id: '8',
name: 'edge',
displayName: 'Edge Canary',
family: 'chromium',
channel: 'canary',
version: '79.0.309.71',
path: '/Applications/Microsoft Edge Canary.app/Contents/MacOS/Microsoft Edge Canary',
majorVersion: '79',
isFocusSupported: true,
},
{
id: '9',
name: 'edge',
displayName: 'Edge Dev',
family: 'chromium',
channel: 'dev',
version: '80.0.309.71',
path: '/Applications/Microsoft Edge Dev.app/Contents/MacOS/Microsoft Edge Dev',
majorVersion: '79',
isFocusSupported: true,
},
{
id: '10',
name: 'firefox',
displayName: 'Firefox',
family: 'firefox',
Expand All @@ -90,8 +109,10 @@ export const longBrowsersList = [
path: '/Applications/Firefox/Contents/MacOS/Firefox',
majorVersion: '69',
unsupportedVersion: true,
isFocusSupported: true,
},
{
id: '11',
name: 'firefox',
displayName: 'Firefox',
family: 'firefox',
Expand All @@ -100,23 +121,28 @@ export const longBrowsersList = [
path: '/Applications/Firefox/Contents/MacOS/Firefox',
majorVersion: '75',
unsupportedVersion: true,
isFocusSupported: true,
},
{
id: '12',
name: 'firefox',
displayName: 'Firefox Developer Edition',
channel: 'dev',
family: 'firefox',
version: '69.0.2',
path: '/Applications/Firefox Developer/Contents/MacOS/Firefox Developer',
majorVersion: '69',
isFocusSupported: true,
},
{
id: '13',
name: 'firefox',
displayName: 'Firefox Nightly',
channel: 'beta',
family: 'firefox',
version: '69.0.3',
path: '/Applications/Firefox Nightly/Contents/MacOS/Firefox Nightly',
majorVersion: '69',
isFocusSupported: false,
},
] as const
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ export const stubMutation: MaybeResolver<Mutation> = {

return {}
},
focusActiveBrowserWindow (sourc, args, ctx) {
return true
},
hideBrowserWindow (source, args, ctx) {
return true
},
Expand Down
4 changes: 4 additions & 0 deletions packages/graphql/schemas/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Browser implements Node {

"""Relay style Node ID field for the Browser field"""
id: ID!
isFocusSupported: Boolean!
isSelected: Boolean!
majorVersion: String
name: String!
Expand Down Expand Up @@ -692,6 +693,9 @@ type Mutation {
"""user has finished migration component specs - move to next step"""
finishedRenamingComponentSpecs: Query

"""Sets focus to the active browser window"""
focusActiveBrowserWindow: Boolean!

"""Generate spec from source"""
generateSpecFromSource(codeGenCandidate: String!, type: CodeGenType!): ScaffoldedFile

Expand Down
3 changes: 3 additions & 0 deletions packages/graphql/src/schemaTypes/objectTypes/gql-Browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ export const Browser = objectType({
t.nonNull.string('name')
t.nonNull.string('path')
t.nonNull.string('version')
t.nonNull.boolean('isFocusSupported', {
resolve: (source, args, ctx) => ctx.browser.isFocusSupported(source),
})
},
sourceType: {
module: '@packages/types',
Expand Down
10 changes: 10 additions & 0 deletions packages/graphql/src/schemaTypes/objectTypes/gql-Mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,16 @@ export const mutation = mutationType({
},
})

t.nonNull.field('focusActiveBrowserWindow', {
type: 'Boolean',
description: 'Sets focus to the active browser window',
resolve: async (_, args, ctx) => {
await ctx.actions.browser.focusActiveBrowserWindow()

return true
},
})

t.nonNull.field('reconfigureProject', {
type: 'Boolean',
description: 'show the launchpad windows',
Expand Down
57 changes: 57 additions & 0 deletions packages/launchpad/cypress/e2e/choose-a-browser.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,63 @@ describe('Choose a Browser Page', () => {
cy.contains('button', 'Close').click()
cy.wait('@closeBrowser')
})

it('performs mutation to focus open browser when focus button is pressed', () => {
cy.openProject('launchpad', ['--e2e'])

cy.visitLaunchpad()

cy.get('h1').should('contain', 'Choose a Browser')

cy.contains('button', 'Start E2E Testing in Chrome').as('launchButton')

// Stub out response to prevent browser launch but not break internals
cy.intercept('mutation-OpenBrowser_LaunchProject', {
body: {
data: {
launchOpenProject: true,
setProjectPreferences: {
currentProject: {
id: 'test-id',
title: 'launchpad',
__typename: 'CurrentProject',
},
__typename: 'Query',
},
},
},
delay: 500,
}).as('launchProject')

cy.get('@launchButton').click()
cy.contains('button', 'Opening E2E Testing in Chrome').should('be.visible')

cy.wait('@launchProject').then(({ request }) => {
expect(request?.body.variables.testingType).to.eq('e2e')
})

cy.intercept('query-OpenBrowser', (req) => {
req.on('before:response', (res) => {
res.body.data.currentProject.isBrowserOpen = true
})
})

cy.contains('button', 'Focus').as('focusButton')

cy.intercept('mutation-OpenBrowser_FocusActiveBrowserWindow').as('focusBrowser')

cy.withCtx((ctx) => {
sinon.spy(ctx.actions.browser, 'focusActiveBrowserWindow')
})

cy.get('@focusButton').click()

cy.wait('@focusBrowser').then(() => {
cy.withCtx((ctx) => {
expect(ctx.actions.browser.focusActiveBrowserWindow).to.be.called
})
})
})
})

describe('No System Browsers Detected', () => {
Expand Down
Loading

0 comments on commit 13792aa

Please sign in to comment.