I am trying to understand expect behaviour in chai.js. I have the code to check if the login fails with invalid credentials. This is my code:
describe('Login', function() {
before(function(done) {
driver.get('').then(done);
});
it('Login with Incorrect credentials', function( done ) {
driver.findElement(webdriver.By.name("username")).sendKeys("[email protected]");
driver.findElement(webdriver.By.name("password")).sendKeys("123");
driver.findElement(webdriver.By.className("client-onboarding-signin-btn")).click().then(function(){
driver.findElement(By.css(".has-error")).getText().then(function (text) {
console.log(text);
try{
expect(text).to.be.a("Invalid email or password");
done();
} catch (e) {
done(e);
}
});
});
});
});
according to my understanding this test case should pass because i am expecting invalid username and password and got the same. However, it throws an Assertion error. That is, 1) Login Login with Incorrect credentials: AssertionError: expected 'Invalid email or password' to be an invalid email or password
I am trying to understand expect behaviour in chai.js. I have the code to check if the login fails with invalid credentials. This is my code:
describe('Login', function() {
before(function(done) {
driver.get('https://pluma-dev.herokuapp.com/client-sign-in').then(done);
});
it('Login with Incorrect credentials', function( done ) {
driver.findElement(webdriver.By.name("username")).sendKeys("[email protected]");
driver.findElement(webdriver.By.name("password")).sendKeys("123");
driver.findElement(webdriver.By.className("client-onboarding-signin-btn")).click().then(function(){
driver.findElement(By.css(".has-error")).getText().then(function (text) {
console.log(text);
try{
expect(text).to.be.a("Invalid email or password");
done();
} catch (e) {
done(e);
}
});
});
});
});
according to my understanding this test case should pass because i am expecting invalid username and password and got the same. However, it throws an Assertion error. That is, 1) Login Login with Incorrect credentials: AssertionError: expected 'Invalid email or password' to be an invalid email or password
Share Improve this question asked Dec 7, 2016 at 7:11 user2987322user2987322 1771 gold badge2 silver badges10 bronze badges1 Answer
Reset to default 21You are using the wrong assertion. You want to use:
expect(text).to.equal("Invalid email or password");
.to.be.a
(which really is just a call to the assertion named a
) asserts that the value has a certain type. Here is some code illustrating the difference:
const expect = require("chai").expect;
it("a", () => expect("foo").to.be.a("foo"));
it("equal", () => expect("foo").to.equal("foo"));
it("correct use of a", () => expect("foo").to.be.a("string"));
The first test will fail. The 2nd and 3rd are correct usages, so they pass.
You can find the documentation for a
here.