主站:http://docs.seleniumhq.org/
Wiki:http://en.wikipedia.org/wiki/Selenium_(software)
大致介紹:Selenium is a suite of tools to automate web browsers across many platforms.這是selenium官網關於selenium的一段描述。Selenium是一個強大的開源的Web功能自動化測試工具系列。目前主要包括以下5部分:
Selenium Core:支持DHTML的測試案例(效果類似數據驅動測試),它是Selenium IDE和Selenium RC的引擎。
Selenium IDE:FireFox的一個插件,支持腳本錄制回放。
Selenium RC(Remote Control):時一個客戶端/服務器,可以控制本地或其他的web瀏覽器。
Selenium WD:Webdriver,是我們系列的重點。
Selenium Grid:允許同時並行地、在不同的環境上運行多個測試任務。
現在研究的是通過Visual Studio創建程序遠程運行腳本
剛剛錄了一段操作,輸出是C# code (based on NUnit)
代碼: |
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
namespace SeleniumTests
{
[TestFixture]
public class NunitSeleniumWsa
{
private IWebDriver driver;
private StringBuilder verificationErrors;
private string baseURL;
private bool acceptNextAlert = true;
[SetUp]
public void SetupTest()
{
driver = new FirefoxDriver();
baseURL = "http://www.westca.com/";
verificationErrors = new StringBuilder();
}
[TearDown]
public void TeardownTest()
{
try
{
driver.Quit();
}
catch (Exception)
{
// Ignore errors if unable to close the browser
}
Assert.AreEqual("", verificationErrors.ToString());
}
[Test]
public void TheNunitSeleniumWsaTest()
{
driver.Navigate().GoToUrl(baseURL + "/pda/lang=tchinese.html");
driver.FindElement(By.XPath("//div[@id='content']/ul[2]/li/a/span")).Click();
driver.FindElement(By.LinkText("我的主貼")).Click();
driver.FindElement(By.XPath("//div[@id='content']/ul/li[9]/a/span")).Click();
driver.FindElement(By.Id("form_reply_save_message")).Click();
driver.FindElement(By.Id("form_reply_save_message")).Clear();
driver.FindElement(By.Id("form_reply_save_message")).SendKeys("好事兒,就是chrome開關tab不如以前方便");
driver.FindElement(By.Id("form_reply_save_")).Click();
}
private bool IsElementPresent(By by)
{
try
{
driver.FindElement(by);
return true;
}
catch (NoSuchElementException)
{
return false;
}
}
private bool IsAlertPresent()
{
try
{
driver.SwitchTo().Alert();
return true;
}
catch (NoAlertPresentException)
{
return false;
}
}
private string CloseAlertAndGetItsText() {
try {
IAlert alert = driver.SwitchTo().Alert();
string alertText = alert.Text;
if (acceptNextAlert) {
alert.Accept();
} else {
alert.Dismiss();
}
return alertText;
} finally {
acceptNextAlert = true;
}
}
}
}
|
恭喜歪脖,可以自動發貼賺棒了。羨慕ing... :lol: