Repository: oppia/oppia
File: core/tests/puppeteer-acceptance-tests/utilities/user/logged-in-user.ts (TypeScript)

Code before:
      return;
    }
    throw new Error(`Lesson not found: ${lessonTitle}`);
  }

  /**
   * Checks if Learner is on the learner dashboard page.
   */
  async expectToBeOnLearnerDashboardPage(): Promise<void> {
    await this.page.waitForFunction(
      (url: string) => {
        return window.location.href.includes(url);
      },
      {},
      '/learner-dashboard'
    );
  }

  /**
   * Checks if greeting has name of the user.
   */
  async expectGreetingToHaveNameOfUser(userName: string): Promise<void> {
    // Check for redesigned dashboard greeting first.
    const redesignedGreetingElement = await this.page.$(
      learnerGreetingsSelector
    );
    if (redesignedGreetingElement) {
      await this.page.waitForSelector(learnerGreetingsSelector, {
        visible: true,
      });
      const greetingText = await this.page.$eval(learnerGreetingsSelector, el =>
        el.textContent?.trim()
      );
      expect(greetingText).toContain(userName);
    } else {
      // Fall back to old dashboard greeting selector.
      await this.page.waitForSelector(greetingSelector, {
        visible: true,
      });
      const greetingElement = await this.page.$(greetingSelector);
      const greetingText = await this.page.evaluate(
        el => el.textContent,
        greetingElement
      );
      expect(greetingText).toContain(userName);
    }
  }

  /**
   * Checks if the suggestion container in Learner Dashboard is empty.
   */

Review comment (suggestion):
In a case where the redesigned dashboard is enabled, but elements hasn't loaded yet, it generates a flaky behaviour. You can use `isElementVisible` that waits sufficiently to remove flakiness.

Code after:
      return;
    }
    throw new Error(`Lesson not found: ${lessonTitle}`);
  }

  /**
   * Checks if Learner is on the learner dashboard page.
   */
  async expectToBeOnLearnerDashboardPage(): Promise<void> {
    await this.expectElementToBeVisible(learnerDashboardContainerSelector);
  }

  /**
   * Checks if greeting has name of the user.
   */
  async expectGreetingToHaveNameOfUser(userName: string): Promise<void> {
    // Check for redesigned dashboard greeting first.
    const isRedesignedGreetingVisible = await this.isElementVisible(
      learnerGreetingsSelector,
      true
    );
    if (isRedesignedGreetingVisible) {
      const greetingText = await this.page.$eval(learnerGreetingsSelector, el =>
        el.textContent?.trim()
      );
      expect(greetingText).toContain(userName);
    } else {
      // Fall back to old dashboard greeting selector.
      await this.expectElementToBeVisible(greetingSelector);
      const greetingElement = await this.page.$(greetingSelector);
      const greetingText = await this.page.evaluate(
        el => el.textContent,
        greetingElement
      );
      expect(greetingText).toContain(userName);
    }
  }

  /**
   * Checks if the suggestion container in Learner Dashboard is empty.
   */
  async expectLearnSomethingNewInLDToBeEmpty(): Promise<void> {
    await this.expectElementToBeVisible(emptySuggestionSectionSelector);
  }

  /**
   * Checks if the continue from where you left off section in Learner Dashboard is present.
   * @param {boolean} visible - Whether the section should be visible or not.
   */
  async expectContinueWhereYouLeftOffSectionInLDToBePresent(
    visible: boolean = true