Skip to content

Instantly share code, notes, and snippets.

@ArneSwinnen
Created February 10, 2016 01:43
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save ArneSwinnen/a5edd4b47ca6a4de38e7 to your computer and use it in GitHub Desktop.
Save ArneSwinnen/a5edd4b47ca6a4de38e7 to your computer and use it in GitHub Desktop.
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