Skip to content

Commit

Permalink
test using requests and API instead of selenium and web UI
Browse files Browse the repository at this point in the history
  • Loading branch information
dwelch-spike committed Jan 9, 2025
1 parent d53814a commit 1c4df16
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 85 deletions.
27 changes: 0 additions & 27 deletions prism-image-search/tests/e2e/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import pytest
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

##########################################
###### GLOBALS
Expand All @@ -25,31 +23,6 @@ def app(request):
###### FIXTURES
##########################################

@pytest.fixture(scope='function')
def browser():

# user agent from non-headless mode
# this is needed so that the website renders normally
# otherwise using headless mode causes some of the elements to not render
user_agent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36"

# Configure Chrome options
# We do this mostly so that we can run headless in github actions
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--window-size=1920,1080")
chrome_options.add_argument(f"--user-agent={user_agent}")

driver = webdriver.Chrome(options=chrome_options)
# Wait up to 2 minutes for elements to appear.
driver.implicitly_wait(120)
yield driver
driver.quit()


@pytest.fixture(scope='session')
def app_url(app):
app_url = f"http://{app}"
Expand Down
23 changes: 7 additions & 16 deletions prism-image-search/tests/e2e/test_search.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
import pytest

from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import requests

def test_search(browser, app_url):
browser.get(app_url + "/search")
search_input = browser.find_element(By.ID, 'textInput')
search_input.send_keys('aerospike')
search_input.send_keys(Keys.RETURN)
def test_search(app_url):

results_div = browser.find_element(By.ID, 'results')
assert results_div.is_displayed(), "Results div is not displayed"
response = requests.post(app_url + "/rest/v1/search", data={"text": "aerospike"})
assert response.status_code == 200, f"Expected status code 200, but got {response.status_code}"

# Find all elements within the search resultsgallery
gallery_items = results_div.find_elements(By.CSS_SELECTOR, ".responsive .gallery .search-result")

# Assert there are exactly 1 elements
# This assumes that the app was started without adding any additional images to the data file
# By default, there is 1 image in the data file
assert len(gallery_items) == 1, f"Expected 1 elements in the gallery, but found {len(gallery_items)}."
# by default there is 1 photo in the data dir so expect 1
results = response.json()["results"]
assert len(results) == 1, f"Expected 1 results, but got {len(results)}"
27 changes: 0 additions & 27 deletions quote-semantic-search/tests/e2e/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import pytest
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

##########################################
###### GLOBALS
Expand All @@ -25,31 +23,6 @@ def app(request):
###### FIXTURES
##########################################

@pytest.fixture(scope='function')
def browser():

# user agent from non-headless mode
# this is needed so that the website renders normally
# otherwise using headless mode causes some of the elements to not render
user_agent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36"

# Configure Chrome options
# We do this mostly so that we can run headless in github actions
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--window-size=1920,1080")
chrome_options.add_argument(f"--user-agent={user_agent}")

driver = webdriver.Chrome(options=chrome_options)
# Wait up to 2 minutes for elements to appear.
driver.implicitly_wait(120)
yield driver
driver.quit()


@pytest.fixture(scope='session')
def app_url(app):
app_url = f"http://{app}"
Expand Down
21 changes: 6 additions & 15 deletions quote-semantic-search/tests/e2e/test_search.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import pytest

from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import requests

def test_search(browser, app_url):
browser.get(app_url + "/search")
search_input = browser.find_element(By.ID, 'textInput')
search_input.send_keys('dog')
search_input.send_keys(Keys.RETURN)
def test_search(app_url):

results_div = browser.find_element(By.ID, 'results')
assert results_div.is_displayed(), "Results div is not displayed"
response = requests.post(app_url + "/rest/v1/search", data={"text": "dog"})
assert response.status_code == 200, f"Expected status code 200, but got {response.status_code}"

# Find all elements within the results gallery
gallery_items = results_div.find_elements(By.CSS_SELECTOR, ".gallery .responsive .quote")

# Assert there are exactly 5 elements
# By default the app returns 5 quotes
assert len(gallery_items) == 5, f"Expected 5 elements in the gallery, but found {len(gallery_items)}."
results = response.json()["results"]
assert len(results) == 5, f"Expected 5 results, but got {len(results)}"

0 comments on commit 1c4df16

Please sign in to comment.