Create a gist now

Instantly share code, notes, and snippets.

@ArneSwinnen /login.py Secret
Created Feb 10, 2016

What would you like to do?
login.py
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
from time import sleep
import unittest, re
import datetime
class Login(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(5)
self.base_url = "http://gmail.com/"
self.verificationErrors = []
self.accept_next_alert = True
def test_login(self):
driver = self.driver
driver.get("https://accounts.google.com/ServiceLogin?service=mail&continue=https://mail.google.com/mail/")
driver.find_element_by_id("Email").clear()
driver.find_element_by_id("Email").send_keys("twofactorpremiumnumber@gmail.com")
sleep(1)
driver.find_element_by_id("next").click()
driver.find_element_by_id("PersistentCookie").click()
driver.find_element_by_id("Passwd").clear()
driver.find_element_by_id("Passwd").send_keys("*****redacted*****")
sleep(1)
driver.find_element_by_id("signIn").click()
driver.find_element_by_id("trustDevice").click()
try:
self.assertNotEqual("Wrong code. Try again.", driver.find_element_by_id("errorMsg").text)
except NoSuchElementException as e:
pass
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException as e: return False
return True
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException as e: return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment