Skip to content

Commit

Permalink
feat(expect-puppeteer): support frames (#275)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mavrin authored and gregberge committed Aug 28, 2019
1 parent 04de87a commit 09703ea
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 13 deletions.
1 change: 1 addition & 0 deletions packages/expect-puppeteer/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ function expectPuppeteer(actual) {
const type = getPuppeteerType(actual)
switch (type) {
case 'Page':
case 'Frame':
return internalExpect(actual, pageMatchers)
case 'ElementHandle':
return internalExpect(actual, elementHandleMatchers)
Expand Down
8 changes: 7 additions & 1 deletion packages/expect-puppeteer/src/matchers/notToMatch.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { setupPage } from './setupPage'

describe('not.toMatch', () => {
beforeEach(async () => {
await page.goto(`http://localhost:${process.env.TEST_SERVER_PORT}`)
})

describe('Page', () => {
describe.each(['Page', 'Frame'])('%s', pageType => {
let page
setupPage(pageType, ({ currentPage }) => {
page = currentPage
})
it('should be ok if text is not in the page', async () => {
await expect(page).not.toMatch('Nop!')
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { setupPage } from './setupPage'

describe('not.toMatchElement', () => {
beforeEach(async () => {
await page.goto(`http://localhost:${process.env.TEST_SERVER_PORT}`)
})

describe('Page', () => {
describe.each(['Page', 'Frame'])('%s', pageType => {
let page
setupPage(pageType, ({ currentPage }) => {
page = currentPage
})
it('should not match using selector', async () => {
await expect(page).not.toMatchElement('wtf')
})
Expand Down
32 changes: 32 additions & 0 deletions packages/expect-puppeteer/src/matchers/setupPage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
function waitForFrame(page) {
let fulfill
const promise = new Promise(resolve => {
fulfill = resolve
})
function checkFrame() {
const frame = page.frames().find(f => f.parentFrame() !== null)
if (frame) fulfill(frame)
else page.once(`frameattached`, checkFrame)
}
checkFrame()
return promise
}

export const setupPage = (pageType, cb) => {
let currentPage = page
beforeEach(async () => {
if (pageType === `Page`) {
cb({
currentPage,
})
return
}
await page.goto(
`http://localhost:${process.env.TEST_SERVER_PORT}/frame.html`,
)
currentPage = await waitForFrame(page)
cb({
currentPage,
})
})
}
8 changes: 7 additions & 1 deletion packages/expect-puppeteer/src/matchers/toClick.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { setupPage } from './setupPage'

describe('toClick', () => {
beforeEach(async () => {
await page.goto(`http://localhost:${process.env.TEST_SERVER_PORT}`)
})

describe('Page', () => {
describe.each(['Page', 'Frame'])('%s', pageType => {
let page
setupPage(pageType, ({ currentPage }) => {
page = currentPage
})
it('should click using selector', async () => {
await expect(page).toClick('a[href="/page2.html"]')
await page.waitForSelector('html')
Expand Down
8 changes: 7 additions & 1 deletion packages/expect-puppeteer/src/matchers/toFill.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { setupPage } from './setupPage'

describe('toFill', () => {
beforeEach(async () => {
await page.goto(`http://localhost:${process.env.TEST_SERVER_PORT}`)
})

describe('Page', () => {
describe.each(['Page', 'Frame'])('%s', pageType => {
let page
setupPage(pageType, ({ currentPage }) => {
page = currentPage
})
it('should fill input', async () => {
await expect(page).toFill('[name="firstName"]', 'James')
const value = await page.evaluate(
Expand Down
8 changes: 7 additions & 1 deletion packages/expect-puppeteer/src/matchers/toFillForm.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { setupPage } from './setupPage'

describe('toFillForm', () => {
beforeEach(async () => {
await page.goto(`http://localhost:${process.env.TEST_SERVER_PORT}`)
})

describe('Page', () => {
describe.each(['Page', 'Frame'])('%s', pageType => {
let page
setupPage(pageType, ({ currentPage }) => {
page = currentPage
})
it('should fill input', async () => {
await expect(page).toFillForm('form', {
firstName: 'James',
Expand Down
8 changes: 7 additions & 1 deletion packages/expect-puppeteer/src/matchers/toMatch.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { setupPage } from './setupPage'

describe('toMatch', () => {
beforeEach(async () => {
await page.goto(`http://localhost:${process.env.TEST_SERVER_PORT}`)
})

describe('Page', () => {
describe.each(['Page', 'Frame'])('%s', pageType => {
let page
setupPage(pageType, ({ currentPage }) => {
page = currentPage
})
it('should be ok if text is in the page', async () => {
await expect(page).toMatch('This is home!')
})
Expand Down
16 changes: 13 additions & 3 deletions packages/expect-puppeteer/src/matchers/toMatchElement.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { setupPage } from './setupPage'

describe('toMatchElement', () => {
beforeEach(async () => {
await page.goto(`http://localhost:${process.env.TEST_SERVER_PORT}`)
})

describe('Page', () => {
describe.each(['Page', 'Frame'])('%s', pageType => {
let page
setupPage(pageType, ({ currentPage }) => {
page = currentPage
})
it('should match using selector', async () => {
const element = await expect(page).toMatchElement('a[href="/page2.html"]')
const textContentProperty = await element.getProperty('textContent')
Expand All @@ -12,7 +18,9 @@ describe('toMatchElement', () => {
})

it('should match using text (string)', async () => {
const element = await expect(page).toMatchElement('a', { text: 'Page 2' })
const element = await expect(page).toMatchElement('a', {
text: 'Page 2',
})
const textContentProperty = await element.getProperty('textContent')
const textContent = await textContentProperty.jsonValue()
expect(textContent).toBe('Page 2')
Expand Down Expand Up @@ -56,7 +64,9 @@ describe('toMatchElement', () => {
}

try {
await expect(page).toMatchElement('.displayed', { visible: true })
await expect(page).toMatchElement('.displayed', {
visible: true,
})
} catch (error) {
expect(error.message).toMatch('Element .displayed not found')
expect(error.message).toMatch('waiting for function failed')
Expand Down
3 changes: 2 additions & 1 deletion packages/expect-puppeteer/src/matchers/toSelect.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable no-restricted-syntax */
import toMatchElement from './toMatchElement'
import { getContext } from '../utils'

function select(page, element, value) {
return page.evaluate(
Expand Down Expand Up @@ -48,7 +49,7 @@ async function toSelect(instance, selector, valueOrText, options) {
if (!option) {
throw new Error(`Option not found "${selector}" ("${valueOrText}")`)
}

const { page } = await getContext(instance, () => document)
await select(page, element, option.value)

// await page.select(selector, foundValue)
Expand Down
8 changes: 7 additions & 1 deletion packages/expect-puppeteer/src/matchers/toSelect.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { setupPage } from './setupPage'

describe('toSelect', () => {
beforeEach(async () => {
await page.goto(`http://localhost:${process.env.TEST_SERVER_PORT}`)
})

describe('Page', () => {
describe.each(['Page', 'Frame'])('%s', pageType => {
let page
setupPage(pageType, ({ currentPage }) => {
page = currentPage
})
it('should select an option using value', async () => {
await expect(page).toSelect('select[name="my-select"]', 'opt1')
const currentValue = await page.evaluate(
Expand Down
7 changes: 6 additions & 1 deletion packages/expect-puppeteer/src/matchers/toUploadFile.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import path from 'path'
import { setupPage } from './setupPage'

describe('toUploadFile', () => {
beforeEach(async () => {
await page.goto(`http://localhost:${process.env.TEST_SERVER_PORT}`)
})

describe('Page', () => {
describe.each(['Page', 'Frame'])('%s', pageType => {
let page
setupPage(pageType, ({ currentPage }) => {
page = currentPage
})
it('should upload a select file', async () => {
await expect(page).toUploadFile(
'input[type="file"]',
Expand Down
3 changes: 2 additions & 1 deletion packages/expect-puppeteer/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export const getPuppeteerType = instance => {
instance &&
instance.constructor &&
instance.constructor.name &&
['Page', 'ElementHandle'].includes(instance.constructor.name) &&
['Page', 'Frame', 'ElementHandle'].includes(instance.constructor.name) &&
instance.$
) {
return instance.constructor.name
Expand All @@ -16,6 +16,7 @@ export const getContext = async (instance, pageFunction) => {
const type = getPuppeteerType(instance)
switch (type) {
case 'Page':
case 'Frame':
return {
page: instance,
handle: await instance.evaluateHandle(pageFunction),
Expand Down
1 change: 1 addition & 0 deletions server/public/frame.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<iframe src="/index.html"></iframe>

0 comments on commit 09703ea

Please sign in to comment.