Dav*_*ngo 3 testing url automation cypress
我有一个网址https://www.xxx/xxxx/xxx/1234。我只需要数字 1234 并将它们存储在我的全局变量中。
cy.log(this.href.substring(this.href.lastIndexOf('/') + 1));
Run Code Online (Sandbox Code Playgroud)
URL 的最后一部分(slug)最容易像这样获得
const url = 'https://www.xxx/xxxx/xxx/1234'
const slug = url.split('/').pop()
expect(slug).to.eq('1234') // ok
Run Code Online (Sandbox Code Playgroud)
您要存储它的位置取决于您将如何使用它(即在同一测试中、同一规范文件中的跨测试、跨规范文件)。
你提到全局变量,那就是这样的
let slug; // define empty global variable
it('gets the slug', () => {
cy.visit('/')
cy.url().then(url => slug = url.split('/').pop())
//log in same test
cy.then(() => { // MUST use .then() to avoid "closure" issues
cy.log(slug) // logs 1234
})
})
it('uses slug', () => {
// log in different test
cy.log(slug) // logs 1234
})
Run Code Online (Sandbox Code Playgroud)