mercredi 16 septembre 2020

Trouble to find element depending from other element in testcafe page object model

Having problems implementing the locator lookup method depending on its parent in POM

Example of DOM (roughly):

<div class="lessons">
  <div [data-test="lesson"]>
    <div class="lesson__info">
      <div ...>
        <h2 [data-test="lessonTitle"]>FirstLesson</h2>
    <div class"lesson__data">
      <div [data-test="lessonDataButton"]>
    <div class"lesson__controls">
      <div [data-test="lessonStartButton"]>
  <div [data-test="lesson"]>
    <div class="lesson__info">
        <div ...>
        <h2 [data-test="lessonTitle"]>SecondLesson</h2>
    <div class"lesson__data">
      <div [data-test="lessonDataButton"]>
    <div class"lesson__controls">
      <div [data-test="lessonStartButton"]>

Example of my POM:

import { Selector, t } from 'testcafe'

class Page {
  constructor() {
    this.lesson = Selector('[data-test="lesson"]')
    this.lessonDataBtn = Selector('[data-test="lessonDataButton"]')
    this.lessonStartBtn = Selector('[data-test="lessonStartButton"]')
    this.lessonTitle = Selector('[data-test="lessonTitle"]')
  }

async getLessonButton(title, lessonButton) {
    const titleLocator = this.lessonTitle.withText(title);
    const currentLesson = this.lesson.filter((node) => {
      return node.contains(titleLocator())
   }, { titleLocator });
   
  const buttonSelector = currentLesson.find((node) => {
    return node === lessonButton();
  }, { lessonButton });
  return buttonSelector;
  }

In my test I'm trying to click "lessonDataButton" in specific lesson filtered by its "title":

await t.click(await schedule.getLessonButton(testData.lesson.data.title, page.lessonDataBtn))

It works correctly only for first occurrence of "lessonDataBtn" on page, but if I try to find the same button in second lesson - it will be an error:

The specified selector does not match any element in the DOM tree.

       > | Selector('[data-test="lesson"]')
         |   .filter([function])
         |   .find([function])

Aucun commentaire:

Enregistrer un commentaire