admin管理员组

文章数量:1415468

In our tool we create a url which quiet a few parameters with values. And I want Cypress to check the contents of this url.

The example url is: /?action=create&type=sw&notifno=70432&repby=TRL&repres=ABC&geo=017&startloc=12345&notiftp=2021-06-15T08:06:42.379Z&scen=1.0&refno=1234567&awsrt=-&vrst=&sbst=&objtp=art&objtxt=&objfc=&tel=084123456&prio=4 Niet urgent&priost=&prioen=&wbi=&facts=&bgeb=AB-CD&bequi=

I have stored the url in 'href' variable but how i can now check all the attr and their values? I really don't have a clue.

In our tool we create a url which quiet a few parameters with values. And I want Cypress to check the contents of this url.

The example url is: http://someUrl./sap/?action=create&type=sw&notifno=70432&repby=TRL&repres=ABC&geo=017&startloc=12345&notiftp=2021-06-15T08:06:42.379Z&scen=1.0&refno=1234567&awsrt=-&vrst=&sbst=&objtp=art&objtxt=&objfc=&tel=084123456&prio=4 Niet urgent&priost=&prioen=&wbi=&facts=&bgeb=AB-CD&bequi=

I have stored the url in 'href' variable but how i can now check all the attr and their values? I really don't have a clue.

Share Improve this question asked Jun 15, 2021 at 9:57 Jasper SlokkerJasper Slokker 952 silver badges14 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

I'd parse it into an object and then use .wrap(), .its(), and .should() mands:

const url = "http://someUrl./sap/?action=create&type=sw&notifno=70432&repby=TRL&repres=ABC&geo=017&startloc=12345&notiftp=2021-06-15T08:06:42.379Z&scen=1.0&refno=1234567&awsrt=-&vrst=&sbst=&objtp=art&objtxt=&objfc=&tel=084123456&prio=4 Niet urgent&priost=&prioen=&wbi=&facts=&bgeb=AB-CD&bequi=";
const arr = url.split('/?')[1].split('&');
const paramObj = {};
arr.forEach(param => {
  const [ key, value ] = param.split('=');
  paramObj[key] = value;
});

cy
  .wrap(paramObj)
  .its('tel')
  .should('eq', '084123456');

or if you want to assert more properties:

cy
  .wrap(paramObj)
  .then(obj => {
    expect(obj.notifno).to.eq('70432');
    expect(obj.tel).to.eq('084123456');
  });

My colleague came with this solution, now the Cucumber line included:

Given('I expect the parameter {string} of the SAP-link on dossier {string} to equal {string}',(parameter:string, dossier:string, value:string) => {
    cy.get('selector').each(ele => {
        if(ele.text().trim().indexOf(dossier) == 0) {
            cy.get('selector')
                .parents('selector')
                .find('selector').should('have.attr', 'href').then((sapUrl: JQuery<HTMLElement>) => {
                cy.log(sapUrl.toString());
                const queryParamString: string = sapUrl.toString().split('?')[1];
                cy.log(queryParamString);
                const queryParamArray: string[] = queryParamString.split('&');
                var params: {} = {};
                queryParamArray.forEach((keyValueString: string) => {
                    const currentParamArray: string[] = keyValueString.split('=');
                    params[currentParamArray[0]] = currentParamArray[1];
                });

                // Actual param check
                expect(params[parameter]).to.equal(value);
            });
        }
    });
});

本文标签: javascriptcheck value of parameters in URL with CypressStack Overflow