Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 55 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ Here are the core functions provided by the package:
- `removeResponseHeaderUrl(name)`: Removes a single response header.
- `removeResponseHeadersUrl(headerNames)`: Removes multiple response headers from an array of names.

### Query Parameters

- `modifyQueryParamUrl(paramName, paramValue, params)`: Add or modify query parameters in URLs.
- `modifyQueryParamsUrl(params)`: Add/modify multiple query parameters using an object.
- `removeQueryParamUrl(paramName, params)`: Remove query parameters from URLs.
- `removeQueryParamsUrl(params)`: Remove multiple query parameters using an array.

### Importing Rules

- `importRules(apiKey)`: Import all rules from your Requestly account using API key.
Expand Down Expand Up @@ -83,6 +90,10 @@ const {
addResponseHeadersUrl,
removeResponseHeaderUrl,
removeResponseHeadersUrl,
modifyQueryParamUrl,
modifyQueryParamsUrl,
removeQueryParamUrl,
removeQueryParamsUrl,
importRules,
closeWelcomePage,
} = require("@requestly/rq-automation");
Expand Down Expand Up @@ -129,6 +140,26 @@ async function seleniumExample() {
removeResponseHeadersUrl(["X-Response-Header", "X-Another-Response-Header"])
);

// Add query parameters for tracking/analytics
await driver.get(modifyQueryParamUrl("debug", "true"));

// Add multiple query parameters for A/B testing
await driver.get(
modifyQueryParamsUrl({
variant: "A",
utm_source: "test",
user_segment: "premium"
})
);

// Remove sensitive query parameters
await driver.get(removeQueryParamUrl("api_key"));

// Remove multiple tracking parameters for privacy testing
await driver.get(
removeQueryParamsUrl(["utm_source", "utm_medium", "fbclid", "gclid"])
);

// Import a shared list of rules
await driver.get(importRules("YOUR_API_KEY"));

