Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions SiteElement.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0"?>
<sitemodel>
<pages>
<page name="LoginPage">
<element Key="LoginUserName">
<searchby>Id</searchby>
<value>login_login_username</value>
</element>
</page>
<page name="LoginPage">
<element Key="LoginPassword">
<searchby>Id</searchby>
<value>login_login_password</value>
</element>
</page>
<page name = "LoginPage">
<element Key="SubmitButton">
<searchby>Id</searchby>
<value>login_submit</value>
</element>
</page>
<page name="DashboardPage"><element Key="LogoutLink">
<searchby>LinkText</searchby>
<value>Logout</value>
</element></page>
<page name="DashboardPage"><element Key="Mahara">
<searchby>LinkText</searchby>
<value>Mahara</value>
</element></page>
</pages>

</sitemodel>
99 changes: 99 additions & 0 deletions ePO_Login.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import unittest
import time
import sys
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC


class ePO_login(unittest.TestCase):

"""
This sample exercise the explicit wait function
The ExpectedConditions are listed below:
prerequsite: my_email and password must be registered at manage.mcafee.com

title_is
title_contains
presence_of_element_located
visibility_of_element_located
visibility_of
presence_of_all_elements_located
text_to_be_present_in_element
text_to_be_present_in_element_value
frame_to_be_available_and_switch_to_it
invisibility_of_element_located
element_to_be_clickable - it is Displayed and Enabled.
staleness_of
element_to_be_selected
element_located_to_be_selected
element_selection_state_to_be
element_located_selection_state_to_be
alert_is_present
"""

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

def test_login(self):
driver = self.driver
driver.get("http://manage.mcafee.com")
assert "Log On" in driver.title
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Failed test

F
======================================================================
FAIL: test_login (__main__.ePO_login)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "ePO_Login.py", line 43, in test_login
    assert "Log On" in driver.title
AssertionError

----------------------------------------------------------------------
Ran 1 test in 3.880s

FAILED (failures=1)

elem = driver.find_element_by_id('logOnEmailAddress')
elem.clear()
elem.send_keys('my@email.com')
elem = driver.find_element_by_id('logOnPassword')
elem.clear()
elem.send_keys('password')
elem = driver.find_element_by_id('logOnButton')
elem.click()
# time.sleep(10) # implicit wait
# explicit wait EC.presence_of_element
try:
element = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.ID, "mfaTitle"))
)
except IOError as e:
print("I/O errors({0}): {1}".format(e.errorno, e.strerror))
except ValueError:
print("Value Error")
except:
print("Unexpected error (can't find the control)")
finally:
pass

elem = driver.find_element_by_xpath("//*[text()='Dashboards']")
elem.click()
# explicit wait EC.text_to_be_present
try:
element = WebDriverWait(driver, 10).until(
EC.text_to_be_present_in_element_value
((By.ID, "mfaTitle"), 'Dashboards')
)
except IOError as e:
print("I/O errors({0}): {1}".format(e.errorno, e.strerror))
except ValueError:
print("Value Error")
except:
print("Unexpected error (can't find the control)")
finally:
pass

elem = driver.find_element_by_xpath("//*[text()='Getting Started']")
elem.click()
elem = driver.find_element_by_xpath("//*[text()='Systems']")
elem.click()
elem = driver.find_element_by_xpath("//*[text()='Queries & Reports']")
elem.click()

assert "ePolicy" in driver.title

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

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

__author__ = 'mpsauto'
55 changes: 55 additions & 0 deletions selenium1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC


class PythonOrgSearch(unittest.TestCase):

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EE.
======================================================================
ERROR: test_explicit_wait (__main__.PythonOrgSearch)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "selenium1.py", line 42, in test_explicit_wait
    EC.presence_of_element_located((By.ID, "myDynamicElement")))
  File "/home/zexi/pyenvs/iclp3test/lib/python3.4/site-packages/selenium/webdriver/support/wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 
Stacktrace:
    at FirefoxDriver.prototype.findElementInternal_ (file:///tmp/tmp28b4_2lv/extensions/fxdriver@googlecode.com/components/driver-component.js:10770)
    at FirefoxDriver.prototype.findElement (file:///tmp/tmp28b4_2lv/extensions/fxdriver@googlecode.com/components/driver-component.js:10779)
    at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmp28b4_2lv/extensions/fxdriver@googlecode.com/components/command-processor.js:12661)
    at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmp28b4_2lv/extensions/fxdriver@googlecode.com/components/command-processor.js:12666)
    at DelayedCommand.prototype.execute/< (file:///tmp/tmp28b4_2lv/extensions/fxdriver@googlecode.com/components/command-processor.js:12608)

======================================================================
ERROR: test_implicit_wait (__main__.PythonOrgSearch)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "selenium1.py", line 48, in test_implicit_wait
    myDynamicElement = driver.find_element_by_id("myDynamicElement")
  File "/home/zexi/pyenvs/iclp3test/lib/python3.4/site-packages/selenium/webdriver/remote/webdriver.py", line 269, in find_element_by_id
    return self.find_element(by=By.ID, value=id_)
  File "/home/zexi/pyenvs/iclp3test/lib/python3.4/site-packages/selenium/webdriver/remote/webdriver.py", line 752, in find_element
    'value': value})['value']
  File "/home/zexi/pyenvs/iclp3test/lib/python3.4/site-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
    self.error_handler.check_response(response)
  File "/home/zexi/pyenvs/iclp3test/lib/python3.4/site-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"id","selector":"myDynamicElement"}
