赛普拉斯会自动重定向到新URL

时间:2019-07-23 08:53:21

标签: javascript cypress

我编写了其中一个网页的一些测试方案,但是在自动重定向到一个特定的ng-container的登录页面期间,我仍然遇到问题。我有3个测试场景,并且按顺序运行都没关系-总是在第一个或第二个包含登录数据的cookie被删除并将用户重定向到登录页面之后运行。有什么方法可以防止重定向到另一个页面并删除Cookie?

import LoginPage from "xxx/LoginPage.spec.js";
import CreateUser from "xxx/CreateUser.spec.js";
import Functions from "Cxxx/Functions.spec.js";

describe('Check role selector', () => {

    const login = new LoginPage();
    const register = new CreateUser();
    const functions = new Functions();

    var usernameCreatedUser = functions.createUsernameAndNick();
    var nickCreatedUser = functions.createUsernameAndNick();

    before(() => {
        cy.visit('login')
        login.fillUsername(Cypress.env('correctUsername'))
        login.fillPassword(Cypress.env('correctPassword'))
        login.checkButtonLogin().should('be.enabled')
            .click()
        login.fillSMScode(Cypress.env('correctSMScode'))
        login.checkButtonLogin().should('be.enabled')
            .click()
        cy.visit('/control-panel/users/add')
        register.fillUsername(usernameCreatedUser)
        register.fillNick(nickCreatedUser)
        register.fillPassword(functions.createPassword())
        register.fillDivision(0)
        register.fillRole(0)
    })



    it('empty', () => {
        register.clearDivision()
        register.getButtonSave().should('be.disabled')
    })

    it('correct - select 2 divisions - only second is selected', () => {
        register.clearDivision()
        register.fillDivision(0)
        register.fillDivision(1)
        register.getDivision("Gdańsk", "Warszawa")
        register.fillDivision(1)
        register.getButtonSave().should('be.enabled')
        debugger 
    })

    it('correct - select 1 division', () => {        
        register.clearDivision()
        register.fillDivision(0)
        register.getDivision("Warszawa", "Gdańsk")
        register.getButtonSave().should('be.enabled')
        debugger
    })

})


class RegisterPage {

  fillDivision(value) {
    cy.get('[formcontrolname="divisionId"]').click()
    cy.get('[class="ng-option-label"]')
      .eq(value)
      .click()
  }

  clearDivision(){
    cy.get('[title="Clear all"]').eq(0)
    .should('be.visible')
    .click()
    cy.get('[formcontrolname="divisionId"]').contains('Wybierz')
    this.getDivisionError()
  }

  getDivision(value1, value2){
    cy.get('[role="combobox"]').contains(value1).should("have.not.value", value2)
  }

  getButtonSave() {
    return cy.get('button').contains('Zapisz')
  }
}
export default RegisterPage;


Console:

cypress_runner.js:174498 console.clear() was prevented due to 'Preserve log'
cypress_runner.js:174490 Command:   get
cypress_runner.js:174490 Yielded:   
(2) [span.ng-clear-wrapper, span.ng-clear-wrapper]
cypress_runner.js:174490 Elements:  2
cypress_runner.js:174490 Selector:  [title="Clear all"]
[Violation] 'click' handler took 213ms
cypress_runner.js:174498 console.clear() was prevented due to 'Preserve log'
cypress_runner.js:174490 Command:     eq
cypress_runner.js:174490 Selector:    0
cypress_runner.js:174490 Applied To:  
(2) [span.ng-clear-wrapper, span.ng-clear-wrapper]
cypress_runner.js:174490 Yielded:     
cypress_runner.js:174490 Elements:    1
[Violation] 'click' handler took 191ms
VM561 login:1 [DOM] Input elements should have autocomplete attributes (suggested: "current-password"): 
cypress_runner.js:174498 console.clear() was prevented due to 'Preserve log'
cypress_runner.js:174490 Command:  assert
cypress_runner.js:174490 Subject:  
jQuery.fn.init [span.ng-clear-wrapper, selector: "0", context: document]
cypress_runner.js:174490 Message:  expected <span.ng-clear-wrapper> to be visible
[Violation] 'click' handler took 227ms
cypress_runner.js:174498 console.clear() was prevented due to 'Preserve log'
cypress_runner.js:174490 Command:     click
cypress_runner.js:174490 Applied To:  
cypress_runner.js:174490 Elements:    1
cypress_runner.js:174490 Coords:      
{x: 867, y: 370}
cypress_runner.js:174542 MouseDown
[Violation] 'click' handler took 231ms
cypress_runner.js:174498 console.clear() was prevented due to 'Preserve log'
cypress_runner.js:174490 Event:        xhr
cypress_runner.js:174490 Method:       GET
cypress_runner.js:174490 URL:          https://xxx/admin-api/division-managed-objects
cypress_runner.js:174490 Status:       401 (Unauthorized)
cypress_runner.js:174490 Duration:     86
cypress_runner.js:174490 Stubbed:      No
cypress_runner.js:174490 Request:      
{headers: {…}, body: null}
cypress_runner.js:174490 Response:     
{headers: {…}, body: 401}
cypress_runner.js:174490 XHR:          
XMLHttpRequest {method: "GET", url: "https:/xxx/admin-api/division-managed-objects", id: "xhr330", __zone_symbol__ON_PROPERTYreadystatechange: ƒ, __zone_symbol__readystatechangefalse: Array(1), …}
cypress_runner.js:174498 console.clear() was prevented due to 'Preserve log'
cypress_runner.js:174490 Event:           new url
cypress_runner.js:174490 New Url:         https:/xxx/login
cypress_runner.js:174490 Url Updated By:  pushState
cypress_runner.js:174490 Args:            
(3) [{…}, "", "/login"]
cypress_runner.js:174498 console.clear() was prevented due to 'Preserve log'
cypress_runner.js:174490 Command:   get
cypress_runner.js:174490 Elements:  0
cypress_runner.js:174490 Selector:  [formcontrolname="divisionId"]
cypress_runner.js:174490 Error:     CypressError: Timed out retrying: Expected to find element: '[formcontrolname="divisionId"]', but never found it.


1 个答案:

答案 0 :(得分:1)

添加cookie白名单似乎有所帮助:

  Cypress.Cookies.defaults({
                whitelist: 'AuthUser'
            })

但是据我所知,赛普拉斯应该仅在特定测试文件中的所有测试方案之后才取消检查,而不是每个测试方案都没有?