| 1 |
import querystring from 'querystring'; |
| 2 |
|
| 3 |
export function clickUntilVisible( clickElement, expectedSelector, timeout = 5000 ) { |
| 4 |
const timeoutTime = Date.now() + timeout; |
| 5 |
|
| 6 |
function clickUntilVisibleWithinTime() { |
| 7 |
cy.get( expectedSelector ).then( ( $element ) => { |
| 8 |
if ( Date.now() > timeoutTime || $element.is( ':visible' ) ) { |
| 9 |
return; |
| 10 |
} |
| 11 |
|
| 12 |
clickElement.click(); |
| 13 |
clickUntilVisibleWithinTime(); |
| 14 |
} ); |
| 15 |
} |
| 16 |
|
| 17 |
clickUntilVisibleWithinTime(); |
| 18 |
} |
| 19 |
|
| 20 |
export function getTestString( prefix = '' ) { |
| 21 |
return prefix + Math.random().toString(); |
| 22 |
} |
| 23 |
|
| 24 |
export function visitTitle( title, query = {} ) { |
| 25 |
cy.visit( `/index.php?title=${ encodeURIComponent( title ) }&${ querystring.stringify( query ) }` ); |
| 26 |
} |
| 27 |
|
| 28 |
export function waitForMWLoader() { |
| 29 |
|
| 30 |
|
| 31 |
cy.window() |
| 32 |
.should( 'have.property', 'mw' ) |
| 33 |
.and( 'have.property', 'loader' ) |
| 34 |
.and( 'have.property', 'using' ); |
| 35 |
} |
| 36 |
|
| 37 |
export function waitForModuleReady( moduleName ) { |
| 38 |
cy.window() |
| 39 |
.should( 'have.property', 'mw' ) |
| 40 |
.and( 'have.property', 'loader' ) |
| 41 |
.and( 'have.property', 'getState' ); |
| 42 |
cy.window() |
| 43 |
.should( |
| 44 |
( win ) => expect( win.mw.loader.getState( moduleName ) ).to.eq( 'ready' ) |
| 45 |
); |
| 46 |
} |
| 47 |
|
| 48 |
export function editPage( title, wikiText ) { |
| 49 |
visitTitle( '' ); |
| 50 |
waitForMWLoader(); |
| 51 |
cy.window().then( async ( win ) => { |
| 52 |
await win.mw.loader.using( 'mediawiki.api' ); |
| 53 |
const response = await new win.mw.Api().postWithEditToken( { |
| 54 |
action: 'edit', |
| 55 |
title: title, |
| 56 |
text: wikiText, |
| 57 |
formatversion: '2' |
| 58 |
} ); |
| 59 |
|
| 60 |
expect( response.edit.result ).to.equal( 'Success' ); |
| 61 |
} ); |
| 62 |
} |
| 63 |
|
| 64 |
export function loginAsAdmin() { |
| 65 |
visitTitle( 'Special:UserLogin' ); |
| 66 |
cy.get( '#wpName1' ).type( cy.config( 'mediawikiAdminUsername' ) ); |
| 67 |
cy.get( '#wpPassword1' ).type( cy.config( 'mediawikiAdminPassword' ) ); |
| 68 |
cy.get( '#wpLoginAttempt' ).click(); |
| 69 |
} |
| 70 |
|
| 71 |
export function getReference( num ) { |
| 72 |
return cy.get( `#mw-content-text .reference:nth-of-type(${ num })` ); |
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
export function getCiteSubBacklink( num ) { |
| 77 |
return cy.get( `.mw-cite-backlink sup:nth-of-type(${ num }) a` ); |
| 78 |
} |
| 79 |
|
| 80 |
export function getCiteMultiBacklink( num ) { |
| 81 |
return cy.get( `.references li:nth-of-type(${ num }) .mw-cite-up-arrow-backlink` ); |
| 82 |
} |
| 83 |
|
| 84 |
export function getCiteSingleBacklink( num ) { |
| 85 |
return cy.get( `.references li:nth-of-type(${ num }) .mw-cite-backlink a` ); |
| 86 |
} |
| 87 |
|
| 88 |
export function getFragmentFromLink( linkElement ) { |
| 89 |
return linkElement.invoke( 'attr', 'href' ).then( ( href ) => href.split( '#' )[ 1 ] ); |
| 90 |
} |
| 91 |
|
| 92 |
export function backlinksIdShouldMatchFootnoteId( supIndex, backlinkIndex, rowNumber ) { |
| 93 |
return getRefsFromArticleSection() |
| 94 |
.eq( supIndex ) |
| 95 |
.invoke( 'attr', 'id' ) |
| 96 |
.then( ( id ) => { |
| 97 |
getRefFromReferencesSection( rowNumber ) |
| 98 |
.find( '.mw-cite-backlink a' ) |
| 99 |
.eq( backlinkIndex ) |
| 100 |
.invoke( 'attr', 'href' ) |
| 101 |
.should( 'eq', `#${ id }` ); |
| 102 |
} ); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
export function getRefsFromArticleSection() { |
| 107 |
return cy.get( '#mw-content-text p sup' ); |
| 108 |
} |
| 109 |
|
| 110 |
export function articleSectionRefMarkersContainCorrectRefName( refMarkerContent ) { |
| 111 |
return getRefsFromArticleSection() |
| 112 |
.find( `a:contains('[${ refMarkerContent }]')` ) |
| 113 |
.each( ( $el ) => { |
| 114 |
cy.wrap( $el ) |
| 115 |
.should( 'have.text', `[${ refMarkerContent }]` ) |
| 116 |
.and( 'have.attr', 'href', `#cite_note-a-${ refMarkerContent }` ); |
| 117 |
} ); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
export function getRefsFromReferencesSection() { |
| 122 |
return cy.get( '#mw-content-text .references li' ); |
| 123 |
} |
| 124 |
|
| 125 |
export function getRefFromReferencesSection( rowNumber ) { |
| 126 |
return getRefsFromReferencesSection().eq( rowNumber - 1 ); |
| 127 |
} |
| 128 |
|
| 129 |
export function referenceSectionRefIdContainsRefName( rowNumber, refName ) { |
| 130 |
const id = refName !== null ? `cite_note-${ refName }-${ rowNumber }` : `cite_note-${ rowNumber }`; |
| 131 |
return getRefFromReferencesSection( rowNumber ).should( 'have.attr', 'id', id ); |
| 132 |
} |
| 133 |
|
| 134 |
export function verifyBacklinkHrefContent( refName, rowNumber, index ) { |
| 135 |
const expectedHref = `#cite_ref-${ refName }_${ rowNumber }-${ index }`; |
| 136 |
return getRefFromReferencesSection( rowNumber ) |
| 137 |
.find( '.mw-cite-backlink a' ) |
| 138 |
.eq( index ) |
| 139 |
.should( 'have.attr', 'href', expectedHref ); |
| 140 |
} |
| 141 |
|
| 142 |
export function abandonReference( id ) { |
| 143 |
cy.get( `:not(.reference-text) > #${ id } a` ) |
| 144 |
.trigger( 'mouseout' ); |
| 145 |
|
| 146 |
|
| 147 |
cy.wait( 500 ); |
| 148 |
} |
| 149 |
|
| 150 |
export function dwellReference( id ) { |
| 151 |
cy.get( `:not(.reference-text) > #${ id } a` ) |
| 152 |
.trigger( 'mouseover' ); |
| 153 |
} |
| 154 |
|
| 155 |
export function assertPreviewIsScrollable( isScrollable ) { |
| 156 |
cy.get( '.mwe-popups-extract .mwe-popups-scroll' ) |
| 157 |
.should( ( $el ) => isScrollable === ( $el.prop( 'scrollHeight' ) > $el.prop( 'offsetHeight' ) ) ); |
| 158 |
} |