Created
August 2, 2018 14:25
-
-
Save wwerner/774cf97b808e322764764aef49c0cb91 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const CFG = require('../codecept.conf.js').config.myConfig | |
const MailSlurpClient = require('mailslurp-client') // https://www.npmjs.com/package/mailslurp-client | |
const emailApi = new MailSlurpClient.InboxcontrollerApi() | |
var assert = require('assert') | |
class EmailHelper extends Helper { // eslint-disable-line no-undef | |
async setupMailbox () { | |
var data = | |
await emailApi.createRandomInboxUsingPOST(CFG.emailApiKey) | |
.catch(err => { console.log(err); assert.fail(err) }) | |
this.emailAddress = data.payload.address | |
this.emailId = data.payload.id | |
} | |
getEmailAddress () { | |
if (!this.emailAddress) assert.fail('Mailbox is not set up. Did you run setupMailbox()?') | |
return this.emailAddress | |
} | |
getEmailId () { | |
if (!this.emailId) assert.fail('Mailbox is not set up. Did you run setupMailbox()?') | |
return this.emailId | |
} | |
async fillFieldWithMyEmailAddress (selector, ...options) { | |
await this.helpers['WebDriverIO'].fillField(selector, this.getEmailAddress()) | |
} | |
async fillFieldWithMyUserId (selector, ...options) { | |
await this.helpers['WebDriverIO'].fillField(selector, this.getEmailId()) | |
} | |
async receiveEmail (subject) { | |
var data = await emailApi | |
.getEmailsForInboxUsingGET(CFG.emailApiKey, this.getEmailId(), { | |
minCount: 1, | |
maxWait: 90, | |
since: new Date(new Date().getTime() - 120000) // look for mails that arrived in the last two minutes | |
}) | |
var latestEmail = data.payload.reduce(function (l, c) { return c.received > l.received ? c : l }) | |
return assert.equal(latestEmail.subject, subject, `expected one email with subject ${subject}, but found ${latestEmail.subject}`) | |
} | |
async clickVerificationLinkInEmail () { | |
var data = await emailApi | |
.getEmailsForInboxUsingGET(CFG.emailApiKey, this.getEmailId(), { | |
minCount: 1, | |
maxWait: 90, | |
since: new Date(new Date().getTime() - 120000) // look for mails that arrived in the last two minutes | |
}) | |
var latestEmail = data.payload.reduce(function (l, c) { return c.received > l.received ? c : l }) | |
var verificationLinkRegEx = /<a href="(https:.+verifyRegistration.+?)"/g | |
var verificationLink = verificationLinkRegEx.exec(latestEmail.body)[1] | |
await this.helpers['WebDriverIO'].amOnPage(verificationLink) | |
} | |
} | |
module.exports = EmailHelper |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment