How do I randomly select from a Link field in a Cypress test?

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?

Hello,

Take a look at this test:

  • 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:

One way to do that is to attach a ā€œcallbackā€ in frappe.route_hooks.after_load that gets triggered here:

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

I fixed the issue with losing manually set focus here:

You neednā€™t worry about focus_on_first_input in your Cypress test after above PR gets merged.

Confirmed working. Thank you!