Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/406.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 是否可以在Cypress中提示的文本框中键入并单击ok按钮_Javascript_Cypress - Fatal编程技术网

Javascript 是否可以在Cypress中提示的文本框中键入并单击ok按钮

Javascript 是否可以在Cypress中提示的文本框中键入并单击ok按钮,javascript,cypress,Javascript,Cypress,是否可以使用Cypress命令并单击okinside alert,在警报中显示的文本框中键入内容。下面的测试显示警报消息,但没有执行任何操作 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')

是否可以使用Cypress命令并单击
ok
inside alert,在
警报中显示的
文本框中键入内容。下面的测试显示警报消息,但没有执行任何操作

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");
    }
}

所以现在根据Cypress的文档,您可以使用存根来更改这样的提示行为

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')

检查进一步的信息

我得到以下CypressError:超时重试:应该找到元素:'.name',但从未找到它。这里的
.name
只是一个示例,您可以在任何地方从提示符返回消息。您可以在那里使用任何选择器。我已经在那里编辑了我的答案。所以,我们不能在警报中的“文本框”字段中键入返回的文本,然后单击警报中的“确定”按钮吗?因此,我们正在修改使用提示的行为。因此,基本上,无论您想在文本框中键入什么,都可以将其替换为
my custom message
。在处理窗口事件的Cypress检查中,它将显示相同的行为。确认中的文本已使用
cy.stub(win,'prompt')传递。返回('my custom message')
this
cy.get('any_selector')。应该('have.value','my custom message')
这用于检查是否传递了正确的文本,您可以跳过此部分。