Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions whatsapp_automation/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,20 @@ def send_whatsapp_message(driver, phone_number, message):
if not str(phone_number).startswith('+'):
phone_number = '+' + str(phone_number)

# URL-encode the message
encoded_message = urllib.parse.quote(message)

# Create the URL to open a chat
url = f"https://web.whatsapp.com/send?phone={phone_number}&text={encoded_message}"
url = f"https://web.whatsapp.com/send?phone={phone_number}"
driver.get(url)

# Wait for the message box to be ready
message_box = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.XPATH, '//div[@role="textbox"]')))
# Wait for the message box to be ready and type the message
message_box = WebDriverWait(driver, 30).until(
EC.presence_of_element_located((By.XPATH, '//div[@data-testid="conversation-compose-box"]//div[@role="textbox"]'))
)
message_box.send_keys(message)

# Click the send button
send_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//button[@data-testid="send"]')))
EC.element_to_be_clickable((By.XPATH, '//button[@data-testid="send"]'))
)
send_button.click()

time.sleep(random.uniform(1, 3)) # Random delay
Expand Down
58 changes: 58 additions & 0 deletions whatsapp_automation/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import unittest
from unittest.mock import MagicMock, patch, call
from selenium.webdriver.common.by import By
import pandas as pd

# This is a bit of a hack to make the import work without changing the project structure
import sys
import os
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
import main

class TestSendMessage(unittest.TestCase):

@patch('main.WebDriverWait')
def test_send_whatsapp_message_sends_keys_and_clicks_send(self, mock_wait):
# Arrange
mock_driver = MagicMock()
mock_message_box = MagicMock()
mock_send_button = MagicMock()

# Configure the mock_wait to return the mock elements.
# We use side_effect to return different mock objects for different waits.
mock_wait.return_value.until.side_effect = [
MagicMock(), # For the initial chat-list-search wait
mock_message_box, # For the conversation-compose-box wait
mock_send_button # For the send button wait
]

phone_number = "1234567890"
message = "Hello, this is a test message."

# Act
success = main.send_whatsapp_message(mock_driver, phone_number, message)

# Assert
# 1. Check if the correct URL was opened
mock_driver.get.assert_called_once_with(f"https://web.whatsapp.com/send?phone=+{phone_number}")

# 2. Check the waits for elements
mock_driver_wait_calls = [
call(mock_driver, 60),
call(mock_driver, 30),
call(mock_driver, 10),
]
self.assertEqual(mock_wait.call_args_list, mock_driver_wait_calls)


# 3. Check if send_keys was called on the message box
mock_message_box.send_keys.assert_called_once_with(message)

# 4. Check if the send button was clicked
mock_send_button.click.assert_called_once()

# 5. Check the function's return value
self.assertTrue(success)

if __name__ == '__main__':
unittest.main()