pinafore/tests/utils.js

330 lines
11 KiB
JavaScript
Raw Normal View History

2018-02-20 01:04:37 +00:00
import { ClientFunction as exec, Selector as $ } from 'testcafe'
2018-03-03 01:54:38 +00:00
import * as images from './images'
import * as blobUtils from './blobUtils'
2018-02-20 01:04:37 +00:00
export const settingsButton = $('nav a[aria-label=Settings]')
export const instanceInput = $('#instanceInput')
2018-04-01 05:08:24 +00:00
export const modalDialog = $('.modal-dialog')
2018-02-21 17:26:22 +00:00
export const modalDialogContents = $('.modal-dialog-contents')
export const closeDialogButton = $('.close-dialog-button')
2018-02-24 22:49:28 +00:00
export const notificationsNavButton = $('nav a[href="/notifications"]')
export const homeNavButton = $('nav a[href="/"]')
2018-03-19 17:09:05 +00:00
export const localTimelineNavButton = $('nav a[href="/local"]')
2018-03-09 02:08:14 +00:00
export const searchNavButton = $('nav a[href="/search"]')
export const communityNavButton = $('nav a[href="/community"]')
2018-04-21 07:33:42 +00:00
export const settingsNavButton = $('nav a[href="/settings"]')
2018-02-24 22:49:28 +00:00
export const formError = $('.form-error-user-error')
2018-02-28 05:20:48 +00:00
export const composeInput = $('.compose-box-input')
2018-03-04 00:12:48 +00:00
export const composeContentWarning = $('.content-warning-input')
2018-02-28 05:20:48 +00:00
export const composeButton = $('.compose-box-button')
export const composeLengthIndicator = $('.compose-box-length')
2018-03-01 02:45:29 +00:00
export const emojiButton = $('.compose-box-toolbar button:first-child')
2018-03-03 05:55:04 +00:00
export const mediaButton = $('.compose-box-toolbar button:nth-child(2)')
2018-03-03 21:23:26 +00:00
export const postPrivacyButton = $('.compose-box-toolbar button:nth-child(3)')
2018-03-04 00:12:48 +00:00
export const contentWarningButton = $('.compose-box-toolbar button:nth-child(4)')
2018-03-01 06:45:42 +00:00
export const emailInput = $('input#user_email')
export const passwordInput = $('input#user_password')
export const authorizeInput = $('button[type=submit]:not(.negative)')
2018-03-07 07:57:06 +00:00
export const logInToInstanceLink = $('a[href="/settings/instances/add"]')
2018-03-09 02:08:14 +00:00
export const searchInput = $('.search-input')
2018-03-10 06:31:26 +00:00
export const postStatusButton = $('.compose-box-button')
2018-03-10 18:54:16 +00:00
export const showMoreButton = $('.more-items-header button')
2018-03-15 05:14:06 +00:00
export const accountProfileName = $('.account-profile .account-profile-name')
export const accountProfileUsername = $('.account-profile .account-profile-username')
export const accountProfileFollowedBy = $('.account-profile .account-profile-followed-by')
export const accountProfileFollowButton = $('.account-profile .account-profile-follow button')
2018-03-16 03:31:58 +00:00
export const goBackButton = $('.dynamic-page-go-back')
export const accountProfileMoreOptionsButton = $('.account-profile-more-options button')
2018-04-21 20:06:46 +00:00
export const addInstanceButton = $('#submitButton')
export const mastodonLogInButton = $('button[type="submit"]')
2018-04-27 05:05:55 +00:00
export const followsButton = $('.account-profile-details > *:nth-child(2)')
export const followersButton = $('.account-profile-details > *:nth-child(3)')
export const avatarInComposeBox = $('.compose-box-avatar')
2018-02-24 22:49:28 +00:00
export const favoritesCountElement = $('.status-favs-reblogs:nth-child(3)').addCustomDOMProperties({
innerCount: el => parseInt(el.innerText, 10)
})
export const reblogsCountElement = $('.status-favs-reblogs:nth-child(2)').addCustomDOMProperties({
innerCount: el => parseInt(el.innerText, 10)
})
2018-02-20 01:04:37 +00:00
2018-03-10 18:54:16 +00:00
export const sleep = timeout => new Promise(resolve => setTimeout(resolve, timeout))
2018-02-20 01:04:37 +00:00
export const getUrl = exec(() => window.location.href)
2018-02-21 17:26:22 +00:00
export const getActiveElementClass = exec(() =>
2018-04-11 03:56:42 +00:00
(document.activeElement && document.activeElement.getAttribute('class')) || ''
2018-02-21 17:26:22 +00:00
)
2018-03-16 03:31:58 +00:00
export const getActiveElementInnerText = exec(() =>
2018-04-11 03:56:42 +00:00
(document.activeElement && document.activeElement.innerText) || ''
2018-03-16 03:31:58 +00:00
)
2018-03-17 02:04:48 +00:00
export const getActiveElementAriaLabel = exec(() =>
2018-04-11 03:56:42 +00:00
(document.activeElement && document.activeElement.getAttribute('aria-label')) || ''
2018-03-17 02:04:48 +00:00
)
export const getActiveElementInsideNthStatus = exec(() => {
let element = document.activeElement
while (element) {
if (element.hasAttribute('aria-posinset')) {
return element.getAttribute('aria-posinset')
}
element = element.parentElement
}
2018-04-11 03:56:42 +00:00
return ''
2018-03-17 02:04:48 +00:00
})
2018-02-21 17:26:22 +00:00
export const goBack = exec(() => window.history.back())
export const forceOffline = exec(() => window.__forceOnline(false))
2018-03-09 02:08:14 +00:00
export const forceOnline = exec(() => window.__forceOnline(true))
2018-03-09 02:08:14 +00:00
2018-03-01 02:45:29 +00:00
export const getComposeSelectionStart = exec(() => composeInput().selectionStart, {
dependencies: { composeInput }
})
export const getBodyClassList = exec(() => Array.prototype.slice.apply(document.body.classList))
2018-03-10 18:54:16 +00:00
export const scrollContainerToTop = exec(() => {
document.getElementsByClassName('container')[0].scrollTop = 0
})
2018-03-03 05:55:04 +00:00
export const uploadKittenImage = i => (exec(() => {
let image = images[`kitten${i}`]
let blob = blobUtils.base64StringToBlob(image.data, 'image/png')
blob.name = image.name
2018-03-03 01:54:38 +00:00
window.__fakeFileInput(blob)
}, {
dependencies: {
images,
2018-03-03 05:55:04 +00:00
blobUtils,
i
2018-03-03 01:54:38 +00:00
}
2018-03-03 05:55:04 +00:00
}))
export const focus = (selector) => (exec(() => {
document.querySelector(selector).focus()
}, {
dependencies: {
selector
}
}))
export function getNthMediaAltInput (n) {
return $(`.compose-box .compose-media:nth-child(${n}) .compose-media-alt input`)
}
2018-03-30 08:06:17 +00:00
export function getNthComposeReplyInput (n) {
return $(`${getNthStatusSelector(n)} .compose-box-input`)
2018-03-30 08:06:17 +00:00
}
export function getNthComposeReplyButton (n) {
return $(`${getNthStatusSelector(n)} .compose-box-button`)
2018-03-30 08:06:17 +00:00
}
2018-03-30 17:04:35 +00:00
export function getNthPostPrivacyButton (n) {
return $(`${getNthStatusSelector(n)} .compose-box-toolbar button:nth-child(3)`)
2018-03-30 17:04:35 +00:00
}
2018-03-25 19:24:38 +00:00
export function getNthAutosuggestionResult (n) {
return $(`.compose-autosuggest-list-item:nth-child(${n}) button`)
}
export function getSearchResultByHref (href) {
return $(`.search-result a[href="${href}"]`)
}
2018-03-09 02:08:14 +00:00
export function getNthSearchResult (n) {
return $(`.search-result:nth-child(${n}) a`)
}
2018-03-03 05:55:04 +00:00
export function getNthMedia (n) {
return $(`.compose-media:nth-child(${n}) img`)
}
2018-03-03 01:54:38 +00:00
2018-03-03 18:27:14 +00:00
export function getNthDeleteMediaButton (n) {
return $(`.compose-media:nth-child(${n}) .compose-media-delete-button`)
}
2018-02-21 05:08:26 +00:00
export function getNthStatus (n) {
return $(getNthStatusSelector(n))
}
export function getNthStatusSelector (n) {
2018-06-11 02:49:39 +00:00
return `.list-item > article[aria-posinset="${n}"]`
2018-02-21 05:08:26 +00:00
}
export function getNthStatusContent (n) {
return $(`${getNthStatusSelector(n)} .status-content`)
}
export function getNthStatusSpoiler (n) {
return $(`${getNthStatusSelector(n)} .status-spoiler`)
}
export function getNthStatusHeader (n) {
return $(`${getNthStatusSelector(n)} .status-header`)
}
export function getNthStatusAndImage (nStatus, nImage) {
return $(`${getNthStatusSelector(nStatus)} .status-media .show-image-button:nth-child(${nImage + 1}) img`)
}
2018-02-24 22:49:28 +00:00
export function getFirstVisibleStatus () {
2018-06-11 02:49:39 +00:00
return $(`.list-item > article[aria-posinset]`).nth(0)
2018-02-24 22:49:28 +00:00
}
2018-03-09 16:45:12 +00:00
export function getNthReplyButton (n) {
return $(`${getNthStatusSelector(n)} .status-toolbar button:nth-child(1)`)
2018-03-09 16:45:12 +00:00
}
2018-04-08 20:42:31 +00:00
export function getNthReplyContentWarningInput (n) {
return $(`${getNthStatusSelector(n)} .content-warning-input`)
2018-04-08 20:42:31 +00:00
}
export function getNthReplyContentWarningButton (n) {
return $(`${getNthStatusSelector(n)} .compose-box-toolbar button:nth-child(4)`)
2018-04-08 20:42:31 +00:00
}
export function getNthReplyPostPrivacyButton (n) {
return $(`${getNthStatusSelector(n)} .compose-box-toolbar button:nth-child(3)`)
2018-04-08 20:42:31 +00:00
}
export function getNthPostPrivacyOptionInDialog (n) {
return $(`.generic-dialog-list li:nth-child(${n}) button`)
}
2018-02-24 22:49:28 +00:00
export function getNthFavoriteButton (n) {
return $(`${getNthStatusSelector(n)} .status-toolbar button:nth-child(3)`)
2018-02-24 22:49:28 +00:00
}
export function getNthStatusOptionsButton (n) {
return $(`${getNthStatusSelector(n)} .status-toolbar button:nth-child(4)`)
}
2018-02-24 22:49:28 +00:00
export function getNthFavorited (n) {
return getNthFavoriteButton(n).getAttribute('aria-pressed')
}
export function getNthShowOrHideButton (n) {
return $(`${getNthStatusSelector(n)} .status-spoiler-button button`)
}
2018-02-24 22:49:28 +00:00
export function getFavoritesCount () {
return favoritesCountElement.innerCount
}
2018-02-25 02:20:33 +00:00
export function getNthReblogButton (n) {
return $(`${getNthStatusSelector(n)} .status-toolbar button:nth-child(2)`)
2018-02-25 02:20:33 +00:00
}
export function getNthReblogged (n) {
return getNthReblogButton(n).getAttribute('aria-pressed')
}
export function getNthDialogOptionsOption (n) {
return $(`.modal-dialog li:nth-child(${n}) button`)
}
2018-02-24 22:49:28 +00:00
export function getReblogsCount () {
return reblogsCountElement.innerCount
2018-02-20 01:04:37 +00:00
}
function getNthPinnedStatusSelector (n) {
return `.pinned-statuses article[aria-posinset="${n}"]`
}
export function getNthPinnedStatus (n) {
return $(getNthPinnedStatusSelector(n))
}
export function getNthPinnedStatusFavoriteButton (n) {
return $(`${getNthPinnedStatusSelector(n)} .status-toolbar button:nth-child(3)`)
}
2018-02-20 02:25:59 +00:00
export async function validateTimeline (t, timeline) {
const timeout = 30000
2018-02-20 01:04:37 +00:00
for (let i = 0; i < timeline.length; i++) {
let status = timeline[i]
// hovering forces TestCafé to scroll to that element: https://git.io/vABV2
await t.hover(getNthStatus(i))
2018-02-20 01:04:37 +00:00
if (status.content) {
await t.expect(getNthStatusContent(i).innerText)
.contains(status.content, { timeout })
2018-02-20 01:04:37 +00:00
}
if (status.spoiler) {
await t.expect(getNthStatusSpoiler(i).innerText)
.contains(status.spoiler, { timeout })
2018-02-20 01:04:37 +00:00
}
if (status.followedBy) {
await t.expect(getNthStatusHeader(i).innerText)
.match(new RegExp(status.followedBy + '\\s+followed you'), { timeout })
2018-02-20 01:04:37 +00:00
}
if (status.rebloggedBy) {
await t.expect(getNthStatusHeader(i).innerText)
.match(new RegExp(status.rebloggedBy + '\\s+boosted your status'), { timeout })
2018-02-20 01:04:37 +00:00
}
if (status.favoritedBy) {
await t.expect(getNthStatusHeader(i).innerText)
.match(new RegExp(status.favoritedBy + '\\s+favorited your status'), { timeout })
2018-02-20 01:04:37 +00:00
}
}
2018-02-20 02:25:59 +00:00
}
2018-02-21 05:08:26 +00:00
2018-02-24 22:49:28 +00:00
export async function scrollToTopOfTimeline (t) {
let i = await getFirstVisibleStatus().getAttribute('aria-posinset')
while (true) {
await t.hover(getNthStatus(i))
.expect($('.loading-footer').exist).notOk()
if (--i <= 0) {
2018-02-24 22:49:28 +00:00
break
}
}
}
2018-02-21 05:08:26 +00:00
export async function scrollToBottomOfTimeline (t) {
2018-02-24 22:49:28 +00:00
let i = 0
2018-02-21 05:08:26 +00:00
while (true) {
2018-02-24 22:49:28 +00:00
await t.hover(getNthStatus(i))
2018-02-21 05:08:26 +00:00
.expect($('.loading-footer').exist).notOk()
2018-02-24 22:49:28 +00:00
let size = await getNthStatus(i).getAttribute('aria-setsize')
if (++i >= size - 1) {
2018-02-21 05:08:26 +00:00
break
}
}
2018-02-21 05:30:16 +00:00
}
2018-02-21 17:26:22 +00:00
export async function scrollToStatus (t, n) {
let timeout = 20000
for (let i = 0; i <= n; i++) {
await t.expect(getNthStatus(i).exists).ok({timeout})
.hover(getNthStatus(i))
2018-03-16 00:33:52 +00:00
.expect($('.loading-footer').exist).notOk()
if (i < n) {
await t.hover($(`${getNthStatusSelector(i)} .status-toolbar`))
2018-03-16 00:33:52 +00:00
.expect($('.loading-footer').exist).notOk()
}
2018-02-21 17:26:22 +00:00
}
await t.hover(getNthStatus(n))
}
export async function clickToNotificationsAndBackHome (t) {
await t.click(notificationsNavButton)
.expect(getUrl()).eql('http://localhost:4002/notifications')
.click(homeNavButton)
.expect(getUrl()).eql('http://localhost:4002/')
}
2018-04-19 03:43:13 +00:00
// like lodash.times but I don't want to try to figure out esm
// just to import lodash-es
export function times (n, cb) {
let arr = []
for (let i = 0; i < n; i++) {
arr.push(cb(i))
}
return arr
}