@@ -130,15 +130,15 @@ export const multipleElementResultsStrategy = async <Actual, Expected>(
130130 { isNot, isSome, iteration } : { isNot : boolean ; isSome : boolean ; iteration : number } ,
131131 { allowEmptyElements = false , allowArrayWithSingleElement = false } = { }
132132) : Promise < StrategyResult < MaybeArrayOrMultiRemoteValues < Actual > > > => {
133- const { selector, other, multiRemote } = await awaitElementOrArray ( unresolvedElements )
133+ const { selector, other, multiRemoteSelector } = await awaitElementOrArray ( unresolvedElements )
134134
135135 if ( iteration > 0 && isStrictlyElementArray ( selector ) ) {
136136 // TODO dprevost adapt for Multi-remote?
137137 // WARNING: This synchronize the element's array with the latest refetched elements and so altering selector state!
138138 await refreshElementArray ( selector )
139139 }
140140
141- const subject = multiRemote ?? selector ?? other
141+ const subject = multiRemoteSelector ?? selector ?? other
142142
143143 // --- Empty / no element case ---
144144 if ( ! selector || ( Array . isArray ( selector ) && selector . length === 0 ) ) {
@@ -176,12 +176,35 @@ export const multipleElementResultsStrategy = async <Actual, Expected>(
176176
177177 let multiRemoteActual : MultiRemoteValuesWithArray < Actual > | undefined
178178 // --- Multi-remote $() single element case ---
179- if ( multiRemote && isMultiRemoteValues ( expectedValues , multiRemote . instances ) ) {
180- multiRemoteActual = { }
179+ if ( multiRemoteSelector && ! Array . isArray ( expectedValues ) ) {
180+ let multiRemoteExpected = expectedValues
181+ if ( ! isMultiRemoteValues ( expectedValues , multiRemoteSelector . instances ) ) {
182+ multiRemoteExpected = multiRemoteSelector . instances . reduce ( ( acc , instance ) => {
183+ acc [ instance ] = expectedValues as Expected
184+ return acc
185+ } , { } as MultiRemoteValues < Expected > )
186+ }
187+
188+ if ( ! isMultiRemoteValues ( multiRemoteExpected , multiRemoteSelector . instances ) ) { throw new Error ( 'multiRemoteExpected is not a MultiRemoteValues' ) }
189+
181190 results = await Promise . all (
182- Object . keys ( expectedValues ) . map ( async ( instance ) => {
183- const element = multiRemote . getInstance ( instance )
184- const expectValue = expectedValues [ instance ]
191+ Object . keys ( multiRemoteExpected ) . map ( async ( instance ) => {
192+ const expectValue = multiRemoteExpected [ instance ]
193+
194+ // TODO: non-existing multi-remote element should probably be a element with error and not to throw??
195+ let element : WebdriverIO . Element
196+ try {
197+ element = multiRemoteSelector . getInstance ( instance )
198+ } catch ( error ) {
199+ if ( error instanceof Error && error . message . includes ( 'Multiremote object has no instance named' ) ) {
200+ if ( ! multiRemoteActual ) { throw new Error ( 'multiRemoteActual is undefined' ) }
201+ multiRemoteActual [ instance ] = undefined as Actual
202+
203+ return { success : false , actual : undefined as Actual }
204+ }
205+ throw error
206+ }
207+
185208 const result = await singleElementCompare ( element , expectValue as Expected )
186209 if ( ! multiRemoteActual ) { throw new Error ( 'multiRemoteActual is undefined' ) }
187210 multiRemoteActual [ instance ] = result . actual
@@ -194,14 +217,28 @@ export const multipleElementResultsStrategy = async <Actual, Expected>(
194217 for ( const [ index , element ] of Array . from ( selector . entries ( ) ) ) {
195218 const instanceResults = await Promise . all (
196219 element . instances . map ( async ( instance ) => {
197- const elementInstance = element . getInstance ( instance )
220+ if ( ! multiRemoteActual ) { throw new Error ( 'multiRemoteActual is undefined' ) }
221+ if ( ! multiRemoteActual [ instance ] ) { multiRemoteActual [ instance ] = [ ] }
222+ const typedActual = multiRemoteActual [ instance ] as Actual [ ]
223+
224+ let elementInstance : WebdriverIO . Element
225+ try {
226+ elementInstance = element . getInstance ( instance )
227+ } catch ( error ) {
228+ if ( error instanceof Error && error . message . includes ( 'Multiremote object has no instance named' ) ) {
229+ if ( ! multiRemoteActual ) { throw new Error ( 'multiRemoteActual is undefined' ) }
230+ typedActual . push ( undefined as Actual )
231+
232+ return { success : false , actual : undefined as Actual }
233+ }
234+ throw error
235+ }
198236
199237 const instanceValue = isMultiRemoteValues ( expectedValues , element . instances ) ? expectedValues [ instance ] : expectedValues
200238 const indexedExpected = Array . isArray ( instanceValue ) ? instanceValue [ index ] : instanceValue
201- if ( ! multiRemoteActual ) { throw new Error ( 'multiRemoteActual is undefined' ) }
202239
203240 const result = await singleElementCompare ( elementInstance , indexedExpected , index )
204- multiRemoteActual [ instance ] = result . actual
241+ typedActual . push ( result . actual )
205242 return result
206243 } )
207244 )
0 commit comments