Stacktrace:
    at FirefoxDriver.prototype.findElementInternal_ (file:///tmp/tmpcasyjxa_/extensions/fxdriver@googlecode.com/components/driver-component.js:10770)
    at fxdriver.Timer.prototype.setTimeout/<.notify (file:///tmp/tmpcasyjxa_/extensions/fxdriver@googlecode.com/components/driver-component.js:625)

----------------------------------------------------------------------
Ran 3 tests in 26.584s

FAILED (errors=2)

'''
Learning point 1: Combination of Unittest framework with the selenium
Learning point 2: Web page navigation: driver.get(), forward(), back()
Learning point 3: Find element(s): id,name,xpath,class_name,link,tag,css
Learning point 3: Web interaction: click, send_keys,
Learning point 4: todo: select from drop-down options
Learning point 5: todo: drag-and-drop,
Learning point 6: todo: switch between windows and frames (iframe)
Learning point 7: todo: handling popup dialog
Learning point 8: todo: explicit wait (conditions) and implicit wait
Learning point 9: todo: execution sequence
Learning point10: todo: PageObjectModel (see python_org.py codes)
'''

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

def test_search_in_python_org(self):
driver = self.driver
driver.get("http://www.google.com")
assert "Google" in driver.title
elem = driver.find_element_by_name('q')
elem.clear()
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source

def test_explicit_wait(self):
driver = self.driver
driver.get("http://somedomain/url_that_delays_loading")
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "myDynamicElement")))

def test_implicit_wait(self):
driver = self.driver
driver.implicitly_wait(10) # sec
driver.get("http://somedomain/url_that_delays_loading")
myDynamicElement = driver.find_element_by_id("myDynamicElement")

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


if __name__ == "__main__":
unittest.main()
59 changes: 59 additions & 0 deletions selenium2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import unittest
import xml.etree.ElementTree as ET
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

tree = ET.parse('SiteElement.xml')
root = tree.getroot()

def getSearchValue(pagenameattribute, elementkeystring):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please fix pep8 error for this file selenium2.py.

xpathstring = "./pages/page[@name='" + \
pagenameattribute+"']/element[@Key='" + \
elementkeystring+"']"
print(xpathstring)
for element in root.findall(xpathstring):
value = element.find('value').text
return value

def getSearchBy(pagenameattribute, elementkeystring):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getSearchBy() never used?

xpathstring = "./page/page[@name='" + \
pagenameattribute+"']/element[@Key='" + \
elementkeystring + "']"
print(xpathstring)
for element in root.findall('searchby').text:
searchby = element.find('searchby')
return searchby


class DemoMaharaOrgLogin(unittest.TestCase):
'''
Learning Point: The page struc is described in SiteElement.xml
'''
def setUp(self):
self.driver = webdriver.Firefox()

def test_login_in_demo_mahara_org(self):
driver = self.driver
driver.get("http://demo.mahara.org/")
self.assertIn("Home - Mahara", driver.title)

username = driver.find_element_by_id(getSearchValue
("LoginPage", "LoginUserName"))
username.send_keys(("student1"))

password = driver.find_element_by_id(
getSearchValue("LoginPage", "LoginPassword"))
password.send_keys("Testing1")

loginbutton = driver.find_element_by_id(getSearchValue
("LoginPage", "SubmitButton"))
loginbutton.click()

self.assertTrue(driver.find_element_by_link_text(
getSearchValue("DashboardPage", "LogoutLink")), "Logout link")

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

if __name__ == "__main__":
unittest.main()
36 changes: 36 additions & 0 deletions selenium3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from selenium import webdriver
from page_objects import PageObject, PageElement


class LoginPage(PageObject):
'''
Page Object Model sample code package (page_objects)
https://page-objects.readthedocs.io/en/latest/tutorial.html
https://media.readthedocs.org/pdf/page-objects/latest/page-objects.pdf

<html>
<head>
<title>Login Page</title>
</head>
<body>
<form type="POST" action="/login">
<input type="text" name="username" id="user-input"></input>
<input type="password" name="password"></input>
<input type="submit">Submit</input>
</form>
</body>
</html>
'''
username = PageElement(id_='user-input')
password = PageElement(name='password')
login = PageElement(css='input[type="submit"]')
form = PageElement(tag_name='form')


driver = webdriver.PhantomJS()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is phantomjs? any traps for installation on Linux 64, Mac, and Windows?

driver.get("http://example.com")
page = LoginPage(driver)
page.username = 'secret'
page.password = 'squirrel'
assert page.username.text == 'secret'
page.login.click()