Skip to content

Commit

Permalink
Add automated browser testing
Browse files Browse the repository at this point in the history
- Moved automated-testing to browser-extension
- Added requirements.txt to reduce setup steps
- Updated slur detection scripts for chrome, firefox
- Added slur detection scripts for brave, chromium, edge
- Updated gitignore
  • Loading branch information
duggalsu committed Sep 20, 2023
1 parent e2e3177 commit 60e33c6
Show file tree
Hide file tree
Showing 14 changed files with 310 additions and 43 deletions.
2 changes: 2 additions & 0 deletions browser-extension/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules/
.scannerwork
automated-testing/venv*
automated-testing/firefox/geckodriver.log
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.os_manager import ChromeType
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By

from selenium.webdriver.common.alert import Alert
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from time import sleep
from selenium.webdriver.remote.webelement import WebElement


# function to expand the shadow element to get the extension id on chrome://extensions
def expand_shadow_element(element):
shadow_root = driver.execute_script('return arguments[0].shadowRoot', element)
print("expand_shadow_element executed!")
return shadow_root

# Configure the necessary command-line option
options = webdriver.ChromeOptions()

# TODO: Note that you will need to download the build of the extension and put the path to the dist folder
options.add_argument(r'--load-extension=/path/to/extension/dist/')

# installing chromedriver
driver = webdriver.Chrome(service=Service(ChromeDriverManager(chrome_type=ChromeType.BRAVE).install()), options = options)
sleep(2)

# loading the extensions page to get extension id
driver.get('chrome://extensions')
sleep(5)

# workflow with the chrome extensions page and its shadow elements
root1 = driver.find_element(By.TAG_NAME, 'extensions-manager')
shadow_root_1 = expand_shadow_element(root1)
root2 = shadow_root_1.find_element(By.CSS_SELECTOR, 'extensions-item-list')
shadow_root_2 = expand_shadow_element(root2)
ele = shadow_root_2.find_element(By.CSS_SELECTOR, 'extensions-item')
extension_id = ele.get_attribute("id")
print(str(extension_id)) # printing the extension id

# going to the extensions page
url = "chrome-extension://" + extension_id + "/options.html"
driver.get(url)

# activate plugin
activate_button = driver.find_element('id', 'app_btn_activate') #activating account
activate_button.click()

# Confirm page reload on activation
WebDriverWait(driver, 10).until(EC.alert_is_present())
driver.switch_to.alert.accept()

# Save settings
save_button = driver.find_element('id', 'app_btn_save') #saving the settings
save_button.click()

# add a hardcoded url leading to a particular tweet which you are using for testing
driver.get('https://twitter.com/jackantonoff/status/1579311659742416896')
sleep(20)
# Check if the extension worked and log the result.
try:
# logic to check if the slur is replaced
ele = driver.find_element('xpath','//div[contains(text(),"▓")]')
print('Success! :-)')
# if no such span is present where the character - ▓ - is not present then the Failure message will show up
# Note that this will return success if the tweet by default contains the replacement character
except NoSuchElementException:
print('Failure! :-(')
finally:
''' Clean up. '''
driver.quit()
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.os_manager import ChromeType
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By

from selenium.webdriver.common.alert import Alert
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from time import sleep
from selenium.webdriver.remote.webelement import WebElement


# function to expand the shadow element to get the extension id on chrome://extensions
def expand_shadow_element(element):
shadow_root = driver.execute_script('return arguments[0].shadowRoot', element)
print("expand_shadow_element executed!")
return shadow_root


# Configure the necessary command-line option
options = webdriver.ChromeOptions()

# TODO: Note that you will need to download the build of the extension and put the path to the dist folder
options.add_argument(r'--load-extension=/path/to/extension/dist/')

# installing chromedriver
driver = webdriver.Chrome(service=Service(ChromeDriverManager(chrome_type=ChromeType.GOOGLE).install()), options = options)
sleep(2)

