1. 向文本框中填写信息
type(java.lang.String locator, java.lang.String value)
eg.selenium.type("salutationText", “abc”);
2. 取出某个文本框中已经填写的信息
java.lang.StringgetValue(java.lang.String locator)
eg.selenium.getValue("xpath=//input[@name='addProfileLastName']");
3. 判断某文本框是否可编辑
booleanisEditable(java.lang.String locator)
eg.selenium.isEditable("xpath=//input[@name='addProfileLastName']");
二.下拉框 Drop down list
1. 向下拉框中选值
select(java.lang.String selectLocator, java.lang.String optionLocator)
eg.selenium.select("typeSelect", "label=Date");
2. 取出某个下拉框中已经选择的值
java.lang.StringgetSelectedLabel(java.lang.String selectLocator)
eg. selenium.getSelectedLabel("xpath=//SELECT[@name='addSatution']")
3. 取出某个下拉框中所有的选项
java.lang.String[]getSelectOptions getSelectOptions getSelectOptions(java.lang.String selectLocato getSelectOptionsr)
eg. selenium.getSelectOptions("//div[@id='mysearch_tips']/select")
三.按钮或链接Button & Link
单击
click(java.lang.String locator)
eg. selenium.click("link=Administration");
selenium.click("xpath=//input[@checkfield='addIndicatorName' and @name='addBtn']");
四. 单选框或多选框 Radio Box & Check Box
1. 选择
check(java.lang.String locator)
eg. selenium.check("otherPhoneFlag");
1. 不选
uncheck(java.lang.String locator)
eg. selenium.uncheck("otherPhoneFlag");
2. 判断是否选择
booleanisChecked(java.lang.String locator)
eg. selenium.isChecked("otherPhoneFlag");
五.表格Table
1. 取出表中某个单元格的值,下标从 0 开始
java.lang.String getTable(java.lang.String tableCellAddress)
eg. selenium.getTable("//div[@id='profiles-search']/div[2]/table.1.1");
六.其他 其他 其他 其他others
1. 取某个元素的特定属性值
java.lang.String getAttribute(java.lang.String attributeLocator)
eg. selenium.getAttribute("xpath=//img[@name='picName']@style")
2.取某元素的 text 值
java.lang.StringgetText(java.lang.String locator)
eg. selenium.getText("link=Contacts")
3. 取当前页面的 Title
java.lang.StringgetTitle()
eg. selenium.getTitle()
4. 判断页面是否有特定的元素
booleanisElementPresent(java.lang.String locator)
eg. booleanisElementPresent(java.lang.String locator)
5. 判断页面是否有特定文字
booleanisTextPresent(java.lang.String pattern)
eg. selenium.isTextPresent(“abc?
6. 判断某个元素是否 invisible,
图片是空的 the "display" property to "none"
或 CSS "visibility" property to "hidden"
booleanisVisible(java.lang.String locator)
eg.selenium.isVisible("xpath=//img[@name='picName']")
7. 模拟 Keyup 事件
keyUp(java.lang.String locator,java.lang.String keySequence)
eg. selenium.keyUp("creditCardExpMonth", "1");
8. 打开一个 url,相对或绝对
open(java.lang.String url)
eg.selenium.open("/login.aspx");
9. 刷新页面
refresh()
eg.selenium.refresh();
10. 控制每步操作间隔的时间,milliseconds
setSpeed(java.lang.String value)
eg. selenium.setSpeed("3000");
11. 开始一个 Selenium session
start()
eg. selenium.start();
stop()
eg. selenium.stop();
13. 等待新页面加载
waitForPageToLoad(java.lang.String timeout)
selenium.waitForPageToLoad("60000");
14. 页面最大化
windowMaximize()
eg.selenium.windowMaximize();
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1.要使用selenium,首先要开启seleniumserver,命令如下(先要进入selenium-server.jar 根目录):
>java -jar selenium-server.jar
2.selenium 常用方法
1) 创建selenium实例对象:
Selenim selenium =new DefaultSelenium("localhost",4444,"*firefox",url);
2)selenium.open(String url)
selenium.open("http://www.google.com");//打开一个网页
3)selenium.type(String locator,String value)
selenium.type("//div[@id='q']","automated testing");//在google输入框中输入automated testing,其中q为输入框的id
-----notes:use Selenium IDE can get this result easily
4)selenium.isElementPresent(String locator),返回true/false
selenium.isElementPresent("//div[@id='wor']");//根据xpath路径查找元素是否存在
5)selenium.click(String locator)
selenim.click("submit")//点击操作
6)selenium.isTextPresent(String pattern);return true/false
selenium.isTextPresent("automated");//查找文本内容是否存在
7)selenium.select(String selectTarget, String selectValue)
selenium.select("//select[@id='page']","value=1234")//在选择框中选择一个选项
8)selenium.geteval_r(String locator);//根据path寻找元素
selenium.geteval_r("this.page().findElement(\"//div[@class='ele']/ul\").childNodes[1].className");
selenium.geteval_r("this.page().findElement(\"dom=document.domain\")");
selenium.geteval_r("this.page().findElement(\"//div[@class='ele']/ul\").childNodes.length");
selenium.geteval_r("this.page().findElement(\"//div[@class='ele']/ul\").childNodes[1].nodeName");
selenium.geteval_r("this.page().findElement(\"//div[@id='emailCheckedLabel']\").getAttribute(\"style\")");
notes:<li id="yes" class="hello"/>
9)selenium.getSelectOptions(String locator);//return String[],返回选择框的全部选项
selenium.getSelectOptions("//select[@id='selectid']");
10)Pattern.compile(String text).matcher(String locator).find();//return boolean,判断是否存在匹配的字符串
Pattern.compile("hello").matcher("//div[@class='ele']").find();
11)selenium.focus(String locator);//将光标焦点定位到文本框中
12)selenium.keyPressNative(Integer.toString(java.awt.event.KeyEvent.VK_ENTER));//输入回车
...
eg:(google search function test)
package com.work.selenium;
import junit.framework.TestCase;
import org.openqa.selenium.server.SeleniumServer;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
public class firstSelenium extends TestCase {
}
----notes:
1.selenium api url:
http://release.seleniumhq.org/selenium-remote-control/0.9.2/doc/java/com/thoughtworks/selenium/Selenium.html
selenium api 中文:http://blog.csdn.net/lh9529/archive/2009/03/01/3946567.aspx