Commit c2d41a96 authored by Michail Tzanatos's avatar Michail Tzanatos
Browse files

fixed logged user journeys bugs

parent 39f307a0
Loading
Loading
Loading
Loading
Loading
+10 −56
Original line number Diff line number Diff line
@@ -3,64 +3,18 @@ describe('logged user journeys', () => {
      cy.visit(window.location.origin);        
      
  });
  // afterEach(() => {
  //   //log user out after each test
  //   cy.get('#navbarDropdown2').click();
  //   cy.get(':nth-child(2) > .dropdown > .dropdown-menu > :nth-child(8)').click();
  //   cy.location("pathname").should("equal", "/");
  // });

  // it.only('logs in through services', () => {
  //   cy.loginPath('[data-cy="servicesPortal"]', '/services');
  //   //back on services marketplace after login
  //   cy.location("pathname").should("equal", "/services/services_marketplace");
  //   //check every 'Manage Services' dropdown item
  //   cy.get(':nth-child(2) > #navbarDropdown').click();
  //   let i=1; 
  //   cy.get('.dropdown-item').each(($el) => {
      
  //     cy.wrap($el).click(); // Click the link
  //     cy.get(':nth-child(1) > .cdk-column-actions > .btn').first().click();
  //     //does the dialog open?
  //     cy.get('mat-dialog-container').should('exist');
  //     cy.go('back');
  //     cy.get(':nth-child(2) > #navbarDropdown').click();
  //     i = i+1;
  //     cy.log(`${i}`);
  //     if(i>2) {
  //       cy.log("it returns now");
  //       return false;
  //     }
  //   });
  //   //access the service specs dropdown
  //   cy.get('#navbarDropdown1').click();
  //   let j=0;
  //   for(let i=1; i<=2; i++) {
  //     cy.get(`.dropdown-submenu > .dropdown-menu > :nth-child(${i}) `).click();
  //     cy.location("pathname").should("not.equal", "/services/services_marketplace");
  //     cy.go('back');
  //     cy.get(':nth-child(2) > #navbarDropdown').click();
  //     cy.get('#navbarDropdown1').click();
  //   }
  //   //check the rest dropdown items
  //   cy.get(':nth-child(4) > .dropdown-item').click();
  //   cy.get('mat-dialog-container').should('exist');
  //   cy.go('back');
  //   cy.get(':nth-child(2) > #navbarDropdown').click();
    
  //   //'Manage Entities' dropdown
  //   cy.manageEntities(':nth-child(3) > #navbarDropdown', 2);
  afterEach(() => {
    //log user out after each test
    cy.get('#navbarDropdown2').click();
    cy.get(':nth-child(2) > .dropdown > .dropdown-menu > :nth-child(8)').click();
    cy.location("pathname").should("equal", "/");
  });

  // });

  it('logs in through services', () => {
    cy.loginPath('[data-cy="servicesPortal"]', '/services');
    cy.location("pathname").should("equal", "/services/services_marketplace");

    cy.navigateThroughDropdowns("Services Marketplace");
    cy.manageEntities(':nth-child(3) > #navbarDropdown', 2);
    cy.manageEntities(':nth-child(4) > #navbarDropdown', 3);

    
  });

@@ -74,14 +28,14 @@ describe('logged user journeys', () => {

  it('logs in through testing', () => {
    cy.loginPath('[data-cy="testingPortal"]', '/testing');
    cy.manageEntities(':nth-child(1) > #navbarDropdown', 2);
    cy.manageEntities(':nth-child(2) > #navbarDropdown', 2);
    cy.location("pathname").should("equal", "/testing/service_tests");
    cy.navigateThroughDropdowns("Test Instances");
  });

  
  it('logs in through products', () => {
    cy.loginPath('[data-cy="productsPortal"]', '/products');
    cy.manageEntities(':nth-child(2) > #navbarDropdown', 4);
    cy.manageEntities(':nth-child(3) > #navbarDropdown', 2);
    cy.location("pathname").should("equal", "/products/marketplace");
    cy.navigateThroughDropdowns("");
  });
})
 No newline at end of file
+153 −104
Original line number Diff line number Diff line
@@ -8,7 +8,7 @@ declare namespace Cypress {
    authUser(): Chainable<any>;
    loginPath(selector, path): Chainable<any>;
    navigateThroughDropdowns(headerTitle): Chainable<any>;
    manageEntities(selector, numOfItems): Chainable<any>;
    // manageEntities(selector, numOfItems): Chainable<any>;
    checkProtectedRoutes(): Chainable<any>;
  }
}
@@ -92,129 +92,178 @@ Cypress.Commands.add("loginPath", (selector, path) => {
  cy.authUser();
});