# loading the extensions page to get extension id
driver.get('chrome://extensions')
sleep(5)

# workflow with the chrome extensions page and its shadow elements
root1 = driver.find_element(By.TAG_NAME, 'extensions-manager')
shadow_root_1 = expand_shadow_element(root1)
root2 = shadow_root_1.find_element(By.CSS_SELECTOR, 'extensions-item-list')
shadow_root_2 = expand_shadow_element(root2)
ele = shadow_root_2.find_element(By.CSS_SELECTOR, 'extensions-item')
extension_id = ele.get_attribute("id")
print(str(extension_id)) # printing the extension id

# going to the extensions page
url = "chrome-extension://" + extension_id + "/options.html"
driver.get(url)

# activate plugin
activate_button = driver.find_element('id', 'app_btn_activate') #activating account
activate_button.click()

# Confirm page reload on activation
WebDriverWait(driver, 10).until(EC.alert_is_present())
driver.switch_to.alert.accept()

# Save settings
save_button = driver.find_element('id', 'app_btn_save') #saving the settings
save_button.click()

# add a hardcoded url leading to a particular tweet which you are using for testing
driver.get('https://twitter.com/jackantonoff/status/1579311659742416896')
sleep(20)
# Check if the extension worked and log the result.
try:
# logic to check if the slur is replaced
ele = driver.find_element('xpath','//div[contains(text(),"▓")]')
print('Success! :-)')
# if no such span is present where the character - ▓ - is not present then the Failure message will show up
# Note that this will return success if the tweet by default contains the replacement character
except NoSuchElementException:
print('Failure! :-(')
finally:
''' Clean up. '''
driver.quit()
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.os_manager import ChromeType
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By

from selenium.webdriver.common.alert import Alert
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from time import sleep
from selenium.webdriver.remote.webelement import WebElement


# function to expand the shadow element to get the extension id on chrome://extensions
def expand_shadow_element(element):
shadow_root = driver.execute_script('return arguments[0].shadowRoot', element)
print("expand_shadow_element executed!")
return shadow_root


# Configure the necessary command-line option
options = webdriver.ChromeOptions()
# TODO: Note that you will need to download the build of the extension and put the path to the dist folder
options.add_argument(r'--load-extension=/path/to/extension/dist/')

# installing chromedriver
driver = webdriver.Chrome(service=Service(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install()), options = options)
sleep(2)

# loading the extensions page to get extension id
driver.get('chrome://extensions')
sleep(5)

# workflow with the chrome extensions page and its shadow elements
root1 = driver.find_element(By.TAG_NAME, 'extensions-manager')
shadow_root_1 = expand_shadow_element(root1)
root2 = shadow_root_1.find_element(By.CSS_SELECTOR, 'extensions-item-list')
shadow_root_2 = expand_shadow_element(root2)
ele = shadow_root_2.find_element(By.CSS_SELECTOR, 'extensions-item')
extension_id = ele.get_attribute("id")
print(str(extension_id)) # printing the extension id

# going to the extensions page
url = "chrome-extension://" + extension_id + "/options.html"
driver.get(url)

# activate plugin
activate_button = driver.find_element('id', 'app_btn_activate') #activating account
activate_button.click()

# Confirm page reload on activation
WebDriverWait(driver, 10).until(EC.alert_is_present())
driver.switch_to.alert.accept()

# Save settings
save_button = driver.find_element('id', 'app_btn_save') #saving the settings
save_button.click()

# add a hardcoded url leading to a particular tweet which you are using for testing
driver.get('https://twitter.com/jackantonoff/status/1579311659742416896')
sleep(20)
# Check if the extension worked and log the result.
try:
# logic to check if the slur is replaced
ele = driver.find_element('xpath','//div[contains(text(),"▓")]')
print('Success! :-)')
# if no such span is present where the character - ▓ - is not present then the Failure message will show up
# Note that this will return success if the tweet by default contains the replacement character
except NoSuchElementException:
print('Failure! :-(')
finally:
''' Clean up. '''
driver.quit()
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from selenium import webdriver
from selenium.webdriver.edge.service import Service
from webdriver_manager.microsoft import EdgeChromiumDriverManager
from webdriver_manager.core.os_manager import ChromeType
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By

