namespace SeleniumTests { ,,,(有关) ,,,public class 登录 ,,,{ ,,,,,,,private IWebDriver 驱动; ,,,,,,,private StringBuilder verificationErrors; ,,,,,,,private string baseURL; ,,,,,,,private bool acceptNextAlert =,真的; ,,,,,,, ,,,,,,,【设置】 ,,,,,,,public void SetupTest () ,,,,,,,{ ,,,,,,,,,,,driver =, new FirefoxDriver (); ,,,,,,,,,,,baseURL =,“URL”; ,,,,,,,,,,,verificationErrors =, new StringBuilder (); ,,,,,,,} ,,,,,,, ,,,,,,,(拆卸) ,,,,,,,public void TeardownTest () ,,,,,,,{ ,,,,,,,,,,,试一试 ,,,,,,,,,,,{ ,,,,,,,,,,,,,,,driver.Quit (); ,,,,,,,,,,,} ,,,,,,,,,,,catch (异常) ,,,,,,,,,,,{ ,,,,,,,,,,,,,,,//,Ignore errors if unable 用close 从而浏览器 ,,,,,,,,,,,} ,,,,,,,,,,,Assert.AreEqual (“”, verificationErrors.ToString ()); ,,,,,,,} ,,,,,,, ,,,,,,,(测试) ,,,,,,,public void TheLoginTest () ,,,,,,,{ ,,,,,,,,,,,driver.Navigate () .GoToUrl (baseURL +,/登录); ,,,,,,,,,,,driver.FindElement (By.Name(“用户名”)).Clear (); ,,,,,,,,,,,driver.FindElement (By.Name(“用户名”)).SendKeys(“用户名”); ,,,,,,,,,,,driver.FindElement (By.Name(“密码”)).Clear (); ,,,,,,,,,,,driver.FindElement (By.Name(“密码”)).SendKeys(“密码”); ,,,,,,,,,,,driver.FindElement (By.XPath(“//按钮(@type='提交']")).Click (); ,,,,,,,} ,,,,} 以前,,}>上面是用selenium ide录制的某个页面的登录操作代码。像是输入用户名和密码的代码就有点重复多余繁琐,那就可以封装一个叫SendKeys的方法(包括清晰和SendKeys的动作),而不需要每次去找这个元素,先清楚,然后再重复去找这个元素再SendKeys。类似这种常用的操作都可以封装起来,放在一个常见的类里(常见的项目)而一些操作案例放在另外的项目中。下面就是对上述例子进行封装操作。
namespace TestSelenium.Test { ,,,(有关) ,,,class 测试 ,,,{ ,,,,,,,TestSelenium.Common.Common Testcorde =, new Common.Common (); ,,,,,,,【设置】 ,,,,,,,public void 设置() ,,,,,,,{ ,,,,,,,,,,,Testcorde.SetupTest (); ,,,,,,,} ,,,,,,,(拆卸) ,,,,,,,public void TearDown () ,,,,,,,{ ,,,,,,,,,,,Testcorde.TeardownTest (); ,,,,,,,} ,,,,,,,(测试) ,,,,,,,public void Test01 () ,,,,,,,{,,,, null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null nullWebdriver (selenium2.0) + NUnit + c#(二)