Selenium.docx
- 文档编号:10621902
- 上传时间:2023-02-21
- 格式:DOCX
- 页数:30
- 大小:29.64KB
Selenium.docx
《Selenium.docx》由会员分享,可在线阅读,更多相关《Selenium.docx(30页珍藏版)》请在冰豆网上搜索。
Selenium
SeleniumUI-ElementReference
Introduction
UI-ElementisaSeleniumfeaturethatmakesitpossibletodefineamappingbetweensemanticallymeaningfulnamesofelementsonwebpages,andtheelementsthemselves.ThemappingisdefinedusingJavaScriptObjectNotation,andmaybesharedbothbytheIDEandtestsrunviaSeleniumRC.Italsooffersasinglepointofupdateshouldtheuserinterfaceoftheapplicationundertestchange.
Terminology
Page
AuniqueURL,andthecontentsavailablebyaccessingthatURL.Apagetypicallyconsistsofseveralinteractivepageelements.ApagemayalsobeconsideredaDOMdocumentobject,completewithURLinformation.
Pageelement
Anelementontheactualwebpage.Generallyspeaking,anelementisanythingtheusermightinteractwith,oranythingthatcontainsmeaningfulcontent.Morespecifically,anelementisrealizedasaDocumentObjectModel(DOM)nodeanditscontents.Sowhenwerefertoapageelement,wemeanbothofthefollowing,atthesametime:
∙somethingonthepage
∙itsDOMrepresentation,includingitsrelationshipwithotherpageelements
Pageset
Asetofpagesthatsharesomesetofcommonpageelements.Forexample,Imightbeabletologintomyapplicationfromseveraldifferentpages.Ifcertainpageelementsoneachofthosepagesappearsimilarly(i.e.theirDOMrepresentationsareidentical),thosepagescanbegroupedintoapagesetwithrespecttothesepageelements.Thereisnorestrictiononhowmanypagesetsagivenpagecanbeamemberof.Similarly,aUIelementbelongtomultiplepagesets.ApagesetiscommonlyrepresentedbyaregularexpressionwhichmatchestheURL'sthatuniquelyidentifypages;however,therearecaseswhenthepagecontentmustbeconsideredtodeterminepagesetmembership.Apagesetalsohasaname.
UIelement
Amappingbetweenameaningfulnameforapageelement,andthemeanstolocatethatpageelement'sDOMnode.Thepageelementislocatedviaalocator.UIelementsbelongtopagesets.
UIargument
AnoptionalpieceoflogicthatdetermineshowthelocatorisgeneratedbyaUIelement.Typicallyusedwhensimilarpageelementsappearmultipletimesonthesamepage,andyouwanttoaddressthemallwithasingleUIelement.Forexample,ifapagepresents20clickablesearchresults,theindexofthesearchresultmightbeaUIargument.
UImap
Acollectionofpagesets,whichinturncontainUIelements.TheUImapisthemediumfortranslatingbetweenUIspecifierstrings,pageelements,andUIelements.
UIspecifierstring
Abitoftextcontainingapagesetname,aUIelementname,andoptionallyargumentsthatmodifythewayalocatorisconstructedbytheUIelement.UIspecifierstringsareintendedtobethehuman-readableidentifierforpageelements.
Rolluprule
LogicthatdescribeshowoneormoreSeleniumcommandscanbegroupedintoasinglecommand,andhowthatsinglecommandmaybeexpandedintoitscomponentSeleniumcommands.Thesinglecommandisreferredtosimplyasa"rollup".
Commandmatcher
Typicallyfoldedintoarolluprule,itmatchesoneormoreSeleniumcommandsandoptionallysetsvaluesforrollupargumentsbasedonthematchedcommands.Arollupruleusuallyhasoneormorecommandmatchers.
Rollupargument
Anoptionalpieceoflogicthatmodifiesthecommandexpansionofarollup.
TheBasics
GettingMotivated
Question:
WhyuseUI-Element?
Answer:
Soyourtestcasescanlooklikethis(boilerplatecodeomitted):
:
section(section=topics)
:
topic(topic=Process)
:
subtopic(subtopic=Creativity)
:
article(index=2)
IncludingtheRightFiles
UI-ElementisnowfullyintegratedwithSelenium.Theonlyadditionalfilethatneedstobespecifiedisyourmapdefinitionsfile.IntheIDE,addittothecomma-delimitedSeleniumCoreextensionsfieldoftheIDEoptions.Asampledefinitionfilecreatedforthewebsiteisincludedinthedistributionandisavailablehere:
chrome:
//selenium-ide/content/selenium-core/scripts/ui-map-sample.js
YoumightwanttoexperimentwiththesamplemaptogetafeelforUI-Element.FortheSeleniumRC,youhavetwooptions.Themapfilemaybeincludedintheuser-extensions.jsfilespecifiedatstartupwiththe-userExtensionsswitch.Or,youmayloaditdynamicallywiththevariantofthesetUserExtensionJscommandinyourdriverlanguage,beforethebrowserisstarted.
MapDefinitionsFileSyntax
Thisisthegeneralformatofamapfile:
varmap=newUIMap();
map.addPageset({
name:
'aPageset'
...
});
map.addElement('aPageset',{...});
map.addElement('aPageset',{...});
...
map.addPageset({
name:
'anotherPageset'
...
});
...
ThemapobjectisinitializedbycreatinganewUIMapobject.Next,apagesetisdefined.ThenoneormoreUIelementsaredefinedforthatpageset.Morepagesetsaredefined,eachwithcorrespondingUIelements.That'sit!
PagesetShorthand
ThemethodsignatureofaddPageset()is(pagesetShorthand).pagesetShorthandisaJSONdescriptionofthepageset.Here'saminimalexample:
map.addPageset({
name:
'allPages'
description:
'containselementscommontoallpages'
pathRegexp:
'.*'
});
Here'satablecontaininginformationabouttheattributesofthePagesetobject.TheconditionallyrequiredorunrequireditemsareforIDErecordingsupportonly.
Name
Required?
Description
Example
name
Yes
(String)thenameofthepageset.Thisshouldbeuniquewithinthemap.
name:
'shopPages'
description
Yes
(String)adescriptionofthepageset.Ideally,thiswillgivethereaderanideaofwhattypesofUIelementsthepagesetwillhave.
description:
'allpagesdisplayingproduct'
pathPrefix
No
(String)thepathoftheURLofallincludedpagesinthispagesetwillcontainthisprefix.Forexample,ifallpagesareoftheformthepageprefixmightbegallery/.
pathPrefix:
'gallery/'
paths
pathRegexp
Conditional
(Array|String)eitheralistofpathstrings,orastringthatrepresentsaregularexpression.Oneortheothershouldbedefined,butnotboth.Ifanarray,itenumeratespagesthatareincludedinthepageset.Ifaregularexpression,anypageswhoseURLpathsmatchtheexpressionareconsideredpartofthepageset.Ineithercase,thepartoftheURLbeingmatched(calledthepath)isthepartfollowingthedomain,lessanytrailingslash,andnotincludingtheCGIparameters.Forexample:
∙
∙
∙
Theentirepathmustmatch(however,pathPrefixistakenintoaccountifspecified).Ifspecifiedasaregularexpression,thetworegularexpressioncharacters^and$markingthestartandendofthematchedstringareincludedimplicitly,andshouldnotbespecifiedinthisstring.Pleasenoticetoothatbackslashesmustbebackslash-escapedinjavascriptstrings.
paths:
[
'gotoHome.do'
'gotoAbout.do'
'gotoFaq.do'
]
pathRegexp:
'goto(Home|About|Faq)\\.do'
paramRegexps
No
(Object)amappingfromURLparameternamestoregularexpressionstringswhichmustmatchtheirvalues.Ifspecified,thesetofpagespotentiallyincludedinthispagesetwillbefurtherfilteredbyURLparametervalues.Thereisnofilteringbyparametervaluebydefault.
paramRegexps:
{
dept:
'^[abcd]$'
team:
'marketing'
}
pageContent
Conditional
(Function)afunctionthattestswhetherapage,representedbyitsdocumentobject,iscontainedinthepageset,andreturnstrueifandonlyifthisisthecase.Ifspecified,thesetofpagespotentiallyincludedinthispagesetwillbefurtherfilteredbycontent,afterURLandURLparameterfiltering.
SincetheURLisavailablefromthedocumentobject(document.location.href),youmayencodethelogicusedforthepathsandpathRegexpattributesallintothedefinitionofpageContent.Thus,youmaychoosetoomittheformerifandonlyifusingpageContent.Ofcourse,youmaycontinuetousethemforclarity.
pageContent:
function(doc){
varid='address-tab';
returndoc.getElementById(id)!
=null;
}
UI-ElementShorthand
ThemethodsignatureofaddElement()is(pagesetName,uiElementShorthand).pagesetNameisthenameofthepagesettheUIelementisbeingaddedto.uiElementShorthandisacompleteJSONdescriptionoftheUIelementobjectinshorthandnotation.
Initssimplestform,aUIelementobjectlookslikethis:
map.addElement('allPages',{
name:
'about_link'
description:
'linktotheaboutpage'
locator:
"//a[contains(@href,'about.php')]"
});
Here'satablecontaininginformationabouttheattributesoftheUIelementobject.Theasterisk(*)meansanystring:
Name
Required?
Description
Example
name
Yes
(String)thenameoftheUIelement
name:
'article'
description
Yes
(String)adescriptionoftheUIelement.ThisisthemaindocumentationforthisUIelement,sothemoredetailed,thebetter.
description:
'frontorissuepagelinktoarticle'
args
No
(Array)alistofargumentsthatmodifythegetLocator()method.Ifunspecified,itwillbetreatedasanemptylist.
[
{name:
'index'
description:
'theindexoftheauthor,byarticle'
defaultValues:
range(1,5)}
]
Seesectionbelowelaboratingonattributesofargumentobjects.
locator
getLocator()
xpathgetXPath()
Yes
(String|Function)eitherafixedlocatorstring,orafunctionthatreturnsalo
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Selenium