Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to selenium test a website that uses Google OAuth

I'm building a website that uses Google OAuth to authenticate users and to perform actions in Google (sending emails, creating calendar events, etc.) on the user's behalf.

I want to use selenium to test that the website works as I expect. This includes website specific stuff (e.g. pressing this button causes this entry in the DB), but also Google specific stuff (e.g. pressing this button causes this exact email to be sent).

How do I reasonably test this using selenium? How can I log in automatically? Is there any way at all for me to test that user X can't perform these certain actions but user Y can?

Currently I save a JSONified user record (with Google credentials) in a file and load that file when the tests set up. If the file can't be found then it boots up a browser window and sleeps until I've manually signed in using that browser window. This feels hacky and fragile. It also prevents me from having CI testing because the user record file is only available on my machine.

like image 574
Billy Avatar asked Apr 15 '16 11:04

Billy


People also ask

What Cannot be tested using selenium?

Bitmap comparison is not possible using Selenium WebDriver. Automating Captcha is not possible using Selenium WebDriver. We can not read bar code using Selenium WebDriver.

Can we automate Gmail login using selenium?

We can automate the Gmail login process using Selenium webdriver in Java. To perform this task, first we have to launch the Gmail login page and locate the email, password and other elements with the findElement method and then perform actions on them.


1 Answers

You could create different accounts allowing different privileges. Then simply automate the login just like a real user would do. This is a Python example to login to StackOverflow through GMail:

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

driver = webdriver.Firefox()
wait = WebDriverWait(driver, 10)

driver.get("https://stackoverflow.com/users/login")

# Click GMail login
driver.find_element_by_xpath("//span[.='Google']").click()

# type email
wait.until(EC.presence_of_element_located((By.ID, "Email"))).send_keys('...')

# click next
wait.until(EC.presence_of_element_located((By.ID, "next"))).click()

# type password
wait.until(EC.presence_of_element_located((By.ID, "Passwd"))).send_keys('...')

# click signin
wait.until(EC.presence_of_element_located((By.ID, "signIn"))).click()

# wait for the end of the redirection
wait.until(EC.presence_of_element_located((By.ID, "nav-questions")))
like image 131
Florent B. Avatar answered Oct 11 '22 15:10

Florent B.