Extract A Part Of The Current Url Using A Regular Expression

Опубликовано: 29 Сентябрь 2022
на канале: gleb bahmutov
2,358
41

We can grab a part of the URL using a named capture group, then save the value using an alias. Later we can get the value using "this.alias" or "cy.get(alias)" commands.

Imagine we are trying to confirm the application URL "/sell/item10001/confirmation"

// yields a string
cy.location('pathname')
.should('match', /\/sell\/item\d+\/confirmation/)
.invoke('match', /\/sell\/item(? id \d+)\/confirmation/)
.its('groups.id', { timeout: 0 })
.should('be.a', 'string')
.as('itemId')
cy.log('printing the id').then(function () {
cy.log(`item id is: ${this.itemId}`)
})
cy.get('@itemId').then(Number).should('be.within', 1_000, 20_000)

Find the full example at https://glebbahmutov.com/cypress-exam...


Смотрите видео Extract A Part Of The Current Url Using A Regular Expression онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь gleb bahmutov 29 Сентябрь 2022, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 2,358 раз и оно понравилось 41 людям.