用赛普拉斯解析令牌

时间:2019-03-06 22:12:31

标签: parsing testing token cypress csrf-token

我该死的沮丧。 我在测试和javascript中都是新手,但我必须为我的单身汉编写测试。

使我正在测试的Web应用程序具有登录空间,您可以在其中购买不同的产品。我可以得到两个不同的令牌。第一个与我的用户ID有关,第二个与“购物车”有关(它不是真正的购物车,因为购买过程是投资过程,而产品是房地产)

所以赛普拉斯建议这样做

    cy.request('POST', 'https://sso.corp.com/auth', { username: 'foo', password: 'bar' })
  .then((response) => {
    // pull out the location redirect
    const loc = response.headers['Location']

    // parse out the token from the url (assuming its in there)
    const token = parseOutMyToken(loc)

    // do something with the token that your web application expects
    // likely the same behavior as what your SSO does under the hood
    // assuming it handles query string tokens like this
    cy.visit('http://localhost:8080?token=' + token)

    // if you don't need to work with the token you can sometimes
    // just visit the location header directly
    cy.visit(loc)
  })

我这样做了:

  it('strategy #1: parse token from HTML', function () {
            // if we cannot change our server code to make it easier
            // to parse out the CSRF token, we can simply use cy.request
            // to fetch the login page, and then parse the HTML contents
            // to find the CSRF token embedded in the page
            cy.request()
                .its('body')
                .then((body) => {
                    // we can use Cypress.$ to parse the string body
                    // thus enabling us to query into it easily
                    const $html = Cypress.$(body)
                    const csrf = $html.find('#user_profile__token[name=user_profile[_token]]').val()

                    cy.loginByCSRF(csrf)
                        .then((resp) => {
                            expect(resp.status).to.eq(200)
                            expect(resp.body).to.include("<h2>dashboard.html</h2>")
                        })
                })
        });

尽管我在github页面上找到了它,但它没有解决。 你们有谁知道吗?

用户令牌ID:input#user_profile__token[name=user_profile[_token]]

产品令牌ID:input#product_filter__token[name=product_filter[_token]]

该任务不仅要解析令牌,还要在cypress中测试时保存它们。

请帮助

我的代码导致401错误:

import * as LogConst from 'C:\\Users\\Kristi\\Desktop\\BATests\\tests\\cypress\\fixtures\\Login_Data.json'

describe('Login & Selection of Investment AIF Dr. Peters', function () {

    before(function () {
        cy.visit('https://' + LogConst.server.name + ':' + LogConst.server.password + '@dev.capitalpioneers.de/')
        cy.contains('Login').click();
        cy.url()
            .should('include', '/login')});

      it('logs in', function () {
            cy.get('#email.form-control')
                .type(LogConst.TestUserCostumer.usercos);
            cy.get('#password.form-control')
                .type(LogConst.TestUserCostumer.usercospass);
                      cy.contains('Anmelden').click();
            cy.url().shoul('include','/investor');
        });

     it('product selection the Dr. Peters Product', function () {
            cy.contains('Produkt').click();
            cy.url()
                .should('include', '/produkte');


 it('selects Dr. Peters AIF Product', function () {


            cy.contains('Dr. Peters').click();
            cy.url().should('include', '\/produkt/hotelimmobilie-aachen/');
            cy.get('#sum_slider[type=range]')
                .invoke('val', 50000)
                .trigger('change')
        });


 it('download nessecary docs', function () {
            cy.get('#ga-btn-invest-now-product-detail-hotelimmobilie-aachen').click();
            cy.contains('Fondsprospekt').click();
            cy.contains('Wesentlichen Anlegerinformationen').click()
    });

   it('sets the right checks', function () {
           cy.get('#pre_check_inGermany').click({force: true});
           cy.get('#pre_check_readDocument1').click({force: true});
           cy.get('#pre_check_readDocument2').click({force: true});
           cy.get('button').contains('Weiter').click();
    });

});

1 个答案:

答案 0 :(得分:0)

因此,真正的应用程序永远不会在HTML中存储令牌,因此我认为这就是您遇到麻烦的原因。

如果令牌需要以localStorage结尾,则可以尝试以下

cy.visit(...)
cy.get('input#user_profile__token').then( $el => {
  const token =$el.attr('name')
  // Parse the token if needed, if it's not in the correct format
  window.localStorage.setItem('user_token', token)

})

但是,再次重申,您似乎甚至不打算尝试使用令牌。