npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@yassinouhadi/cypress-generic-package

v1.0.1

Published

Cypress tests generic pom objects package

Downloads

4

Readme

Cypress Generic Component Library

This package provides a set of generic components for testing Angular applications using Cypress. It includes various components such as forms, fields, buttons, tables, menus, and graphs, all of which can be easily extended and customized for specific testing needs.

Features

  • Generic Components: Reusable and extendable UI components.

  • Component Interactions: Methods for interacting with components, such as clicking, typing, selecting, etc.

  • Customizable Requests: The ability to intercept and handle API requests triggered by component interactions.

  • Assertions and Validations: Built-in assertions to validate the state and behavior of components.

  • Custom error messages: Error messages were personalized by rewriting error-handling listeners in Cypress.

Generic Testing Implementation

Generic tests represent an integration between the Keyword-Driven Testing (KDT) approach, Keyword-Driven Testing (DDT) and the Page Object Model (POM) methodology.

  • KDT: Keywords that serve as generic functions, either for actions or assertions.

  • DDT: This approach aligns with Cypress concepts, such as fixtures; see the Cypress documentation for details.

  • POM:Page Object Model objects represent web interfaces that can be extended and initialized with data.

We have identified generic objects, each object has specific actions and assertions. The table below lists some of the identified elements, their actions, and assertions:

| Element | Actions | Assertions | |-------------|-------------|----------------| | Page | Navigate, Refresh | Verify title, Verify URL | | Table | Sort, Filter, Select a row | Verify row count, Verify data presence | | Form | Fill, Submit | Verify validation, Verify errors | | Button | Click, Double click | Verify state (enabled/disabled), Verify existence | | Input | Enter text, Clear | Verify value, Verify placeholder | | Select | Select an option, Deselect | Verify available options, Verify selection | | Menu | Open, Close, Navigate | Verify menu items, Verify state (open/closed) |

Installation

To install the Cypress Generic Component Library, use npm:

npm i @yassinouhadi/cypress-generic-package

Usage

To use the Cypress Generic Component Library in your Cypress tests, start by importing the required components:

import { 
  GenericForm
} from '@yassinouhadi/cypress-generic-package';

Define form Cypress selectors: Use the cy.get() method to define selectors. It's better to include a 'data-cy' attribute in the HTML elements to simplify TDD and BDD testing approaches.

const MyFormVariables = {
    
    form: () => cy.get('[data-cy="form"]'),

    FIELDS: {

        name: () => ({
            field: () => cy.get('[data-cy="name"]'),
            entry: ['INPUT'],
            isOptional: false
        }),

        role: () => ({
            field: () => cy.get('[data-cy="role"]'),
            entry: ['SELECT']
        }),

        tags: () => ({
            field: () => cy.get('[data-cy="tags"]'),
            entry: ['INPUT' , 'MULTISELECT']
        })
    },    

    MENUS: {

        role: () => cy.get('[data-cy="rolemenu"]'),

        tags: () => cy.get('[data-cy="tagsmenu"]')
    },

    URLS: {

        tags: () => ({ 
            onLoadRequestUrl: 'https://',,
            onLoadRequestHttpMethod: 'GET', 
            isByDefaultOnLoaded: true,
            onEventRequestUrl: 'https://',,
            onEventRequestHttpMethod: 'POST'
        })
    },

    BUTTONS: {

        submit: () => ({
            button: () => cy.get('[data-cy="submit"]'),
            type: 'SUBMIT' 
        })
    }
}

Define the form POM object by extending from the generic form and initializing it with the corresponding fields, buttons, etc.

class MyForm extends GenericForm {

  constructor() {
    super('FORM', 
        cy.get('app-correlation-form'), 
        'https://',
        "GET",
        true,
        'https://',
        'POST'
        );
  }

  override init() {

    // Define form fields
    this.setFields(
      MyFormVariables.FIELDS,
      MyFormVariables.MENUS,
      MyFormVariables.URLS
    );

    // Define buttons
    this.setButtons(
      MyFormVariables.BUTTONS,
      MyFormVariables.URLS
    );
  }
}

Below is an example of a data.json file that contains test data for the form fields. This file should be placed in the cypress/fixtures directory of your project.

// cypress/fixtures/data.json
[
  {
    "name": "John Doe",
    "role": "Admin",
    "tags": ["cypress", "testing"]
  },
  {
    "name": "Jane Smith",
    "role": "User",
    "tags": ["automation", "qa"]
  }
]
And once we have POM objects, we can use the actions and assertions provided by the library directly:

```bash
/// <reference types="Cypress" />

import { Given, When, Then } from "@badeball/cypress-cucumber-preprocessor";
import { Assertions } from "@watch/cypress-generic-package";

let successfullySubmit =  false;
let fieldsToIgnore = [];
let data;

beforeEach(function () {
  // Load data from the fixtures file
  cy.fixture('data.json').then((jsonData) => {
    data = jsonData;
  });
});

When('I fill out the form completely', () => {
  myform.fill(data).then(() => {
    successfullySubmit = true;
  });
});

When('I fill out the form with some fields empty', () => {
  data.name = '';
  myform.fill(data).then(() => {
    successfullySubmit = false;
  });
});

When('I submit the form', () => {
  form.submitForm();

  if (successfullySubmit) {
    form.waitForEventResponse()
        .then((interception: { response: { statusCode: any; } }) => { 
      const status = interception.response?.statusCode;

      SubmitResult.expect('/', status, false).toBeSuccessful();
    });
  }
});

If you find a bug, please report it on our GitHub Issues page.

License

This package is licensed under the MIT License.