How to selenium test a website that uses Google OAuth - selenium

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.

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")))

Related

Bypassing Youtube Login + reCaptcha with Selenium

I'm trying to get the e-mail of Youtube Channel. This is an example link. As you can see, to see the e-mail you have to LOGIN and then bypass RECAPTCHA. Honestly saying, I'm stuck at the first stage. Google doesn't think the selenium browser isn't safe to login and blocks it. The reason (I'm guessing) is because it thinks I'm an automized browser. So basically I have to let it think it's not a bot. I tried randomizing user-agent, and turning off the auto extension by code, but it still blocks my scraper. I have also checked if the selenium browser has "turned-off" the javascript since Google told me to check if it is.
So, I'm guessing there is a way to scrape using Scrapy or requests? Or I haven't tried enough with Selenium? I've seen an useful video that scrapes subtitles from Youtube without loggin by TechLead. So it seems it's not impossible. I'm still working on it, so if anyone has an answer or advice please let me know, thank you for reading.
Refence
Google login block Selenium Google Login Block
Randomizing User Agent Way to change Google Chrome user agent in Selenium?
Websites detecting Selenium Automation How to make Selenium script undetectable using GeckoDriver and Firefox through Python?
Try this code, for bypassing Gmail login.
Use Seleniumwire with undetected browser v2
Note: put chromedriver in your sys path.
from seleniumwire.undetected_chromedriver.v2 import Chrome, ChromeOptions
import time
options = {}
chrome_options = ChromeOptions()
chrome_options.add_argument('--user-data-dir=hash')
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--incognito")
chrome_options.add_argument("--disable-dev-shm-usage")
# chrome_options.add_argument("--headless")
browser = Chrome(seleniumwire_options=options, options=chrome_options)
browser.get('https://gmail.com')
browser.find_element_by_xpath('//*[#id="identifierId"]').send_keys('your-email')
browser.find_element_by_xpath('//*[#id="identifierNext"]/div/button').click()
time.sleep(5)
browser.find_element_by_xpath('//*[#id="password"]/div[1]/div/div[1]/input').send_keys('you-password')
browser.find_element_by_xpath('//*[#id="passwordNext"]/div/button').click()
In addition to this, selenium wire has many awesome features, check out Github repository

Change WebDriver from GUI to Headless

I want to automate an application for an upload process with selenium.Therefore I am using geckodriver. Right now I am doing the login site headless but I want to do the login by the user and then change to headless. Is there a way to do that or at least a work around?
There is no way to do that using the same browser instance... The headless flag is a setting passed to the browser on startup and there is no way to dynamically change that.
If the site uses cookie-based authentication, here is an alternate approach:
login with browser in normal (GUI) mode
export saved cookies
instantiate a new browser in headless mode
navigate to the site again with the headless driver
add saved cookies to the new headless driver
... at that point, you can navigate to an authenticated page. You should be "logged in" since you are re-using the cookies from the original browser session.

Selenium Webdriver Login activity on Amazon.com: “Verification needed”

I am trying to run some automated tests on amazon.com by using Selenium Webdriver. I tried to log in to the system but after entering the password, system either directs to “Verification needed” page and says “We will email you a code to verify your identity. This is required when something about your sign-in activity changes, like signing in from a new device or location.”
or directs to “wrong password or email” page (eventhough I try correct account information).
So my question is: How can I handle that two factor authentication in my test script?
Note: I am using ChromeDriver and Java
there are two possible approaches:
first one, you can as Kiril S mentioned, get the link from email (mailinator.com is fine inbox for such testing).
second approach is avoiding the second verification with existing browser profile. Selenium uses a temporary profile, that's probably the reason of the second verification. I'm using existing browser profile (firefox), see Selenium Chromedriver - Open Chrome Normally. For Chrome should be similar.

Can't login to gmail via Selenium Webdriver, asks for recovery email?

I am using Selenium Webdriver version 3.4 and chromedriver 2.35.528157. My test consists of entering a system through google, so when I click login through google it asks me to enter my email. When I enter my email and click next it asks for the screenshot attached, it does not get to enter password screen. I tried adding a recovery mail but if I follow the steps it just sends a verification code to the recovery email and complicates things all over. The weird part is if I do this manually in an incognito browser, Firefox driver or in headless mode on chrome everything works fine.
from selenium.webdriver.common.keys import Keys
variableForTextFieldId.send_keys(Keys.ENTER)
I was having the same issue as you when using either .click() or .submit(). However, I ended up using the Keys variable ENTER and it allowed me to get to the next part of login (The password screen).
You must be using the class for the xpath, which is same for 'Next' and 'Forgot email' links. Therefore it clicks Forgot Email and lands on the Email Recovery page.
There are 3 classes in the Next button itself. Try using the id of the first one.
Command:
driver.findElement(By.id("identifierNext")).click();
This will lead you to the Password page. Let me know it helped.

Diffrence between opening a browser/site through selenium and Manually

I have started learning selenium Webdriver API recently. I was trying to automate a simple task of launching gmail.com and logging into account using Chrome browser.
The difference I noticed is when I open browser manually it lists my accounts on the Home page and then I can select the account, enter the password and login in complete
When I launch gmail.com using selenium it directly gives me log in window when I can enter email and password
Any reason why we see the difference while launching browser through selenium