cy.url() and/or cy.location('href') 不 return 一个字符串
cy.url() and/or cy.location('href') does not return a string
我有一个编辑页面。当我添加任何内容并单击 "Save" 按钮时,我的 URL 将发生变化,在 URL 中添加一个随机 ID。我想在每次单击 "Save button" 时检查我的 ID 是否发生变化。
我将 URL 结果保存在变量中并想检查它,我这样做:
const currentURL = cy.url();
cy.get('.editor-toolbar-actions-save').click();
cy.url().should('not.eq', currentURL);
但是我的 currentURL
变量的类型不是字符串:
expected http://localhost:8080/editor/37b44d4d-48b7-4d19-b3de-56b38fc9f951 to not equal { Object (chainerId, firstCall) }
如何使用我的变量?
这些命令return 是可链接的类型,而不是像字符串这样的原始值,因此将它们分配给变量需要对 'extract' 字符串采取进一步的操作。
为了得到url字符串,你需要做
cy.url().then(urlString => //do whatever)
tl;博士
Cypress commands are asynchronous, you have to use then
使用他们的产量。
cy.url().then(url => {
cy.get('.editor-toolbar-actions-save').click();
cy.url().should('not.eq', url);
});
说明
有人在 GitHub, and the official document on aliases 上提出了类似的问题,非常详细地解释了这种现象:
You cannot assign or work with the return values of any Cypress command. Commands are enqueued and run asynchronously.
也显示了解决方案:
To access what each Cypress command yields you use .then()
.
cy.get('button').then(($btn) => {
// $btn is the object that the previous
// command yielded us
})
查看 core concepts docs's section on asynchronicity 也是一个好主意。
参考下面的代码片段,在这里你可以获取当前的URL并将其存储在一个变量中,通过cy.log()
进行打印
context('Get Current URL', () => {
it('Get current url and print', () => {
cy.visit('https://docs.cypress.io/api/commands/url')
cy.url().then(url => {
const getUrl = url
cy.log('Current URL is : '+getUrl)
})
})
})
我一直遇到同样的问题,到目前为止最一致的方法是将 URL 保存到文件并在您需要再次访问它时从文件中读取它:
//store the url into a file so that we can read it again elsewhere
cy.url().then(url => {
const saveLocation = `cypress/results/data/${Cypress.spec.name}.location.txt`
cy.writeFile(saveLocation, getUrl)
})
//elsewhere read the file and do thing with it
cy.readFile(`cypress/results/data/${Cypress.spec.name}.location.txt`).then((url) => {
cy.log(`returning back to editor ${url}`)
cy.visit(url)
})
我有一个编辑页面。当我添加任何内容并单击 "Save" 按钮时,我的 URL 将发生变化,在 URL 中添加一个随机 ID。我想在每次单击 "Save button" 时检查我的 ID 是否发生变化。
我将 URL 结果保存在变量中并想检查它,我这样做:
const currentURL = cy.url();
cy.get('.editor-toolbar-actions-save').click();
cy.url().should('not.eq', currentURL);
但是我的 currentURL
变量的类型不是字符串:
expected http://localhost:8080/editor/37b44d4d-48b7-4d19-b3de-56b38fc9f951 to not equal { Object (chainerId, firstCall) }
如何使用我的变量?
这些命令return 是可链接的类型,而不是像字符串这样的原始值,因此将它们分配给变量需要对 'extract' 字符串采取进一步的操作。
为了得到url字符串,你需要做
cy.url().then(urlString => //do whatever)
tl;博士
Cypress commands are asynchronous, you have to use then
使用他们的产量。
cy.url().then(url => {
cy.get('.editor-toolbar-actions-save').click();
cy.url().should('not.eq', url);
});
说明
有人在 GitHub, and the official document on aliases 上提出了类似的问题,非常详细地解释了这种现象:
You cannot assign or work with the return values of any Cypress command. Commands are enqueued and run asynchronously.
也显示了解决方案:
To access what each Cypress command yields you use
.then()
.cy.get('button').then(($btn) => { // $btn is the object that the previous // command yielded us })
查看 core concepts docs's section on asynchronicity 也是一个好主意。
参考下面的代码片段,在这里你可以获取当前的URL并将其存储在一个变量中,通过cy.log()
进行打印context('Get Current URL', () => {
it('Get current url and print', () => {
cy.visit('https://docs.cypress.io/api/commands/url')
cy.url().then(url => {
const getUrl = url
cy.log('Current URL is : '+getUrl)
})
})
})
我一直遇到同样的问题,到目前为止最一致的方法是将 URL 保存到文件并在您需要再次访问它时从文件中读取它:
//store the url into a file so that we can read it again elsewhere
cy.url().then(url => {
const saveLocation = `cypress/results/data/${Cypress.spec.name}.location.txt`
cy.writeFile(saveLocation, getUrl)
})
//elsewhere read the file and do thing with it
cy.readFile(`cypress/results/data/${Cypress.spec.name}.location.txt`).then((url) => {
cy.log(`returning back to editor ${url}`)
cy.visit(url)
})