Tanakhs / Frontend

0 stars 0 forks source link

google login - unit test #3

Open eli-entelis opened 1 year ago

eli-entelis commented 1 year ago

Description: As part of the software testing effort for the upcoming release, a Selenium test needs to be developed to verify the login functionality of the web application.

Acceptance Criteria:

  1. click on google login button => authenticate => check that access token in the response
  2. If the login fails due to incorrect credentials, the test should display an appropriate error message.
:warning: WARNING
this is not an integration test, only client side
marking2 commented 1 year ago

Testing login is practically impossible using Chrome as the driver detects a bot sign-in. This function probably has to be tested in Firefox, though the logic is the same. Need to understand how to disguise the password in the code though(to provide the sign in info)

eli-entelis commented 1 year ago

If you can do it in any browser, great :) about the password, you can put it in .env file, see API project example. we use load_dotenv() to load the variables in the file (by default it searches for a file named .env). make sure you add this file to .gitignore. and maybe it would be good to create a Gmail testing account, so that you wont need to use yours

eli-entelis commented 1 year ago

this is from chatgpt

import time
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class TestAuth(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Chrome()

    def tearDown(self):
        self.driver.close()

    def test_auth(self):
        # Open the website
        self.driver.get("https://example.com")

        # Click the Google login button
        google_login_button = self.driver.find_element(By.XPATH, '//button[@id="google-login-button"]')
        google_login_button.click()

        # Switch to the Google login window
        self.driver.switch_to.window(self.driver.window_handles[-1])

        # Enter the email address and click next
        email_input = self.driver.find_element(By.XPATH, '//input[@type="email"]')
        email_input.send_keys("your_google_email@gmail.com")
        next_button = self.driver.find_element(By.XPATH, '//button[@id="identifierNext"]')
        next_button.click()

        # Enter the password and click next
        password_input = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, '//input[@type="password"]')))
        password_input.send_keys("your_google_password")
        next_button = self.driver.find_element(By.XPATH, '//button[@id="passwordNext"]')
        next_button.click()

        # Wait for the Google OAuth to complete and switch back to the main window
        self.driver.switch_to.window(self.driver.window_handles[0])
        WebDriverWait(self.driver, 10).until(EC.url_contains("example.com"))
        time.sleep(2)  # Wait for the login to complete

        # Assert that the user is logged in by checking the user avatar
        user_avatar = self.driver.find_element(By.XPATH, '//div[@class="user-avatar"]')
        self.assertIsNotNone(user_avatar)

if __name__ == "__main__":
    unittest.main()