{% extends 'base.html' %} {% block title %}æ¥å£ææ¡£{% endblock %} {% block head_style %} {% endblock %} {% block head_script %} {% endblock %} {% block head %}{% include 'common/head.html' %}{% endblock %} {% block content %}
monitor.sendError/monitor.error;
åéé误信æ¯
monitor.sendLog/monitor.log;
åéæè¿°ä¿¡æ¯ï¼ä¸æ¯ä¸ä¸ªé误ï¼
monitor.sendWarn/monitor.warn;
åéè¦åä¿¡æ¯
monitor.sendSMS/monitor.sms;
åéçä¿¡éç¥
monitor.sendMail/monitor.mail;
åéé®ä»¶éç¥
monitor.complete();
æ¾ç¤ºè°ç¨è¯¥åæµæ§è¡å®æï¼å»ºè®®æ¯ä¸ä¸ªåæµé½æ¾ç¤ºè°ç¨completeæ¹æ³
monitor.run
é¾å¼æ§è¡æ¹æ³ä½
monitor.jsonp(callback_name,handle)
å¤ç页é¢jsonp请æ±ï¼éªè¯æ°æ®ç
monitor.step(xxx,handler)
å½å¶é¡µé¢è¡ä¸ºåæ§è¡æ¯ä¸æ¥çå½æ°(å·²åºå¼ï¼ä½åæåè½ä¸å)
monitor.createAction(option)
å建ä¸ä¸ªè¡ä¸ºå¯¹è±¡ï¼æåç¨æ·è¡ä¸ºå侧代ç çæ¶å使ç¨
monitor.runæ¹æ³ï¼runæ¹æ³å¯ä»¥é¾å¼è°ç¨
monitor.run(function(){ //åééè¯¯ä¿¡æ¯ monitor.sendError("xxx"); // or monitor.error("xxx"); }).complete(); /*monitor.sendLog("xxx")*/ monitor.run(function(){ monitor.sendLog("xxx"); // or monitor.log("xxx"); }).complete();
é¾å¼è°ç¨run
monitor.run(function(){ if(document.title.indexOf("ç¾åº¦") !== 0){ monitor.sendError("页é¢titleé误"); } }).run(function(){ if(!document.getElementById("test")){ monitor.sendWarn("ID为testçDOMå ç´ ä¸åå¨"); // or monitor.warn("xxxx"); } }).complete();
循ç¯åé
monitor.run(function(){ var links = document.getElementsByTagName("a"); for(var i = 0,len = links.length; i < len; i++){ if(links[i].getAttribute("href").indexOf("http://") == -1){ monitor.sendWarn("页é¢åå¨ç¸å¯¹é¾æ¥ï¼" + links[i].href); } } monitor.complete(); });
monitor.jsonp
//è¿æ ·è°ç¨ä¹åï¼é¡µé¢ä¸åèµ·ç该jsonp_callbackçjsonp请æ±ï¼å°±ä¼å¤çä¸é¢çåè°å½æ°ï¼åæçé»è¾ä¹ä¼æ£å¸¸æ§è¡ monitor.jsonp("window.jsonp_callback",function(data){ if(!data.name){ monitor.error("nameæ°æ®ä¸åå¨"); } });
monitor.setp
//éè¦ç¹å«æ³¨æä¸ç¹çæ¯ï¼step第ä¸ä¸ªåæ°åé¢çæ°åï¼æ¯æ¶é´åºåï¼é½æ¯ç¸å¯¹äºç¬¬ä¸ä¸ªstepçæ¶é´é´é //å³æ¯æ¯ä¸ä¸ªstepçæ§è¡æ¶é´ï¼é½æ¯åå»ç¬¬ä¸ä¸ªstepåçæ¶é´åºåï¼æç §è¿ä¸ªåºåæ§è¡æ¯ä¸ä¸ªstep //å æ¤ï¼å¨æå¨åwaitè¡ä¸ºçæ¶åï¼éè¦æ³¨æä¸ä¸ï¼æ¶é´æ°å¼è¦æ¯ä¸ä¸ä¸ªstepä¸çæ¶é´æ°å¼å¤§å°±è¡ /* ç®åmonitor.stepæ¹æ³æå ç§ç±»åï¼ type: 对ä¸ä¸ªè¾å ¥æ¡(input,textarea,select) èµå¼ click: 触åä¸ä¸ªå ç´ çclickæä½ mouseover: 触åä¸ä¸ªå ç´ çmouseoveræä½ wait: çå¾ å ç§å继ç»æ§è¡ï¼è¿ä¸ªä¸»è¦æ¯æåçstepç±»åï¼ */ // typeãclickãmouseoverç±»åçæåä¸ä¸ªåè°å½æ°çåæ°ï¼å°±æ¯å½åçDOMå ç´ monitor.step("type::#op_train_start::æ¬::0",function(el){}) .step("type::#op_train_start::::239",function(el){}) .step("type::#op_train_start::b::1479",function(el){}) .step("click::#tangram-suggestion--TANGRAM__3-item4::3346",function(el){}) .step("type::#op_train_end::s::5160",function(el){}) .step("click::#tangram-suggestion--TANGRAM__g-item4::7137",function(el){}) .step("click::#1>tbody>tr>td>font:nth-child(3)>div:nth-child(1)>table>tbody>tr:nth-child(1)>td:nth-child(3)>a::9242",function(el){}) .step("click::#1>tbody>tr>td>font:nth-child(3)>div:nth-child(1)>table>tbody>tr:nth-child(1)>td:nth-child(3)>a::10305",function(el){}); //waitç±»åçstep: monitor.step("click::#example::0") .step("wait::2000",function(){ //è¿éç¼åç¹å»2såçéªè¯ä»£ç });
monitor.createAction(option)
/* Tipï¼å¡æ¯æ¶åå°ä¼ éç®æ å ç´ åæ°çï¼é½æ¯æselectoræè DOMå ç´ çå¼ç¨çæ¹å¼ */ /* å建ä¸ä¸ªè¡ä¸ºï¼ä¸ä¸ªcaseéé¢å¯ä»¥å£°æå¤ä¸ªè¡ä¸ºï¼*/ var action = monitor.createAction(); //å¯ä»¥å¸¦ä¸ªåæ°ï¼æå®åå§ç®æ å ç´ var action = monitor.createAction({ target : "#txt" }); //ç®åActionä¸çæ¹æ³æï¼type/click/clickOnce/mouseover/mouseup/mousedown/wait/complete/endï¼typeãclickãmouseoverãwaitæ¹æ³æ¯æé¾å¼è°ç¨ // type: 对ä¸ä¸ªè¾å ¥æ¡(input,textarea,select) èµå¼ // click: 触åä¸ä¸ªå ç´ çclickæä½(ä¼ä½¿ç¨ä¸ç§æ¹å¼å»è§¦åï¼åççfireEventãjQuery triggerãè°ç¨å ç´ çclickæ¹æ³) // clickOnce : 触åä¸ä¸ªå ç´ çclickæä½ï¼åªä½¿ç¨ä¸ç§æ¹æ³è§¦åäºä»¶ï¼åççfireEventï¼ // mouseover : 触åä¸ä¸ªå ç´ çmouseoveræä½ // mouseup : 触åä¸ä¸ªå ç´ çmouseupæä½ // mousedown : 触åä¸ä¸ªå ç´ çmousedownæä½ // wait: çå¾ å¤é¿æ¶é´ç»§ç»æ§è¡ä¸ä¸ä¸ªæ¥éª¤ // complete/end: æ¾ç¤ºè°ç¨è¯¥è¡ä¸ºç»æï¼å¹¶ä¸è®¾ç½®è¯¥å侧已å®æï¼å¦ææå¤ä¸ªè¡ä¸ºï¼åæåä¸ä¸ªè¡ä¸ºè°ç¨complete/endæ¹æ³å³å¯ï¼å¦æåªæä¸ä¸ªï¼é£ä¹è¯¥è¡ä¸ºè°ç¨end以ç»æ该åä¾§ï¼ /* * ç¹å«è¯´æä¸ä¸click/clickOnceçåå¨ç®çï¼æäºåºæ¯å»è§¦åclickäºä»¶ï¼ä½¿ç¨ä¸é¢ä¸ç§æ¹æ³ä¸çæä¸ç§é½æ²¡æ³è§¦åï¼ * æ以ç»åä¸ç§æ åµå»è§¦åï¼ * ä½æ¯è¿æ ·çè¯ï¼æå¯è½äºä»¶ä¼è¢«è§¦åä¸æ¬¡ï¼æè 被触åä¸æ¬¡ï¼å ¶ä»ç触åæ æï¼è¿æ¯ä¸ºäºä¿è¯clickä¸å®ä¼è¢«è§¦åã * clickOnceå°±æ¯ä¸ºäºä¸é¢ç被触åå¤æ¬¡åæ¬èº«åºç°é®é¢ï¼æ以å®å°±æ¯ç¨åççfireEventå»è§¦åä¸æ¬¡ã */ /* å¼å§æ§è¡è¡ä¸º */ action.type("123456"); //æè æå®ç®æ å ç´ action.type("#txt","123456"); //or action.type(document.getElementById("txt"),"123456").complete(); //or action.type("123456",function(el){ console.log(el.value); }).complete(); //带ç¹å»è¡ä¸ºï¼mouseoverè¡ä¸ºç±»ä¼¼ï¼ action.type("#txt","12345").click().complete(); //带ä¸ä¸ªåè°ï¼åè°å½æ°çåæ°å°±æ¯ç®æ å ç´ çDOMå¼ç¨ action.type("#txt","123").wait(1000).click(function(el){ console.log(el.value); }).complete(); //å¨è¡ä¸ºä¸ä¹å¯ä»¥éæ°æå®ç®æ å ç´ action.type("123").wait(2000).click("#btn",function(el){}).complete(); //å ¶å®ï¼wait/endæ¹æ³ä¹æ¯æåè°å½æ°çï¼å¯ä»¥ç¨å¨ä¸äºç¹å»æé®åéjsonpçåºæ¯éé¢ï¼éªè¯ææ action.type("#txt","123").wait(2000).click("#btn").wait(2000,function(){ console.log($("#txt").val()); }); //å½å¶åºæ¥ç代ç å¦ä¸æç¤ºï¼ var action = monitor.createAction(); .type("#op_hangban_start","s",function(el){}) .wait(654,function(){}) .click("#tangram-suggestion--TANGRAM__3-item5",function(el){}) .wait(2248,function(){}) .type("#op_hangban_end","s",function(el){}) .wait(639,function(){}) .click("#tangram-suggestion--TANGRAM__g-item7",function(el){}) .complete(function(){});
monitor.define(name,handler)
å®ä¹ä¸ä¸ªmoduleéç¨æµè¯ä»£ç ï¼ä¾å
¶ä»çæµè¯ä»£ç 使ç¨
monitor.require(names,handler)
使ç¨ä¸ä¸ªæè
å¤ä¸ªmoduleséç¨æµè¯ä»£ç çåè½
monitor.define(name,handler)
/* * monitor.defineæ¥å£ï¼å®ä¹ä¸ä¸ªmoduleçèªå®ä¹éç¨æµè¯ä»£ç ï¼æ¯å¦å°è£ ä¸äºéç¨çæµè¯ä»£ç ï¼é¿å éå¤ç代ç ç¼å * defineæ¹æ³ä¸æ¯æä¾èµçåè½ï¼æ¯ä¸ä¸ªdefineé½æ¯ä¸ä¸ªç¬ç«çéç¨æµè¯ä»£ç ï¼ä¾å ¶ä»çæµè¯ç¨ä¾ä½¿ç¨ * å®çè¿åå¼å¯ä»¥æ¯ä»»ä½ä¸è¥¿ï¼é»è®¤æ¯{}空对象 */ monitor.define("module",function(){ var mod = {}; mod.sayHello = function(){ alert("hello module"); } return mod; });
monitor.require(names,handler)
/* * monitor.requireæ¥å£ï¼å¯ä»¥ä½¿ç¨ä¸ä¸ªmoduleï¼ä¹å¯ä»¥ä¼ å ¥å¤ä¸ªmodulesï¼å½ç¶ï¼åæ°ä¹å¯ä»¥åªæ¯ä¸ä¸ªå½æ° */ //ä¼ å ¥ä¸ä¸ªmodule monitor.require("module",function(module){ module.sayHello(); }); //ä¼ å ¥å¤ä¸ªmodules monitor.require(["module1","module2"],function(module1,module2){ module1.sayHello(); module2.sayHi(); }); //å¥moduleé½æ²¡æ monitor.require(function(){ //code here ... });
should(xxx)
shouldæ¹æ³ï¼ç±»ä¼¼ä¸jQueryç$
should(xxx).be()
beä»è¯
should(xxx).have()
haveä»è¯
should(xxx).has()
hasä»è¯
should(xxx).not()
notä»è¯ï¼ç¸åçææ
should(xxx).and()
andä»è¯
should(xxx).to()
toä»è¯
should(xxx).have().property(xxx)/should().haveProperty()/should().hasProperty()
å¤ææ¯å¦æå±æ§ï¼ä¸»è¦å¯¹objectç±»å
should(xxx).not().have().property(xxx)
å¤æ没æ该å±æ§ï¼å
¶å®çç±»å
should(xxx).have().attr(xxx)/should().haveAttr()/should().hasAttr()
å¤ææ¯å¦æattributeå±æ§ï¼ä¸»è¦å¯¹domå
ç´
should(xxx).beTrue/beTrues|beFalse/beFalse
å¤ææ¯å¦ä¸ºtrue(åå¨)åfalse(ä¸åå¨)
should(xxx).have().ownProperty(xxx)/should().hasOwnProperty()
should(xxx).have().lengthOf(number).should().beLengthOf()
should(xxx).not().have().property(xxx)
should(xxx).have().keys([xxx])/should().haveKeys()/should().hasKeys()
should(xxx).have().include(string)
å¤ææ¯å¦å
å«æ个å
容(object->propery,array->item)
should(xxx).be().empty()/should().beEmpty()
å¤ææ¯å¦ä¸ºç©º(dom,string,array,object)
should(xxx).be().a(xxx)/should().beA()
å¤ææ¯ä»ä¹ç±»å(dom,string,array,object,null,undefined...)
should(xxx).be().above(number)/should().beAbove()
å¤æç»å®çå¼æ¯å¦å¤§äºæ个æ°
should(xxx).be().below(number)/should().beBelow()
å¤æç»å®çå¼æ¯å¦å°äºæ个æ°
should(xxx).equal(xxx)/should(xxx).beEqual(xxx)
å¤æç»å®çå¼æ¯å¦è·åæ°çç¸ç
should(xxx).exist()
å¤ææ¯å¦åå¨
should(xxx).be().match(regexp)
æ£åå¹é
should(xxx).have().child(dom)/should().haveChild()/should().hasChild()
æ¯å¦å«æç´æ¥åå
ç´
should(xxx).have().parent(dom)/should().haveParent()/should()/hasParent()
should(xxx).have().next(dom)/should().haveNext()/should().hasNext()
should(xxx).have().prev(dom)/should().havePrev()/should().hasPrev()
should(xxx).have().prev(dom).fail/success(function(){})
failåsuccesså¨åé¢å¤ææ¯å¦okçæ¶åè°ç¨
should(xxx).not().be().empty().error(xxx)
å¦ææé误ï¼ååéé误信æ¯
should(xxx).have().include(xxx).mail(xxx)
å¦ææé误ï¼ååéé®ä»¶ä¿¡æ¯
should(xxx).have().include(xxx).sms(xxx)
å¦ææé误ï¼ååéçä¿¡ä¿¡æ¯
should(xxx).have().include(xxx).log(xxx)
å¦ææé误ï¼ååéæ¥å¿ä¿¡æ¯
should(xxx).have().include(xxx).warn(xxx)
å¦ææé误ï¼ååéè¦åä¿¡æ¯
should(xxx).be().visible()
å¤ææå®çDOMå
ç´ åºè¯¥æ¯æ¾ç¤ºç¶æï¼åªé对display
should(xxx).be().hide()
å¤ææå®çDOMå
ç´ åºè¯¥æ¯éèç¶æï¼åªé对display
should(xxx).haveHrefKeys(keys)
å¤æè¶
é¾æ¥DOMå
ç´ çhrefå±æ§ä¸æ¯å¦å
å«æå®çå¤ä¸ªkeyæ¯å¦ææå®çå¼
should(xxx).haveHrefKey(key,value)
å¤æè¶
é¾æ¥DOMå
ç´ çhrefå±æ§ä¸æ¯å¦å
å«æå®çä¸ä¸ªkeyæ¯å¦ææå®çå¼
should(xxx).haveFormKeys(keys)
å¤æä¸ä¸ªform表åä¸æå®çå
¨é¨nameçæ§ä»¶çå¼æ¯å¦çäºæå®çå¼
should(xxx).haveFormKey(key,value)
å¤æä¸ä¸ªform表åæå®çnameçæ§ä»¶çå¼æ¯å¦çäºæå®çå¼
shouldä»è¯
should("xxx").be() should("xxx").not() should("xxx").and() should("xxx").have() should(xxx).has()
should(xxx).have().property
should({name : 1}).have().property("name").fail(function(){ monitor.sendError("have not property"); }).success(function(){ cnosole.log("have property"); });
should(xxx).have().property
should({name : 1}).have().property("name").fail(function(){ monitor.sendError("have not property"); });
should(xxx).have().attr
should(document.getElementById("test")).have().attr("cid").fail(function(){ monitor.sendError("have not attribute"); });
should(xxx).not().have().property
should({name : 1}).not().have().property("name").fail(function(){ monitor.sendError("it have property"); });
should(xxx).beTrue()/beTrues()|beFalse()/beFalses()
should(1).beTrue().error("ä¸ä¸ºtrue");
should(xxx).have().keys
should([1,2,5,6,7,8]).have().keys([1,2,7]).fail(function(){ monitor.sendError("not have all the keys"); });
should(xxx).include
should([1,2,4]).include(2).fail(function(){ monitor.sendError("not include"); });
should(xxx).be().empty
should([]).be().empty().fail(function(){ monitor.sendError("not empty"); });
should(xxx).be().a
should({name : 1}).be().a("object").fail(function(){ monitor.sendError("not a object"); }); should([1,2,4]).be().a("array").fail(function(){ monitor.sendError("not a array"); }); should(document.getElementById("test")).be().a("dom").faild(function(){ monitor.sendError("not a dom element"); });
should(xxx).have().match
should("youname").have().match(/name/).fail(function(){ monitor.sendError("not match"); });
should(xxx).have().parent
should(document.getElementById("test")).have().parent(document.getElementById("parent")).fail(function(){ monitor.sendError("not have id is parent"); });
shouldé¾å¼è°ç¨
should({name : 1}).not().be().empty().fail(function(){ monitor.sendError("it is empty"); }).and().have().property("name").fail(function(){ monitor.sendError("have no name property"); });
should(xxx).error()/warn()/log()/sms()/mail()
should("").not().be().empty().error("æå®çåæ°ä¸ä¸ºç©º"); should("").not().be().empty().warn("æå®çåæ°ä¸ä¸ºç©º"); should("").not().be().empty().log("æå®çåæ°ä¸ä¸ºç©º"); should("").not().be().empty().sms("æå®çåæ°ä¸ä¸ºç©º"); should("").not().be().empty().mail("æå®çåæ°ä¸ä¸ºç©º");
should(xxx).be().visible()/should(xxx).be().hide()
should($("#container")).be().visible().error("没ææ£å¸¸æ¾ç¤º"); should($("#container")).be().hide().error("没ææ£å¸¸å ³é");
should(xxx).haveHrefKeys(keys)
//å¤æä¸ä¸ªè¶ é¾æ¥çhrefå±æ§åæ°ä¸æ¯å¦å å«äºwdåpnåæ°ï¼å¹¶ä¸å¼æ¯å¦ç¸ç should($("#page>a:nth-child(11)")).haveHrefKeys({ "wd" : "123", "pn" : 10 }).error("error");
should(xxx).haveHrefKey(key,value)
//å¦æä¸æå®valueï¼åä¼éªè¯è¯¥keyæ¯å¦åå¨ should($("#page>a:nth-child(11)")).haveHrefKey("wd").error("error"); //éªè¯æ¯å¦çäºæå®çå¼ should($("#page>a:nth-child(11)")).haveHrefKey("wd","æ们").error("error");
should(xxx).haveFormKeys(keys)
//æå®çkeyæ¯è¾å ¥æ§ä»¶çnameå±æ§çå¼ should("#example-form").haveFormKeys({ "username" : "safy", "password" : "iloveyou" }).error("error message");
should(xxx).haveFormKey(key,value)
should("#example-form").haveFormKey("username","safy").error('error message');
driver.find_element_by_id()
éè¿IDè·åDOMå
ç´
driver.find_element_by_name()
éè¿nameå±æ§å¼è·å第ä¸ä¸ªå¹é
çDOMå
ç´
driver.find_element_by_tag_name()
éè¿tagNameè·å第ä¸ä¸ªå¹é
çDOMå
ç´
driver.find_element_by_class_name()
éè¿classNameè·å第ä¸ä¸ªå¹é
çDOMå
ç´
driver.find_element_by_link_text()
éè¿é¾æ¥çå
容è·å第ä¸ä¸ªå¹é
çDOMå
ç´
driver.find_element_by_partial_link_text()
éè¿è¶
é¾æ¥çé¨åå
容è·å第ä¸ä¸ªå¹é
çDOMå
ç´
driver.find_element_by_css_selector()
éè¿CSS Selectorè·å第ä¸ä¸ªå¹é
çDOMå
ç´
driver.find_element_by_xpath()
éè¿XPathè·å第ä¸ä¸ªå¹é
çDOMå
ç´
driver.find_elements_by_name()
éè¿nameå±æ§å¼è·åå
¨é¨å¹é
çDOMå
ç´
driver.find_elements_by_tag_name()
éè¿tagNameè·åå
¨é¨å¹é
çDOMå
ç´
driver.find_elements_by_class_name()
éè¿classNameè·åå
¨é¨å¹é
çDOMå
ç´
driver.find_elements_by_link_text()
éè¿é¾æ¥çå
容è·åå
¨é¨å¹é
çDOMå
ç´
driver.find_elements_by_partial_link_text()
éè¿é¾æ¥é¨åå
容è·åå
¨é¨å¹é
çDOMå
ç´
driver.find_elements_by_css_selector()
éè¿CSS Selectorè·åå
¨é¨å¹é
çDOMå
ç´
driver.find_elements_by_xpath()
éè¿xpathè·åå
¨é¨å¹é
çDOMå
ç´
driver.find_element(By.NAME,"xxx")
éè¿nameå±æ§å¼è·å第ä¸ä¸ªå¹é
çDOMå
ç´
driver.find_element(By.XPATH,"xxx")
éè¿XPathè·å第ä¸ä¸ªå¹é
çDOMå
ç´
driver.find_element(By.CLASS_NAME,"xxx")
éè¿classNameè·å第ä¸ä¸ªå¹é
çDOMå
ç´
driver.find_element(By.TAG_NAME)
åä¸
driver.find_element(By.ID)
åä¸
driver.find_element(By.CSS_SELECTOR)
åä¸
driver.find_element(By.LINK_TEXT)
åä¸
driver.find_element(By.PARTIAL_LINK_TEXT)
åä¸
driver.find_elements(By.NAME,"xxx")
åä¸
driver.find_elements(By.XPATH,"xxx")
åä¸
driver.find_elements(By.CLASS_NAME,"xxx")
åä¸
driver.find_elements(By.TAG_NAME)
åä¸
driver.find_elements(By.ID)
åä¸
driver.find_elements(By.CSS_SELECTOR)
åä¸
driver.find_elements(By.LINK_TEXT)
åä¸
driver.find_elements(By.PARTIAL_LINK_TEXT)
åä¸
driver.execute_script(xxx)
æ§è¡ä¸æ®µjs代ç ï¼å¹¶è·åæ§è¡è¿åç»æ
driver
//driverè¿ä¸ªå¯¹è±¡æ¯å ç½®çï¼å¨ç¼åå侧代ç çæ¶åç´æ¥è°ç¨å³å¯ï¼å®å å«ä¸é¢ç½åçæ¹æ³ï¼ä¸é¢çæ¹æ³è°ç¨åï¼ //è¿åä¸ä¸ªwebElementç对象ï¼webElementè¿å¯ä»¥ç»§ç»è°ç¨ä¸é¢çæ¹æ³ï¼è·ååå ç´ ãè·åå±æ§å¼ãeachãclickãsubmitçæ´å¤çæä½
driver.find_element_by_id()
var input = driver.find_element_by_id("kw"); should(input.get_attribute("value")).be().equal("123").error("xxxx");
driver.find_element_by_name()
//è·åä¸ä¸ªname var input = driver.find_element_by_name("kw"); should(input.get_attribute("value")).be().equal("123").error("xxxx"); //è·åå¤ä¸ªname var inputs = driver.find_elements_by_name("user"); inputs.each(function(index,item){ should($(item).val()).be().equal("123").error("xxx"); })
driver.find_element_by_tag_name()
//è·å第ä¸ä¸ªselectæ§ä»¶ var selectElement = driver.find_element_by_tag_name("select"); var optionsElement = selectElement.find_element_by_tag_name("option"); optionsElement.each(function(index,item){ // code here ... });
driver.find_element_by_class_name()
var inputElement = driver.find_element_by_class_name(".kw"); inputElement.send_keys("123"); inputElement.click(); // or var formElement = driver.find_element_by_class_name(".myform"); forElement.submit();
driver.find_element_by_link_text()
// HTML : <a href="http://www.baidu.com">baidu link</a> var link = driver.find_element_by_link_text("baidu link"); link.get_attribute("href");
driver.find_element_by_partial_link_text()
// HTML : <a href="http://www.baidu.com">baidu link</a> var link = driver.find_element_by_partial_link_text("baidu"); link.get_attribute("href");
driver.find_element_by_css_selector()
var exampleElement = driver.find_element_by_css_selector("#example"); link.get_text();
driver.find_element(By.CSS_SELECTOR,"xxx")
/* * Byç°å¨æä¸é¢å ç§ç±»åï¼å¯¹åºçä¸é¢çæ¹æ³ï¼ * ID,NAME,CLASS_NAME,TAG_NAME,LINK_TEXT,PARTIAL_LINK_TEXT,CSS_SELECTOR,XPATH */ var exampleElement = driver.find_element(By.CSS_SELECTOR,"#example"); //è·å#exampleä¸é¢çå ¨é¨divåå ç´ exampleElement.find_elements(By.TAG_NAME,"div").each(function(index,item){ // code here ... }); // å ¶ä»Byçä¾ååä¸
driver.execute_script()
var labels = driver.find_elements_by_tag_name("label"); var inputs = driver.execute_script( "var labels = arguments[0],inputs = [];" + "for(var i =0; i < labels.length; i++){" + " inputs.push(document.getElementById(labels[i].getAttribut('for')));" + "}" + "return inputs;", labels.get_elements() //è·åå°DOMå ç´ çéå );
éªè¯æ个æé®çäºä»¶æ¯å¦æ£å¸¸æ§è¡
var action = monitor.createAction(); action.click("#example",function(el){ //å¨è¿ééªè¯è¯¥å ç´ çååï¼æ¯å¦ç¹å»åèæ¯åçåå should($(el).css("backgroundColor")).be().equal("#c00").error("èæ¯é¢è²æ²¡åå"); }) .wait(2000,function(){ //å¨è¿ééªè¯ç¹å»å产ççææï¼æ¯å¦éªè¯ç¹å»åå¼æ¥è¯·æ±çææï¼å 容æ¯å¦ä¸ºç©º should($("#content").html()).be().exist().error("å¼æ¥è¯·æ±å 容为空"); }) .complete();
å¤ææ个èç¹çææ¬å 容æ¯å¦çäºæå®çå¼
should($("#example").text()).be().equal("xxxx");
ç¹å»æ个æé®ï¼åéJSONPå¼æ¥è¯·æ±ï¼éªè¯ç»æ并éªè¯æ°æ®
var action = monitor.createAction(); action.click("#example") .wait(2000,function(){ //è¿ä¸ªåè°å½æ°éé¢ï¼å¯ä»¥åé¢å¤çæµè¯ä»£ç å»å¤æ页é¢çææï¼æ¯å¦å ç´ æ¾ç¤ºéèï¼æ°æ®æ¯å¦å±ç¤ºï¼æ°æ®æ示æ¯å¦ok }) .complete(); //ç»ææµè¯ //éªè¯JSONPå¼æ¥æ°æ®æ¬èº«å 容(monitor.jsonpæ¹æ³ç第ä¸ä¸ªåæ°ï¼å°±æ¯jsonpåè°æ§è¡çå½æ°åï¼å°±æ¯cbåæ°ä¼ çå¼) monitor.jsonp("window.json_callback",function(json){ if(json.status != 0){ monitor.error("æ°æ®é误") } });
ç¹å»Tabç»ä»¶çæ¯ä¸ä¸ªTabé项å¡ï¼å¹¶éªè¯æ¯ä¸ä¸ªtabå 容æ¯å¦æ£å¸¸æ¾ç¤ºåºæ¥
//è¿ä¸ªåè½å¯ä»¥ä½¿ç¨å½å¶çæ¹å¼ï¼éªè¯æ¯å¦æ£å¸¸æ¾ç¤ºï¼éè¦å¨åè°å½æ°ä¸ç®ååä¸ç¹ä»£ç æ¥éªè¯ var action = monitor.createAction(); action.click("#tab2",function(el){ //è¿é¨åæå代ç éªè¯ should($("#tab2-box").css("display")).beEqual("block").error("tabæ¾ç¤ºä¸æ£ç¡®"); }) .wait(1000) .click("#tab3",function(el){ should($("#tab3-box").css("display")).beEqual("block").error("tabæ¾ç¤ºä¸æ£ç¡®"); }) .complete();
å¤ææ个é¿æä¸çç»æéé¢å¾çæ°éè³å°å¤§äº1个
//对äºä¸äºå¤æé¿æä¸æ°æ®æ¯å¦åå¨çï¼å éè¦å¤æä¸ä¸è¯¥æ¡é¿æä¸æ¯å¦åå¨ï¼ //å¦åï¼æ个queryä¸ï¼è¯¥é¿æä¸ä¸åå¨äºï¼ä¹å»å¤æï¼å°±ä¼åºç°é误ã误æ¥äºã //å æ¤ï¼é¦å è¦å¤æ该é¿æä¸æ¯åå¨çï¼æè½åæ¥ä¸æ¥çå¤æ if(monitor.util.hasAladdin("tieba2")){ should($("table[tpl=tieba2] img").size()).be().above(0).error("ä¸ä¸ªå¾çé½ä¸åå¨"); }
ç¹å»ç±»ä¼¼âæ´å¤âï¼âæ¶èµ·âçæä½ï¼DOMå ç´ çæ¾ç¤ºåéè
var action = monitor.createAction(); action.click("#open",function(){ should($("#hide-box")).be().visible().error("没ææ£å¸¸æ¾ç¤ºå®¹å¨"); }) .wait(1000) .click("#close",function(){ should($("#hide-box")).be().hide().error("没ææ£å¸¸éè容å¨"); }) .complete();
ç¹å»æé®ï¼æå¼æ°çªå£ï¼å¤ææ¼è£ çURLæ¯å¦æ£ç¡®
//æ¯å¦ä¸äºæºç¥¨ãç«è½¦ç¥¨æ¥è¯¢çé¿æä¸ï¼å¡«åfromãtoç¶åç¹å»æ¥è¯¢ï¼è·³å°ç¬¬ä¸æ¹ç½ç« var action = monitor.createAction(); action.click("#example-btn",function(el){ should($(el).attr("href")).match(/wd=([^&]+)/).error("URLæ¼è£ æé®é¢"); }) .complate();
IPæ¥è¯¢ï¼æè ææºå·ç æ¥è¯¢çé¿æä¸ï¼å¯¹äºè¾å ¥é误çå 容ï¼ä¼æ¾ç¤ºé误æ示çéªè¯
var action = monitor.createAction(); action.type("#cell_txt","sdfsdf") .click("#btn") .wait(2000,function(){ should($("#error_tip")).be().visible().error("é误æ示æªæ¾ç¤º"); }) .complete();
对äºä¸äºSugåè½çéªè¯ï¼æ¯å¦éæ©æ个sugåï¼éªè¯è¾å ¥æ¡ä¸æ¯å¦æ¯éæ©çå¼
//é¦å ï¼å¯¹äºsugçåè½ï¼è¯·ä½¿ç¨å½å¶åè½è¿è¡å½å¶ç¨æ·è¡ä¸ºï¼èªå¨çæ代ç ï¼å¦ä¸ï¼ var action = monitor.createAction(); action.type("#op_hangban_start","s",function(el){}) .wait(491,function(){}) .mouseover("#tangram-suggestion--TANGRAM__3-item0",function(el){}) .mouseover("#tangram-suggestion--TANGRAM__3-item4",function(el){}) .wait(422,function(){}) .click("#tangram-suggestion--TANGRAM__3-item4",function(el){ //è¿éå°±æ¯ç¹å»äºsugçæä¸ä¸ªé项ï¼ç¶åå¨è¿éå¯ä»¥åéªè¯äºï¼å ¶ä»ç±»ä¼¼çéªè¯ï¼é½å¯ä»¥éè¿è¿æ ·æ¹å¼ï¼ //è¿éçelåæ°ï¼å°±æ¯å½åç¹å»çå ç´ should($(el).text()).be().equal($("#op_hangban_start").val()).error("sugçå¼å¯¹ä¸ä¸"); }) .wait(1150,function(){}) .type("#op_hangban_end","f",function(el){}) .wait(481,function(){}) .mouseover("#tangram-suggestion--TANGRAM__g-item0",function(el){}) .wait(150,function(){}) .mouseover("#tangram-suggestion--TANGRAM__g-item1 span.op_hangban_hilight",function(el){}) .wait(518,function(){}) .click("#tangram-suggestion--TANGRAM__g-item1 span.op_hangban_hilight",function(el){}) .complete(function(){});