Skip to content

Commit

Permalink
Merge branch 'Selenium' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris-G32 committed Apr 29, 2024
2 parents fb6230d + 175602a commit 2388f3f
Show file tree
Hide file tree
Showing 6 changed files with 426 additions and 0 deletions.
89 changes: 89 additions & 0 deletions .github/workflows/test-selenium.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
name: Run Selenium Test Suite
on:
push:
branches: [ "main","dev","Selenium","selenium-test-room-change","selenium-behavioral-tests"]
pull_request:
branches: [ "main","dev" ]
jobs:
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 #Not sure what this
run: apt list --installed
#Chrome dependencies
- 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 #This probably isnt necessary to have
run: sudo apt purge google-chrome-stable
continue-on-error: true
# Actually installing chrome
- name: Install Chrome
run: |
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
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 -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 &
- name: Running behavioral tests
run: behave tests/selenium/behavioral/menu.feature
# Run tests
- name: Running the Python script
run: python3 tests/selenium/seleniumTests.py

10 changes: 10 additions & 0 deletions tests/selenium/behavioral/menu.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Feature: menu works

Scenario Outline: Using menu
Given the site is loaded
When we click "<button_name>"
Then the game state is "<goal_state>"

Examples: Main Menu
| button_name | goal_state |
| start | PLAYING |
44 changes: 44 additions & 0 deletions tests/selenium/behavioral/steps/behavioral.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
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 = [
# 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)
36 changes: 36 additions & 0 deletions tests/selenium/classExample.py
Original file line number Diff line number Diff line change
@@ -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.")
5 changes: 5 additions & 0 deletions tests/selenium/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
selenium
chromedriver_autoinstaller
pyvirtualdisplay
webdriver_manager
behave
Loading

0 comments on commit 2388f3f

Please sign in to comment.