Created
July 5, 2013 07:16
-
-
Save supersha/5932590 to your computer and use it in GitHub Desktop.
Safy平台中,为QA准备的selenium python的driver API,因为QA都习惯使用python在selenium中写单侧,所以创建JS版本的,方便QA写单侧。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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.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元素的集合 | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ##################### Selenium Python Case API ##################### | |
win.__Case._WebElement = function(elem){ | |
this.elements = elem || null; | |
} | |
win.__Case._WebElement.prototype = { | |
find_element_by_name : function(name,flag){ | |
var elem = this.elements || "html"; | |
var count = flag ? null : 0; | |
this.elements = $(elem).find("[name=" + name + "]").get(count); | |
return this; | |
}, | |
find_elements_by_name : function(name){ | |
this.find_element_by_name(name,true); | |
return this; | |
}, | |
find_element_by_id : function(id){ | |
var elem = this.elements || "html"; | |
this.elements = $(elem).find("#" + id).get(0); | |
return this; | |
}, | |
find_element_by_tag_name : function(tagName,flag){ | |
var elem = this.elements || "html"; | |
var count = flag ? null : 0; | |
this.elements = $(elem).find(tagName).get(count); | |
return this; | |
}, | |
find_elements_by_tag_name : function(tagName){ | |
this.find_element_by_tag_name(tagName,true); | |
return this; | |
}, | |
find_element_by_class_name : function(classname,flag){ | |
var elem = this.elements || "html"; | |
var count = flag ? null : 0; | |
this.elements = $(elem).find(classname).get(count); | |
return this; | |
}, | |
find_elements_by_class_name : function(classname){ | |
this.find_element_by_class_name(classname,true); | |
return this; | |
}, | |
find_element_by_link_text : function(text,flag){ | |
var elem = this.elements || "html"; | |
var target = []; | |
var bool = false; | |
$(elem).find("a").each(function(index,item){ | |
if(flag){ | |
if($.trim($(item).text()) === text){ | |
target.push(item); | |
} | |
}else{ | |
if(bool){ return; } | |
if($.trim($(item).text()) === text){ | |
target.push(item); | |
bool = true; | |
} | |
} | |
}); | |
this.elements = target; | |
return this; | |
}, | |
find_elements_by_link_text : function(text){ | |
this.find_elements_by_link_text(text,true); | |
return this; | |
}, | |
find_element_by_partial_link_text : function(text,flag){ | |
var elem = this.elements || "html"; | |
var target = []; | |
var bool = false; | |
$(elem).find("a").each(function(index,item){ | |
if(flag){ | |
if($.trim($(item).text()).indexOf(text) !== -1){ | |
target.push(item); | |
} | |
}else{ | |
if(bool){ return; } | |
if($.trim($(item).text()).indexOf(text) !== -1){ | |
target.push(item); | |
bool = true; | |
} | |
} | |
}); | |
this.elements = target; | |
return this; | |
}, | |
find_elements_by_partial_link_text : function(text){ | |
this.find_element_by_partial_link_text(text,true); | |
return this; | |
}, | |
find_element_by_css_selector : function(selector,flag){ | |
var elem = this.elements || "html"; | |
var count = flag ? null : 0; | |
this.elements = $(elem).find(selector).get(count); | |
return this; | |
}, | |
find_elements_by_css_selector : function(selector){ | |
this.find_element_by_css_selector(selector,true); | |
return this; | |
}, | |
find_element_by_xpath : function(xpath,flag){ | |
var results = document.evaluate("//button",document,null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); | |
var i = 0,res = null,bool = false,target = []; | |
while((res = results.snapshotItem(i)) != null){ | |
i++; | |
if(flag){ | |
target.push(res); | |
}else{ | |
if(bool){ return; } | |
target.push(res); | |
bool = true; | |
} | |
} | |
return; | |
}, | |
find_elements_by_xpath : function(xpath){ | |
this.find_element_by_xpath(xpath,true); | |
return this; | |
}, | |
find_element : function(type,value,flag){ | |
switch(type){ | |
case 1 : | |
this.find_element_by_id(value); | |
break; | |
case 2 : | |
this.find_element_by_class_name(value,flag); | |
break; | |
case 3 : | |
this.find_element_by_name(value,flag); | |
break; | |
case 4 : | |
this.find_element_by_tag_name(value,flag); | |
break; | |
case 5 : | |
this.find_element_by_link_text(value,flag); | |
break; | |
case 6 : | |
this.find_element_by_partial_link_text(value,flag); | |
break; | |
case 7 : | |
this.find_element_by_css_selector(value,flag); | |
break; | |
case 8 : | |
this.find_element_by_xpath(value,flag); | |
break; | |
} | |
return this; | |
}, | |
find_elements : function(type,value){ | |
this.find_element(type,value,true); | |
return this; | |
}, | |
each : function(fn){ | |
var that = this; | |
$(this.elements).each(function(index,item){ | |
fn.call(that,index,item); | |
}) | |
return this; | |
}, | |
get_attribute : function(attr){ | |
return $(this.elements).attr(attr); | |
}, | |
send_keys : function(value){ | |
$(this.elements).val(value); | |
return this; | |
}, | |
send_keys_to_element : function(elem,value){ | |
$(elem).val(value); | |
return this; | |
}, | |
clear : function(){ | |
$(this.elements).val(""); | |
return this; | |
}, | |
get_elements : function(){ | |
return $(this.elements).get(); | |
}, | |
get_text : function(){ | |
return $(this.elements).text(); | |
}, | |
click : function(){ | |
$(this.elements).each(function(index,item){ | |
Util.event.fire(item,"click"); | |
item.click(); | |
$(item).trigger("click"); | |
}); | |
return this; | |
}, | |
submit : function(){ | |
$(this.elements).each(function(index,item){ | |
item.submit(); | |
}); | |
return this; | |
} | |
} | |
win.__Case._WebDriver = {}; | |
win.__Case._WebDriver.By = { | |
"ID" : 1, | |
"CLASS_NAME" : 2, | |
"NAME" : 3, | |
"TAG_NAME" : 4, | |
"LINK_TEXT" : 5, | |
"PARTIAL_LINK_TEXT" : 6, | |
"CSS_SELECTOR" : 7, | |
"XPATH" : 8 | |
} | |
win.__Case._WebDriver.execute_script = function(code){ | |
var func = new Function(code),args = []; | |
if(arguments.length !== 1){ | |
for(var i = 1;i < arguments.length;i++){ | |
args.push(arguments[i]); | |
} | |
} | |
return func.apply(this,args); | |
} | |
for(var key in win.__Case._WebElement.prototype){ | |
win.__Case._WebDriver[key] = (function(k){ | |
return function(){ | |
var webelement = new win.__Case._WebElement(); | |
webelement[k].apply(webelement,arguments); | |
return webelement; | |
} | |
})(key); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment