Cypress equalIgnoreCase - mocha.js

I am using Cypress for testing but I can't find equalIgnoreCase in the documentation.
here is my testing code and I want equalIgnoreCase in this case:
cy.get('#SelectedToAddress')
.select('United Arab Emirates').should('have.value','AE')
I found in chai this for equalIgnoreCase
chai.assert.match("chrome", /Chrome/i); //should return true
But I don't how to use same with value property in Cypress
Also, I found this chai plugin
http://www.chaijs.com/plugins/chai-string/
But I don't know how could I add a chai plugin to Cypress

Maybe you can also do this:
cy.get('#SelectedToAddress')
.select('United Arab Emirates')
.its('value')
.then($value => {
let valueRead = $value.toLowerCase();
expect(value).to.eq(<expected_value_in_lowercase>);
})

You can use invoke command:
cy.get('#SelectedToAddress')
.select('United Arab Emirates')
.invoke('val')
.should('match',/^AE$/i)

Related

cypress should be called with value test

Looking for a test like below. Basically I get it to work using cy.get('#myLog').should('be.called') but I'm trying to test the value console log is using when called. In jest it would be toHaveBeenCalledWith() so the equivalent in cypress is what I'm after ?
cy.window().then((win) => {
cy.wrap(cy.spy(win.console, 'log')).as('myLog')
})
cy.get('#myLog')
.should('be.called')
.and('have.value', 'clicked')
cy.spy() and cy.stub() you can use Sinon-chai assertions. So for you instance you may want to use be.calledWith.
cy.window().then((win) => {
cy.wrap(cy.spy(win.console, 'log')).as('myLog')
})
cy.get('#myLog')
.should('be.calledWith', 'clicked')

cy.origin is not a function error displayed

I am trying to use the new origin function and I got this error:
No commands were issued in this test.
**TypeError
The following error originated from your test code, not from Cypress.
cy.origin is not a function**
The code is really basic:
cy.origin('https://www.acme.com', () => {
cy.visit('/history/founder')
cy.get('h1').contains('About our Founder, Marvin Acme')
})
Thanks a lot!
Two things to check
have you added experimentalSessionAndOrigin: true to cypress.json?
are you using Cypres v9.6.0+

Cypress - get element without assertion

How do I get an element in Cypress without it asserting that it is present?
cy.get('.something')
Sometimes my element might not be there and I don't want it to fail the test.
Is there a different command I should be using?
You can use cy.$$('selector') to synchronously query for an element (jquery).
If you want this to happen after a cypress command, you'll need a .then:
cy.visit('/')
cy.get('element-one').then(() => {
const $el2 = cy.$$('element-two')
if ($el2.length) {
// do this
} else {
// do that
}
})
You might want to check this section of the docs in Cypress
https://docs.cypress.io/guides/core-concepts/conditional-testing.html#Element-existence

Post result to external api after each test case

I'm setting up a CI-chain and decided to use Cypress for the UI testing. I need to get the result for each individual testcase in my suite. Preferably from within Node in for example a afterEach statement.
Has anyone done this before? Is there any built-in support for this?
I do not want to parse the end result for testcases preferably.
It was possible by using Mocha's this.currentState in conjunction with Cypress plugins.
This is how I solved it:
cypress/plugins/index.js
on("task", {
testFinished(event) {
console.log(event.title, event.result);
return null;
}
});
in my testsuite
afterEach(function() {
cy.task("testFinished", { title: this.currentTest.title, result: this.currentTest.state });
});
The console.log in plugins can now easily be switched for a POST request to wherever you want to store the results.

chai: Cannot read property 'not' of undefined

I am new to chai and mocha and I use the sample code for my first test case. Here is my code.
var chai = require("chai");
var mocha = require("mocha");
var expect = chai.expect;
describe("Test", function() {
it("Test", function() {
expect([1, 2]).to.be.an('array').that.does.not.include(3);
});
});
I run mocha test.js
The result is:
TypeError: Cannot read property 'not' of undefined
What is wrong with me? It seems .does return undefined. I remove .does and it works correctly. What is the correct usage?
The following code works.
expect([1, 2]).to.be.an('array').that.not.include(3);
If I run your code with Chai 4, it works. When I downgrade it to Chai 3 I get the error you get. does was added as a no-op assertion in Chai 4.0.0. You are using a version of Chai that predates 4.0.0.
If you check the releases information, you'll find for version 4.0.0, this:
Add does and but as new no-op assertion. (Related Issues: #700, #339 PRs: #621, #701)
(You can find the same information in the Github release, with the added benefit that the issue numbers are links to the actual issues.)

Resources