我需要访问一个新的窗口,单击按钮后会弹出

时间:2019-02-15 20:47:06

标签: reactjs automated-tests qa cypress

有人可以帮忙吗?

这是我无法解决的问题。

我需要访问单击按钮后弹出的新窗口。

我使用cy.get('。app-card__content')。click()单击名为“创建新插件”的按钮。单击按钮后,将在新窗口中创建该应用程序,但我不知道如何获取该窗口并继续进行测试。

此新窗口URL是动态的,每当有人单击它时,App ID的数字就会增加。

例如。 > https://www.website.io/apps/app_name/standalone?id=DYNAMIC_ID_NUMBER

我能够登录该网站并创建一个新的应用程序,但是,我不知道如何使用URL来获取新窗口。

对不起,但是我仅使用Cypress 2天,这是我知道的一个新手问题。

任何帮助都非常欢迎。

干杯。

我无法使此代码正常工作

describe('window open', function () {
  beforeEach(function () {
    cy.visit('/index.html', {
      onBeforeLoad(win) {
        cy.stub(win, 'open').as('windowOpen')
      }
    })
  })

  it('see window open being called with url', function () {
    cy.get('#open-window').click()

    cy.get('@windowOpen').should('be.calledWith', 'page1.html')
  })
})


Here is the code I wrote:

// This test consists of Accessing Staging and Create a new App.
// Go to website.io.
describe('Staging Test v1', function(){
    it('Accessing Staging', function(){
        cy.visit('https://www.website.io')
        cy.wait(2000)
    })

    // Get login button.
    it('Get login button', function(){
        cy.get('.signed-out-container > .signInTab').click()
        cy.wait(2000)
    })

    // Fill out the modal and log in
    it('Fill out the modal and log in', function(){
        cy.get('#sign_in_email').type('user@website.io', {delay:110}).should('have.value', 'user@website.io')
        cy.wait(1000)

        cy.get('#new_sign_in_password').type('password', {delay:110}).should('have.value', 'password')
        cy.wait(1000)

        cy.get('#sign-in-submit').click()
        cy.wait(6000)
    })

    // Click on new Create New Plugin button.
    it('Click on create a new plugin button', function(){
        cy.get('.dashboard-header__create-new-container > .button').should('be.visible').click({force:true})
        cy.wait(2000)
    })

    // Search and create a new App
    it('Search and create a new App', function(){

        cy.get('.app-search__search-input').click({force:true}).type('App name', {force:true})
        cy.wait(3000)
        cy.get('.app-card__content').click()
        cy.wait(10000)
    })

    // Grab the new window to continue the test

    // Need to find out how to grab the new window passing along cookies and sessions to remain logged in.

    // Develop part 2 of the test


})

我需要创建一个新应用,该应用会在新窗口中弹出,抓住新窗口,然后继续其余测试。

在保存应用程序时,我还需要在新窗口中保持登录状态。

2 个答案:

答案 0 :(得分:1)

我刚刚解决了一个类似的问题。也许您可以将其用作参考。 就我而言,单击按钮后会打开新窗口,并且 url 是动态的。

cy.window().then(win => {
    cy.stub(win, 'open').as('windowOpen');
});

// you can try exclude the 'should' below
// in my code it worked without this 'should' first
// after merging the latest changes this part failed somehow though no change was made here
// after investigation I found that the stub argument was not ready immediately
// so I added 'should' here to wait the argument load
// before visiting the url contained within it

cy.get('@windowOpen').should('be.calledWith', Cypress.sinon.match.string).then(stub => {
    cy.visit(stub.args[0][0]);
    stub.restore;
});

答案 1 :(得分:0)

据我所知,在赛普拉斯测试中,您最多只能处理一个标签页/窗口。只需看一下文档的这一部分:

https://docs.cypress.io/guides/references/trade-offs.html#Multiple-tabs

当我面对这个问题时,我使用了它们提供的解决方法,因此您无需“抢”其他窗口;如果可以的话,请继续进行以下操作:

// We can remove the offending attribute - target='_blank'
// that would normally open content in a new tab.
  cy.get('.app-card__content').invoke('removeAttr', 'target').click()

当然,只有在元素确实具有该属性的情况下,您才可以使用它。

看看他们为该主题提供的其他解决方案:https://github.com/cypress-io/cypress-example-recipes/blob/master/examples/testing-dom__tab-handling-links/cypress/integration/tab_handling_anchor_links_spec.js