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

  1:Selenium中對(duì)瀏覽器的操作
  首先生成一個(gè)Web對(duì)象
  IWebDriver driver = new FirefoxDriver();
  //打開(kāi)指定的URL地址
  driver.Navigate().GoToUrl(@"http://12.99.102.196:9080/corporbank/logon_pro.html");
  //關(guān)閉瀏覽器
  Driver.quit();
  網(wǎng)銀瀏覽器兼容性測(cè)試過(guò)程中,關(guān)閉瀏覽器后會(huì)有對(duì)話框,此問(wè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強(qiáng)制轉(zhuǎn)換成JS執(zhí)行器類型
  ((IJavaScriptExecutor) driver).ExecuteScript("js文件名");
  3:Selenium中定位頁(yè)面元素
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定位并操作
  //點(diǎn)擊classname為指定值的按鈕
  Driver.FindElement(By.ClassName(“WotherDay”)).click();
  3.3根據(jù)元素的linktext定位并操作
  Driver.FindElement(By.LinkText(“選擇賬號(hào)”)).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定位并元素并操作
  //使用多個(gè)屬性定位元素
  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();
  因?yàn)閄path()方法的性能和效率低下。
  4:Selenium中清空文本框中的默認(rèn)內(nèi)容
  //清空文本框clear()
  Driver.FindElement(By.Id("tranAmtText")).clear();
  5:Selenium中在指定的文本框中輸入指定的字符串
  //在文本框中輸入指定的字符串sendkeys()
  Driver.FindElement(By.Id("tranAmtText")).SendKeys(“123456”);
  6:Selenium中移動(dòng)光標(biāo)到指定的元素上
  //移動(dòng)光標(biāo)到指定的元素上perform
  Actions action=new Actions(driver);
  action.MoveToElement(Find(By.XPath("//input[@id='submit' and @value='確定']"))).Perform();
  7:Selenium中點(diǎn)擊按鈕/鏈接
  //點(diǎn)擊按鈕/鏈接click()
  Driver.FindElement(By.XPath("//input[@id='submit' and @value='下一步']")).click();
  8:Selenium中等待頁(yè)面上的元素加載完成
  //等待頁(yè)面元素加載完成
  //默認(rèn)等待100秒
  WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(100));
  //等待頁(yè)面上ID屬性值為submitButton的元素加載完成
  wait.Until((d) => { return WaitForObject(By.Id("submitButton")); });
  9:Selenium中模擬鼠標(biāo)晃動(dòng)
  //模擬光標(biāo)晃動(dòng)movebyoffset()
  Actions action = new Actions(driver);
  action.MoveByOffset(2, 4);
  10:Selenium中對(duì)iframe中元素的定位
  5.1:切換焦點(diǎn)到id為固定值的iframe上
  進(jìn)入頁(yè)面后,光標(biāo)默認(rèn)焦點(diǎn)在DefaultContent中,若想要定位到iframe 需要轉(zhuǎn)換焦點(diǎn)
  driver.SwitchTo().DefaultContent();
  //切換焦點(diǎn)到mainFrame
  driver.SwitchTo().Frame("mainFrame");
  需要注意的是:切換焦點(diǎn)之后若想切換焦點(diǎn)到其他iframe上 需要先返回到defaultcontent,再切換焦點(diǎn)到指定的iframe上。
  5.2切換焦點(diǎn)到id值為動(dòng)態(tài)值的iframe上
  有時(shí)候 頁(yè)面上浮出層的id為動(dòng)態(tài)值,此時(shí)需要先獲取所有符合記錄的iframe放置在數(shù)組中,然后遍歷數(shù)組切換焦點(diǎn)到目標(biāo)iframe上。
  如下方法:
protected string bizFrameId = string.Empty;
protected string bizId = string.Empty;
//獲取動(dòng)態(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)閉多個(gè)子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中對(duì)下拉框的操作
//選擇下拉框
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中對(duì)confirm ,alert ,prompt的操作
//在本次瀏覽器兼容性測(cè)試項(xiàng)目中遇到的只有confirm和alert
//下面舉例說(shuō)明confirm和alert的代碼,prompt類似
//confirm的操作
IAlert confirm = driver.SwitchTo().Alert();
confirm.Accept();
//Alert的操作
//個(gè)人網(wǎng)銀中同樣的業(yè)務(wù)有時(shí)候不會(huì)彈對(duì)alert,此時(shí)需要判斷alert是否存在
//對(duì)Alert提示框作確定操作,默認(rèn)等待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);
 

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