mirror of
https://github.com/Second-Hand-Friends/kleinanzeigen-bot.git
synced 2026-03-12 10:31:50 +01:00
fix: improve login detection with fallback element (#493)
- Add fallback check for user-email element when mr-medium is not found - Improve login detection reliability - Add test case for alternative login element
This commit is contained in:
@@ -540,9 +540,16 @@ class KleinanzeigenBot(WebScrapingMixin):
|
|||||||
|
|
||||||
async def is_logged_in(self) -> bool:
|
async def is_logged_in(self) -> bool:
|
||||||
try:
|
try:
|
||||||
|
# Try to find the standard element first
|
||||||
user_info = await self.web_text(By.CLASS_NAME, "mr-medium")
|
user_info = await self.web_text(By.CLASS_NAME, "mr-medium")
|
||||||
if self.config["login"]["username"].lower() in user_info.lower():
|
if self.config["login"]["username"].lower() in user_info.lower():
|
||||||
return True
|
return True
|
||||||
|
except TimeoutError:
|
||||||
|
try:
|
||||||
|
# If standard element not found, try the alternative
|
||||||
|
user_info = await self.web_text(By.ID, "user-email")
|
||||||
|
if self.config["login"]["username"].lower() in user_info.lower():
|
||||||
|
return True
|
||||||
except TimeoutError:
|
except TimeoutError:
|
||||||
return False
|
return False
|
||||||
return False
|
return False
|
||||||
|
|||||||
@@ -324,6 +324,15 @@ class TestKleinanzeigenBotAuthentication:
|
|||||||
with patch.object(configured_bot, "web_text", return_value = "Welcome testuser"):
|
with patch.object(configured_bot, "web_text", return_value = "Welcome testuser"):
|
||||||
assert await configured_bot.is_logged_in() is True
|
assert await configured_bot.is_logged_in() is True
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_is_logged_in_returns_true_with_alternative_element(self, configured_bot:KleinanzeigenBot) -> None:
|
||||||
|
"""Verify that login check returns true when logged in with alternative element."""
|
||||||
|
with patch.object(configured_bot, "web_text", side_effect = [
|
||||||
|
TimeoutError(), # First try with mr-medium fails
|
||||||
|
"angemeldet als: testuser" # Second try with user-email succeeds
|
||||||
|
]):
|
||||||
|
assert await configured_bot.is_logged_in() is True
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_is_logged_in_returns_false_when_not_logged_in(self, configured_bot:KleinanzeigenBot) -> None:
|
async def test_is_logged_in_returns_false_when_not_logged_in(self, configured_bot:KleinanzeigenBot) -> None:
|
||||||
"""Verify that login check returns false when not logged in."""
|
"""Verify that login check returns false when not logged in."""
|
||||||
|
|||||||
Reference in New Issue
Block a user