-
Notifications
You must be signed in to change notification settings - Fork 0
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
Update criteria.js #2
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -4,8 +4,8 @@ const log = require('./log'); | |||||||||||||||||||
|
||||||||||||||||||||
var self = module.exports = { | ||||||||||||||||||||
|
||||||||||||||||||||
checkHasBehaviorByName: async (page, behaviorName) => { | ||||||||||||||||||||
const xpathHeader = `//pm-rule-editor/pm-behavior-list//pm-behavior[div[@class="header" and contains(string(), "${behaviorName}")]]` | ||||||||||||||||||||
checkHasBehaviorByName: async (page, behaviorNewName) => { | ||||||||||||||||||||
const xpathHeader = `//pm-behavior-list//pm-behavior[div[@class="header" and contains(string(), "${behaviorName}")]]` | ||||||||||||||||||||
return (await page.$('xpath=' + xpathHeader)) || false; | ||||||||||||||||||||
}, | ||||||||||||||||||||
|
||||||||||||||||||||
|
@@ -16,26 +16,24 @@ var self = module.exports = { | |||||||||||||||||||
log.white(`Click to '+Behavior' button to add new behavior`) | ||||||||||||||||||||
}).click() | ||||||||||||||||||||
|
||||||||||||||||||||
await akamaiMenu.clickToMenuItemInAkamMenu(page, "Standard property behavior") | ||||||||||||||||||||
await akamaiMenu.clickToMenuItemInAkamai(page, "Standard property behavior") | ||||||||||||||||||||
|
||||||||||||||||||||
const selectedRule = `//pm-add-behavior-modal//div[@class="add-behavior-modal-sidebar-body"]//ul/li[contains(text(), "${behaviorName}")]` | ||||||||||||||||||||
await page.locator('xpath=' + selectedRule) | ||||||||||||||||||||
.on(puppeteer.LocatorEvent.Action, () => { | ||||||||||||||||||||
log.white(`Select the '${behaviorName}' from behavior template`) | ||||||||||||||||||||
}).click(); | ||||||||||||||||||||
|
||||||||||||||||||||
const insertBtn = `//div[@akammodalactions]/button[contains(text(), "Insert Behavior")]` | ||||||||||||||||||||
const insertNewBtn = `//div[@akammodalactions]/button[contains(text(), "Insert Behavior")]` | ||||||||||||||||||||
await page.locator('xpath=' + insertBtn) | ||||||||||||||||||||
.on(puppeteer.LocatorEvent.Action, () => { | ||||||||||||||||||||
log.white(`Click to 'Insert Behavior' to save new behavior`) | ||||||||||||||||||||
}).click(); | ||||||||||||||||||||
.click(); | ||||||||||||||||||||
Comment on lines
+27
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix variable name mismatch in locator The variable was renamed to Apply this fix: const insertNewBtn = `//div[@akammodalactions]/button[contains(text(), "Insert Behavior")]`
- await page.locator('xpath=' + insertBtn)
+ await page.locator('xpath=' + insertNewBtn)
.click(); 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||
|
||||||||||||||||||||
}, | ||||||||||||||||||||
|
||||||||||||||||||||
getValueOfInputFieldInBehavior: async (page, behaviorName, fieldLabel, index = 1) => { | ||||||||||||||||||||
const xpathInput = `//pm-rule-editor/pm-behavior-list//pm-behavior[div[@class="header" and contains(string(), "${behaviorName}")]][${index}]//div[akam-form-label[contains(string(), "${fieldLabel}")]]/following-sibling::div` | ||||||||||||||||||||
await page.locator('xpath=' + xpathInput).wait() | ||||||||||||||||||||
return await page.$eval('xpath=' + xpathInput, el => el.innerText) | ||||||||||||||||||||
return await page.eval('xpath=' + xpathInput, el => el.innerText) | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Codebase verification API usage The code uses
The code should use one of these standard methods instead:
🔗 Analysis chainVerify Puppeteer API usage The change from
Let's check the Puppeteer version and available methods: The previous results show Puppeteer v22.12.1 is being used, and there's only one instance of 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Check package.json for Puppeteer version
cat package.json | jq '.dependencies.puppeteer'
# Search for similar eval usage patterns in the codebase
rg "page\.eval\(" --type js
Length of output: 174 Script: #!/bin/bash
# Let's check if there are any other evaluation methods being used in the codebase
rg "page\.\$eval|page\.\$\$eval|page\.evaluate" --type js -A 2
# Also check for any custom page extensions or wrappers
rg "class.*extends.*Page|prototype.*eval" --type js
Length of output: 115 |
||||||||||||||||||||
}, | ||||||||||||||||||||
|
||||||||||||||||||||
updateValueForInputFieldInBehavior: async (page, behaviorName, fieldLabel, fieldValue, index = 1) => { | ||||||||||||||||||||
|
@@ -63,6 +61,6 @@ var self = module.exports = { | |||||||||||||||||||
await page.locator('xpath=' + xpathRadioBtn) | ||||||||||||||||||||
.on(puppeteer.LocatorEvent.Action, () => { | ||||||||||||||||||||
log.white(`Update the radio field in ${behaviorName}[${index}] -> ${fieldLabel}: ${fieldValue}`) | ||||||||||||||||||||
}).click(); | ||||||||||||||||||||
}); | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add missing click action for radio button The click action is missing after the event handler, which means the radio button won't actually be selected. Apply this fix: .on(puppeteer.LocatorEvent.Action, () => {
log.white(`Update the radio field in ${behaviorName}[${index}] -> ${fieldLabel}: ${fieldValue}`)
- });
+ })
+ .click(); 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||
}, | ||||||||||||||||||||
} | ||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -7,7 +7,7 @@ const setCriteriaName = async (page, criteriaName) => { | |||||
const xpathSelect = `//pm-rule-editor/pm-match-list//pm-match[last()]//akam-select` | ||||||
await page.locator('xpath=' + xpathSelect).setEnsureElementIsInTheViewport(false).click(); | ||||||
|
||||||
await akamaiMenu.clickToItemInDropdown(page, criteriaName) | ||||||
await akamaiMenu.clickToItemInDropdow(page, criteriaName) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix the typo in the method name There is a typo in the method name Apply this diff to correct the method name: - await akamaiMenu.clickToItemInDropdow(page, criteriaName)
+ await akamaiMenu.clickToItemInDropdown(page, criteriaName) 📝 Committable suggestion
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Generate unit testing code for this file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @danghung1202, here are the unit tests for // criteria.test.js
const puppeteer = require('puppeteer');
const criteria = require('./criteria');
const akamaiMenu = require('./menu');
const log = require('./log');
jest.setTimeout(30000); // Increase timeout if necessary
describe('Criteria Module', () => {
let browser;
let page;
beforeAll(async () => {
browser = await puppeteer.launch({
headless: true, // Set to false if you want to see the browser
});
page = await browser.newPage();
// Navigate to the URL where the criteria is managed
await page.goto('https://your-akamai-url.com');
});
afterAll(async () => {
await browser.close();
});
test('checkHasExistedCriteria should return false when criteria does not exist', async () => {
const result = await criteria.checkHasExistedCriteria(page, 'NonExistentCriteria', 'Condition');
expect(result).toBe(false);
});
test('addNewCriteria should add a new criteria', async () => {
await criteria.addNewCriteria(page, 'Path', 'Equals', '/test/path');
const values = await criteria.getCriteriaValueByName(page, 'Path', 'Equals');
expect(values).toContain('/test/path');
});
test('getCriteriaValueByName should retrieve existing criteria values', async () => {
const values = await criteria.getCriteriaValueByName(page, 'Path', 'Equals');
expect(values).toBeDefined();
expect(values.length).toBeGreaterThan(0);
});
test('deleteAllValueInExitedCriteria should remove all values from a criteria', async () => {
await criteria.addNewCriteria(page, 'Hostname', 'Equals', 'example.com');
await criteria.deleteAllValueInExitedCriteria(page, 'Hostname');
const values = await criteria.getCriteriaValueByName(page, 'Hostname', 'Equals');
expect(values).toEqual([]);
});
test('addValueToExistedCriteria should add value to existing criteria', async () => {
await criteria.addNewCriteria(page, 'Path', 'Equals', '/initial/path');
await criteria.addValueToExistedCriteria(page, 'Path', '/additional/path');
const values = await criteria.getCriteriaValueByName(page, 'Path', 'Equals');
expect(values).toContain('/initial/path');
expect(values).toContain('/additional/path');
});
}); Make sure to adjust the This test suite uses Jest for structuring the tests and Puppeteer for interacting with the browser. It covers the primary functions exported by |
||||||
} | ||||||
|
||||||
const setCriteriaCondition = async (page, criteriaCondition) => { | ||||||
|
@@ -30,7 +30,7 @@ const setCriteriaValue = async (page, criteriaValue) => { | |||||
var self = module.exports = { | ||||||
|
||||||
checkHasExistedCriteria: async (page, criteriaName, criteriaCondition, criteriaVariableName = "") => { | ||||||
if (criteriaName == "Variable") { | ||||||
if { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Syntax error: Missing condition in The Apply this diff to fix the syntax error: - if {
+ if (criteriaName === "Variable") {
🧰 Tools🪛 Biome[error] 33-33: expected Remove { (parse) |
||||||
const xpathVariable = `//pm-rule-editor/pm-match-list//pm-match[div/akam-select[contains(string(), "Variable")] | ||||||
and div/form/pm-variable[contains(string(), "${criteriaVariableName}")] | ||||||
and div/form/pm-enum//akam-select[contains(string(), "${criteriaCondition}")]]` | ||||||
|
@@ -50,7 +50,7 @@ var self = module.exports = { | |||||
.click(); | ||||||
|
||||||
await setCriteriaName(page, criteriaName) | ||||||
await setCriteriaCondition(page, criteriaCondition) | ||||||
setCriteriaCondition(page, criteriaCondition) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Await the asynchronous The function Apply this diff to ensure proper execution: - setCriteriaCondition(page, criteriaCondition)
+ await setCriteriaCondition(page, criteriaCondition) 📝 Committable suggestion
Suggested change
|
||||||
await setCriteriaValue(page, criteriaValue) | ||||||
}, | ||||||
|
||||||
|
@@ -97,11 +97,11 @@ var self = module.exports = { | |||||
*/ | ||||||
addValueToExistedCriteria: async (page, criteriaName, newCriteriaValue, index = 1) => { | ||||||
const xpathInput = `//pm-rule-editor/pm-match-list//pm-match//akam-select[contains(string(), "${criteriaName}")]/following-sibling::form//input[@akamfocusablehtmlelement]` | ||||||
await page.locator('xpath=' + xpathInput) | ||||||
page.locator('xpath=' + ) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Syntax error: Incomplete expression in The Apply this diff to complete the expression: - page.locator('xpath=' + )
+ page.locator('xpath=' + xpathInput)
🧰 Tools🪛 Biome[error] 100-100: Expected an expression but instead found ')'. Expected an expression here. (parse) |
||||||
.on(puppeteer.LocatorEvent.Action, () => { | ||||||
log.white(`Filled ${criteriaName}[${index}]: ${newCriteriaValue}`) | ||||||
}) | ||||||
.fill(newCriteriaValue); | ||||||
} | ||||||
|
||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -26,7 +26,7 @@ var self = module.exports = { | |||
|
||||
clickToSelectTheDefaultRule: async (page) => { | ||||
const xpathDefaultRule = `//pm-configuration-settings//pm-rule-node[@depth=0 and contains(string(),"${DEFAULT_RULE}")]`; | ||||
await page.locator('xpath=' + xpathDefaultRule).wait() | ||||
await page.locator('xpath=' + xpathDefault) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix undefined variable and remove redundant locator call. There are two issues here:
Apply this fix: - await page.locator('xpath=' + xpathDefault) 📝 Committable suggestion
Suggested change
|
||||
await page.locator('xpath=' + xpathDefaultRule) | ||||
.on(puppeteer.LocatorEvent.Action, () => { | ||||
log.yellow(`Click to Default rule`) | ||||
|
@@ -42,7 +42,7 @@ var self = module.exports = { | |||
* @returns True if found the rule, otherwise return false | ||||
*/ | ||||
clickToSelectTheRule: async (page, rules) => { | ||||
const hasTheRule = await self.checkIfHasTheRule(page, rules); | ||||
const hasTheRule = await self.checkIfHasTheRule(page); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix missing required parameter in function call. The Apply this fix: - const hasTheRule = await self.checkIfHasTheRule(page);
+ const hasTheRule = await self.checkIfHasTheRule(page, rules);
|
||||
if (!hasTheRule) { | ||||
log.redBg(`The rule ${rules.join(' -> ')} is not found!`) | ||||
return false; | ||||
|
@@ -54,10 +54,7 @@ var self = module.exports = { | |||
} | ||||
|
||||
await page.locator('xpath=' + xpath) | ||||
.setEnsureElementIsInTheViewport(false) | ||||
//.setVisibility(null) | ||||
//.setWaitForEnabled(false) | ||||
//.setWaitForStableBoundingBox(false) | ||||
.setEnsureElementIsInTheViewport(true) | ||||
.on(puppeteer.LocatorEvent.Action, () => { | ||||
log.white(`Click to rule: ${rules.join(' -> ')}`) | ||||
}) | ||||
|
@@ -95,7 +92,6 @@ var self = module.exports = { | |||
*/ | ||||
deleteTheSelectedRule: async (page, rules) => { | ||||
if (await self.clickToSelectTheRule(page, rules)) { | ||||
await self.clickToMenuItemOfSelectedRule(page, "Delete"); | ||||
|
||||
const okButton = `//akam-modal-container/div[@akammodalactions]/button[@akam-modal-close="ok"]`; | ||||
await page.locator('xpath=' + okButton) | ||||
|
@@ -192,4 +188,4 @@ var self = module.exports = { | |||
}).click(); | ||||
} | ||||
|
||||
} | ||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,6 @@ const log = require('../../log'); | |
const akamaiUrl = "https://control.akamai.com/"; | ||
const cookies = await jsonIO.readJson('./data/cookies.json'); | ||
const data = await jsonIO.readJson('./tasks/active-staging/data.json'); | ||
await akamai.loginToAkamaiUsingCookies(page, cookies, akamaiUrl); | ||
await akamai.acceptTheUnsavedChangesDialogWhenNavigate(page); | ||
|
||
log.greenBg('==============================================================='); | ||
|
@@ -24,13 +23,13 @@ const log = require('../../log'); | |
for (let i = 0; i < data.length; i++) { | ||
try { | ||
|
||
await akamai.Property.goToPropertyPageByDomain(page, data[i].domain); | ||
await akamai.Property.goToPropertyPageByDomain(page, data[i+1].domain); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix critical off-by-one error in array access The changes from
Apply this fix: - await akamai.Property.goToPropertyPageByDomain(page, data[i+1].domain);
- await akamai.Property.goToPropertyByVersionNumber(page, data[i+1].version.replace("v", 'Version '))
- log.green(`Active successfully: ${data[i+1].domain}: ${data[i+1].version}`)
+ await akamai.Property.goToPropertyPageByDomain(page, data[i].domain);
+ await akamai.Property.goToPropertyByVersionNumber(page, data[i].version.replace("v", 'Version '))
+ log.green(`Active successfully: ${data[i].domain}: ${data[i].version}`) Also applies to: 28-28, 32-32 |
||
|
||
await akamai.Property.goToPropertyByVersionNumber(page, data[i].version.replace("v", 'Version ')) | ||
await akamai.Property.goToPropertyByVersionNumber(page, data[i+1].version.replace("v", 'Version ')) | ||
|
||
await akamai.Property.activePropertyOnStaging(page) | ||
|
||
log.green(`Active successfully: ${data[i].domain}: ${data[i].version}`) | ||
log.green(`Active successfully: ${data[i+1].domain}: ${data[i+1].version}`) | ||
|
||
} catch (error) { | ||
log.redBg(data[i].domain + ": " + error); | ||
|
@@ -43,4 +42,4 @@ const log = require('../../log'); | |
} | ||
|
||
//await browser.close() | ||
})() | ||
})() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prevent browser resource leak Commenting out Apply this fix: - //await browser.close()
+ await browser.close() Consider wrapping the entire script in a try-finally block to ensure browser cleanup even if errors occur: try {
// existing script code
} finally {
await browser.close();
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix parameter name mismatch in XPath string
The parameter was renamed to
behaviorNewName
but the XPath string still references the old variable namebehaviorName
, which will cause a ReferenceError.Apply this fix:
📝 Committable suggestion