from selenium.webdriver.common.alert import Alert
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from time import sleep
from selenium.webdriver.remote.webelement import WebElement


# function to expand the shadow element to get the extension id on chrome://extensions
def expand_shadow_element(element):
shadow_root = driver.execute_script('return arguments[0].shadowRoot', element)
print("expand_shadow_element executed!")
return shadow_root


# Configure the necessary command-line option
options = webdriver.ChromeOptions()
# Note that you will need to download the build of the extension and put the path to the dist folder
options.add_argument(r'--load-extension=/path/to/extension/dist/')

# installing chromedriver
driver = webdriver.Edge(service=Service(EdgeChromiumDriverManager().install()), options = options)
sleep(2)

# loading the extensions page to get extension id
driver.get('chrome://extensions')
sleep(5)

# workflow with the chrome extensions page and its shadow elements
root1 = driver.find_element(By.TAG_NAME, 'extensions-manager')
shadow_root_1 = expand_shadow_element(root1)
root2 = shadow_root_1.find_element(By.CSS_SELECTOR, 'extensions-item-list')
shadow_root_2 = expand_shadow_element(root2)
ele = shadow_root_2.find_element(By.CSS_SELECTOR, 'extensions-item')
extension_id = ele.get_attribute("id")
print(str(extension_id)) # printing the extension id

# going to the extensions page
url = "chrome-extension://" + extension_id + "/options.html"
driver.get(url)

# activate plugin
activate_button = driver.find_element('id', 'app_btn_activate') #activating account
activate_button.click()

# Confirm page reload on activation
WebDriverWait(driver, 10).until(EC.alert_is_present())
driver.switch_to.alert.accept()

# Save settings
save_button = driver.find_element('id', 'app_btn_save') #saving the settings
save_button.click()

# add a hardcoded url leading to a particular tweet which you are using for testing
driver.get('https://twitter.com/jackantonoff/status/1579311659742416896')
sleep(20)
# Check if the extension worked and log the result.
try:
# logic to check if the slur is replaced
ele = driver.find_element('xpath','//div[contains(text(),"▓")]')
print('Success! :-)')
# if no such span is present where the character - ▓ - is not present then the Failure message will show up
# Note that this will return success if the tweet by default contains the replacement character
except NoSuchElementException:
print('Failure! :-(')
finally:
''' Clean up. '''
driver.quit()
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,17 @@
from selenium.webdriver.common.by import By


# Configure the necessary command-line option
options = webdriver.FirefoxOptions()
driver = webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install()))

driver = webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install()), options = options)
# Note that you will need to run the web-ext command inside the dist folder
# put the location to the xpi file here, it should end in /path/to/folder/web-ext-artifacts/[id].xpi
driver.install_addon('path/to/xpi-file')
# TODO: Add path to dist folder
driver.install_addon('/path/to/extension/dist/', temporary=True)
sleep(5)

# add a hardcoded url leading to a particular tweet which you are using for testing
# a test url is already given
driver.get('https://twitter.com/jackantonoff/status/1579311659742416896')


sleep(5)

# Check if the extension worked and log the result.
try:
# logic to check if the slur is replaced
Expand Down
2 changes: 2 additions & 0 deletions browser-extension/automated-testing/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
selenium == 4.12.0
webdriver-manager == 4.0.0
35 changes: 0 additions & 35 deletions ogbv-ml-rest/automated-testing/slur_detection_tester_chrome.py

This file was deleted.

0 comments on commit 60e33c6

Please sign in to comment.