之前一直是用selenium進行web自動化測試的,把之前用selenium寫用例的過程中需要注意的地方和問題點和大家一起分享一下
1、Selenium簡介
Selenium Selenium 是ThoughtWorks專門爲Web應用程序編寫的一個驗收測試工具。
Selenium測試直接運行在瀏覽器中,就像真正的用戶在操作一樣。支持的瀏覽器包括IE、Google Chrome、Mozilla Firefox、Mozilla Suite等。這個工具的主要功能包括:測試與瀏覽器的兼容性——測試你的應用程序看是否能夠很好得工作在不同瀏覽器和操作系統之上。測試系統功能——創建衰退測試檢驗軟件功能和用戶需求。支持自動錄製動作和自動生成。Net、Java、Perl等不同語言的測試腳本。
2、Selenium+Visual Studio環境配置
先到官網先下載IEDriverServer(32位或者64位),解壓文件將IEDriverServer.exe存放到一個指定路徑,要記住這個路徑,配置過程中要用到。(http://docs.seleniumhq.org/download/ )
如果使用chrome,對應的驅動文件是chromedriver.exe,也是需要把chromedriver.exe存放到指定路徑。(http://chromedriver.storage.googleapis.com/index.html)
如果是火狐瀏覽器,對應的驅動文件是geckodriver.exe,同樣也是需要把geckodriver.exe存放到指定路徑。(http://tv.cctv.com/live/)
打開VS(爲了便於查看,我用的是中文版的,英文版的請自己對照位置),打開“工具”菜單下的“擴展管理器”:
我們需要在“擴展管理器”中安裝“NuGet”,單擊下載安裝,然後重啓你的VS。
創建一個測試項進行測試
右鍵“引用”,選擇“NuGet程序包”。
在這裏選擇“聯機”,搜索“selenium”。然後安裝就行了。
安裝Selenium包後項目引用裏可看到如下動態庫
創建一個測試類進行環境驗證(注意到“C:\IEDriverServer”了嗎?這就是存放IEDriverServer.exe的路徑)
可能遇到的問題:
1):未找到驅動服務
參考方案: 就看看IEDriverServer.exe的路徑是不是錯了。 另外:瀏覽器的“啓動保護模式”我給取消勾選了,如果勾選的話偶爾也會報異常。
2):用戶代碼未處理
參考方案: 看看是不是瀏覽器的驅動用錯了(32位用了64位的,或者反過來)。
3、Selenium—頁面元素定位
By.className(className))
By.cssSelector(selector)
By.id(id)
By.linkText(linkText)
By.name(name)
By.partialLinkText(linkText)
By.tagName(name)
By.xpath(xpathExpression)
注意:
selenium-webdriver通過findElement()\findElements()等find方法調用"By"對象來定位和查詢元素。
By類只是提供查詢的方式進行分類。findElement返回一個元素對象否則拋出異常,findElements返回符合條 件的元素List,如果不存在符合條件的就返回一個空的list。
各方法使用優先級爲:id,name,linktext優先使用,cssselector次之,最後使用xpath。
4、Selenium自動化--對象操作
1):點擊按鈕/鏈接click()
Driver.FindElement(By.XPath(“//input[@id=‘submit’ and @value=‘下一步’]”)).click();
2):清空文本框clear()
Driver.FindElement(By.Id(“tranAmtText”)).clear();
3):在文本框中輸入指定的字符串sendkeys()
Driver.FindElement(By.Id("tranAmtText")).SendKeys(“123456”);
4):移動光標到指定的元素上perform
Actions action=new Actions(driver);
action.MoveToElement(Find(By.XPath("//input[@id='submit' and @value='確定']"))).Perform();
5):模擬光標晃動movebyoffset()
Actions action = new Actions(driver);
action.MoveByOffset(2, 4);
6):等待頁面元素加載完成,默認等待100秒
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(100));
//等待頁面上ID屬性值爲submitButton的元素加載完成
wait.Until((d) => { return WaitForObject(By.Id("submitButton")); });
5、Selenium自動化—操作下拉框
Select操作//選擇下拉框
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();
}
}
6、Selenium自動化—操作iframe
1):切換焦點到id爲固定值的iframe上
進入頁面後,光標默認焦點在DefaultContent中,若想要定位到iframe 需要轉換焦點
driver.SwitchTo().DefaultContent();
//切換焦點到mainFrame
driver.SwitchTo().Frame("mainFrame");
需要注意的是:切換焦點之後若想切換焦點到其他iframe上 需要先返回到defaultcontent,再切換焦點到指定的iframe上。
2):切換焦點到id值爲動態值的iframe上
有時候 頁面上浮出層的id爲動態值,此時需要先獲取所有符合記錄的iframe放置在數組中,然後遍歷數組切換焦點到目標iframe上。 如下方法:
protected string bizFrameId = string.Empty;
protected string bizId = string.Empty;
//獲取動態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];
}
}
}
7、Selenium自動化—alert prompt confirm
//在本次瀏覽器兼容性測試項目中遇到的只有confirm和alert
//下面舉例說明confirm和alert的代碼,prompt類似
//confirm的操作
IAlert confirm = driver.SwitchTo().Alert();
confirm.Accept();
//Alert的操作
//健康直播間中同樣的業務有時候不會彈對alert,此時需要判斷alert是否存在
//對Alert提示框作確定操作,默認等待50毫秒
protected void AlertAccept() {
AlertAccept(0.05);
}
//等待幾秒,可以爲小數,單位爲秒
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();
}
}
8、Selenium自動化—瀏覽器操作
首先生成一個Web對象
WebDriver driver = new FirefoxDriver();
//打開指定的URL地址
driver.Navigate().GoToUrl(@"http://doctor.guahao.com");
//關閉瀏覽器
Driver.quit();
//獲取所有的WindowHandle,關閉所有子窗口
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);