Expand Down Expand Up @@ -178,6 +209,8 @@ const { getExtension } = require("@requestly/rq-automation");
const path = require("path");
const {
addRequestHeaderUrl,
modifyQueryParamsUrl,
removeQueryParamsUrl,
// ... import other functions
importRules,
closeWelcomePage,
Expand All @@ -199,7 +232,19 @@ async function playwrightExample() {
const page = await browser.newPage();

// Add a request header
await page.goto(addRequestHeaderUrl("X-Request-By", "Puppeteer"));
await page.goto(addRequestHeaderUrl("X-Request-By", "Playwright"));

// Add query parameters for feature testing
await page.goto(
modifyQueryParamsUrl({
feature_flag: "new_ui",
test_variant: "B",
debug: "true"
})
);

// Remove tracking parameters for privacy testing
await page.goto(removeQueryParamsUrl(["utm_source", "fbclid"]));

// Import a shared list of rules
await page.goto(importRules("YOUR_API_KEY"));
Expand Down Expand Up @@ -246,6 +291,8 @@ const puppeteer = require("puppeteer");
const { getExtension } = require("@requestly/rq-automation");
const {
addRequestHeaderUrl,
modifyQueryParamUrl,
removeQueryParamsUrl,
// ... import other functions
importRules,
closeWelcomePage,
Expand All @@ -266,6 +313,12 @@ async function puppeteerExample() {
const page = await browser.newPage();
await page.goto(addRequestHeaderUrl("X-Request-By", "Puppeteer"));

// Add query parameter for API testing
await page.goto(modifyQueryParamUrl("api_version", "v2"));

// Remove multiple sensitive parameters
await page.goto(removeQueryParamsUrl(["session_token", "api_key"]));

// Import a shared list of rules
await page.goto(importRules("YOUR_API_KEY"));

Expand All @@ -285,4 +338,4 @@ puppeteerExample();

## Keywords

`requestly` `selenium` `playwright` `puppeteer` `webdriver` `chrome` `modify` `headers` `request headers` `response headers` `redirect` `delay` `throttle` `automation` `testing`
`requestly` `selenium` `playwright` `puppeteer` `webdriver` `chrome` `modify` `headers` `request headers` `response headers` `query parameters` `url parameters` `redirect` `delay` `throttle` `automation` `testing` `a/b testing` `feature flags`
96 changes: 96 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,98 @@ function removeResponseHeadersUrl(headers) {
return removeResponseHeaderUrl(null, headers);
}

/**
* Add or modify query parameters in URLs.
* @param {string} paramName - The name of the query parameter to add/modify.
* @param {string} paramValue - The value of the query parameter.
* @param {Object.<string, string>} [params] - Object containing multiple parameters to add/modify.
* @returns {string} The automation URL for modifying query parameters.
* @throws {Error} If neither paramName/paramValue nor params are provided.
*/
function modifyQueryParamUrl(paramName, paramValue, params) {
let query = "";
if (params && typeof params === "object") {
// Validate all parameters
Object.entries(params).forEach(([name, value]) => {
if (!name || typeof name !== "string") {
throw new Error("Parameter name must be a non-empty string");
}
if (typeof value !== "string") {
throw new Error("Parameter value must be a string");
}
});
query = buildQueryString(params);
} else if (paramName !== null && paramName !== undefined && typeof paramValue !== "undefined") {
if (!paramName || typeof paramName !== "string") {
throw new Error("Parameter name must be a non-empty string");
}
if (typeof paramValue !== "string") {
throw new Error("Parameter value must be a string");
}
query = `${encodeURIComponent(paramName)}=${encodeURIComponent(paramValue)}`;
} else {
throw new Error("Provide either params object or paramName and paramValue");
}
return `https://app.requestly.io/automation/modify-query-param?${query}`;
}

/**
* Shortcut: Add/modify multiple query parameters using an object.
* @param {Object.<string, string>} params - Key-value pairs of query parameters to add/modify.
* @returns {string} The automation URL for modifying multiple query parameters.
* @throws {Error} If params is not provided or not an object.
*/
function modifyQueryParamsUrl(params) {
if (!params || typeof params !== "object" || Array.isArray(params)) {
throw new Error("params parameter is required and must be an object");
}
return modifyQueryParamUrl(null, null, params);
}

/**
* Remove query parameters from URLs.
* @param {string} paramName - The name of the query parameter to remove.
* @param {string[]} [params] - Array of parameter names to remove.
* @returns {string} The automation URL for removing query parameters.
* @throws {Error} If neither paramName nor params are provided.
*/
function removeQueryParamUrl(paramName, params) {
let query = "";
if (params && Array.isArray(params)) {
if (params.length === 0) {
throw new Error("params array cannot be empty");
}
// Validate all parameter names
params.forEach(name => {
if (!name || typeof name !== "string") {
throw new Error("Parameter name must be a non-empty string");
}
});
query = params.map(name => `param=${encodeURIComponent(name)}`).join("&");
} else if (paramName !== null && paramName !== undefined) {
if (typeof paramName !== "string" || !paramName) {
throw new Error("Parameter name must be a non-empty string");
}
query = `param=${encodeURIComponent(paramName)}`;
} else {
throw new Error("Provide either paramName or params array");
}
return `https://app.requestly.io/automation/remove-query-param?${query}`;
}

/**
* Shortcut: Remove multiple query parameters using an array.
* @param {string[]} params - Array of parameter names to remove.
* @returns {string} The automation URL for removing multiple query parameters.
* @throws {Error} If params is not provided or not an array.
*/
function removeQueryParamsUrl(params) {
if (!Array.isArray(params)) {
throw new Error("params parameter must be an array");
}
return removeQueryParamUrl(null, params);
}

/**
* Get a URL to import rules using an API key.
* @param {string} api_key - The API key for importing rules.
Expand All @@ -253,6 +345,10 @@ module.exports = {
addResponseHeadersUrl,
removeResponseHeaderUrl,
removeResponseHeadersUrl,
modifyQueryParamUrl,
modifyQueryParamsUrl,
removeQueryParamUrl,
removeQueryParamsUrl,
importRules,
getExtension,
closeWelcomePage,
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,17 @@
"headers",
"request headers",
"response headers",
"query parameters",
"url parameters",
"redirect",
"redirect network requests",
"delay",
"delay network requests",
"throttle",
"throttle network requests"
"throttle network requests",
"automation testing",
"web testing",
"api testing"
],
"peerDependencies": {
"selenium-webdriver": "^4.0.0",
Expand Down
Loading