Skip to content

Commit eaafa7b

Browse files
committed
Merge branch 'main' into treefrontend-ui
2 parents b58cacc + 65ea334 commit eaafa7b

File tree

3 files changed

+92
-151
lines changed

3 files changed

+92
-151
lines changed

visual-tree-search-backend/app/api/lwats/webagent_utils_async/utils/playwright_manager.py

Lines changed: 41 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from dotenv import load_dotenv
66
from browserbase import Browserbase
77
import aiohttp
8+
import boto3
89

910
# Load environment variables from .env file
1011
load_dotenv()
@@ -97,117 +98,46 @@ async def restore_cookies(browser_tab: Page, cookie_file_path: str):
9798

9899

99100
async def authenticate(browser_tab: Page, cookie_file_path: str):
100-
"""Authenticate to Magento using Playwright form submission, then show a detailed cookie table."""
101-
print("Attempting login with Playwright form submission")
102-
username = "emma.lopez@gmail.com"
103-
password = "Password.123"
104-
105-
# Start fresh without cookies
106-
await browser_tab.context.clear_cookies()
107-
print("Cleared cookies before login.\n")
108-
109-
# Optional: set a test cookie
110-
await browser_tab.context.add_cookies([{
111-
"name": "test_cookie",
112-
"value": "1",
113-
"domain": "128.105.145.205",
114-
"path": "/",
115-
}])
116-
117-
# Navigate to login page
118-
await browser_tab.goto(SITE_LOGIN_URL)
119-
await browser_tab.wait_for_load_state("networkidle")
120-
121-
print("Current URL:", browser_tab.url)
122-
print("Filling in login form...\n")
123-
124-
# Fill the username
125-
email_field = await browser_tab.query_selector("#email")
126-
if email_field:
127-
await email_field.fill(username)
128-
print("Filled email field")
129-
else:
130-
print("⚠️ Could not find email field")
131-
132-
# Fill the password
133-
password_field = await browser_tab.query_selector("#pass")
134-
if password_field:
135-
await password_field.fill(password)
136-
print("Filled password field")
137-
else:
138-
print("⚠️ Could not find password field")
139-
140-
print("Examining form elements...\n")
141-
form_elements = await browser_tab.query_selector_all("form.form-login input, form#login-form input")
142-
for element in form_elements:
143-
name = await element.get_attribute("name")
144-
value = await element.get_attribute("value")
145-
input_type = await element.get_attribute("type")
146-
if name:
147-
print(f" Form input: {name} = {value if value else '[empty]'} (type: {input_type})")
148-
149-
print("\nClicking login button...")
150-
login_button = (
151-
await browser_tab.query_selector(".action.login.primary")
152-
or await browser_tab.query_selector("#send2")
153-
or await browser_tab.query_selector("button[type='submit']")
154-
)
155-
156-
if login_button:
157-
print(f"Found login button: id={await login_button.get_attribute('id')} type={await login_button.get_attribute('type')}")
158-
try:
159-
async with browser_tab.expect_navigation(wait_until="networkidle", timeout=15000):
160-
await login_button.click()
161-
print("Clicked login button and waited for navigation.\n")
162-
except Exception as e:
163-
print(f"Navigation timeout or error after clicking login: {e}")
164-
else:
165-
print("⚠️ Could not find login button!")
166-
167-
# Save the cookies regardless of success
168-
await store_cookies(browser_tab, cookie_file_path)
169-
170-
# Check if login succeeded
171-
print("Checking if login succeeded...\n")
172-
173-
cookies = await browser_tab.context.cookies()
174-
print(f"Cookies after login attempt ({len(cookies)}) (Markdown Table):\n")
175-
print()
176-
177-
# Check for Magento 2's typical session cookie (PHPSESSID) or Magento 1's (frontend)
178-
magento_session_cookies = [c for c in cookies if c["name"] in ("frontend", "frontend_cid", "PHPSESSID")]
179-
if magento_session_cookies:
180-
print(f"✅ Found {len(magento_session_cookies)} potential Magento session cookie(s): {', '.join(c['name'] for c in magento_session_cookies)}")
181-
else:
182-
print("❌ No Magento 'frontend' or 'PHPSESSID' cookie found - likely not authenticated.\n")
183-
184-
# Navigate to account page to confirm
185-
await browser_tab.goto(f"{SITE_URL}/customer/account/")
186-
await browser_tab.wait_for_load_state("networkidle")
187-
188-
# Check if we're truly logged in by searching for My Account or a welcome message
189-
is_logged_in = False
190-
welcome_msg = await browser_tab.query_selector(".box-information .box-content p") or await browser_tab.query_selector(".welcome-msg")
191-
if welcome_msg:
192-
welcome_text = await welcome_msg.text_content()
193-
if "Emma" in welcome_text:
194-
is_logged_in = True
195-
print(f"✅ Found welcome message containing 'Emma': {welcome_text.strip()}")
196-
197-
page_title = await browser_tab.title()
198-
if "My Account" in page_title and "Login" not in page_title:
199-
is_logged_in = True
200-
print(f"✅ Page title indicates logged in: {page_title}")
201-
202-
if is_logged_in:
203-
print("✅ Successfully logged in!\n")
204-
return True
205-
else:
206-
current_title = await browser_tab.title()
207-
print(f"❌ Login verification failed. Current page: {browser_tab.url} | Title: {current_title}\n")
208-
content = await browser_tab.content()
209-
snippet = content[:500].replace("\n", " ")
210-
print(f"Page content snippet:\n{snippet}...\n")
101+
"""Authenticate using remote API call and store cookies if successful"""
102+
print("Attempting authentication via remote API")
103+
104+
auth_url = os.environ["AUTHENTICATE_URL"]
105+
auth_data = {
106+
"username": "emma.lopez@gmail.com",
107+
"password": "Password.123",
108+
"site_url": SITE_URL
109+
}
110+
111+
try:
112+
async with aiohttp.ClientSession() as session:
113+
headers = {
114+
'Content-Type': 'application/json',
115+
'Connection': 'close'
116+
}
117+
async with session.post(auth_url, json=auth_data, headers=headers) as response:
118+
result = await response.json()
119+
120+
if response.status == 200 and result.get('status') == 'success':
121+
print("✅ Remote authentication successful")
122+
123+
# Navigate to account page to verify
124+
s3_client = boto3.client('s3')
125+
s3_client.download_file('test-litewebagent', 'shopping.json', cookie_file_path)
126+
print("✅ downloaded cookies from s3")
127+
cookies_restored = await restore_cookies(browser_tab, cookie_file_path)
128+
print("✅ restored cookies")
129+
await browser_tab.goto(f"{SITE_URL}/customer/account/")
130+
await browser_tab.wait_for_load_state("networkidle")
131+
132+
# Store cookies if login succeeded
133+
# await store_cookies(browser_tab, cookie_file_path)
134+
return True
135+
else:
136+
print(f"❌ Remote authentication failed: {result.get('message', 'Unknown error')}")
137+
return False
138+
139+
except Exception as e:
140+
print(f"❌ Remote authentication error: {str(e)}")
211141
return False
212142

