Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a flexible and simple mechanism to filter entries from the HAR #163

Closed
derevnjuk opened this issue Jan 17, 2023 · 2 comments · Fixed by #172
Closed

Provide a flexible and simple mechanism to filter entries from the HAR #163

derevnjuk opened this issue Jan 17, 2023 · 2 comments · Fixed by #172
Assignees
Labels

Comments

@derevnjuk
Copy link
Member

Description
It would be useful to have a way to filter recorded entries from the resulting HAR file, as the current options (i.e. cy.recordHar({ /* ... */})) do not provide enough flexibility for certain use cases.

Possible solution
We can add a query parameter that allows the user to filter the recorded entries using a Lucene-like syntax, XPath, or JSONPath. For example:

cy.recordHar({ query: 'url:*example.com/* AND contentLength:[10 TO 100]' });

or

cy.recordHar({ query: `$[?(@.url.includes('example.com') && @.contentLength >= 10 && @.contentLength <= 100)]` });

Alternatively, we can allow specifying the path to the module being used to filter them as follows:

// filter.js
module.exports = request => {
  return /example\.com/.test(request.url) && request.contentLength < 100 && request.contentLength > 10;
}

// in the test
cy.recordHar({
  filter: "path/to/filter.js"
});

Additional context
Cypress is spawning an independent child process that runs a plugin. The test is actually running in the browser. While communicating with each other, Cypress serializes command arguments and passes them to the plugin via WebSocket. Therefore, passing a plain JS function is hardly possible. This feature will give the users more control over the data that is saved and make it more efficient.

@derevnjuk derevnjuk added the Type: enhancement New feature or request. label Jan 17, 2023
@derevnjuk derevnjuk self-assigned this Jan 17, 2023
derevnjuk added a commit that referenced this issue Jan 23, 2023
derevnjuk added a commit that referenced this issue Jan 23, 2023
derevnjuk added a commit that referenced this issue Jan 24, 2023
derevnjuk added a commit that referenced this issue Jan 24, 2023
derevnjuk added a commit that referenced this issue Jan 24, 2023
…te (#172)

You can specify the `filter` option as a path to a JS/TS module that
exports a function to filter out unwanted entries from the HAR. The
function should take an [Entry
object](http://www.softwareishard.com/blog/har-12-spec/#entries) as a
parameter and return a boolean indicating whether the entry should be
included in the final HAR or not.

Here's an example of how to use the `filter` option:

```js
cy.recordHar({ filter: '../support/include-password.ts' });
```

And here's an example of what the `include-password.ts` filter module
might look like:

```ts
import { Entry } from 'har-format';

export default async (req: Entry) => {
  try {
    return /\"password":/.test(req.request.postData.text ?? '');
  } catch {
    return false;
  }
};
```

In this example, the `filter` function will only exclude entries in the
HAR where the request body contains a JSON object with a password field.

You can also specify a `rootDir` option that will be used to resolve the
path of the `filter` option. By default, the path is relative to the
spec folder. But by providing a `rootDir` it will look for the module in
the provided directory:

```js
cy.recordHar({
  filter: 'cypress/support/include-password.ts',
  rootDir: Cypress.config('projectRoot')
});
```

closes #163
@derevnjuk
Copy link
Member Author

@cheapsteak I am pleased to inform you that the feature you have been interested in (#85 (comment)) has been released in version 5.13.0.

@cheapsteak
Copy link

Thank you so much!
image

derevnjuk added a commit that referenced this issue Jan 25, 2023
It is now possible to utilize regular expressions rather than strings when specifying the excludePaths or includeHosts options.

**Before**:

```ts
cy.recordHar({ excludePaths: ['^\\/api\\/products$', '^\\/api\\/users$'] });
```

**After**:

```ts
cy.recordHar({ excludePaths: [/^\/api\/products$/, '^\\/api\\/users$'] });
```

closes #163
derevnjuk added a commit that referenced this issue Jan 25, 2023
…#175)

It is now possible to utilize regular expressions rather than strings
when specifying the excludePaths or includeHosts options.

**Before**:

```ts
cy.recordHar({ excludePaths: ['^\\/api\\/products$', '^\\/api\\/users$'] });
```

**After**:

```ts
cy.recordHar({ excludePaths: [/^\/api\/products$/, '^\\/api\\/users$'] });
```

closes #163
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants