Skip to content

Commit

Permalink
[patch] [34] issue - missing assertions (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmisty committed Oct 1, 2023
1 parent a40bd51 commit 5e97ee4
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 12 deletions.
150 changes: 150 additions & 0 deletions integration/e2e/assertions/assertions.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import { visitHtml } from '../../common/helper';

describe('assertions', () => {
beforeEach(() => {
//visitHtml();
visitHtml({
body: `
<div data-test-id="item">
<div>Apple</div>
<div data-test-id="other-item">Hello</div>
</div>
</br>
<div data-test-id="item">
<div>Banana</div>
<div data-test-id="other-item">Buy</div>
</div>
</br>
<div data-test-id="item">
<div>Orange</div>
<div data-test-id="other-item">Is Round</div>
</div>
<div data-qa-id="task-card-title">
<div>Task card</div>
</div>
`,
script: `
// add element after some time
setTimeout(()=> {
var child = document.createElement("div");
child.setAttribute('data-test-id','item');
child.innerHTML = '<div>Lichi</div><div data-test-id="other-item">Fruit</div>';
document.body.appendChild(child);
}, 300);
// adds second after 300ms
setTimeout(()=> {
var child2 = document.createElement("div");
child2.setAttribute('data-test-id','item');
child2.innerHTML = '<div>Lichi</div><div data-test-id="other-item">Tropical Fruit</div>';
document.body.appendChild(child2);
}, 600);
// adds second after 1000ms
setTimeout(()=> {
var child2 = document.createElement("div");
child2.setAttribute('data-qa-id','task-card-title');
child2.innerHTML = '<div>Task Card 2</div>';
document.body.appendChild(child2);
}, 1000);
`,
});
});

it('test', () => {
cy.get('div').should(() => {
expect(1, ' 1 should eq').eq(1);
});
});

it('assertion: synchronous asserts', () => {
expect(1).eq(1);
expect(1, 'sync assert').eq(1);
});

it('assertion: exist cypress function', () => {
cy.get('div').should('exist');
});

it('assertion: exist cypress function', () => {
cy.get('div').should('exist');
});

it('assertion: exist as chai function', () => {
cy.get('div').should(t => {
expect(t).to.exist;
});
});

it('assertion: exist as chai function with custom message', () => {
cy.get('div').should(t => {
expect(t, 'custom assert').to.exist;
});
});

it('01 assertion: should contain', () => {
cy.window().then(w => {
cy.get('div').should('contain', 'Some text');

cy.url().should('contain', 'mytest.com/123');
cy.qaId('task-card-title').should('contain', 'Task Card 2');
setTimeout(() => {
w.location.href = '/mytest.com/123';
//cy.visit('/mytest.com/123');
}, 500);
setTimeout(() => {
w.location.href = '/mytest.com';
//cy.visit('/mytest.com/123');
}, 800);
cy.url().should('contain', 'mytest.com');
cy.url().should('contain', 'mytest.com');
cy.url().should('contain', 'mytest.com');
});
});

it('02 several url', () => {
cy.window().then(w => {
setTimeout(() => {
w.location.href = '/mytest.com/123';
}, 500);
cy.url().should('contain', 'mytest.com/123');
cy.url().should('contain', 'mytest.com');
cy.url().should('contain', 'mytest.com');
});
});

it('03 several urls', () => {
cy.url().should('contain', 'mytest.com');
cy.url().should('contain', 'mytest.com');
cy.url().should('contain', 'mytest.com');
});

it('04 several asserts', () => {
cy.qaId('task-card-title').should('contain', 'Task Card 2');
cy.qaId('task-card-title').should('contain', 'Task Card 2');
cy.qaId('task-card-title').should('contain', 'Task Card 2');
cy.qaId('task-card-title').should('contain', 'Task Card 2');
cy.qaId('task-card-title').should('contain', 'Task Card 2');
cy.qaId('task-card-title').should('contain', 'Task Card 2');
cy.wait(1000);
});

it('assertion: should contain after some time', () => {
cy.get('div').should('contain', 'Tropical Fruit').and('contain', 'Lichi');
});

it('assertion: url should', () => {
cy.url().should('contain', 'mytest.com');
});

it('assertion: several sync assertions', () => {
cy.get('div');

for (let t = 0; t < 20; t++) {
expect(t).eq(t);
}
});
});
18 changes: 6 additions & 12 deletions src/setup/cypress-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,11 @@ export const handleCyLogEvents = (
const ingoreAllCommands = () =>
[...ignoreCommands(), 'should', 'then', 'allure', 'doSyncCommand'].filter(t => t.trim() !== '');
const customCommands: string[] = [];
const commands: string[] = [];
const logCommands: string[] = [];
const allLogged: string[] = [];
const emit = createEmitEvent(runner);

Cypress.Allure.on('test:started', () => {
commands.splice(0, commands.length);
logCommands.splice(0, logCommands.length);
allLogged.splice(0, allLogged.length);
});

const allureAttachRequests = Cypress.env('allureAttachRequests')
Expand Down Expand Up @@ -363,19 +361,17 @@ export const handleCyLogEvents = (
withTry('report log:added', () => {
const cmdMessage = stepMessage(log.name, log.message === 'null' ? '' : log.message);
const logName = log.name;
const lastCommand = commands[commands.length - 1];
const lastLogCommand = logCommands[logCommands.length - 1];
const lastAllLoggedCommand = allLogged[allLogged.length - 1];
// const isEnded = log.end;

// logs are being added for all from command log, need to exclude same items
if (
cmdMessage !== lastCommand &&
cmdMessage !== lastLogCommand &&
cmdMessage !== lastAllLoggedCommand &&
!cmdMessage.match(/its:\s*\..*/) && // its already logged as command
!ingoreAllCommands().includes(logName) &&
logName !== COMMAND_REQUEST
) {
logCommands.push(cmdMessage);
allLogged.push(cmdMessage);
debug(`step: ${cmdMessage}`);

Cypress.Allure.startStep(cmdMessage);
Expand Down Expand Up @@ -428,7 +424,7 @@ export const handleCyLogEvents = (
debug(`started: ${cmdMessage}`);
Cypress.Allure.startStep(cmdMessage);

commands.push(cmdMessage);
allLogged.push(cmdMessage);

withTry('report command:attachment', () => {
const requestAndLogRequests = allureAttachRequests && name === COMMAND_REQUEST;
Expand Down Expand Up @@ -462,8 +458,6 @@ export const handleCyLogEvents = (
return;
}

commands.pop();

if (name === COMMAND_REQUEST) {
withTry('report attach:requests', () => {
attachRequests(allureAttachRequests, command, { compactAttachments: allureCompactAttachmentsRequests });
Expand Down

0 comments on commit 5e97ee4

Please sign in to comment.