是否可以在赛普拉斯提示符中显示的文本框中输入内容,然后单击“确定”按钮

时间:2018-12-18 00:50:25

标签: javascript cypress

是否可以使用Cypress命令在textbox内显示的alert内键入内容,然后单击警报内的ok。下面的测试显示警报消息,但未执行任何操作。

describe('Type some text inside the textbox in an alert', function() {
  it.only('Type some text inside the textbox test', function() {
    cy.visit('http://sometesturl.com')
    const stub = cy.stub()
     cy.on ('window:alert', stub)
     cy
      .get('#pidforinput').click()
      .then(($stub) => {
      const myWin = $stub.find('input');
      cy.wrap(myWin ).type("This is a text box");
    })
  })
})

//以下是我的js代码,用于在其中显示警报和文本框

function promptMessage() {
    var favColor = prompt("What is your favorite color?", "");
    if (favColor != null){      
    alert("Your favorite color is " + favColor);        
    }
    else {
    alert("You did not specify your favorite color");
    }
}

<input id="pidforinput" type="button" onclick="promptMessage()" value="Click here to specify your favorite color" />

1 个答案:

答案 0 :(得分:1)

现在,根据赛普拉斯的文档,您可以使用存根更改此类提示的行为

cy.visit('http://localhost:3000', {
  onBeforeLoad(win) {
    cy.stub(win, 'prompt').returns('my custom message')
  }
})

cy.window().its('prompt').should('be.called')
cy.get('any_selector').should('have.value', 'my custom message')

检查this以获取更多信息