|
//THIS IS CODE IN which IT included all the functionalities cummulated. |
|
// Test suite for sending various types of WhatsApp messages |
|
//Each test case verifies that the corresponding message type can be successfully sent using the Infobip API, and it checks for a successful response from the API. |
|
//The code provided demonstrates how to interact with Infobip's WhatsApp API using Cypress for testing purposes. |
|
// It begins with sending a WhatsApp template message. |
|
//This template message is constructed with specific placeholders filled with content, such as the recipient's name. |
|
//The message is then sent to a recipient's WhatsApp number. |
|
//After sending the template message, the code proceeds to test sending various other types of messages: text, document, video, contact, image, and location. |
|
describe("Sending WhatsApp Messages as Text, video, images, docs, videos", () => { |
|
// API key for authorization |
|
const apiKey = 'b43f4d67f5bd098d3e3f8c4ceaa004ae-ff08ce05-2c51-40fe-8d5f-18bb4410c4dd'; |
|
|
|
//This test sends a WhatsApp template message using the specified template name and data.//It constructs the template message payload with placeholders. |
|
//The POST request is made to the Infobip API for sending the template message. |
|
//Assertions are made to check the response status and the presence of the 'messages' property in the response body |
|
|
|
it("Successfully sends a WhatsApp template message", () => { |
|
// Template message payload |
|
const tempMsg = { |
|
"messages": [{ |
|
"from": "447860099299", |
|
"to": "919065562412", |
|
"messageId": "238bd887-d0f4-4a5d-9c09-c633b44230d9", |
|
"content": { |
|
"templateName": "message_test", |
|
"templateData": { |
|
"body": { |
|
"placeholders": ["SHAIL PUJAN"] |
|
} |
|
}, |
|
"language": "en" |
|
} |
|
}] |
|
}; |
|
|
|
// Convert payload to JSON string |
|
const requestBody = JSON.stringify(tempMsg); |
|
|
|
// Send a POST request to Infobip API for sending a template message |
|
cy.request({ |
|
method: "POST", |
|
url: "https://6g3w9r.api.infobip.com/whatsapp/1/message/template", |
|
headers: { |
|
'Content-Type': 'application/json', |
|
Authorization: `App ${apiKey}` |
|
}, |
|
body: requestBody, |
|
redirect: "follow" |
|
}).then((response) => { |
|
// Check if the response status is 200 (OK) |
|
expect(response.status).to.eq(200); |
|
// Check if the response body has 'messages' property |
|
expect(response.body).to.have.property('messages'); |
|
// Log the response body |
|
cy.log(JSON.stringify(response.body)); |
|
}); |
|
}); |
|
|
|
// Test for sending a WhatsApp text message |
|
/*This test sends a simple text message. |
|
It creates the text message payload with the sender, recipient, and message content. |
|
The POST request is sent to the Infobip API for sending the text message. |
|
Assertions are made to check the response status.*/ |
|
it('Sends a WhatsApp text message', () => { |
|
// Text message payload |
|
const textmsg = { |
|
from: '447860099299', |
|
to: '919065562412', |
|
messageId: '7a9473d7-4907-404f-8bdc-8f8ddbf2ca80', |
|
content: { |
|
text: 'Hello I am SHAIL PUJAN. I am developing the test case to ensure the functionality and reliability of the proposed features!!!' |
|
} |
|
}; |
|
|
|
// Send a POST request to Infobip API for sending a text message |
|
cy.request({ |
|
method: 'POST', |
|
url: 'https://6g3w9r.api.infobip.com/whatsapp/1/message/text', |
|
headers: { |
|
'Content-Type': 'application/json', |
|
Authorization: `App ${apiKey}` |
|
}, |
|
body: textmsg |
|
}).then((response) => { |
|
// Check if the response status is 200 (OK) |
|
expect(response.status).to.eq(200); |
|
// Log the response body |
|
cy.log(JSON.stringify(response.body)); |
|
}); |
|
}); |
|
|
|
// Test for sending a WhatsApp document message |
|
/* This test sends a document message, typically a PDF. |
|
**The document message payload includes the media URL of the document to be sent. |
|
**The POST request is sent to the Infobip API for sending the document message. |
|
**Assertions are made to check the response status.*/ |
|
it('Sends a WhatsApp document message', () => { |
|
// Document message payload |
|
const documentmsg = { |
|
from: '447860099299', |
|
to: '919065562412', |
|
messageId: '7a9473d7-4907-404f-8bdc-8f8ddbf2ca80', |
|
content: { |
|
//here you have to put url of document you want to send |
|
mediaUrl: 'https://css4.pub/2015/textbook/somatosensory.pdf' |
|
} |
|
}; |
|
|
|
// Send a POST request to Infobip API for sending a document message |
|
cy.request({ |
|
method: 'POST', |
|
url: 'https://6g3w9r.api.infobip.com/whatsapp/1/message/document', |
|
headers: { |
|
'Content-Type': 'application/json', |
|
Authorization: `App ${apiKey}` |
|
}, |
|
body: documentmsg |
|
}).then((response) => { |
|
// Check if the response status is 200 (OK) |
|
expect(response.status).to.eq(200); |
|
// Log the response body |
|
cy.log(JSON.stringify(response.body)); |
|
}); |
|
}); |
|
|
|
// Test for sending a WhatsApp video message |
|
/*This test sends a video message. |
|
The video message payload includes the media URL of the video to be sent. |
|
The POST request is sent to the Infobip API for sending the video message. |
|
Assertions are made to check the response status.*/ |
|
it('Sends a WhatsApp video message', () => { |
|
// Video message payload |
|
const videomsg = { |
|
from: '447860099299', |
|
to: '919065562412', |
|
messageId: '7a9473d7-4907-404f-8bdc-8f8ddbf2ca80', |
|
content: { |
|
//Here you have to give the url of the video youant to send to the recipient |
|
mediaUrl: 'https://media.istockphoto.com/id/1449503427/video/aerial-top-down-view-of-a-teenage-girl-jumping-on-trampoline-in-a-backyard.mp4?s=mp4-640x640-is&k=20&c=FFCzW6d03rXTvE2vDdOrNB9rhZAI9ZqMIQdciT_zOoI=' |
|
} |
|
}; |
|
|
|
// Send a POST request to Infobip API for sending a video message |
|
cy.request({ |
|
method: 'POST', |
|
url: 'https://6g3w9r.api.infobip.com/whatsapp/1/message/video', |
|
headers: { |
|
'Content-Type': 'application/json', |
|
Authorization: `App ${apiKey}` |
|
}, |
|
body: videomsg |
|
}).then((response) => { |
|
// Check if the response status is 200 (OK) |
|
expect(response.status).to.eq(200); |
|
// Log the response body |
|
cy.log(JSON.stringify(response.body)); |
|
}); |
|
}); |
|
|
|
// Test for sending a WhatsApp contact message |
|
/* This test sends a contact message. |
|
The contact message payload includes details like addresses, emails, name, organization, and phones of the contact. |
|
The POST request is sent to the Infobip API for sending the contact message. |
|
Assertions are made to check the response status.*/ |
|
it('Should send a WhatsApp contact with the given payload', () => { |
|
// Contact message payload |
|
const contactmsg = { |
|
from: "447860099299", |
|
to: "919065562412", |
|
messageId: "7a9473d7-4907-404f-8bdc-8f8ddbf2ca80", |
|
content: { |
|
contacts: [{ |
|
addresses: [{ |
|
street: "Boring Road", |
|
city: "Patna", |
|
zip: "80001", |
|
country: "India", |
|
countryCode: "HR", |
|
type: "WORK" |
|
}, |
|
{ |
|
street: "boring Road", |
|
city: "Patna", |
|
zip: "80001", |
|
country: "India", |
|
countryCode: "HR", |
|
type: "HOME" |
|
} |
|
], |
|
birthday: "2000-08-19", |
|
emails: [{ |
|
email: "muskan@gmail.com", |
|
type: "WORK" |
|
}, |
|
{ |
|
email: "shail8690@gmail.com", |
|
type: "HOME" |
|
} |
|
], |
|
name: { |
|
firstName: "Yah", |
|
lastName: "Pujan", |
|
middleName: "Raj", |
|
namePrefix: "Mr.", |
|
formattedName: "Mr. Yash Raj Pujan" |
|
}, |
|
org: { |
|
company: "Company Name", |
|
department: "Department", |
|
title: "Director" |
|
}, |
|
phones: [{ |
|
phone: "919065562412", |
|
type: "HOME", |
|
waId: "9065562412" |
|
}, |
|
{ |
|
phone: "919065562412", |
|
type: "WORK", |
|
waId: "9065562412" |
|
} |
|
] |
|
}] |
|
} |
|
}; |
|
|
|
// Send a POST request to Infobip API for sending a contact message |
|
cy.request({ |
|
method: 'POST', |
|
url: 'https://6g3w9r.api.infobip.com/whatsapp/1/message/contact', |
|
body: contactmsg, |
|
headers: { |
|
'Content-Type': 'application/json', |
|
Authorization: `App ${apiKey}` |
|
} |
|
}).then((response) => { |
|
// Check if the response status is 200 (OK) |
|
expect(response.status).to.eq(200); |
|
// Log the response body |
|
cy.log(JSON.stringify(response.body)); |
|
// Additional assertions can be added as needed |
|
}); |
|
}); |
|
|
|
// Test for sending a WhatsApp image message |
|
/*This test sends an image message. |
|
The image message payload includes the media URL of the image to be sent. |
|
The POST request is sent to the Infobip API for sending the image message. |
|
Assertions are made to check the response status.*/ |
|
it('Sends a WhatsApp image message', () => { |
|
// Image message payload |
|
const imagemessage = { |
|
from: '447860099299', |
|
to: '919065562412', |
|
messageId: '7a9473d7-4907-404f-8bdc-8f8ddbf2ca80', |
|
content: { |
|
mediaUrl: 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ1Go-H8GMEh1cmMNiS_JV5om--MYYmxlsjCa_kZkUJhg&s' |
|
} |
|
}; |
|
|
|
// Send a POST request to Infobip API for sending an image message |
|
cy.request({ |
|
method: 'POST', |
|
url: 'https://6g3w9r.api.infobip.com/whatsapp/1/message/image', |
|
headers: { |
|
'Content-Type': 'application/json', |
|
Authorization: `App ${apiKey}` |
|
}, |
|
body: imagemessage |
|
}).then((response) => { |
|
// Check if the response status is 200 (OK) |
|
expect(response.status).to.eq(200); |
|
// Log the response body |
|
cy.log(JSON.stringify(response.body)); |
|
// Additional assertions can be added as needed |
|
}); |
|
}); |
|
|
|
// Test for sending a WhatsApp location message |
|
/*This test sends a location message. |
|
The location message payload includes the latitude and longitude of the location. |
|
The POST request is sent to the Infobip API for sending the location message. |
|
Assertions are made to check the response status.*/ |
|
it('Successfully sends a WhatsApp location message', () => { |
|
// Location message payload |
|
const locationmap = { |
|
from: '447860099299', |
|
to: '919065562412', |
|
messageId: '7a9473d7-4907-404f-8bdc-8f8ddbf2ca80', |
|
content: { |
|
latitude: 44.9526862,//add the latitude of the location you want to send |
|
longitude: 13.8545217//add the longtitude of the location you want to send |
|
} |
|
}; |
|
|
|
// Send a POST request to Infobip API for sending a location message |
|
cy.request({ |
|
method: 'POST', |
|
url: 'https://6g3w9r.api.infobip.com/whatsapp/1/message/location', |
|
headers: { |
|
'Content-Type': 'application/json', |
|
Authorization: `App ${apiKey}` |
|
}, |
|
body: locationmap |
|
}).then((response) => { |
|
// Check if the response status is 200 (OK) |
|
expect(response.status).to.eq(200); |
|
// Log the response body |
|
cy.log(JSON.stringify(response.body)); |
|
// Additional assertions can be added as needed |
|
}); |
|
}); |
|
}); |