-
-
Save Rishit30G/b8dfbbdabc3219ece9552e441a3b3441 to your computer and use it in GitHub Desktop.
Test for Socket
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const { chromium } = require("playwright"); | |
| const expect = require("chai").expect; | |
| let browser; | |
| let page; | |
| before(async () => { | |
| browser = await chromium.launch(); | |
| page = await browser.newPage(); | |
| await page.goto("http://localhost:3003"); | |
| }); | |
| after(async () => { | |
| await browser.close(); | |
| }); | |
| describe("Socket.io Chatbot Connection Tests", () => { | |
| it("should check if the socket is connected", async () => { | |
| const isConnected = await page.evaluate(() => { | |
| return typeof io !== "undefined" && io.socket.connected; | |
| }); | |
| expect(isConnected).to.be.true; | |
| }); | |
| it("should ensure socket is connected before sending a request", async () => { | |
| // Ensure the socket is disconnected before the test | |
| await page.evaluate(() => { | |
| if (typeof io !== "undefined" && io.socket.connected) { | |
| io.socket.disconnect(); | |
| } | |
| }); | |
| // Click the send button next to input box to trigger a request | |
| await page.click("#send-button"); | |
| // Check for the existence of the error message | |
| const errorMessageElement = await page.$("#error-message"); | |
| if (!errorMessageElement) { | |
| throw new Error("Error messages if any"); | |
| } | |
| const errorMessage = await page.$eval("#error-message", (el) => el.innerText); | |
| // Check that the appropriate error message is displayed | |
| expect(errorMessage).to.include("Not connected"); | |
| // Cleanup: Reconnect the socket if it was disconnected for the test | |
| await page.evaluate(() => { | |
| if (typeof io !== "undefined" && !io.socket.connected) { | |
| io.socket.connect(); | |
| } | |
| }); | |
| }); | |
| it('should ensure multiple connections are not triggered', async () => { | |
| // Inject a script to track number of connections | |
| await page.evaluate(() => { | |
| window.connectionCount = 0; | |
| if (typeof io !== 'undefined') { | |
| io.socket.on('connect', () => { | |
| window.connectionCount += 1; | |
| }); | |
| } | |
| }); | |
| // Here, you'd want to simulate whatever actions might accidentally trigger a reconnect. | |
| // For simplicity, I'm simulating a page reload. | |
| await page.reload(); | |
| const connectionCount = await page.evaluate(() => window.connectionCount); | |
| expect(connectionCount).to.equal(1); | |
| }); | |
| it("should receive a bot response when a ChatItem is clicked", async () => { | |
| // First, determine if there's at least one active ChatItem | |
| const hasActiveChatItems = await page.$eval(".chat-item-active", el => !!el); // Assuming chat items with active bots have a class of .chat-item-active | |
| if (hasActiveChatItems) { | |
| // Click the first active ChatItem | |
| await page.click(".chat-item-active"); | |
| // Wait for bot response. As previously assumed, bot responses show up in a div with class .bot-response. | |
| await page.waitForSelector(".bot-response"); | |
| // Check the bot's response | |
| const botResponse = await page.$eval(".bot-response", el => el.innerText); | |
| expect(botResponse).to.not.be.empty; | |
| } else { | |
| // Handle case when no active ChatItems are present | |
| console.log("No active ChatItems to test."); | |
| } | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment