From 27cbff60ff991c0ba83e9a3c56f88bb4f9c58615 Mon Sep 17 00:00:00 2001 From: Chris-G32 Date: Sun, 14 Apr 2024 19:36:32 -0400 Subject: [PATCH 01/18] Uncleaned up, but functioning action --- .github/workflows/test-selenium.yml | 85 +++++++++++++++++++++++++++++ tests/requirements.txt | 4 ++ tests/webdriver.py | 65 ++++++++++++++++++++++ 3 files changed, 154 insertions(+) create mode 100644 .github/workflows/test-selenium.yml create mode 100644 tests/requirements.txt create mode 100644 tests/webdriver.py diff --git a/.github/workflows/test-selenium.yml b/.github/workflows/test-selenium.yml new file mode 100644 index 0000000..5c3be06 --- /dev/null +++ b/.github/workflows/test-selenium.yml @@ -0,0 +1,85 @@ +name: Run Selenium Test Suite +on: + workflow_dispatch: +jobs: + scrape: + runs-on: ubuntu-latest + steps: + - name: Checking out repo + uses: actions/checkout@v3 + - name: Setting up Python + uses: actions/setup-python@v4 + with: + python-version: '3.9' + - name: Installing package list + run: apt list --installed + - name: update + run: sudo apt update -y + - name: Install Chrome Shared Libraries + run: | + sudo apt-get update && sudo apt-get install -y \ + ca-certificates \ + fonts-liberation \ + libasound2 \ + libatk-bridge2.0-0 \ + libatk1.0-0 \ + libc6 \ + libcairo2 \ + libcups2 \ + libdbus-1-3 \ + libexpat1 \ + libfontconfig1 \ + libgbm1 \ + libgcc1 \ + libglib2.0-0 \ + libgtk-3-0 \ + libnspr4 \ + libnss3 \ + libpango-1.0-0 \ + libpangocairo-1.0-0 \ + libstdc++6 \ + libx11-6 \ + libx11-xcb1 \ + libxcb1 \ + libxcomposite1 \ + libxcursor1 \ + libxdamage1 \ + libxext6 \ + libxfixes3 \ + libxi6 \ + libxrandr2 \ + libxrender1 \ + libxss1 \ + libxtst6 \ + lsb-release \ + wget \ + xdg-utils + - name: Removing previous chrome instances on runner + run: sudo apt purge google-chrome-stable + continue-on-error: true + - name: chrome install + uses: browser-actions/setup-chrome@v1 + - run: chrome --version + - name: Use Node.js + uses: actions/setup-node@v2 + - name: Install dependencies + run: npm install + - name: Install Chrome + run: | + wget https://dl-ssl.google.com/linux/linux_signing_key.pub -O /tmp/google.pub + gpg --no-default-keyring --keyring /etc/apt/keyrings/google-chrome.gpg --import /tmp/google.pub + echo 'deb [arch=amd64 signed-by=/etc/apt/keyrings/google-chrome.gpg] http://dl.google.com/linux/chrome/deb/ stable main' | sudo tee /etc/apt/sources.list.d/google-chrome.list + sudo apt-get update -y + sudo apt-get install google-chrome-stable -y + + sudo apt-get install -y chromium-browser chromium-chromedriver -y + sudo apt-get update -y + # Need to fetch reqs if needed + - name: Installing all necessary packages + run: pip install chromedriver-autoinstaller selenium pyvirtualdisplay webdriver_manager + - name: Install xvfb + run: sudo apt install xvfb -y + continue-on-error: true + + - name: Running the Python script + run: python3 tests/webdriver.py \ No newline at end of file diff --git a/tests/requirements.txt b/tests/requirements.txt new file mode 100644 index 0000000..980464f --- /dev/null +++ b/tests/requirements.txt @@ -0,0 +1,4 @@ +selenium +chromedriver_autoinstaller +pyvirtualdisplay +webdriver_manager \ No newline at end of file diff --git a/tests/webdriver.py b/tests/webdriver.py new file mode 100644 index 0000000..4d478fb --- /dev/null +++ b/tests/webdriver.py @@ -0,0 +1,65 @@ +from selenium import webdriver +from selenium.webdriver.chrome.options import Options +from selenium.webdriver.chrome.service import Service +from selenium import webdriver +from selenium.webdriver.common.keys import Keys +from selenium.webdriver.common.by import By +# from webdriver_manager.chrome import ChromeDriverManager +import time +import chromedriver_autoinstaller + +chromedriver_autoinstaller.install() # Check if the current version of chromedriver exists + # and if it doesn't exist, download it automatically, + # then add chromedriver to path + +chrome_options = webdriver.ChromeOptions() +# Add your options as needed +options = [ + # Define window size here + "--ignore-certificate-errors", + "--headless", + #"--disable-gpu", + #"--window-size=1920,1200", + #"--ignore-certificate-errors", + #"--disable-extensions", + "--no-sandbox" + #"--disable-dev-shm-usage", + #'--remote-debugging-port=9222' +] + +for option in options: + chrome_options.add_argument(option) + + + +def test_wikipedia_python_results(): + print("testing wikipedia results to make sure BDFL is mentioned") + browser = webdriver.Chrome(options = chrome_options) + browser.get("http://wikipedia.org") + query = "Python (programming language)" + search_input = browser.find_element(By.ID,"searchInput") + search_input.clear() + search_input.send_keys(query) + search_input.send_keys(Keys.RETURN) + assert "Guido" in browser.page_source + time.sleep(10) + browser.close() + +def test_wikipedia_CPP_results(): + print("testing wikipedia results to make sure Bjarne is mentioned") + browser = webdriver.Chrome(options = chrome_options) + browser.get("http://wikipedia.org") + query = "C++" + search_input = browser.find_element(By.ID,"searchInput") + search_input.clear() + search_input.send_keys(query) + search_input.send_keys(Keys.RETURN) + assert "Bjarne Stroustrup" in browser.page_source + time.sleep(3) + browser.close() + + +if __name__ == "__main__": + test_wikipedia_python_results() + test_wikipedia_CPP_results() + print("done.") From 62dc59f27b165f1cc9cdae1102862837aa7fe1b9 Mon Sep 17 00:00:00 2001 From: Chris-G32 Date: Sun, 14 Apr 2024 19:58:30 -0400 Subject: [PATCH 02/18] commented and changed file pathing --- .github/workflows/test-selenium.yml | 38 +++++++++++++-------------- tests/{ => selenium}/requirements.txt | 0 tests/{ => selenium}/webdriver.py | 0 3 files changed, 18 insertions(+), 20 deletions(-) rename tests/{ => selenium}/requirements.txt (100%) rename tests/{ => selenium}/webdriver.py (100%) diff --git a/.github/workflows/test-selenium.yml b/.github/workflows/test-selenium.yml index 5c3be06..c59389b 100644 --- a/.github/workflows/test-selenium.yml +++ b/.github/workflows/test-selenium.yml @@ -1,20 +1,25 @@ name: Run Selenium Test Suite on: - workflow_dispatch: + push: + branches: [ "main","dev","Selenium" ] + pull_request: + branches: [ "main","dev" ] jobs: - scrape: + Test-Selenium: runs-on: ubuntu-latest steps: + #Set up environment - name: Checking out repo uses: actions/checkout@v3 - name: Setting up Python uses: actions/setup-python@v4 with: python-version: '3.9' - - name: Installing package list + - name: Installing package list #Not sure what this run: apt list --installed - - name: update - run: sudo apt update -y + #Chrome dependencies + - name: Update + run: sudo apt update -y - name: Install Chrome Shared Libraries run: | sudo apt-get update && sudo apt-get install -y \ @@ -53,17 +58,11 @@ jobs: libxtst6 \ lsb-release \ wget \ - xdg-utils - - name: Removing previous chrome instances on runner + xdg-utils + - name: Removing previous chrome instances on runner #This probably isnt necessary to have run: sudo apt purge google-chrome-stable - continue-on-error: true - - name: chrome install - uses: browser-actions/setup-chrome@v1 - - run: chrome --version - - name: Use Node.js - uses: actions/setup-node@v2 - - name: Install dependencies - run: npm install + continue-on-error: true + # Actually installing chrome - name: Install Chrome run: | wget https://dl-ssl.google.com/linux/linux_signing_key.pub -O /tmp/google.pub @@ -74,12 +73,11 @@ jobs: sudo apt-get install -y chromium-browser chromium-chromedriver -y sudo apt-get update -y + # Need to fetch reqs if needed - name: Installing all necessary packages - run: pip install chromedriver-autoinstaller selenium pyvirtualdisplay webdriver_manager - - name: Install xvfb - run: sudo apt install xvfb -y - continue-on-error: true + run: pip install -r tests/selenium/requirements.txt + # Run tests - name: Running the Python script - run: python3 tests/webdriver.py \ No newline at end of file + run: python3 tests/selenium/webdriver.py \ No newline at end of file diff --git a/tests/requirements.txt b/tests/selenium/requirements.txt similarity index 100% rename from tests/requirements.txt rename to tests/selenium/requirements.txt diff --git a/tests/webdriver.py b/tests/selenium/webdriver.py similarity index 100% rename from tests/webdriver.py rename to tests/selenium/webdriver.py From 9b662c5be405374ffbc0a11232f2edb302b9f742 Mon Sep 17 00:00:00 2001 From: Chris-G32 Date: Sun, 14 Apr 2024 20:00:20 -0400 Subject: [PATCH 03/18] Updated to sudo perms --- .github/workflows/test-selenium.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test-selenium.yml b/.github/workflows/test-selenium.yml index c59389b..0bf2b24 100644 --- a/.github/workflows/test-selenium.yml +++ b/.github/workflows/test-selenium.yml @@ -65,8 +65,8 @@ jobs: # Actually installing chrome - name: Install Chrome run: | - wget https://dl-ssl.google.com/linux/linux_signing_key.pub -O /tmp/google.pub - gpg --no-default-keyring --keyring /etc/apt/keyrings/google-chrome.gpg --import /tmp/google.pub + sudo wget https://dl-ssl.google.com/linux/linux_signing_key.pub -O /tmp/google.pub + sudo gpg --no-default-keyring --keyring /etc/apt/keyrings/google-chrome.gpg --import /tmp/google.pub echo 'deb [arch=amd64 signed-by=/etc/apt/keyrings/google-chrome.gpg] http://dl.google.com/linux/chrome/deb/ stable main' | sudo tee /etc/apt/sources.list.d/google-chrome.list sudo apt-get update -y sudo apt-get install google-chrome-stable -y From 87da0b4d46106a8e4067c95faa55242ec0e5402f Mon Sep 17 00:00:00 2001 From: Chris-G32 Date: Sun, 14 Apr 2024 23:40:16 -0400 Subject: [PATCH 04/18] See if this works on the pushed branch --- .github/workflows/test-selenium.yml | 5 ++- tests/selenium/webdriver.py | 50 ++++++++++++++++++++++++++--- 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/.github/workflows/test-selenium.yml b/.github/workflows/test-selenium.yml index 0bf2b24..7d9e6e8 100644 --- a/.github/workflows/test-selenium.yml +++ b/.github/workflows/test-selenium.yml @@ -77,7 +77,10 @@ jobs: # Need to fetch reqs if needed - name: Installing all necessary packages run: pip install -r tests/selenium/requirements.txt - + - name : Start Python's HTTP Server + run: | + echo "$ python3 -m http.server" + python3 -m http.server --bind 127.0.0.1 & # Run tests - name: Running the Python script run: python3 tests/selenium/webdriver.py \ No newline at end of file diff --git a/tests/selenium/webdriver.py b/tests/selenium/webdriver.py index 4d478fb..fc71310 100644 --- a/tests/selenium/webdriver.py +++ b/tests/selenium/webdriver.py @@ -3,7 +3,9 @@ from selenium.webdriver.chrome.service import Service from selenium import webdriver from selenium.webdriver.common.keys import Keys +from selenium.webdriver import Keys, ActionChains from selenium.webdriver.common.by import By + # from webdriver_manager.chrome import ChromeDriverManager import time import chromedriver_autoinstaller @@ -42,7 +44,7 @@ def test_wikipedia_python_results(): search_input.send_keys(query) search_input.send_keys(Keys.RETURN) assert "Guido" in browser.page_source - time.sleep(10) + browser.close() def test_wikipedia_CPP_results(): @@ -55,11 +57,51 @@ def test_wikipedia_CPP_results(): search_input.send_keys(query) search_input.send_keys(Keys.RETURN) assert "Bjarne Stroustrup" in browser.page_source - time.sleep(3) + browser.close() +def open_browser(link='127.0.0.1:8000/index.html'): + browser = webdriver.Chrome(options = chrome_options) + browser.get(link) + return browser +def test_execute(): + browser = webdriver.Chrome(options = chrome_options) + browser.get("127.0.0.1:8000/index.html") + time.sleep(1) + browser.find_element(By.NAME,'start').click() + time.sleep(4) + print(browser.execute_script('return testFunc();')) +def test_rooms(): + browser=open_browser() + time.sleep(3) + browser.find_element(By.NAME,'start').click() + time.sleep(2) + + print("Testing player can move rooms...") + start_room=browser.execute_script('return player.room;') + print("Starting in room: ",start_room) + #Move player to the room to the right, hopefully no obstacles :) + ActionChains(browser)\ + .key_down('d')\ + .perform() + time.sleep(2.5) + ActionChains(browser)\ + .key_down('w')\ + .perform() + time.sleep(1) + ActionChains(browser)\ + .key_up('w')\ + .key_up('d')\ + .perform() + time.sleep(3) + new_room=browser.execute_script('return player.room;') + assert(new_room['x']!=start_room) + print("Rooms changed!") + + if __name__ == "__main__": - test_wikipedia_python_results() - test_wikipedia_CPP_results() + # test_wikipedia_python_results() + # test_wikipedia_CPP_results() + test_rooms() print("done.") From cd9f30497f7dc123fac0a3408156ec3d19f6fc80 Mon Sep 17 00:00:00 2001 From: Chris-G32 Date: Sun, 14 Apr 2024 23:40:45 -0400 Subject: [PATCH 05/18] Run on test branch --- .github/workflows/test-selenium.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test-selenium.yml b/.github/workflows/test-selenium.yml index 7d9e6e8..2985241 100644 --- a/.github/workflows/test-selenium.yml +++ b/.github/workflows/test-selenium.yml @@ -1,7 +1,7 @@ name: Run Selenium Test Suite on: push: - branches: [ "main","dev","Selenium" ] + branches: [ "main","dev","Selenium",'selenium-test-room-change' ] pull_request: branches: [ "main","dev" ] jobs: From f1e95752885bcbf5e96f93d94d2bb75bb59cf604 Mon Sep 17 00:00:00 2001 From: Chris-G32 Date: Sun, 14 Apr 2024 23:45:39 -0400 Subject: [PATCH 06/18] Fixing link --- tests/selenium/webdriver.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/selenium/webdriver.py b/tests/selenium/webdriver.py index fc71310..f58bfd1 100644 --- a/tests/selenium/webdriver.py +++ b/tests/selenium/webdriver.py @@ -19,7 +19,7 @@ options = [ # Define window size here "--ignore-certificate-errors", - "--headless", + # "--headless", #"--disable-gpu", #"--window-size=1920,1200", #"--ignore-certificate-errors", @@ -59,7 +59,7 @@ def test_wikipedia_CPP_results(): assert "Bjarne Stroustrup" in browser.page_source browser.close() -def open_browser(link='127.0.0.1:8000/index.html'): +def open_browser(link='http://127.0.0.1:8000/index.html'): browser = webdriver.Chrome(options = chrome_options) browser.get(link) return browser From 624dbe3f2dd689e43925aaa66f414acbb28642be Mon Sep 17 00:00:00 2001 From: Chris-G32 Date: Sun, 14 Apr 2024 23:48:00 -0400 Subject: [PATCH 07/18] Accidentally left out headless --- tests/selenium/webdriver.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/selenium/webdriver.py b/tests/selenium/webdriver.py index f58bfd1..af6859e 100644 --- a/tests/selenium/webdriver.py +++ b/tests/selenium/webdriver.py @@ -19,7 +19,7 @@ options = [ # Define window size here "--ignore-certificate-errors", - # "--headless", + "--headless", #"--disable-gpu", #"--window-size=1920,1200", #"--ignore-certificate-errors", From c294e22f012efba86811d3d50642ce0cdfe054a1 Mon Sep 17 00:00:00 2001 From: Cars-n Date: Mon, 15 Apr 2024 14:55:29 -0400 Subject: [PATCH 08/18] added test for basic movement --- tests/selenium/webdriver.py | 85 +++++++++++++++++++++++++++++++++---- 1 file changed, 77 insertions(+), 8 deletions(-) diff --git a/tests/selenium/webdriver.py b/tests/selenium/webdriver.py index af6859e..d76da7d 100644 --- a/tests/selenium/webdriver.py +++ b/tests/selenium/webdriver.py @@ -20,13 +20,13 @@ # Define window size here "--ignore-certificate-errors", "--headless", - #"--disable-gpu", - #"--window-size=1920,1200", - #"--ignore-certificate-errors", - #"--disable-extensions", + # "--disable-gpu", + # "--window-size=1920,1200", + # "--ignore-certificate-errors", + # "--disable-extensions", "--no-sandbox" - #"--disable-dev-shm-usage", - #'--remote-debugging-port=9222' + # "--disable-dev-shm-usage", + # '--remote-debugging-port=9222' ] for option in options: @@ -70,6 +70,7 @@ def test_execute(): browser.find_element(By.NAME,'start').click() time.sleep(4) print(browser.execute_script('return testFunc();')) + def test_rooms(): browser=open_browser() time.sleep(3) @@ -96,12 +97,80 @@ def test_rooms(): new_room=browser.execute_script('return player.room;') assert(new_room['x']!=start_room) print("Rooms changed!") - - +def test_movement(): + browser=open_browser() + time.sleep(3) + browser.find_element(By.NAME,'start').click() + time.sleep(4) + + print("Testing player can move upwards...") + y_pos = browser.execute_script('return player.y;') + print("Player Y position: ", y_pos) + ActionChains(browser)\ + .key_down('w')\ + .perform() + time.sleep(.1) + ActionChains(browser)\ + .key_up('w')\ + .perform() + time.sleep(.1) + new_y_pos = browser.execute_script('return player.y;') + print("Player Y position after moving upwards: ", new_y_pos) + assert new_y_pos < y_pos, "Failed to move upwards" + print("Upward movement passed") + + print("Testing player can move downwards...") + y_pos = browser.execute_script('return player.y;') + print("Player Y position: ", y_pos) + ActionChains(browser)\ + .key_down('s')\ + .perform() + time.sleep(.1) + ActionChains(browser)\ + .key_up('s')\ + .perform() + time.sleep(.1) + new_y_pos = browser.execute_script('return player.y;') + print("Player Y position after moving downwards: ", new_y_pos) + assert new_y_pos > y_pos, "Failed to move downwards" + print("Downward movement passed") + + print("Testing player can move to the left...") + x_pos = browser.execute_script('return player.x;') + print("Player X position: ", x_pos) + ActionChains(browser)\ + .key_down('a')\ + .perform() + time.sleep(.1) + ActionChains(browser)\ + .key_up('a')\ + .perform() + time.sleep(.1) + new_x_pos = browser.execute_script('return player.x;') + print("Player X position after moving to the left: ", new_x_pos) + assert new_x_pos < x_pos, "Failed to move to the left" + print("Left movement passed") + + print("Testing player can move to the right...") + x_pos = browser.execute_script('return player.x;') + print("Player X position: ", x_pos) + ActionChains(browser)\ + .key_down('d')\ + .perform() + time.sleep(.1) + ActionChains(browser)\ + .key_up('d')\ + .perform() + time.sleep(.1) + new_x_pos = browser.execute_script('return player.x;') + print("Player X position after moving to the right: ", new_x_pos) + assert new_x_pos > x_pos, "Failed to move to the right" + print("Right movement passed") if __name__ == "__main__": # test_wikipedia_python_results() # test_wikipedia_CPP_results() + test_movement() test_rooms() print("done.") From 14834831d6bb5ef28d30731c7b7400ccefbfc3f1 Mon Sep 17 00:00:00 2001 From: Cars-n Date: Mon, 15 Apr 2024 16:09:57 -0400 Subject: [PATCH 09/18] added tests for movement and death --- tests/selenium/webdriver.py | 138 +++++++++++++++++++++++++++++++++++- 1 file changed, 136 insertions(+), 2 deletions(-) diff --git a/tests/selenium/webdriver.py b/tests/selenium/webdriver.py index 4d478fb..b9e2173 100644 --- a/tests/selenium/webdriver.py +++ b/tests/selenium/webdriver.py @@ -25,6 +25,8 @@ "--no-sandbox" #"--disable-dev-shm-usage", #'--remote-debugging-port=9222' + #"--disable-dev-shm-usage", + #'--remote-debugging-port=9222' ] for option in options: @@ -57,9 +59,141 @@ def test_wikipedia_CPP_results(): assert "Bjarne Stroustrup" in browser.page_source time.sleep(3) browser.close() +def open_browser(link='http://127.0.0.1:8000/index.html'): + browser = webdriver.Chrome(options = chrome_options) + browser.get(link) + return browser +def test_execute(): + browser = webdriver.Chrome(options = chrome_options) + browser.get("127.0.0.1:8000/index.html") + time.sleep(1) + browser.find_element(By.NAME,'start').click() + time.sleep(4) + print(browser.execute_script('return testFunc();')) + +def test_rooms(): + browser=open_browser() + time.sleep(3) + browser.find_element(By.NAME,'start').click() + time.sleep(2) + + print("Testing player can move rooms...") + start_room=browser.execute_script('return player.room;') + print("Starting in room: ",start_room) + #Move player to the room to the right, hopefully no obstacles :) + ActionChains(browser)\ + .key_down('d')\ + .perform() + time.sleep(2.5) + ActionChains(browser)\ + .key_down('w')\ + .perform() + time.sleep(1) + ActionChains(browser)\ + .key_up('w')\ + .key_up('d')\ + .perform() + time.sleep(3) + new_room=browser.execute_script('return player.room;') + assert(new_room['x']!=start_room) + print("Rooms changed!") + +def test_movement(): + browser=open_browser() + time.sleep(3) + browser.find_element(By.NAME,'start').click() + time.sleep(4) + + print("Testing player can move upwards...") + y_pos = browser.execute_script('return player.y;') + print("Player Y position: ", y_pos) + ActionChains(browser)\ + .key_down('w')\ + .perform() + time.sleep(.1) + ActionChains(browser)\ + .key_up('w')\ + .perform() + time.sleep(.1) + new_y_pos = browser.execute_script('return player.y;') + print("Player Y position after moving upwards: ", new_y_pos) + assert new_y_pos < y_pos, "Failed to move upwards" + print("Upward movement passed") + + print("Testing player can move downwards...") + y_pos = browser.execute_script('return player.y;') + print("Player Y position: ", y_pos) + ActionChains(browser)\ + .key_down('s')\ + .perform() + time.sleep(.1) + ActionChains(browser)\ + .key_up('s')\ + .perform() + time.sleep(.1) + new_y_pos = browser.execute_script('return player.y;') + print("Player Y position after moving downwards: ", new_y_pos) + assert new_y_pos > y_pos, "Failed to move downwards" + print("Downward movement passed") + + + print("Testing player can move to the left...") + x_pos = browser.execute_script('return player.x;') + print("Player X position: ", x_pos) + ActionChains(browser)\ + .key_down('a')\ + .perform() + time.sleep(.1) + ActionChains(browser)\ + .key_up('a')\ + .perform() + time.sleep(.1) + new_x_pos = browser.execute_script('return player.x;') + print("Player X position after moving to the left: ", new_x_pos) + assert new_x_pos < x_pos, "Failed to move to the left" + print("Left movement passed") + + + print("Testing player can move to the right...") + x_pos = browser.execute_script('return player.x;') + print("Player X position: ", x_pos) + ActionChains(browser)\ + .key_down('d')\ + .perform() + time.sleep(.1) + ActionChains(browser)\ + .key_up('d')\ + .perform() + time.sleep(.1) + new_x_pos = browser.execute_script('return player.x;') + print("Player X position after moving to the right: ", new_x_pos) + assert new_x_pos > x_pos, "Failed to move to the right" + print("Right movement passed") + +def test_death(): + browser=open_browser() + time.sleep(3) + browser.find_element(By.NAME,'start').click() + time.sleep(4) + print("Testing player death") + player_health = browser.execute_script('return player.health') + assert player_health > 0, "Player is dead on start" + print("Player has ",player_health," health") + browser.execute_script('player.health -= 200') + alert = browser.switch_to.alert + alert_text = alert.text + assert "died" in alert_text, "Expected alert didn't appear" + alert.dismiss() + gamestate = browser.execute_script('return GAMESTATE') + print(gamestate) + assert gamestate != "PLAYING", "Game state did not change" + print("Player death passed") if __name__ == "__main__": - test_wikipedia_python_results() - test_wikipedia_CPP_results() + # test_wikipedia_python_results() + # test_wikipedia_CPP_results() + test_movement() + test_rooms() + test_death() print("done.") From aaa6f534ec69c19821168516af5e0a1fca24a4c4 Mon Sep 17 00:00:00 2001 From: Cars-n <104526149+Cars-n@users.noreply.github.com> Date: Mon, 15 Apr 2024 17:00:10 -0400 Subject: [PATCH 10/18] added actionchains from selenium --- tests/selenium/webdriver.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/tests/selenium/webdriver.py b/tests/selenium/webdriver.py index b9e2173..f7683e4 100644 --- a/tests/selenium/webdriver.py +++ b/tests/selenium/webdriver.py @@ -3,7 +3,9 @@ from selenium.webdriver.chrome.service import Service from selenium import webdriver from selenium.webdriver.common.keys import Keys +from selenium.webdriver import Keys, ActionChains from selenium.webdriver.common.by import By + # from webdriver_manager.chrome import ChromeDriverManager import time import chromedriver_autoinstaller @@ -18,15 +20,13 @@ # Define window size here "--ignore-certificate-errors", "--headless", - #"--disable-gpu", - #"--window-size=1920,1200", - #"--ignore-certificate-errors", - #"--disable-extensions", + # "--disable-gpu", + # "--window-size=1920,1200", + # "--ignore-certificate-errors", + # "--disable-extensions", "--no-sandbox" - #"--disable-dev-shm-usage", - #'--remote-debugging-port=9222' - #"--disable-dev-shm-usage", - #'--remote-debugging-port=9222' + # "--disable-dev-shm-usage", + # '--remote-debugging-port=9222' ] for option in options: @@ -44,7 +44,7 @@ def test_wikipedia_python_results(): search_input.send_keys(query) search_input.send_keys(Keys.RETURN) assert "Guido" in browser.page_source - time.sleep(10) + browser.close() def test_wikipedia_CPP_results(): @@ -57,7 +57,7 @@ def test_wikipedia_CPP_results(): search_input.send_keys(query) search_input.send_keys(Keys.RETURN) assert "Bjarne Stroustrup" in browser.page_source - time.sleep(3) + browser.close() def open_browser(link='http://127.0.0.1:8000/index.html'): browser = webdriver.Chrome(options = chrome_options) @@ -136,7 +136,6 @@ def test_movement(): assert new_y_pos > y_pos, "Failed to move downwards" print("Downward movement passed") - print("Testing player can move to the left...") x_pos = browser.execute_script('return player.x;') print("Player X position: ", x_pos) @@ -153,7 +152,6 @@ def test_movement(): assert new_x_pos < x_pos, "Failed to move to the left" print("Left movement passed") - print("Testing player can move to the right...") x_pos = browser.execute_script('return player.x;') print("Player X position: ", x_pos) From 278d524cd8c0ddaccd7f8dbeb898424197a99256 Mon Sep 17 00:00:00 2001 From: Cars-n <104526149+Cars-n@users.noreply.github.com> Date: Mon, 15 Apr 2024 18:56:07 -0400 Subject: [PATCH 11/18] added death test --- tests/selenium/webdriver.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/selenium/webdriver.py b/tests/selenium/webdriver.py index d76da7d..f7683e4 100644 --- a/tests/selenium/webdriver.py +++ b/tests/selenium/webdriver.py @@ -168,9 +168,30 @@ def test_movement(): assert new_x_pos > x_pos, "Failed to move to the right" print("Right movement passed") +def test_death(): + browser=open_browser() + time.sleep(3) + browser.find_element(By.NAME,'start').click() + time.sleep(4) + + print("Testing player death") + player_health = browser.execute_script('return player.health') + assert player_health > 0, "Player is dead on start" + print("Player has ",player_health," health") + browser.execute_script('player.health -= 200') + alert = browser.switch_to.alert + alert_text = alert.text + assert "died" in alert_text, "Expected alert didn't appear" + alert.dismiss() + gamestate = browser.execute_script('return GAMESTATE') + print(gamestate) + assert gamestate != "PLAYING", "Game state did not change" + print("Player death passed") + if __name__ == "__main__": # test_wikipedia_python_results() # test_wikipedia_CPP_results() test_movement() test_rooms() + test_death() print("done.") From 3c208379b211dab5040e48f87e7296d22d66f741 Mon Sep 17 00:00:00 2001 From: Cars-n <104526149+Cars-n@users.noreply.github.com> Date: Mon, 15 Apr 2024 18:59:15 -0400 Subject: [PATCH 12/18] removed test_death --- tests/selenium/webdriver.py | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/tests/selenium/webdriver.py b/tests/selenium/webdriver.py index f7683e4..d76da7d 100644 --- a/tests/selenium/webdriver.py +++ b/tests/selenium/webdriver.py @@ -168,30 +168,9 @@ def test_movement(): assert new_x_pos > x_pos, "Failed to move to the right" print("Right movement passed") -def test_death(): - browser=open_browser() - time.sleep(3) - browser.find_element(By.NAME,'start').click() - time.sleep(4) - - print("Testing player death") - player_health = browser.execute_script('return player.health') - assert player_health > 0, "Player is dead on start" - print("Player has ",player_health," health") - browser.execute_script('player.health -= 200') - alert = browser.switch_to.alert - alert_text = alert.text - assert "died" in alert_text, "Expected alert didn't appear" - alert.dismiss() - gamestate = browser.execute_script('return GAMESTATE') - print(gamestate) - assert gamestate != "PLAYING", "Game state did not change" - print("Player death passed") - if __name__ == "__main__": # test_wikipedia_python_results() # test_wikipedia_CPP_results() test_movement() test_rooms() - test_death() print("done.") From 60aab959fde3641752d29fa228de30b705f52a11 Mon Sep 17 00:00:00 2001 From: Cars-n <104526149+Cars-n@users.noreply.github.com> Date: Mon, 15 Apr 2024 19:34:59 -0400 Subject: [PATCH 13/18] added sleep for alert --- tests/selenium/webdriver.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/tests/selenium/webdriver.py b/tests/selenium/webdriver.py index f7683e4..1abfdae 100644 --- a/tests/selenium/webdriver.py +++ b/tests/selenium/webdriver.py @@ -20,13 +20,13 @@ # Define window size here "--ignore-certificate-errors", "--headless", - # "--disable-gpu", - # "--window-size=1920,1200", - # "--ignore-certificate-errors", - # "--disable-extensions", + #"--disable-gpu", + #"--window-size=1920,1200", + #"--ignore-certificate-errors", + #"--disable-extensions", "--no-sandbox" - # "--disable-dev-shm-usage", - # '--remote-debugging-port=9222' + #"--disable-dev-shm-usage", + #'--remote-debugging-port=9222' ] for option in options: @@ -179,6 +179,7 @@ def test_death(): assert player_health > 0, "Player is dead on start" print("Player has ",player_health," health") browser.execute_script('player.health -= 200') + time.sleep(4) alert = browser.switch_to.alert alert_text = alert.text assert "died" in alert_text, "Expected alert didn't appear" From 3dd139537892a3a116b3dfebadaca4a92b154e74 Mon Sep 17 00:00:00 2001 From: Cars-n Date: Wed, 17 Apr 2024 21:42:38 -0400 Subject: [PATCH 14/18] removed wikipedia example and renamed selenium test file --- .github/workflows/test-selenium.yml | 2 +- tests/selenium/classExample.py | 36 +++++++++++++++++++ .../{webdriver.py => seleniumTests.py} | 29 --------------- 3 files changed, 37 insertions(+), 30 deletions(-) create mode 100644 tests/selenium/classExample.py rename tests/selenium/{webdriver.py => seleniumTests.py} (84%) diff --git a/.github/workflows/test-selenium.yml b/.github/workflows/test-selenium.yml index 2985241..33e7ca4 100644 --- a/.github/workflows/test-selenium.yml +++ b/.github/workflows/test-selenium.yml @@ -83,4 +83,4 @@ jobs: python3 -m http.server --bind 127.0.0.1 & # Run tests - name: Running the Python script - run: python3 tests/selenium/webdriver.py \ No newline at end of file + run: python3 tests/selenium/seleniumTests.py \ No newline at end of file diff --git a/tests/selenium/classExample.py b/tests/selenium/classExample.py new file mode 100644 index 0000000..86ccacc --- /dev/null +++ b/tests/selenium/classExample.py @@ -0,0 +1,36 @@ +from selenium import webdriver +from selenium.webdriver.common.keys import Keys +from selenium.webdriver.common.by import By +import time + +def test_wikipedia_python_results(): + print("testing wikipedia results to make sure BDFL is mentioned") + browser = webdriver.Chrome() + browser.get("http://wikipedia.org") + query = "Python (programming language)" + search_input = browser.find_element(By.ID,"searchInput") + search_input.clear() + search_input.send_keys(query) + search_input.send_keys(Keys.RETURN) + assert "Guido" in browser.page_source + time.sleep(10) + browser.close() + +def test_wikipedia_CPP_results(): + print("testing wikipedia results to make sure Bjarne is mentioned") + browser = webdriver.Chrome() + browser.get("http://wikipedia.org") + query = "C++" + search_input = browser.find_element(By.ID,"searchInput") + search_input.clear() + search_input.send_keys(query) + search_input.send_keys(Keys.RETURN) + assert "Bjarne Stroustrup" in browser.page_source + time.sleep(3) + browser.close() + + +if __name__ == "__main__": + test_wikipedia_python_results() + test_wikipedia_CPP_results() + print("done.") \ No newline at end of file diff --git a/tests/selenium/webdriver.py b/tests/selenium/seleniumTests.py similarity index 84% rename from tests/selenium/webdriver.py rename to tests/selenium/seleniumTests.py index 8b2a643..ccb2d9e 100644 --- a/tests/selenium/webdriver.py +++ b/tests/selenium/seleniumTests.py @@ -32,33 +32,6 @@ for option in options: chrome_options.add_argument(option) - - -def test_wikipedia_python_results(): - print("testing wikipedia results to make sure BDFL is mentioned") - browser = webdriver.Chrome(options = chrome_options) - browser.get("http://wikipedia.org") - query = "Python (programming language)" - search_input = browser.find_element(By.ID,"searchInput") - search_input.clear() - search_input.send_keys(query) - search_input.send_keys(Keys.RETURN) - assert "Guido" in browser.page_source - - browser.close() - -def test_wikipedia_CPP_results(): - print("testing wikipedia results to make sure Bjarne is mentioned") - browser = webdriver.Chrome(options = chrome_options) - browser.get("http://wikipedia.org") - query = "C++" - search_input = browser.find_element(By.ID,"searchInput") - search_input.clear() - search_input.send_keys(query) - search_input.send_keys(Keys.RETURN) - assert "Bjarne Stroustrup" in browser.page_source - - browser.close() def open_browser(link='http://127.0.0.1:8000/index.html'): browser = webdriver.Chrome(options = chrome_options) browser.get(link) @@ -190,8 +163,6 @@ def test_death(): print("Player death passed") if __name__ == "__main__": - # test_wikipedia_python_results() - # test_wikipedia_CPP_results() test_movement() test_rooms() test_death() From 172563ec08e8321c6b9f307c07de736c079c3eec Mon Sep 17 00:00:00 2001 From: Cars-n Date: Wed, 24 Apr 2024 17:22:56 -0400 Subject: [PATCH 15/18] added test for inventory --- tests/selenium/seleniumTests.py | 73 +++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/tests/selenium/seleniumTests.py b/tests/selenium/seleniumTests.py index ccb2d9e..f4cb716 100644 --- a/tests/selenium/seleniumTests.py +++ b/tests/selenium/seleniumTests.py @@ -162,8 +162,81 @@ def test_death(): assert gamestate != "PLAYING", "Game state did not change" print("Player death passed") +def test_inventory(): + browser=open_browser() + time.sleep(3) + browser.find_element(By.NAME,'start').click() + time.sleep(4) + + print("Testing player inventory") + print("Checking if player is empty on start") + empty_inventory = browser.execute_script('\ + for (let j = 0; j < InventoryController.INVENTORY_HEIGHT; j++) {\ + for (let i = 0; i < InventoryController.INVENTORY_WIDTH; i++) {\ + if (inventory.inventory[j][i] != "") return false;\ + }\ + }\ + return true;\ + ') + assert empty_inventory == True, "Player's inventory is not empty on start" + print("Creating flashlight item on player") + browser.execute_script('\ + flashlight = new Item(player.x,player.y, "FlashLight", 2,1,8,20,flashlightImage);\ + ') + time.sleep(.1) + has_item = browser.execute_script('return inventory.hasItem(flashlight)') + assert has_item == True, "Player did not pick up flashlight" + time.sleep(5) + + print("Creating gun item on player") + browser.execute_script('\ + gun = new Item(player.x,player.y, "Gun", 2,1,33,6,gunImage);\ + ') + time.sleep(.1) + has_item = browser.execute_script('return inventory.hasItem(gun)') + assert has_item == True, "Player did not pick up gun" + time.sleep(5) + + print("Creating bullet item on player") + browser.execute_script('\ + bulletItem = new Item(player.x,player.y, "Bullet", 1,1,4,3,bulletImage);\ + ') + time.sleep(.1) + has_item = browser.execute_script('return inventory.hasItem(bulletItem)') + assert has_item == True, "Player did not pick up bullet" + + print("Creating key item on player") + browser.execute_script('\ + key = new Item(player.x,player.y, "Key", 1,1,10,5,keyImage);\ + ') + time.sleep(.1) + has_item = browser.execute_script('return inventory.hasItem(key)') + assert has_item == True, "Player did not pick up key" + + print("Checking if player's inventory is full") + has_space = browser.execute_script('return inventory.hasSpace(1,1)') + assert has_space["orientation"] == "none", "Player has storage space left" + + print("Checking if items are not visible") + assert browser.execute_script('return flashlight.itemSprite.visible') == False, "Flashlight sprite is still visible" + assert browser.execute_script('return gun.itemSprite.visible') == False, "Gun sprite is still visible" + assert browser.execute_script('return bulletItem.itemSprite.visible') == False, "Bullet sprite is still visible" + assert browser.execute_script('return key.itemSprite.visible') == False, "Key sprite is still visible" + + print("Spawning item on player while inventory is full") + browser.execute_script('\ + key = new Item(player.x,player.y, "Key", 1,1,10,5,keyImage);\ + ') + time.sleep(.1) + has_item = browser.execute_script('return inventory.hasItem(key)') + assert has_item == False, "Player picked up key" + assert browser.execute_script('return key.itemSprite.visible') == True, "Key sprite is not visible" + print("Inventory test passed") + + if __name__ == "__main__": test_movement() test_rooms() test_death() + test_inventory() print("done.") From a629685a48c72655caf89890599b7510d4208d56 Mon Sep 17 00:00:00 2001 From: Chris-G32 Date: Mon, 29 Apr 2024 10:50:55 -0400 Subject: [PATCH 16/18] Added behavioral tests --- .github/workflows/test-selenium.yml | 4 +- tests/selenium/behavioral/menu.feature | 10 +++++ tests/selenium/behavioral/steps/behavioral.py | 41 +++++++++++++++++++ tests/selenium/requirements.txt | 3 +- 4 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 tests/selenium/behavioral/menu.feature create mode 100644 tests/selenium/behavioral/steps/behavioral.py diff --git a/.github/workflows/test-selenium.yml b/.github/workflows/test-selenium.yml index 33e7ca4..ad43826 100644 --- a/.github/workflows/test-selenium.yml +++ b/.github/workflows/test-selenium.yml @@ -83,4 +83,6 @@ jobs: python3 -m http.server --bind 127.0.0.1 & # Run tests - name: Running the Python script - run: python3 tests/selenium/seleniumTests.py \ No newline at end of file + run: python3 tests/selenium/seleniumTests.py + - name: Running behavioral tests + run: behave tests/selenium/behavioral/menu.feature \ No newline at end of file diff --git a/tests/selenium/behavioral/menu.feature b/tests/selenium/behavioral/menu.feature new file mode 100644 index 0000000..945b339 --- /dev/null +++ b/tests/selenium/behavioral/menu.feature @@ -0,0 +1,10 @@ +Feature: menu works + +Scenario Outline: Using menu + Given the site is loaded + When we click "" + Then the game state is "" + + Examples: Main Menu + | button_name | goal_state | + | start | PLAYING | diff --git a/tests/selenium/behavioral/steps/behavioral.py b/tests/selenium/behavioral/steps/behavioral.py new file mode 100644 index 0000000..74e2ac6 --- /dev/null +++ b/tests/selenium/behavioral/steps/behavioral.py @@ -0,0 +1,41 @@ +from selenium import webdriver +from selenium.webdriver.common.keys import Keys +from selenium.webdriver.common.by import By +from behave import * +import time + + +def open_browser(link='http://127.0.0.1:8000/index.html'): + chrome_options = webdriver.ChromeOptions() + # Add your options as needed + options = [ + # Define window size here + "--ignore-certificate-errors", + "--headless", + "--no-sandbox" + ] + + for option in options: + chrome_options.add_argument(option) + + browser = webdriver.Chrome(options = chrome_options) + browser.get(link) + time.sleep(3)#Sleep to load page + print("Page loaded...") + + # res=browser.find_element(By.NAME,"controls") + return browser + +@given(u'the site is loaded') +def step_impl(context): + context.browser=open_browser() + +@when(u'we click "{button_name}"') +def step_impl(context, button_name): + context.browser.find_element(By.NAME,button_name).click() + time.sleep(2) + + +@then(u'the game state is "{goal_state}"') +def step_impl(context, goal_state): + assert(context.browser.execute_script(f"return GAMESTATE;")==goal_state) \ No newline at end of file diff --git a/tests/selenium/requirements.txt b/tests/selenium/requirements.txt index 980464f..b4997e3 100644 --- a/tests/selenium/requirements.txt +++ b/tests/selenium/requirements.txt @@ -1,4 +1,5 @@ selenium chromedriver_autoinstaller pyvirtualdisplay -webdriver_manager \ No newline at end of file +webdriver_manager +behave \ No newline at end of file From 73cbb9b95c48cc14f3dc7046c6cbb986d8d9218a Mon Sep 17 00:00:00 2001 From: Chris-G32 Date: Mon, 29 Apr 2024 10:52:11 -0400 Subject: [PATCH 17/18] test on my branch --- .github/workflows/test-selenium.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test-selenium.yml b/.github/workflows/test-selenium.yml index ad43826..4711901 100644 --- a/.github/workflows/test-selenium.yml +++ b/.github/workflows/test-selenium.yml @@ -1,7 +1,7 @@ name: Run Selenium Test Suite on: push: - branches: [ "main","dev","Selenium",'selenium-test-room-change' ] + branches: [ "main","dev","Selenium","selenium-test-room-change","selenium-behavioral-tests"] pull_request: branches: [ "main","dev" ] jobs: From f4bf3ab7973eec2639d9b2660e41064f490b2fbd Mon Sep 17 00:00:00 2001 From: Chris-G32 Date: Mon, 29 Apr 2024 10:59:20 -0400 Subject: [PATCH 18/18] hail mary --- .github/workflows/test-selenium.yml | 5 +++-- tests/selenium/behavioral/steps/behavioral.py | 3 +++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test-selenium.yml b/.github/workflows/test-selenium.yml index 4711901..9698b23 100644 --- a/.github/workflows/test-selenium.yml +++ b/.github/workflows/test-selenium.yml @@ -81,8 +81,9 @@ jobs: run: | echo "$ python3 -m http.server" python3 -m http.server --bind 127.0.0.1 & + - name: Running behavioral tests + run: behave tests/selenium/behavioral/menu.feature # Run tests - name: Running the Python script run: python3 tests/selenium/seleniumTests.py - - name: Running behavioral tests - run: behave tests/selenium/behavioral/menu.feature \ No newline at end of file + \ No newline at end of file diff --git a/tests/selenium/behavioral/steps/behavioral.py b/tests/selenium/behavioral/steps/behavioral.py index 74e2ac6..19d1e31 100644 --- a/tests/selenium/behavioral/steps/behavioral.py +++ b/tests/selenium/behavioral/steps/behavioral.py @@ -3,9 +3,12 @@ from selenium.webdriver.common.by import By from behave import * import time +import chromedriver_autoinstaller +chromedriver_autoinstaller.install() def open_browser(link='http://127.0.0.1:8000/index.html'): + time.sleep(1) chrome_options = webdriver.ChromeOptions() # Add your options as needed options = [