Skip to content

Instantly share code, notes, and snippets.

@yakup
Forked from dmitrika/puppeteer_and_cucumber.js
Created January 11, 2019 14:08
Show Gist options
  • Save yakup/4f80eaebfa6858f66bf0300fb258fd89 to your computer and use it in GitHub Desktop.
Save yakup/4f80eaebfa6858f66bf0300fb258fd89 to your computer and use it in GitHub Desktop.
const puppeteer = require('puppeteer');
const { defineSupportCode } = require('cucumber')
defineSupportCode(({ Before, Given, When, Then }) => {
Before({ timeout: 60 * 1000 }, async function testCase() {
this.browser = await puppeteer.launch()
})
Given('I am on google with puppeteer', { timeout: 60 * 1000 }, async function testCase() {
this.page = await this.browser.newPage()
await this.page.goto('https://google.com')
})
When('I enter {string}', async function testCase(text) {
await this.page.waitFor('input[name=q]')
await this.page.type('input[name=q]', text)
await this.page.click('input[type="submit"]')
})
Then('I should see {string}', async function testCase(text) {
await this.page.waitForSelector('h3 a')
const links = await this.page.evaluate(() => {
const anchors = Array.from(document.querySelectorAll('h3 a'))
return anchors.map(anchor => anchor.textContent)
})
console.log(links.join('\n'))
console.log(text)
await this.browser.close()
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment