tattle-made / Uli

Software and Resources for Mitigating Online Gender Based Violence in India
https://uli.tattle.co.in
GNU General Public License v3.0
40 stars 29 forks source link

Add selenium test for local archiving on Chrome #181

Closed dennyabrain closed 2 years ago

dennyabrain commented 2 years ago

Clicking on the camera icon in Uli downloads an image containing the screenshot of that tweet. Lets write a selenium test that does the following :

  1. Lauch chrome/firefox with uli loaded into it
  2. Click on the camera icon
  3. Check if an image was saved locally
alpesh-rama commented 2 years ago

Please assign me to this issue. Thank you

Bhargav-Dave commented 2 years ago

Hey @alpesh-rama pasting some code here for your reference, the following should be useful to load up the extension and run the relevant tests. The code I'm pasting is a simple script useful for testing whether the slur detection is working fine.

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.common.exceptions import NoSuchElementException

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

''' 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\dist_folder_of_extension')

''' installing chromedriver '''
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options = options)
sleep(2)
''' add a hardcoded url leading to a particular tweet which you are using for testing '''
driver.get('your url here')
sleep(5)
''' Check if the extension worked and log the result. '''
try:
    ele = driver.find_element('xpath','//span[contains(text(),"▓")]')
    print('Success! :-)')
except NoSuchElementException:
    print('Failure! :-(')
finally:
    ''' Clean up. '''
    driver.quit()

Let me know if you have any doubts regarding the same

alpesh-rama commented 2 years ago

Thank you! The issue I was having was coding a sleep to allow for logging in. I can see you seem to have helped here, so I'll give that another go ...

Bhargav-Dave commented 2 years ago

Sure! It might be better to work with hard coded urls and a locally downloaded build of Uli as of now instead of logging into twitter. The Uli features would work regardless of you logging into twitter, just that you would not be able to verify it working on the "infinite scroll" of your timeline. Just use hard coded urls leading to any tweets for the purpose of your code as of now, that should save up a lot of time!

Bhargav-Dave commented 2 years ago

Hey @alpesh-rama, here's some more code to help you out. I wrote the following code to test the archiving feature for chrome and I'm fairly certain about it functioning however you can run it on your end and confirm. The task now only remains to be done on firefox. Can you focus on the same? It'll be great if you let me know any progress you've made on your end or any specific roadblock you are facing.

Here's the code I wrote:


from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.common.exceptions import NoSuchElementException
from time import sleep
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.common.by import By
import os

options = webdriver.ChromeOptions()
# add the absolute path to your extension here
options.add_argument(r'--load-extension=path\to\dist_folder_of_extension')

# initiate the driver for chrome
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options = options)
sleep(2)
# hardcode the tweet you wish to load up for testing, a sample url is alraedy present with a known slur
driver.get('https://twitter.com/jackantonoff/status/1579311659742416896')
sleep(5)
# Check if the extension worked and log the result.
try:
    # add the absolute path to the directory where your browser downloads files by default (you can verify this by commenting the files counting steps once an seeing where the new files are)
    initial_count = len(os.listdir(r'path\to\downloads_folder')) # count the number of files in your downloads directory
    twt_control_bar = driver.find_element(By.CLASS_NAME,'ogbv-tweetcontrol-bar') 
    icons = twt_control_bar.find_elements(By.TAG_NAME, 'svg')
    icons[0].click() # initiates download
    sleep(10)
    # sleep(100) if you wish to check where your download folder is
    final_count = len(os.listdir(r'C:\Users\bharg\Downloads')) #recount the number of files in your downloads folder
    if(final_count > initial_count):
        print('File archived successfully! :-)')  
    else:
        print('Please check the path to the downloads folder!') 
except NoSuchElementException:
    print('Failure! :-(')
finally:
    # Clean up.
    driver.quit()

You can reference this for Firefox: https://intoli.com/blog/firefox-extensions-with-selenium/

dennyabrain commented 2 years ago

This has been developed for chrome. We have to get this working on firefox now. Closing the issue. For any progress on firefox, lets move the discussion here - https://github.com/tattle-made/OGBV/issues/191