Monday, March 21, 2011

Using FITNesse and Selenium for acceptance testing, example 2

This example is about using FITNesse QueryTable fixture with Selenium. Let's consider next scenario. There is a list of SharePoint sites and an administrator can grant permissions to users for any site in this list. A result of such action is a report that contains list of the users with sites they have permissions for.

The best way to check that the action is competed succefully is to check that given user is presented in this report and has appropriate permissions set. It is very easy to do using the QueryTable fixture.
!|script|
|start|Browser|
|$port=|get port|
|go|http://${IP_HOST}:$port/make/search?filter=wappl&search_limit=20|
|wait for load complete;|
|set selected;|id=${wa7}|
|set selected;|id=${wa8}|
|click|id=btnGrant-button|
!|script|
|is element displayed;|id=dialogGrant|
|check|get_value|id=id_scope_grant|${wa8};${wa7};|
|type;|id=id_users_grant|${domain}\${U1};${domain}\${U2}|
|click|xpath=//input[@name="Full Control"]|
|submit|xpath=//form[@id="formGrantPermissions"]|
!|script|
|wait for element is not displayed;|id=perpetum|${actionTimeout}|
|Subset Query:permissions actions report|${domain}\${U1}|
|site|level|
|${SITE_COLLECTION1}|${PM}|
|${SITE_COLLECTION2}|${PM}|
|${wa7}|${PM}|
|${wa8}|${PM}|

Just define a class with the query method and a constructor that takes a user name. Pay attention that this QueryTable class should aggregate the Browser class instance to access data on the html page.
class PermissionsActionsReport():
def __init__(self,u):
self.ip = Browser()
self.user = u
def query(self):
ret = []
i=0
while True:
i=i+1
try:
curuser = self.ip.get_text('xpath=//*[@id="result"]/ul/li[%s]/b'%str(i))
if self.user.lower() == curuser.lower():
break
except:
return []
j=0
while True:
j=j+1
try:
url = self.ip.get_text('xpath=//*[@id="result"]/ul/li[%s]/table/tbody/tr[%s]/td/div/span/a'%(str(i),str(j)))
permissions = self.ip.get_text('xpath=//*[@id="result"]/ul/li[%s]/table/tbody/tr[%s]/td[2]'%(str(i),str(j)))
print "%i. %s %s" % (j, url, permissions)
if type(url)!=type(u""):
return ret
ret.append([['site',url],['level',permissions]])
except:
return ret
view raw QueryTable.py hosted with ❤ by GitHub

But actually here is a problem. An architecture of FITNesse does not allow the PermissionsActionsReport QueryTable class instance to access existed instance of the Browser class that was used to make the action and that contains the result of this action. Fortunately this obstacle can be solved easy using the singleton pattern.
class Browser(object):
__active = False
def __new__(type, browser):
if not '_instance' in type.__dict__:
type._instance = object.__new__(type)
return type._instance
def __init__(self, browser):
if browser == 'chrome':
from selenium.chrome.webdriver import WebDriver
elif browser == 'firefox':
from selenium.firefox.webdriver import WebDriver
else:
from selenium.ie.webdriver import WebDriver
if not self.__active:
self.__wd = WebDriver()
self.__active = True
view raw webdrive-2.py hosted with ❤ by GitHub

No comments:

Post a Comment