diff --git a/packages/react-native-editor/__device-tests__/helpers/utils.js b/packages/react-native-editor/__device-tests__/helpers/utils.js index f250af69282baa..2dec6c6790e0a8 100644 --- a/packages/react-native-editor/__device-tests__/helpers/utils.js +++ b/packages/react-native-editor/__device-tests__/helpers/utils.js @@ -46,8 +46,6 @@ const strToKeycode = { [ backspace ]: 67, }; -const editorReadyTimeout = 8000; - const timer = ( ms ) => new Promise( ( res ) => setTimeout( res, ms ) ); const isAndroid = () => { @@ -159,9 +157,6 @@ const setupDriver = async () => { // eslint-disable-next-line no-console console.log( status ); - await driver.setImplicitWaitTimeout( editorReadyTimeout ); - await timer( editorReadyTimeout ); - await driver.setOrientation( 'PORTRAIT' ); return driver; }; @@ -454,6 +449,35 @@ const toggleOrientation = async ( driver ) => { } }; +const isEditorVisible = async ( driver ) => { + const postTitleLocator = isAndroid() + ? `//android.widget.EditText[contains(@content-desc, "Post title")]` + : `(//XCUIElementTypeScrollView/XCUIElementTypeOther/XCUIElementTypeOther[contains(@name, "Post title")])`; + + await waitForVisible( driver, postTitleLocator ); +}; + +const waitForVisible = async ( driver, elementLocator, iteration = 0 ) => { + const maxIteration = 25; + const timeout = 1000; + + if ( iteration >= maxIteration ) { + throw new Error( + `"${ elementLocator }" is still not visible after ${ iteration } retries!` + ); + } else if ( iteration !== 0 ) { + // wait before trying to locate element again + await driver.sleep( timeout ); + } + + const locator = await driver.elementsByXPath( elementLocator ); + if ( locator.length !== 1 ) { + // if locator is not visible, try again + return waitForVisible( driver, elementLocator, iteration + 1 ); + } + return locator[ 0 ]; +}; + module.exports = { backspace, timer, @@ -474,4 +498,6 @@ module.exports = { toggleHtmlMode, toggleOrientation, doubleTap, + isEditorVisible, + waitForVisible, }; diff --git a/packages/react-native-editor/__device-tests__/pages/editor-page.js b/packages/react-native-editor/__device-tests__/pages/editor-page.js index 918670059a9424..202f0db13ce1ea 100644 --- a/packages/react-native-editor/__device-tests__/pages/editor-page.js +++ b/packages/react-native-editor/__device-tests__/pages/editor-page.js @@ -12,10 +12,13 @@ const { swipeFromTo, longPressMiddleOfElement, doubleTap, + isEditorVisible, + waitForVisible, } = require( '../helpers/utils' ); const initializeEditorPage = async () => { const driver = await setupDriver(); + await isEditorVisible( driver ); return new EditorPage( driver ); }; @@ -36,8 +39,6 @@ class EditorPage { this.accessibilityIdXPathAttrib = 'content-desc'; this.accessibilityIdKey = 'contentDescription'; } - - driver.setImplicitWaitTimeout( 5000 ); } async getBlockList() { @@ -159,12 +160,11 @@ class EditorPage { async getTextViewForHtmlViewContent() { const accessibilityId = 'html-view-content'; - let blockLocator = `//*[@${ this.accessibilityIdXPathAttrib }="${ accessibilityId }"]`; + const htmlViewLocator = isAndroid() + ? `//*[@${ this.accessibilityIdXPathAttrib }="${ accessibilityId }"]` + : `//XCUIElementTypeTextView[starts-with(@${ this.accessibilityIdXPathAttrib }, "${ accessibilityId }")]`; - if ( ! isAndroid() ) { - blockLocator = `//XCUIElementTypeTextView[starts-with(@${ this.accessibilityIdXPathAttrib }, "${ accessibilityId }")]`; - } - return await this.driver.elementByXPath( blockLocator ); + return await waitForVisible( this.driver, htmlViewLocator ); } // Returns html content