在测试前必须先启动他,启动过程:开始-运行-cmd-cd <服务器端目录>-java -jar selenium-server.jar(服务器端其实就是个Jar文件)
然后就可以进行客户端,本文用C#来进行测试,首先建立一个C#类库工程,添加引用selenium-dotnet-client-driver-1.0.1目录下的所有DLL,具体如下图示。
下面,新建类SeleniumTest,具体代码如下:
1 [TestFixture] 2 public class SeleniumTest 3 { 4 private ISelenium selenium; 5 private StringBuilder verificationErrors; 6 7 [SetUp] 8 public void SetupTest() 9 { 10 selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://localhost:2896/WebTestSite/"); 11 selenium.Start(); 12 13 verificationErrors = new StringBuilder(); 14 } 15 16 [TearDown] 17 public void TeardownTest() 18 { 19 try 20 { 21 selenium.Stop(); 22 } 23 catch (Exception) 24 { 25 // Ignore errors if unable to close the browser 26 } 27 Assert.AreEqual("", verificationErrors.ToString()); 28 } 29 30 [Test] 31 public void TheSeleniumTest() 32 { 33 selenium.Open("/WebTestSite/"); 34 selenium.Type("TextBox1", "qeq"); 35 selenium.Type("TextBox2", "qwe"); 36 selenium.Click("Button1"); 37 38 //判断是否出现alert("fail") 39 Assert.AreEqual("fail", selenium.GetAlert()); 40 41 selenium.Type("TextBox1", "123"); 42 selenium.Type("TextBox2", "123"); 43 selenium.Click("Button1"); 44 Assert.AreEqual("fail", selenium.GetAlert()); 45 46 //点击链接 47 selenium.Click("link=2"); 48 //等待 49 selenium.WaitForPageToLoad("30000"); 50 selenium.Click("link=3"); 51 selenium.WaitForPageToLoad("30000"); 52 53 } 54 [Test] 55 public void TestTitle() 56 { 57 selenium.Open("/WebTestSite/**.aspx"); 58 Assert.AreEqual("yourtitle", selenium.GetTitle()); 59 60 } 61 } |
这样,就建好了,可以打开NUit进行测试,也可以直接写个main进行测试。
seleniumhq官方文档:
http://seleniumhq.org/docs/05_selenium_rc.html#introduction