Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Cypress, how would I write a simple test to check that a logo image exists on a page

Tags:

specifically, I would like to test that the logo appears on the home page of the app. I guess I am not sure what I should use to look for the image.

I tried

it('has a logo', function () {     cy.visit('http://localhost:3000')     cy.get('img').should('contains' , 'My-Logo.png')   }) 

instead of cy.get I also tried to just use

cy.contains('My-Logo.png') 

but it also fails.

I wasn't sure what element I should use or if I should be using get, but it fails. When I look at the source code for the web page, the logo is hidden within the javascript (nodeJS, vueJS,and expressJS application) and I noticed the javascript seems to add a sequence of numbers and letters to the image when I go to the image page even though the image name in the assets folder does not have it on there. My-Logo.d63b7f9.png.

like image 431
general Avatar asked May 11 '18 01:05

general


People also ask

How do you check if an element is present in Cypress?

Conditionally check whether an element has certain text: get('body'). then(($body) => { // synchronously ask for the body's text // and do something based on whether it includes // another string if ($body. text(). includes('some string')) { // yup found it cy.

Where can I find an image in Cypress?

it("should display a image in element div with class image", () => { cy. get('div[class="image"]'). find("img"); // some code that test that image is loaded so that it is displaye on the web page }); it("shoul diplay a image in element div with class image inside class extra", () => { cy.

How do you verify text in Cypress?

Cypress can validate the text on an element with the help of jQuery text() method. This method shall help us to fetch the text content on the selected element. We can also put assertions on the text content of the element. cy.


2 Answers

I figured out the solution on my own.

cy.get('form').find('img').should('have.attr', 'src').should('include','My-Logo') 

I inspected the element and found the <img src... line was embedded within a <form>. I could do a cy.get('form') and pass, but could not do a cy.get('img') to pass. So then I chained them together and it passed. I am not sure why I cannot just simply add the second should statement, but it failed when I tried to just run:

cy.get('form').find('img').should('include','My-Logo') 

I am not entirely sure why, but it needed the first "should" statement. I got around VUE adding the sequence of numbers and letters by just asking for the name of the file without the extension. I hope this maybe helps someone else as the documentation did not seem to cover this.

like image 133
general Avatar answered Sep 16 '22 14:09

general


you can use only one should statement like:

cy.get('form').find('img').should('have.attr', 'src', 'My-Logo') 

the third arg of should is the value to match with the element attribute.

like image 26
brennda Avatar answered Sep 18 '22 14:09

brennda