您的位置:軟件測試 > 開源軟件測試 > 開源功能測試工具 > Selenium
Selenium+C#自動化腳本開發(fā)學(xué)習(xí)
作者:Gougougogogo 發(fā)布時間:[ 2016/8/25 13:42:51 ] 推薦標簽:自動化測試 功能測試

  1:Selenium中對瀏覽器的操作
  首先生成一個Web對象
  IWebDriver driver = new FirefoxDriver();
  //打開指定的URL地址
  driver.Navigate().GoToUrl(@"http://12.99.102.196:9080/corporbank/logon_pro.html");
  //關(guān)閉瀏覽器
  Driver.quit();
  網(wǎng)銀瀏覽器兼容性測試過程中,關(guān)閉瀏覽器后會有對話框,此問題解決方法如下:
public void logout()
{
System.Diagnostics.Process[] myProcesses;
myProcesses = System.Diagnostics.Process.GetProcessesByName("IEXPLORE");
foreach (System.Diagnostics.Process instance in myProcesses)
{
instance.Kill();
}
}
  2:Selenium中執(zhí)行JS腳本
  //需要將driver強制轉(zhuǎn)換成JS執(zhí)行器類型
  ((IJavaScriptExecutor) driver).ExecuteScript("js文件名");
  3:Selenium中定位頁面元素
driver.FindElement(By.Id("cp1_btnModify")).click();
By.ClassName(className));
By.CssSelector(selector) ;
By.Id(id);
By.LinkText(linkText);
By.Name(name);
By.PartialLinkText(linkText);
By.TagName(name);
By.Xpath(xpathExpression);
  3.1根據(jù)元素id定位并操作
  //向指定的文本框輸入字符串500001
  Driver.FindElement(By.Id("amount")).SendKeys("500001");
  3.2根據(jù)元素classname定位并操作
  //點擊classname為指定值的按鈕
  Driver.FindElement(By.ClassName(“WotherDay”)).click();
  3.3根據(jù)元素的linktext定位并操作
  Driver.FindElement(By.LinkText(“選擇賬號”)).click();
  3.4根據(jù)元素的Name定位并操作
  Driver.FindElement(By.Name(“quality”)).perform();
  3.5使用CssSelector定位并操作
  string order = "#resultTable.result_table tbody tr.bg1 td.center a";
  driver.FindElement (By.CssSelector(order)).click();
  3.6使用Xpath定位并元素并操作
  //使用多個屬性定位元素
  Driver.FindElement(By.XPath("//input[@id='submit' and @value='下一步']")).click();
  //使用路徑定位元素
  string path = "/html/body/div[4]/div/div/div[2]/table/tbody/tr/td/a";
  Driver.FindElement(By.Xpath(path)).click();
  各方法使用優(yōu)先原則:
  優(yōu)先使用id,name,classname,link;次之使用CssSelector();后使用Xpath();
  因為Xpath()方法的性能和效率低下。
  4:Selenium中清空文本框中的默認內(nèi)容
  //清空文本框clear()
  Driver.FindElement(By.Id("tranAmtText")).clear();
  5:Selenium中在指定的文本框中輸入指定的字符串
  //在文本框中輸入指定的字符串sendkeys()
  Driver.FindElement(By.Id("tranAmtText")).SendKeys(“123456”);
  6:Selenium中移動光標到指定的元素上
  //移動光標到指定的元素上perform
  Actions action=new Actions(driver);
  action.MoveToElement(Find(By.XPath("//input[@id='submit' and @value='確定']"))).Perform();
  7:Selenium中點擊按鈕/鏈接
  //點擊按鈕/鏈接click()
  Driver.FindElement(By.XPath("//input[@id='submit' and @value='下一步']")).click();
  8:Selenium中等待頁面上的元素加載完成
  //等待頁面元素加載完成
  //默認等待100秒
  WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(100));
  //等待頁面上ID屬性值為submitButton的元素加載完成
  wait.Until((d) => { return WaitForObject(By.Id("submitButton")); });
  9:Selenium中模擬鼠標晃動
  //模擬光標晃動movebyoffset()
  Actions action = new Actions(driver);
  action.MoveByOffset(2, 4);
  10:Selenium中對iframe中元素的定位
  5.1:切換焦點到id為固定值的iframe上
  進入頁面后,光標默認焦點在DefaultContent中,若想要定位到iframe 需要轉(zhuǎn)換焦點
  driver.SwitchTo().DefaultContent();
  //切換焦點到mainFrame
  driver.SwitchTo().Frame("mainFrame");
  需要注意的是:切換焦點之后若想切換焦點到其他iframe上 需要先返回到defaultcontent,再切換焦點到指定的iframe上。
  5.2切換焦點到id值為動態(tài)值的iframe上
  有時候 頁面上浮出層的id為動態(tài)值,此時需要先獲取所有符合記錄的iframe放置在數(shù)組中,然后遍歷數(shù)組切換焦點到目標iframe上。
  如下方法:
protected string bizFrameId = string.Empty;
protected string bizId = string.Empty;
//獲取動態(tài)iframe的id值
protected void SetIframeId()
{
ReadOnlyCollection<IWebElement> els = driver.FindElements(By.TagName("iframe"));
foreach (var e in driver.FindElements(By.TagName("iframe")))
{
string s1 = e.GetAttribute("id");
if (s1.IndexOf("window") >= 0 && s1.IndexOf("content") >= 0)
{
bizFrameId = e.GetAttribute("id");
string[] ss = s1.Split(new char[] { '_' });
bizId = ss[1];
}
}
}
  11:Selenium中關(guān)閉多個子Browser窗口
//獲取所有的WindowHandle,關(guān)閉所有子窗口
string oldwin = driver.CurrentWindowHandle;
ReadOnlyCollection<string> windows = driver.WindowHandles;
foreach (var win in windows)
{
if (win != oldwin)
{
driver.SwitchTo().Window(win).Close();
}
}
driver.SwitchTo().Window(oldwin);
  12:Selenium中對下拉框的操作
//選擇下拉框
protected void SelectUsage(string selectid, string text)
{
IWebElement select = Find(By.Id(selectid));
IList<IWebElement> AllOptions = select.FindElements(By.TagName("option"));
foreach (IWebElement option in select.FindElements(By.TagName("option")))
{
if (option.GetAttribute("value").Equals(text))
option.Click();
}
}
  13:Selenium中對confirm ,alert ,prompt的操作
//在本次瀏覽器兼容性測試項目中遇到的只有confirm和alert
//下面舉例說明confirm和alert的代碼,prompt類似
//confirm的操作
IAlert confirm = driver.SwitchTo().Alert();
confirm.Accept();
//Alert的操作
//個人網(wǎng)銀中同樣的業(yè)務(wù)有時候不會彈對alert,此時需要判斷alert是否存在
//對Alert提示框作確定操作,默認等待50毫秒
protected void AlertAccept()
{
AlertAccept(0.05);
}
//等待幾秒,可以為小數(shù),單位為秒
protected void AlertAccept(double waitseSonds)
{
double nsleepMillon = waitseSonds * 1000;
int k=0;
int split=50;
IAlert alert = null;
do
{
k++;
Thread.Sleep(split);
alert = driver.SwitchTo().Alert();
} while (k * split <= nsleepMillon || alert==null);
if (alert != null)
{
alert.Accept();
}
}
  14:Selenium WebDriver的截圖功能
  //WebDriver中自帶截圖功能
  Screenshot screenShotFile = ((ITakesScreenshot)driver).GetScreenshot();
  screenShotFile.SaveAsFile("test",ImageFormat.Jpeg);
 

軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd