selenium基本使用语句
selenium大致可分为两个版本Selenium Core和SeleniumRC,第一种是以网页的形式编写TestCase,而后一种用XUnit,并且支持多种语言。
seleniumRC下载地址:http://selenium-rc.openqa.org/download.jsp运行测试前在DOS命令下启动selenium-server.jar(无需放到环境目录下):java -jar selenium-server.jar(需安装java环境)
网上抄来的测试案例一个,C#语言版本的selenium基本使用语句:
[Test]
public void JustTest()
{
// 每个测试步骤间隔1秒
selenium.SetSpeed("1000");
// 打开测试的网页
selenium.Open("http://localhost:8080/TestProject/webform1.aspx");
// 获取标题
Assert.AreEqual("selenium", selenium.GetTitle());
Assert.IsTrue(selenium.IsElementPresent("xpath=//input[@name='username']"));
// 向input中type为text的栏位键入信息
selenium.Type("xpath=//input[@name='username']", "chenxu");
selenium.Type("xpath=//input[@name='password']", "password");
// 选择下拉列表
selenium.Select("xpath=//select[@name='select']", "index=1");
Assert.AreEqual("1", selenium.GetSelectedIndex("xpath=//select[@name='select']"));
Assert.AreEqual("chenxu", selenium.GetSelectedValue("xpath=//select[@name='select']"));
// 勾选或去选check/radio
selenium.Check("xpath=//input[@id='check2']");
//获取name为check栏位的id属性
selenium.GetAttribute("check@id");
Assert.IsTrue(selenium.IsChecked("xpath=//input[@id='check2']"));
selenium.Uncheck("xpath=//input[@id='check2']");
Assert.IsFalse(selenium.IsChecked("xpath=//input[@id='check2']"));
Assert.AreEqual("chenxu", selenium.GetValue("xpath=//select[@name='select']"));
// 点击提交按钮
selenium.Click("xpath=//input[@type='submit']");
// 等待页面载入
selenium.WaitForPageToLoad("3000");
// 获取新页面标题
Assert.AreEqual("success", selenium.GetTitle());
}
这里ElementLocator使用xpath方式查找页面元素。
