使用赛普拉斯处理Windows确认弹出窗口

时间:2020-05-05 12:47:00

标签: javascript cypress

我正在艰难地学习赛普拉斯:在具有框架:(

我读到赛普拉斯自动接受警报,但在这里我有一个确认弹出窗口,需要用户输入。但是,我正在努力关闭此窗口以确认pop ip询问“取消”或“确定”。

触发弹出窗口的元素位于一个框架(不是iFrame)中,如下所示:

<a href="/tasksgui/manageScheduledJobs.do?userAction=runnow&amp;userAction=select&amp;selectedIndex=1&amp;formContextKey=ListChunk[SchedulerJobsSearchResults]{1588676256461}1" 
onclick="return(confirmRunNow())" ;>
RunJobs</a>

我知道Cypress API提供了一种处理这些问题的方法:

cy.on('window:confirm', (str) => {
    //code here
}

但是我不清楚如何将其合并到我的测试块中”

it('gets the post', (done) => {
cy.visit('http://myapp.co.uk/mygui/index.jsp');
getLeftFrameBody().findByText('Manage Tasks').click();
cy.wait(2000)
getContentFrameBody().should('include.text', 'Scheduled Tasks')

getContentFrameBody().findByText('Task Name');
getContentFrameBody().find('input[name="jobName"]').type('Task one');
getContentFrameBody().findByText('Search').click();
cy.wait(2000)
cy.on('window:confirm', function(confirmText){
  return true
});

getContentFrameBody().find('.resultrowone').find('a').eq(5).click();
})

1 个答案:

答案 0 :(得分:0)

通过使函数异步,您可以等待窗口确认,然后继续,如下面的示例所示。

it('gets the post', async (done) => {
  cy.visit('http://companyapp.co.uk/mygui/index.jsp');
  getLeftFrameBody().findByText('Manage Tasks').click();
  cy.wait(2000)
  getContentFrameBody().should('include.text', 'Scheduled Tasks')

  getContentFrameBody().findByText('Job Name');
  getContentFrameBody().find('input[name="jobName"]').type('runTasks');
  getContentFrameBody().findByText('Search').click();
  cy.wait(2000);

  await new Promise(resolve => {
    cy.on('window:confirm', (str) => {
      resolve();
    });

    getContentFrameBody().find('.resultrowone').find('a').eq(5).click();
  });

  // Continue after window was confirmed
});