// Cypress.Commands.add(
//   "manageEntities",
//   (selector, numOfItems) => {
//     for (let i = 1; i <= numOfItems; i++) {
//       cy.get(selector).click();
//       cy.get(`.nav-item.open > #navbarDropdown > .dropdown-menu > :nth-child(${i}) > .dropdown-item`).click();
//       cy.get('.text-md-right > .btn, .jumbotron-heading > .btn').should('exist');
//       cy.go("back");
//     }
//   } 
// );

Cypress.Commands.add("navigateThroughDropdowns", (headerTitle: string) => {
  cy.get('[data-cy^="dropdown-"]').its('length').then((dropdownCount: number) => {

    const ensureDropdownOpen = (dropdownSelector: string, itemPattern: string) => {
      cy.get(dropdownSelector).then(($dropdown) => {
        const isOpen = $dropdown.attr('aria-expanded') === 'true' || $dropdown.hasClass('show');
        if (!isOpen) {
          cy.wrap($dropdown).should('exist').click();
        }
      });

      //wait until items exist & are visible
      cy.get(itemPattern, { timeout: 10000 })
        .should('have.length.greaterThan', 0)
        .filter(':visible')
        .should('have.length.greaterThan', 0);
    };

    const processDropdown = (di: number) => {
      if (di >= dropdownCount) return;

  cy.get('[data-cy="dropdown"]').each(($dropdown, dropdownIndex) => {
    if (dropdownIndex > 0) {
      const dropdownSelector = `[data-cy="dropdown-${di}"]`;
      const itemPattern = `[data-cy^="dropdown-${di}-item-"]`;

      ensureDropdownOpen(dropdownSelector, itemPattern);

      cy.get(itemPattern).its('length').then((itemCount: number) => {
        if (itemCount === 0) {
          cy.log(`Dropdown ${di} had no items, skipping.`);
          processDropdown(di + 1);
          return;
        }
    cy.get('[data-cy="dropdown"]').eq(dropdownIndex).click();

    cy.get('[data-cy="dropdown-item-1"]').each(($item, itemIndex) => {
      cy.location('pathname').then((initialPath) => {
        const processItem = (ii: number) => {
          if (ii >= itemCount) {
            cy.log(`Finished dropdown ${di}`);
            processDropdown(di + 1);
            return;
          }

        cy.get('[data-cy="dropdown-item-1"]').eq(itemIndex).click();
          const itemSelector = `[data-cy="dropdown-${di}-item-${ii}"]`;

        cy.get('body').then(($body) => {
          ensureDropdownOpen(dropdownSelector, itemPattern);

          cy.location('pathname').then((newPath) => {
          cy.get(itemSelector).filter(':visible').invoke('text').then((rawText) => {
            const itemText = rawText.trim();

            if (newPath === initialPath && $item.text().trim() !== headerTitle) {
              cy.get('[data-cy="dropdown-item-2"]').each((nestedItem, nestedIndex) => {
                cy.get('[data-cy="dropdown-item-2"]').eq(nestedIndex).click();
                const dialogExists = $body.find('mat-dialog').length > 0;
                if(dialogExists) {
                  cy.get('#mat-dialog-0').should('be.visible');
            cy.location('pathname').then((initialPath) => {
              cy.log(`Clicking ${itemSelector}`);
              cy.get(itemSelector).filter(':visible').should('exist').click();

                  cy.go('back');
                  cy.location('pathname').should('equal', initialPath);
                  cy.get('[data-cy="dropdown"]').eq(dropdownIndex).click();
              cy.get('body').then(() => {
                cy.location('pathname').then((newPath) => {
                  if (newPath === initialPath && itemText !== headerTitle) {
                    const nestedPattern = `[data-cy^="dropdown-${di}-item-${ii}-subitem-"]`;
                    cy.get(nestedPattern).its('length').then((nestedCount: number) => {
                      if (nestedCount === 0) {
                        processItem(ii + 1);
                        return;
                      }

                      const processNested = (nk: number) => {
                        if (nk >= nestedCount) {
                          processItem(ii + 1);
                          return;
                        }
                else {
                  cy.location('pathname').should('not.equal', initialPath);

                        const nestedSelector = `[data-cy="dropdown-${di}-item-${ii}-subitem-${nk}"]`;
                        cy.get(nestedSelector).filter(':visible').should('exist').click();

                        cy.get('body').then(($bodyAfterNested) => {
                          const dialogExists =
                            $bodyAfterNested.find('mat-dialog').length > 0 ||
                            $bodyAfterNested.find('#mat-dialog-0').length > 0;

                          if (dialogExists) {
                            cy.get('#mat-dialog-0').should('be.visible');
                            cy.go('back');
                            cy.wait(200);
                            ensureDropdownOpen(dropdownSelector, itemPattern);
                            cy.get(itemSelector).filter(':visible').click();
                            processNested(nk + 1);
                          } else {
                            cy.location('pathname').then((afterNestedPath) => {
                              if (afterNestedPath !== initialPath) {
                                cy.go('back');
                  cy.location('pathname').should('equal', initialPath);
                  cy.get('[data-cy="dropdown"]').eq(dropdownIndex).click();
                  cy.get('[data-cy="dropdown-item-1"]').eq(itemIndex).click();
                                cy.wait(200);
                              }
                              ensureDropdownOpen(dropdownSelector, itemPattern);
                              cy.get(itemSelector).filter(':visible').click();
                              processNested(nk + 1);
                            });
                          }
                        });
                      };

            } else if ($item.text().trim() === headerTitle) {
              cy.location('pathname').should('equal', initialPath);
              cy.get('[data-cy="dropdown"]').eq(dropdownIndex).click();

                      processNested(0);
                    });
                  } else if (itemText === headerTitle) {
                    processItem(ii + 1);
                  } else {
              cy.location('pathname').should('not.equal', initialPath);
                    cy.go('back');
              cy.location('pathname').should('equal', initialPath);
              cy.get('[data-cy="dropdown"]').eq(dropdownIndex).click();
                    cy.wait(200);
                    processItem(ii + 1);
                  }

                });
          
              });
            });
          });
        };

        processItem(0);
      });
    };

    processDropdown(0);
  });
});

Cypress.Commands.add("checkProtectedRoutes", () => {
  for (let route of Cypress.env("protectedRoutes")) {
    cy.intercept(route);
    cy.log(route);
    cy.visit(route);
    cy.location("pathname").should("equal", "/");
  }
});


Cypress.Commands.add("manageEntities", (selector, numOfItems) => {
  function clickItem(index) {
    if (index > numOfItems+1) return;
// Cypress.Commands.add("manageEntities", (selector, numOfItems) => {
//   function clickItem(index) {
//     if (index > numOfItems+1) return;

    cy.get(selector).click(); // open navbar dropdown
//     cy.get(selector).click(); // open navbar dropdown

    cy.get('body').then(($body) => {
      const menuItem = $body.find(
        `.nav-item.open > #navbarDropdown > .dropdown-menu > :nth-child(${index}) > .dropdown-item`
      );
//     cy.get('body').then(($body) => {
//       const menuItem = $body.find(
//         `.nav-item.open > #navbarDropdown > .dropdown-menu > :nth-child(${index}) > .dropdown-item`
//       );

      if (menuItem.length === 0) {
        cy.log(`Skipping missing item at index ${index}`);
        cy.get('body').click();
        clickItem(index + 1);
        return;
      }
//       if (menuItem.length === 0) {
//         cy.log(`Skipping missing item at index ${index}`);
//         cy.get('body').click();
//         clickItem(index + 1);
//         return;
//       }

      cy.wrap(menuItem).click();
//       cy.wrap(menuItem).click();

      cy.get('body').then(($page) => {
        const hasButton = $page.find('.text-md-right > .btn, .jumbotron-heading > .btn').length > 0;
        const hasHeader = $page.find('h2.shadowed').length > 0;
//       cy.get('body').then(($page) => {
//         const hasButton = $page.find('.text-md-right > .btn, .jumbotron-heading > .btn').length > 0;
//         const hasHeader = $page.find('h2.shadowed').length > 0;

        expect(
          hasButton || hasHeader,
          `Expected either a button or a header on page for item ${index}`
        ).to.be.true;
//         expect(
//           hasButton || hasHeader,
//           `Expected either a button or a header on page for item ${index}`
//         ).to.be.true;

        if (hasButton) {
          cy.get('.text-md-right > .btn, .jumbotron-heading > .btn')
            .first()
            .click();
        } else {
          cy.log(`Found header instead of button for item ${index}`);
        }
//         if (hasButton) {
//           cy.get('.text-md-right > .btn, .jumbotron-heading > .btn')
//             .first()
//             .click();
//         } else {
//           cy.log(`Found header instead of button for item ${index}`);
//         }

        cy.go('back').then(() => clickItem(index + 1));
      });
    });
  }
//         cy.go('back').then(() => clickItem(index + 1));
//       });
//     });
//   }

//   clickItem(1);
// });

  clickItem(1);
});
Cypress.Commands.add("checkProtectedRoutes", () => {
  for (let route of Cypress.env("protectedRoutes")) {
    cy.intercept(route);
    cy.log(route);
    cy.visit(route);
    cy.location("pathname").should("equal", "/");
  }
});
+40 −40

File changed.

Preview size limit exceeded, changes collapsed.