PromucFlow_constructor/app/client/cypress/support/Pages/ApiPage.ts
albinAppsmith 629999f124
feat: [epic] appsmith design system version 2 deduplication (#22030)
## Description

### Fixes
- [x] https://github.com/appsmithorg/appsmith/issues/19383
- [x] https://github.com/appsmithorg/appsmith/issues/19384
- [x] https://github.com/appsmithorg/appsmith/issues/19385
- [x] https://github.com/appsmithorg/appsmith/issues/19386
- [x] https://github.com/appsmithorg/appsmith/issues/19387
- [x] https://github.com/appsmithorg/appsmith/issues/19388
- [x] https://github.com/appsmithorg/appsmith/issues/19389
- [x] https://github.com/appsmithorg/appsmith/issues/19390
- [x] https://github.com/appsmithorg/appsmith/issues/19391
- [x] https://github.com/appsmithorg/appsmith/issues/19392
- [x] https://github.com/appsmithorg/appsmith/issues/19393
- [x] https://github.com/appsmithorg/appsmith/issues/19394
- [x] https://github.com/appsmithorg/appsmith/issues/19395
- [x] https://github.com/appsmithorg/appsmith/issues/19396
- [x] https://github.com/appsmithorg/appsmith/issues/19397
- [x] https://github.com/appsmithorg/appsmith/issues/19398
- [x] https://github.com/appsmithorg/appsmith/issues/19399
- [x] https://github.com/appsmithorg/appsmith/issues/19400
- [x] https://github.com/appsmithorg/appsmith/issues/19401
- [x] https://github.com/appsmithorg/appsmith/issues/19402
- [x] https://github.com/appsmithorg/appsmith/issues/19403
- [x] https://github.com/appsmithorg/appsmith/issues/19404
- [x] https://github.com/appsmithorg/appsmith/issues/19405
- [x] https://github.com/appsmithorg/appsmith/issues/19406
- [x] https://github.com/appsmithorg/appsmith/issues/19407
- [x] https://github.com/appsmithorg/appsmith/issues/19408
- [x] https://github.com/appsmithorg/appsmith/issues/19409

Fixes # (issue)
> if no issue exists, please create an issue and ask the maintainers
about this first


Media
> A video or a GIF is preferred. when using Loom, don’t embed because it
looks like it’s a GIF. instead, just link to the video


## Type of change

> Please delete options that are not relevant.

- Bug fix (non-breaking change which fixes an issue)
- New feature (non-breaking change which adds functionality)
- Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- Chore (housekeeping or task changes that don't impact user perception)
- This change requires a documentation update


## How Has This Been Tested?
> Please describe the tests that you ran to verify your changes. Provide
instructions, so we can reproduce.
> Please also list any relevant details for your test configuration.
> Delete anything that is not important

- Manual
- Jest
- Cypress

### Test Plan
> Add Testsmith test cases links that relate to this PR

### Issues raised during DP testing
> Link issues raised during DP testing for better visiblity and tracking
(copy link from comments dropped on this PR)


## Checklist:
### Dev activity
- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] PR is being merged under a feature flag


### QA activity:
- [ ] Test plan has been approved by relevant developers
- [ ] Test plan has been peer reviewed by QA
- [ ] Cypress test cases have been added and approved by either SDET or
manual QA
- [ ] Organized project review call with relevant stakeholders after
Round 1/2 of QA
- [ ] Added Test Plan Approved label after reveiwing all Cypress test

---------

Co-authored-by: Ankita Kinger <ankita@appsmith.com>
Co-authored-by: akash-codemonk <67054171+akash-codemonk@users.noreply.github.com>
Co-authored-by: Tanvi Bhakta <tanvi@appsmith.com>
Co-authored-by: Arsalan <arsalanyaldram0211@outlook.com>
Co-authored-by: Aman Agarwal <aman@appsmith.com>
Co-authored-by: Rohit Agarwal <rohit_agarwal@live.in>
Co-authored-by: Nilesh Sarupriya <nilesh@appsmith.com>
Co-authored-by: Nilesh Sarupriya <20905988+nsarupr@users.noreply.github.com>
Co-authored-by: Tanvi Bhakta <tanvibhakta@gmail.com>
Co-authored-by: Aishwarya UR <aishwarya@appsmith.com>
Co-authored-by: Parthvi Goswami <parthvigoswami@Parthvis-MacBook-Pro.local>
Co-authored-by: Vijetha-Kaja <vijetha@appsmith.com>
Co-authored-by: Parthvi <80334441+Parthvi12@users.noreply.github.com>
Co-authored-by: Apple <nandan@thinkify.io>
Co-authored-by: Saroj <43822041+sarojsarab@users.noreply.github.com>
Co-authored-by: Sangeeth Sivan <74818788+berzerkeer@users.noreply.github.com>
Co-authored-by: Ashok Kumar M <35134347+marks0351@users.noreply.github.com>
Co-authored-by: Aishwarya-U-R <91450662+Aishwarya-U-R@users.noreply.github.com>
Co-authored-by: rahulramesha <rahul@appsmith.com>
Co-authored-by: Aswath K <aswath.sana@gmail.com>
Co-authored-by: Preet Sidhu <preetsidhu.bits@gmail.com>
Co-authored-by: Vijetha-Kaja <119562824+Vijetha-Kaja@users.noreply.github.com>
Co-authored-by: Shrikant Sharat Kandula <shrikant@appsmith.com>
2023-05-20 00:07:06 +05:30

413 lines
13 KiB
TypeScript

import { ObjectsRegistry } from "../Objects/Registry";
type RightPaneTabs = "datasources" | "connections";
export class ApiPage {
public agHelper = ObjectsRegistry.AggregateHelper;
public locator = ObjectsRegistry.CommonLocators;
private _createapi = ".t--createBlankApiCard";
_resourceUrl = ".t--dataSourceField";
private _headerKey = (index: number) =>
".t--actionConfiguration\\.headers\\[" +
index +
"\\]\\.key\\." +
index +
"";
private _headerValue = (index: number) =>
".t--actionConfiguration\\.headers\\[" +
index +
"\\]\\.value\\." +
index +
"";
private _paramKey = (index: number) =>
".t--actionConfiguration\\.queryParameters\\[" +
index +
"\\]\\.key\\." +
index +
"";
public _paramValue = (index: number) =>
".t--actionConfiguration\\.queryParameters\\[" +
index +
"\\]\\.value\\." +
index +
"";
private _importedKey = (index: number, keyValueName: string) =>
`.t--${keyValueName}-key-${index}`;
private _importedValue = (index: number, keyValueName: string) =>
`.t--${keyValueName}-value-${index}`;
_bodyKey = (index: number) =>
".t--actionConfiguration\\.bodyFormData\\[0\\]\\.key\\." + index + "";
_bodyValue = (index: number) =>
".t--actionConfiguration\\.bodyFormData\\[0\\]\\.value\\." + index + "";
_bodyTypeDropdown =
"//span[text()='Type'][@class='rc-select-selection-placeholder']/ancestor::div";
_apiRunBtn = ".t--apiFormRunBtn";
private _queryTimeout =
"//input[@name='actionConfiguration.timeoutInMillisecond']";
_responseBody = ".CodeMirror-code span.cm-string.cm-property";
private _blankAPI = "span:contains('New blank API')";
private _apiVerbDropdown = ".t--apiFormHttpMethod div";
private _verbToSelect = (verb: string) =>
"//div[contains(@class, 'rc-select-item-option')]//div[contains(text(),'" +
verb +
"')]";
private _bodySubTab = (subTab: string) =>
`//div[@data-testid="t--api-body-tab-switch"]//span[text()='${subTab}']`;
private _rightPaneTab = (tab: string) =>
"//span[contains(text(), '" + tab + "')]/parent::button";
_visibleTextSpan = (spanText: string) => "//span[text()='" + spanText + "']";
_visibleTextDiv = (divText: string) => "//div[text()='" + divText + "']";
_noBodyMessageDiv = "#NoBodyMessageDiv";
_noBodyMessage = "This request does not have a body";
_imageSrc = "//img/parent::div";
private _trashDelete = "[data-testid=t--trash-icon]";
private _onPageLoad = "input[name='executeOnLoad'][type='checkbox']";
private _confirmBeforeRunningAPI =
"input[name='confirmBeforeExecute'][type='checkbox']";
private _paginationTypeLabels = ".t--apiFormPaginationType label";
_saveAsDS = ".t--store-as-datasource";
_responseStatus = ".t--response-status-code";
private _blankGraphqlAPI = "span:contains('New blank GraphQL API')";
private _importedDataButton = ".t--show-imported-datas";
public _responseTabHeader = "[data-testid=t--tab-headers]";
public _autoGeneratedHeaderInfoIcon = (key: string) =>
`.t--auto-generated-${key}-info`;
private _createQuery = ".t--create-query";
CreateApi(
apiName = "",
apiVerb: "GET" | "POST" | "PUT" | "DELETE" | "PATCH" = "GET",
aftDSSaved = false,
) {
if (aftDSSaved) this.agHelper.GetNClick(this._createQuery);
else {
cy.get(this.locator._createNew).click();
cy.get(this._blankAPI).eq(0).click({ force: true });
}
this.agHelper.ValidateNetworkStatus("@createNewApi", 201);
// cy.get("@createNewApi").then((response: any) => {
// expect(response.response.body.responseMeta.success).to.eq(true);
// cy.get(this.agHelper._actionName)
// .click()
// .invoke("text")
// .then((text) => {
// const someText = text;
// expect(someText).to.equal(response.response.body.data.name);
// });
// }); // to check if Api1 = Api1 when Create Api invoked
if (apiName) this.agHelper.RenameWithInPane(apiName);
cy.get(this._resourceUrl).should("be.visible");
if (apiVerb != "GET") this.SelectAPIVerb(apiVerb);
}
CreateAndFillApi(
url: string,
apiName = "",
queryTimeout = 10000,
apiVerb: "GET" | "POST" | "PUT" | "DELETE" | "PATCH" = "GET",
aftDSSaved = false,
) {
this.CreateApi(apiName, apiVerb, aftDSSaved);
this.EnterURL(url);
//this.agHelper.Sleep(2000);// Added because api name edit takes some time to reflect in api sidebar after the call passes.
cy.get(this._apiRunBtn).should("not.be.disabled");
if (queryTimeout != 10000) this.SetAPITimeout(queryTimeout);
}
EnterURL(url: string) {
this.agHelper.EnterValue(url, {
propFieldName: this._resourceUrl,
directInput: true,
inputFieldName: "",
});
this.agHelper.AssertAutoSave();
}
EnterHeader(hKey: string, hValue: string, index = 0) {
this.SelectPaneTab("Headers");
this.agHelper.EnterValue(hKey, {
propFieldName: this._headerKey(index),
directInput: true,
inputFieldName: "",
});
this.agHelper.PressEscape();
this.agHelper.EnterValue(hValue, {
propFieldName: this._headerValue(index),
directInput: true,
inputFieldName: "",
});
this.agHelper.PressEscape();
this.agHelper.AssertAutoSave();
}
EnterParams(pKey: string, pValue: string, index = 0, escape = true) {
this.SelectPaneTab("Params");
this.agHelper.EnterValue(pKey, {
propFieldName: this._paramKey(index),
directInput: true,
inputFieldName: "",
});
this.agHelper.PressEscape();
this.agHelper.EnterValue(pValue, {
propFieldName: this._paramValue(index),
directInput: true,
inputFieldName: "",
});
if (escape) {
this.agHelper.PressEscape();
}
this.agHelper.AssertAutoSave();
}
EnterBodyFormData(
subTab: "FORM_URLENCODED" | "MULTIPART_FORM_DATA",
bKey: string,
bValue: string,
type = "",
toTrash = false,
) {
this.SelectPaneTab("Body");
this.SelectSubTab(subTab);
if (toTrash) {
cy.get(this._trashDelete).first().click();
cy.xpath(this._visibleTextSpan("Add more")).click();
}
this.agHelper.EnterValue(bKey, {
propFieldName: this._bodyKey(0),
directInput: true,
inputFieldName: "",
});
this.agHelper.PressEscape();
if (type) {
cy.xpath(this._bodyTypeDropdown).eq(0).click();
cy.xpath(this._visibleTextDiv(type)).click();
}
this.agHelper.EnterValue(bValue, {
propFieldName: this._bodyValue(0),
directInput: true,
inputFieldName: "",
});
this.agHelper.PressEscape();
this.agHelper.AssertAutoSave();
}
RunAPI(
toValidateResponse = true,
waitTimeInterval = 20,
validateNetworkAssertOptions?: { expectedPath: string; expectedRes: any },
) {
this.agHelper.GetNClick(this._apiRunBtn, 0, true, waitTimeInterval);
toValidateResponse &&
this.agHelper.ValidateNetworkExecutionSuccess("@postExecute");
// Asserting Network result
validateNetworkAssertOptions?.expectedPath &&
validateNetworkAssertOptions?.expectedRes &&
this.agHelper.ValidateNetworkDataAssert(
"@postExecute",
validateNetworkAssertOptions.expectedPath,
validateNetworkAssertOptions.expectedRes,
);
}
SetAPITimeout(timeout: number) {
this.SelectPaneTab("Settings");
cy.xpath(this._queryTimeout).clear().type(timeout.toString(), { delay: 0 }); //Delay 0 to work like paste!
this.agHelper.AssertAutoSave();
this.SelectPaneTab("Headers");
}
ToggleOnPageLoadRun(enable = true || false) {
this.SelectPaneTab("Settings");
if (enable) this.agHelper.CheckUncheck(this._onPageLoad, true);
else this.agHelper.CheckUncheck(this._onPageLoad, false);
}
ToggleConfirmBeforeRunningApi(enable = true || false) {
this.SelectPaneTab("Settings");
if (enable) this.agHelper.CheckUncheck(this._confirmBeforeRunningAPI, true);
else this.agHelper.CheckUncheck(this._confirmBeforeRunningAPI, false);
}
SelectPaneTab(
tabName:
| "Headers"
| "Params"
| "Body"
| "Pagination"
| "Authentication"
| "Settings"
| "Response"
| "Errors"
| "Logs"
| "Inspect entity",
) {
this.agHelper.PressEscape();
this.agHelper.GetNClick(this._visibleTextSpan(tabName), 0, true);
}
SelectSubTab(
subTabName:
| "NONE"
| "JSON"
| "FORM_URLENCODED"
| "MULTIPART_FORM_DATA"
| "RAW",
) {
this.agHelper.GetNClick(this._bodySubTab(subTabName));
}
AssertRightPaneSelectedTab(tabName: RightPaneTabs) {
cy.xpath(this._rightPaneTab(tabName)).should(
"have.attr",
"aria-selected",
"true",
);
}
SelectRightPaneTab(tabName: RightPaneTabs) {
this.agHelper.GetNClick(this._rightPaneTab(tabName));
}
ValidateQueryParams(param: { key: string; value: string }) {
this.SelectPaneTab("Params");
this.agHelper.ValidateCodeEditorContent(this._paramKey(0), param.key);
this.agHelper.ValidateCodeEditorContent(this._paramValue(0), param.value);
}
ValidateHeaderParams(header: { key: string; value: string }) {
this.SelectPaneTab("Headers");
this.agHelper.ValidateCodeEditorContent(this._headerKey(0), header.key);
this.agHelper.ValidateCodeEditorContent(this._headerValue(0), header.value);
}
ValidateImportedHeaderParams(
isAutoGeneratedHeader = false,
header: { key: string; value: string },
index = 0,
) {
let keyValueName = "Header";
if (isAutoGeneratedHeader) {
keyValueName = "autoGeneratedHeader";
}
this.SelectPaneTab("Headers");
this.ValidateImportedKeyValueContent(
this._importedKey(index, keyValueName),
header.key,
);
this.ValidateImportedKeyValueContent(
this._importedValue(index, keyValueName),
header.value,
);
}
public ValidateImportedKeyValueContent(
selector: string,
contentToValidate: any,
) {
this.agHelper.GetNAssertElementText(
selector,
contentToValidate,
"have.text",
);
}
public ValidateImportedKeyValueOverride(index: number, isOverriden = true) {
let assertion = "";
if (isOverriden) {
assertion = "have.css";
} else {
assertion = "not.have.css";
}
cy.get(this._importedKey(index, "autoGeneratedHeader")).should(
assertion,
"text-decoration",
"line-through solid rgb(76, 86, 100)",
);
cy.get(this._importedValue(index, "autoGeneratedHeader")).should(
assertion,
"text-decoration",
"line-through solid rgb(76, 86, 100)",
);
}
ValidateImportedHeaderParamsAbsence(
isAutoGeneratedHeader = false,
index = 0,
) {
let keyValueName = "Header";
if (isAutoGeneratedHeader) {
keyValueName = "autoGeneratedHeader";
}
this.SelectPaneTab("Headers");
this.ValidateImportedKeyValueAbsence(
this._importedKey(index, keyValueName),
);
this.ValidateImportedKeyValueAbsence(
this._importedValue(index, keyValueName),
);
}
public ValidateImportedKeyValueAbsence(selector: string) {
this.agHelper.AssertElementAbsence(selector);
}
ReadApiResponsebyKey(key: string) {
let apiResp = "";
cy.get(this._responseBody)
.contains(key)
.siblings("span")
.invoke("text")
.then((text) => {
apiResp = `${text
.match(/"(.*)"/)?.[0]
.split('"')
.join("")} `;
cy.log("Key value in api response is :" + apiResp);
cy.wrap(apiResp).as("apiResp");
});
}
SwitchToResponseTab(tabIdentifier: string) {
cy.get(tabIdentifier).click();
}
public SelectAPIVerb(verb: "GET" | "POST" | "PUT" | "DELETE" | "PATCH") {
cy.get(this._apiVerbDropdown).click();
cy.xpath(this._verbToSelect(verb)).should("be.visible").click();
}
ResponseStatusCheck(statusCode: string) {
this.agHelper.AssertElementVisible(this._responseStatus);
this.agHelper.GetNAssertContains(this._responseStatus, statusCode);
}
public SelectPaginationTypeViaIndex(index: number) {
cy.get(this._paginationTypeLabels).eq(index).click({ force: true });
}
CreateAndFillGraphqlApi(url: string, apiName = "", queryTimeout = 10000) {
this.CreateGraphqlApi(apiName);
this.EnterURL(url);
this.agHelper.AssertAutoSave();
//this.agHelper.Sleep(2000);// Added because api name edit takes some time to reflect in api sidebar after the call passes.
cy.get(this._apiRunBtn).should("not.be.disabled");
if (queryTimeout != 10000) this.SetAPITimeout(queryTimeout);
}
CreateGraphqlApi(apiName = "") {
cy.get(this.locator._createNew).click({ force: true });
cy.get(this._blankGraphqlAPI).click({ force: true });
this.agHelper.ValidateNetworkStatus("@createNewApi", 201);
if (apiName) this.agHelper.RenameWithInPane(apiName);
cy.get(this._resourceUrl).should("be.visible");
}
}