-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_final.py
More file actions
186 lines (154 loc) · 6.18 KB
/
Copy pathtest_final.py
File metadata and controls
186 lines (154 loc) · 6.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env python3
"""
Test final : envoi réel sur 3 entreprises pour valider le fix
ATTENTION : Ce test envoie de VRAIES candidatures !
"""
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException, NoSuchElementException, WebDriverException
from webdriver_manager.chrome import ChromeDriverManager
import time
import csv
import logging
# Configuration des logs
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
# Paramètres du candidat
CANDIDATURE = {
"nom": "TEST",
"prenom": "Test",
"email": "test@test.com", # EMAIL DE TEST
"telephone": "0600000000",
"objet": "Test candidature",
"message": "Ceci est un test - merci d'ignorer ce message"
}
print("🧪 TEST FINAL - Envoi sur 3 entreprises")
print("⚠️ ATTENTION : Ce test envoie de VRAIES candidatures !")
print(" (avec des informations de TEST)")
print("\n" + "="*60)
response = input("\nContinuer ? (oui/non): ")
if response.lower() != "oui":
print("Test annulé.")
exit(0)
# Charger 3 URLs de test
test_urls = []
with open("urls_entreprises.csv", "r") as f:
reader = csv.reader(f)
next(reader)
for i, row in enumerate(reader):
if i >= 3:
break
test_urls.append(row[0])
print(f"\n📋 {len(test_urls)} entreprises à tester:")
for url in test_urls:
print(f" - {url}")
# Initialiser le driver
def init_driver():
options = webdriver.ChromeOptions()
options.add_argument("--headless")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--disable-gpu")
options.add_argument("--window-size=1920,1080")
options.add_experimental_option("excludeSwitches", ["enable-logging"])
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=options)
driver.set_page_load_timeout(30)
return driver
driver = init_driver()
# Fonction d'envoi (identique à sender.py)
def tester_envoi(url):
try:
driver.get(url)
wait = WebDriverWait(driver, 15)
# Gérer les cookies
try:
time.sleep(2)
cookie_button = driver.find_element(By.ID, "accepter-les-cookies")
if cookie_button.is_displayed():
cookie_button.click()
time.sleep(1)
except:
pass
# Remplir le formulaire
nom_field = wait.until(EC.presence_of_element_located((By.NAME, "organization_contact[last_name]")))
nom_field.clear()
nom_field.send_keys(CANDIDATURE["nom"])
driver.find_element(By.NAME, "organization_contact[first_name]").clear()
driver.find_element(By.NAME, "organization_contact[first_name]").send_keys(CANDIDATURE["prenom"])
driver.find_element(By.NAME, "organization_contact[email]").clear()
driver.find_element(By.NAME, "organization_contact[email]").send_keys(CANDIDATURE["email"])
driver.find_element(By.NAME, "organization_contact[phone]").clear()
driver.find_element(By.NAME, "organization_contact[phone]").send_keys(CANDIDATURE["telephone"])
driver.find_element(By.NAME, "organization_contact[subject]").clear()
driver.find_element(By.NAME, "organization_contact[subject]").send_keys(CANDIDATURE["objet"])
driver.find_element(By.NAME, "organization_contact[message]").clear()
driver.find_element(By.NAME, "organization_contact[message]").send_keys(CANDIDATURE["message"])
# Cliquer sur le bouton submit
try:
submit_button = driver.find_element(By.XPATH, "//button[@type='submit'] | //input[@type='submit']")
driver.execute_script("arguments[0].scrollIntoView(true);", submit_button)
time.sleep(0.5)
submit_button.click()
except NoSuchElementException:
form = driver.find_element(By.TAG_NAME, "form")
form.submit()
# Attendre la confirmation
try:
confirmation = wait.until(
EC.presence_of_element_located((By.XPATH, "//*[contains(text(), 'Votre message a bien été envoyé') or contains(text(), 'bien été envoyé')]"))
)
if confirmation.is_displayed():
return True, "Confirmation reçue"
else:
return False, "Confirmation non visible"
except TimeoutException:
page_text = driver.find_element(By.TAG_NAME, "body").text
if "422" in page_text or "rejetée" in page_text.lower():
return False, "Erreur 422 - Rejeté par le serveur"
else:
return False, "Pas de confirmation"
except Exception as e:
return False, f"Erreur: {str(e)[:100]}"
# Tester les envois
print("\n" + "="*60)
print("DÉBUT DES TESTS")
print("="*60)
succes = 0
echecs = 0
for i, url in enumerate(test_urls, 1):
print(f"\n[{i}/{len(test_urls)}] Test de {url}")
resultat, message = tester_envoi(url)
if resultat:
succes += 1
print(f" ✅ SUCCÈS - {message}")
else:
echecs += 1
print(f" ❌ ÉCHEC - {message}")
# Pause entre les envois
if i < len(test_urls):
print(f" Pause de 8 secondes...")
time.sleep(8)
# Résultats
driver.quit()
print("\n" + "="*60)
print("RÉSULTATS DU TEST")
print("="*60)
print(f"Succès: {succes}/{len(test_urls)}")
print(f"Échecs: {echecs}/{len(test_urls)}")
if succes == len(test_urls):
print("\n✅ TEST RÉUSSI - Le fix fonctionne parfaitement !")
print(" Vous pouvez maintenant lancer le script complet:")
print(" python sender.py")
elif succes > 0:
print(f"\n⚠️ TEST PARTIEL - {succes}/{len(test_urls)} réussis")
print(" Le fix fonctionne mais il peut y avoir des échecs occasionnels.")
else:
print("\n❌ TEST ÉCHOUÉ - Aucun envoi réussi")
print(" Il faut investiguer davantage.")
print("="*60)