Skip to content

Commit

Permalink
remove unnecessary imports, clean up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lalalune committed Jul 18, 2023
1 parent 76d8424 commit 8e5ee78
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 50 deletions.
20 changes: 13 additions & 7 deletions agentbrowser/browser.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import asyncio
import re
from pyppeteer import launch
import os
import platform

browser = None


def get_browser():
if browser is None:
init_browser()
return browser


async def async_get_browser():
if browser is None:
await async_init_browser()
Expand Down Expand Up @@ -65,14 +66,18 @@ async def async_init_browser(headless=True, executable_path=None):
executable_path = find_chrome()

if browser is None:
browser = await launch(headless=headless, executablePath=executable_path, autoClose=False,
# set handleSIGINT to False to allow for graceful shutdown
handleSIGINT=False,
handleSIGTERM=False,
handleSIGHUP=False
)
browser = await launch(
headless=headless,
executablePath=executable_path,
autoClose=False,
# set handleSIGINT to False to allow for graceful shutdown
handleSIGINT=False,
handleSIGTERM=False,
handleSIGHUP=False,
)
return browser


# async version of create_page
async def async_create_page(site=None):
global browser
Expand Down Expand Up @@ -114,6 +119,7 @@ async def async_get_body_text(page):
output = await page.querySelectorEval("body", "(element) => element.innerText")
return output.strip()


async def async_get_body_text_raw(page):
output = await page.querySelectorEval("body", "(element) => element.innerText")
return output.strip()
Expand Down
48 changes: 6 additions & 42 deletions agentbrowser/test.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
import asyncio
import pytest
from agentbrowser import (
async_get_browser,
async_init_browser,
async_navigate_to,
async_get_body_html,
async_get_body_text,
async_get_document_html,
async_create_page,
async_close_page,
async_evaluate_javascript,
async_get_body_text_raw,
create_page,
evaluate_javascript,
get_body_html,
Expand All @@ -23,31 +11,7 @@
test_article = "https://test-page-to-crawl.vercel.app"


@pytest.fixture(scope="function")
async def browser_fixture(request):
# Initialize a new event loop for this test
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

# Initialize the browser
await async_init_browser()
assert await async_get_browser() is not None, "Browser initialization failed."

async def fin():
# cleanup after tests
if await async_get_browser() is not None:
await (await async_get_browser()).close()

loop.run_until_complete(
asyncio.sleep(0.250)
) # gives tasks a bit of time to finish

request.addfinalizer(fin)

yield


def test_navigation(browser_fixture):
def test_navigation():
test_page = create_page("https://www.google.com")

# navigate to google
Expand All @@ -61,35 +25,35 @@ def test_navigation(browser_fixture):
print("test_navigation passed.")


def test_body_html(browser_fixture):
def test_body_html():
test_page = create_page(test_article)
body_html = get_body_html(test_page)
assert body_html is not None, "Failed to get body html."
print("test_body_html passed.")


def test_document_html(browser_fixture):
def test_document_html():
test_page = create_page(test_article)
html = get_document_html(test_page)
assert html is not None, "Failed to get document html."
print("test_document_html passed.")


def test_body_text(browser_fixture):
def test_body_text():
test_page = create_page(test_article)
body = get_body_text(test_page)
assert body is not None, "Failed to get body text."
print("test_body_text passed.")


def test_body_text_raw(browser_fixture):
def test_body_text_raw():
test_page = create_page(test_article)
body_text_raw = get_body_text_raw(test_page)
assert body_text_raw is not None, "Failed to get raw body text."
print("test_body_text_raw passed.")


def test_javascript_evaluation(browser_fixture):
def test_javascript_evaluation():
test_page = create_page(test_article)
result = evaluate_javascript(
"""
Expand Down
2 changes: 1 addition & 1 deletion agentbrowser/test_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,4 @@ async def test_async_create_page(browser_fixture):
test_page,
)
assert result == 3, "Javascript evaluation failed."
print("test_async_javascript_evaluation passed.")
print("test_async_javascript_evaluation passed.")

0 comments on commit 8e5ee78

Please sign in to comment.