如何对赛普拉斯授权标头中收到的令牌执行“重定向”?

时间:2019-05-01 00:21:31

标签: javascript cypress

如何使用赛普拉斯授权标头中收到的令牌执行redirect?我正在使用Single Sign On Microsoft帐户执行测试。感谢有人能在这里阐明一些观点!

我现在遇到以下错误,cypress.json设置中的Refused to display " https://login.microsoftonline.com/9a76540e-bf7b-4a3f-...." in a frame because it set 'X-Frame-Options' to 'deny'. Cypress chrome browser displays below...Also my“ chromeWebSecurity”:false`

enter image description here

Cypress.Commands.add("loginRedirect", ()=>{
  return cy.request({
    method: 'POST',
    url: 'Url_here',
    headers: {
      'content-type': 'application/html',
      'server': "Kestrel",
      'accept': "*/*",
  },
    form: true,
    body: {
      "username": 'some_username',
      "password": "my_password",
      "grant_type": "password",
      "scope": "user.read openid offline_access profile",
      "client_id": "client_id_here",
      "client_secret": "some_secret ",
      "state": "12345",
      "response_type":"token"
    }
  }).then(response => {
    //I need to get the token from the response here
    expect(response.status).to.eq(200);
    const id_token = response.body.id_token;
    const client_info = response.body.access_token;
    const refresh_token = response.body.refresh_token;
    console.log(response);
    const token = jwt_decode(id_token)
    console.log(token);
    // I have tried so far ...
window.localStorage.setItem('accesstoken', id_token);
window.localStorage.setItem('Cookie', 'ARRAffinity=6431c4049d794a4845846c5c4074b4a99ca7608076e12415bf06a68dd41f2e24');
cy.visit('#id_token='+id_token+'&client_info='+client_info+'&refresh_token='+refresh_token);

   })
 })

1 个答案:

答案 0 :(得分:0)

我已经使用puppeteer工具完成了此操作。将CD复制到测试文件夹,然后使用puppeteer安装npm i puppeteer插件。然后使用以下代码创建文件getSiteToken.js,并将其保存在根文件夹中。

const puppeteer = require("puppeteer");
const test_file = require("fs");

puppeteer
  .launch({ headless: true, chromeWebSecurity: false, args: ['--no-sandbox'] })
  .then(async browser => {
    const page = await browser.newPage();
    await page.goto("desired_test_url");

    await page.waitFor(2000);
    await page.waitForSelector("input[name=loginInput]");
    await page.type("input[name=loginInput]", "test_email", {
      applyDelay: 50
    });

    await page.waitForSelector("input[type=submit]");
    await page.click("input[type=submit]");

    await page.waitForSelector("input[name=passwordInput]");
    await page.click("input[name=passwordInput]");

    await page.waitFor(2000);
    await page.type("input[name=passwordInput]", "test_password", {
      applyDelay: 50
    });

    await page.waitForSelector("input[type=submit]");
    await page.click("input[type=submit]");

    await applyDelay(5000);

    let tokenValues = await page.evaluate(() => {
      let values = {};
      for (var i = 0, len = localStorage.length; i < len; ++i) {
        values[localStorage.key(i)] = localStorage.getItem(localStorage.key(i));
      }
      return values;
    });

    test_file.writeFileSync(
      "savetokenData.json",
      JSON.stringify(tokenValues)
    );
    browser.close();
  });

function applyDelay(time) {
  return new Promise(function (resolve) {
    setTimeout(resolve, time);
  });
}

loadTokensValues文件中创建一个commands.js函数,并在测试内部进行调用。

Cypress.Commands.add('loadTokensValues', () => {
    cy.clearLocalStorage(); // ideally add this clearLocalStorage
    return cy.fixture('savetokenData.json').then(data => {
        const keys = Object.keys(data);
        keys.forEach(key => {
            window.localStorage.setItem(key, data[key]);
        });
    });
});

设置package.json文件中的条目:

"scripts": {
 "cy:run": "cypress run",
 "get-token-admin-site": "node getSiteToken.js && mv savetokenData.json cypress-admin-site/cypress/fixtures",
 "cy:open:admin-site": "npm run get-token-admin-site && cypress open -P cypress-admin-site --env configFile=admin-site",
 "cy:test-admin-site": "set CYPRESS_RETRIES=2 && npm run get-token-admin-site && cypress run -P cypress-admin-site --env configFile=cypress-admin-site --browser chrome",
 // rest of the entries in the ........
}

我的测试如下所示,并在beforeEach()中调用loadTokensValues()

context('Perform Test Actions',()=>{

    beforeEach(() => {
      cy.loadTokensValues();
      cy.saveLocalStorage();
      cy.visit('test_site_url');
    })

    it('Check whether the details are displaying',()=>{
      Cypress.currentTest.retries(1);
      // rest of the test code follows here for the admin-site
    })
})