使用cypress访问网站在测试中提供的JavaScript变量

使用cypress访问网站在测试中提供的JavaScript变量,javascript,cypress,Javascript,Cypress,我想知道是否有可能访问我正在测试的页面在JS中提供的变量 网站提供Javascript: var varyingUrl = 'www.google.com'; cy.window().then((win) => { // use this area to code using the variable, prepending with "win." eg. cy.log(win.varyingUrl) // or console.log(win.va

我想知道是否有可能访问我正在测试的页面在JS中提供的变量

网站提供Javascript:

var varyingUrl  = 'www.google.com';
cy.window().then((win) => {
    // use this area to code using the variable, prepending with "win." eg.
    cy.log(win.varyingUrl) // or console.log(win.varyingUrl)

})
'www.google.com'
柏树测试

Cypress.Commands.add('closeAccount', () => {
    var url = varyingUrl;
    cy.request(url)
})
var varyingUrl  = 'www.google.com';

我几乎可以肯定这是不可能的,但我想如果有人找到了使用cypress访问dom可用javascript变量的方法,我会问这个问题。这个答案最初是为一个回答者找到的-他的答案可以是

本质上,我们只需使用cypress调用窗口,然后使用该窗口访问变量

使用上面的示例,您可以获得通过js源文件加载服务器的变量:

解决方案 要获取的变量

Cypress.Commands.add('closeAccount', () => {
    var url = varyingUrl;
    cy.request(url)
})
var varyingUrl  = 'www.google.com';
柏树代码:

var varyingUrl  = 'www.google.com';
cy.window().then((win) => {
    // use this area to code using the variable, prepending with "win." eg.
    cy.log(win.varyingUrl) // or console.log(win.varyingUrl)

})
'www.google.com'
控制台输出:

var varyingUrl  = 'www.google.com';
cy.window().then((win) => {
    // use this area to code using the variable, prepending with "win." eg.
    cy.log(win.varyingUrl) // or console.log(win.varyingUrl)

})
'www.google.com'

假设该变量是全局变量,或者可以通过全局变量访问