213143
async def check_login_status(browser_tab: Page) -> bool:

visual-tree-search-backend/app/api/shopping.json

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,140 @@
11
[
22
{
3-
"name": "test_cookie",
4-
"value": "1",
3+
"name": "mage-cache-storage",
4+
"value": "{}",
55
"domain": "128.105.145.205",
66
"path": "/",
7-
"expires": -1,
7+
"expires": 1775110732,
88
"httpOnly": false,
99
"secure": false,
1010
"sameSite": "Lax"
1111
},
1212
{
13-
"name": "PHPSESSID",
14-
"value": "a4b3619e458d899519d83b16fd5a0e2e",
13+
"name": "mage-cache-storage-section-invalidation",
14+
"value": "{}",
1515
"domain": "128.105.145.205",
1616
"path": "/",
17-
"expires": 1775112451.726664,
18-
"httpOnly": true,
17+
"expires": 1775110732,
18+
"httpOnly": false,
1919
"secure": false,
2020
"sameSite": "Lax"
2121
},
2222
{
23-
"name": "form_key",
24-
"value": "mSsyEuoNV6CB0esX",
23+
"name": "mage-messages",
24+
"value": "",
2525
"domain": "128.105.145.205",
2626
"path": "/",
27-
"expires": 1775112451.726782,
27+
"expires": 1775110735,
2828
"httpOnly": false,
2929
"secure": false,
30-
"sameSite": "Lax"
30+
"sameSite": "Strict"
3131
},
3232
{
33-
"name": "mage-cache-storage",
33+
"name": "recently_viewed_product",
3434
"value": "{}",
3535
"domain": "128.105.145.205",
3636
"path": "/",
37-
"expires": 1775112434,
37+
"expires": 1775110732,
3838
"httpOnly": false,
3939
"secure": false,
4040
"sameSite": "Lax"
4141
},
4242
{
43-
"name": "mage-cache-storage-section-invalidation",
43+
"name": "recently_viewed_product_previous",
4444
"value": "{}",
4545
"domain": "128.105.145.205",
4646
"path": "/",
47-
"expires": 1775112434,
47+
"expires": 1775110732,
4848
"httpOnly": false,
4949
"secure": false,
5050
"sameSite": "Lax"
5151
},
5252
{
53-
"name": "mage-cache-sessid",
54-
"value": "true",
53+
"name": "recently_compared_product",
54+
"value": "{}",
5555
"domain": "128.105.145.205",
5656
"path": "/",
57-
"expires": 1775112451,
57+
"expires": 1775110732,
5858
"httpOnly": false,
5959
"secure": false,
6060
"sameSite": "Lax"
6161
},
6262
{
63-
"name": "mage-messages",
64-
"value": "",
63+
"name": "recently_compared_product_previous",
64+
"value": "{}",
6565
"domain": "128.105.145.205",
6666
"path": "/",
67-
"expires": 1775112451,
67+
"expires": 1775110732,
6868
"httpOnly": false,
6969
"secure": false,
70-
"sameSite": "Strict"
70+
"sameSite": "Lax"
7171
},
7272
{
73-
"name": "recently_viewed_product",
73+
"name": "product_data_storage",
7474
"value": "{}",
7575
"domain": "128.105.145.205",
7676
"path": "/",
77-
"expires": 1775112434,
77+
"expires": 1775110732,
7878
"httpOnly": false,
7979
"secure": false,
8080
"sameSite": "Lax"
8181
},
8282
{
83-
"name": "recently_viewed_product_previous",
84-
"value": "{}",
83+
"name": "private_content_version",
84+
"value": "843052e37c1703f047c79850de356543",
8585
"domain": "128.105.145.205",
8686
"path": "/",
87-
"expires": 1775112434,
87+
"expires": 1778134733.861287,
8888
"httpOnly": false,
8989
"secure": false,
9090
"sameSite": "Lax"
9191
},
9292
{
93-
"name": "recently_compared_product",
94-
"value": "{}",
93+
"name": "PHPSESSID",
94+
"value": "e60e354f4484dbe490e863a6e3c5b4b9",
9595
"domain": "128.105.145.205",
9696
"path": "/",
97-
"expires": 1775112434,
98-
"httpOnly": false,
97+
"expires": 1775110737.116277,
98+
"httpOnly": true,
9999
"secure": false,
100100
"sameSite": "Lax"
101101
},
102102
{
103-
"name": "recently_compared_product_previous",
104-
"value": "{}",
103+
"name": "X-Magento-Vary",
104+
"value": "9bf9a599123e6402b85cde67144717a08b817412",
105105
"domain": "128.105.145.205",
106106
"path": "/",
107-
"expires": 1775112434,
107+
"expires": 1775110737.116485,
108+
"httpOnly": true,
109+
"secure": false,
110+
"sameSite": "Lax"
111+
},
112+
{
113+
"name": "form_key",
114+
"value": "M0iOyk8VTBOyKC1q",
115+
"domain": "128.105.145.205",
116+
"path": "/",
117+
"expires": 1775110737.116428,
108118
"httpOnly": false,
109119
"secure": false,
110120
"sameSite": "Lax"
111121
},
112122
{
113-
"name": "product_data_storage",
114-
"value": "{}",
123+
"name": "mage-cache-sessid",
124+
"value": "true",
115125
"domain": "128.105.145.205",
116126
"path": "/",
117-
"expires": 1775112434,
127+
"expires": 1775110735,
118128
"httpOnly": false,
119129
"secure": false,
120130
"sameSite": "Lax"
121131
},
122132
{
123133
"name": "section_data_ids",
124-
"value": "{%22messages%22:1743576451%2C%22customer%22:1743576451%2C%22compare-products%22:1743576451%2C%22last-ordered-items%22:1743576451%2C%22cart%22:1743576451%2C%22directory-data%22:1743576451%2C%22captcha%22:1743576451%2C%22instant-purchase%22:1743576451%2C%22loggedAsCustomer%22:1743576451%2C%22persistent%22:1743576451%2C%22review%22:1743576451%2C%22wishlist%22:1743576451%2C%22recently_viewed_product%22:1743576451%2C%22recently_compared_product%22:1743576451%2C%22product_data_storage%22:1743576451%2C%22paypal-billing-agreement%22:1743576451}",
134+
"value": "{%22messages%22:1743574735%2C%22customer%22:1743574735%2C%22compare-products%22:1743574735%2C%22last-ordered-items%22:1743574735%2C%22cart%22:1743574735%2C%22directory-data%22:1743574735%2C%22captcha%22:1743574735%2C%22instant-purchase%22:1743574735%2C%22loggedAsCustomer%22:1743574735%2C%22persistent%22:1743574735%2C%22review%22:1743574735%2C%22wishlist%22:1743574735%2C%22recently_viewed_product%22:1743574735%2C%22recently_compared_product%22:1743574735%2C%22product_data_storage%22:1743574735%2C%22paypal-billing-agreement%22:1743574735}",
125135
"domain": "128.105.145.205",
126136
"path": "/",
127-
"expires": 1775112451,
137+
"expires": 1775110735,
128138
"httpOnly": false,
129139
"secure": false,
130140
"sameSite": "Lax"

visual-tree-search-backend/requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ aiolimiter==1.1.0
2323
transformers==4.34.0
2424
nltk==3.8.1
2525
browserbase
26-
aiohttp
26+
aiohttp
27+
boto3==1.34.34

0 commit comments

Comments
 (0)