代码之家  ›  专栏  ›  技术社区  ›  soccerway

是否可以在Cypress的提示中显示的文本框内键入并单击OK按钮?

  •  2
  • soccerway  · 技术社区  · 6 年前

    可以在里面打字吗 textbox 显示在 alert 使用Cypress命令并单击 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 回复  |  直到 6 年前
        1
  •  1
  •   Nimish Gupta    6 年前

    所以现在根据柏树的文档,你可以使用树桩来改变提示的行为,就像这样

    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 更多信息