Cypress: Error: Syntax error, unrecognized expression – How to escape
Fabian Blechschmidt
I’m writing Cypress tests for our first hopefully-soon-to-be-purchased Shopware plugin and stumbled over:
Error: Syntax error, unrecognized expression: a[href=#/sw/product/create]
The problem? Cypress is using jQuery internally and therefore you need to escape the parameter for cy.get properly.
Thankfully K K Agrawal wrote a blogpost about how to do that! He added a method to do it but unfortunately no easy to read list of all the characters which needs to be escaped, so here is our list.
Escaping works with one backslash, like \.!
# So my
cy.get('a[href^="#/sw/product/create"]').click();
# needed to be
cy.get('a\[href\^\=\"\#\/sw\/product\/create\"\]').click();
P.S. I don’t know why, but my tests with Cypress.$.escapeSelector() didn’t work ?
Other articles from this category
Shopware 6 Hidden Gems #16: CloneBehavior — entity duplication with overrides, one call
You know the admin’s „duplicate“ button on products. Now the ticket says: „We need that, but from code — duplicate a product per sales channel, with adjusted name and its own product number.“ Or: „Campaign landing pages: clone this CMS page 20 times with different headlines.“ I’ve watched this get implemented as: load entity with […]
Shopware 6 Hidden Gems #15: ChangeSet — before/after values for every write, no second query
The requirement sounds harmless every time: „Log when a product price changes, with old and new value.“ Or: „When an order’s email changes, notify the old address.“ Or the audit-trail classic: „Who changed what, from what, to what?“ And every time, the naive implementation is the same sad shape: subscribe to product.written, load the entity […]
Shopware 6 Hidden Gems #14: WriteProtected & ApiAware — field-level security without a single subscriber
A code review scenario. A plugin adds a purchasePrice-style field to a custom entity — internal margin data. The review question: „Can the Store API read this?“ Followed by: „Can the Admin API write that computed field you recalculate in a subscriber?“ The answers, in the plugin at hand, were „yes“ and „yes, and then […]