I am trying to write my first Cypress test. To start with, I need to create a Sales Order. I want to select the Customer, Items, etc. randomly.
I tried something like this as a first proof of concept using a fixed name for the Customer (which already exists in the system).
it("Create Sales Order", () => {
cy.login("Administrator");
cy.visit("/app/sales-order/new-sales-order-1");
cy.intercept("POST", "/api/method/frappe.client.validate_link").as("validate_link");
cy.get(".frappe-control[data-fieldname=customer] input").type("Dennis Ritchie", { delay: 1000 });
cy.wait("@validate_link");
});
The problem is, as soon as it starts typing, the field is getting cleared and instead of āDennis Ritchieā, I end up with āeā (last letter). Iām not sure what to do here. Looking at the requests, I think it is getting cleared as it validates the link. Not sure how to handle this.
Ideally, I want to search from the dropdown menu (which also seems to disappear) and pick a random one. I looked at the examples in the integrations folder but I did not find anything specific to this. Iād imagine this is a pretty common thing to do in tests, but Iām not sure how to solve this.
Iāve tried adding some delays but that did not work either. Because of this, I am unable to write any tests to test the functional side.
What am I doing wrong?
snv
August 26, 2023, 3:14pm
2
Hello,
Take a look at this test:
{
label: "Select Gender",
fieldname: "link",
fieldtype: "Link",
options: "Gender",
},
],
});
}
it("should set the valid value", () => {
get_dialog_with_link().as("dialog");
cy.insert_doc(
"Property Setter",
{
doctype: "Property Setter",
doc_type: "ToDo",
property: "show_title_field_in_link",
property_type: "Check",
doctype_or_field: "DocType",
In the beforeEach
function above, a new ToDo is being created for each test.
Within the test, observe that:
weāre setting focus on the input element first.
weāre waiting for search_link
to complete before continuing.
then weāre typing with a delay.
then waiting again for relevant results.
then selecting the first result.
However, for the form view, you will have to additionally ensure that the above actions happen after focus_on_first_input
called:
() => this.refresh_fields(),
// call trigger
() => this.script_manager.trigger("refresh"),
// call onload post render for callbacks to be fired
() => {
if (this.cscript.is_onload) {
this.onload_post_render();
return this.script_manager.trigger("onload_post_render");
}
},
() => this.cscript.is_onload && this.is_new() && this.focus_on_first_input(),
() => this.run_after_load_hook(),
() => this.dashboard.after_refresh(),
]);
} else {
this.refresh_header(switched);
}
this.$wrapper.trigger("render_complete");
frappe.after_ajax(() => {
One way to do that is to attach a ācallbackā in frappe.route_hooks.after_load
that gets triggered here:
// call trigger
() => this.script_manager.trigger("refresh"),
// call onload post render for callbacks to be fired
() => {
if (this.cscript.is_onload) {
this.onload_post_render();
return this.script_manager.trigger("onload_post_render");
}
},
() => this.cscript.is_onload && this.is_new() && this.focus_on_first_input(),
() => this.run_after_load_hook(),
() => this.dashboard.after_refresh(),
]);
} else {
this.refresh_header(switched);
}
this.$wrapper.trigger("render_complete");
frappe.after_ajax(() => {
$(document).ready(() => {
Hope this helps.
EDIT: if you just want to set a value quickly, cur_frm.set_value
might do the trick (but it doesnāt mimic user behavior).
2 Likes
snv
August 26, 2023, 4:07pm
3
I fixed the issue with losing manually set focus here:
frappe:develop
ā resilient-tech:fix-focus-on-first
opened 04:02PM - 26 Aug 23 UTC
### Before
![fix focus before](https://github.com/frappe/frappe/assets/163156ā¦ 50/ce096aef-f81e-46b8-aa01-d46de0f8f634)
### After
![fix focus after](https://github.com/frappe/frappe/assets/16315650/accc4c6d-c6ef-4689-9f81-501c0d17da7c)
Also causes issue detailed here:
https://discuss.frappe.io/t/how-do-i-randomly-select-from-a-link-field-in-a-cypress-test/109068
You neednāt worry about focus_on_first_input
in your Cypress test after above PR gets merged.
Confirmed working. Thank you!