Selenium With C# 基礎教程

Selenium API

Selenium API中的Actions類(==注意不要跟.NET自帶的委託Action混淆==)提供了一些方法用於用戶與瀏覽器進行較爲複雜的交互操作,可以讓用戶通過鍵盤和鼠標等進行一系列的操作。詳細的方法,我們可以通過查看Actions類,如下圖所示:

11-1 Selenium API.jpg

通過Actions類我們大致可以提取出以下幾種交互方式

  • 鼠標事件
    Click()
    ClickAndHold()
    ContextClick()
    DoubleClick()
    DragAndDrop()
    DragAndDropToOffset()
    MoveByOffset()
    MoveToElement()
    Release()
  • 鍵盤事件
    KeyDown()
    KeyUp()
    SendKeys()

使用方法

  • 第一步:需要引用OpenQA.Selenium.Interactions
using OpenQA.Selenium.Interactions;
  • 第二步:調用方法
new Actions(driver).+"需要調用的方法"+.Perform();

鼠標單擊控件

這個在之前的文章已經多次使用過了,示例代碼如下:

IWebDriver driver=new ChromeDriver();
driver.Url = "https://www.baidu.com";
IWebElement eles = driver.FindElement(By.LinkText("關於百度"));
Actions actionsObj = new Actions(driver);
actionsObj.Click(eles).Perform();

上面是通過Actions類來實現鼠標單擊,其實也可以直接使用IWebElement中自帶的Click方法

鼠標雙擊控件

IWebDriver driver=new ChromeDriver();
IWebElement eles=driver.FindElement(By.Id("id"));
Actions actionsObj = new Actions(driver);
actionsObj.DoubleClick(eles).Perform();

鼠標單擊拖動

下面的代碼演示的是從第1個控件單擊並拖動至第5個控件,並選中這5個控件,如下所示:

driver.Url = "http://www.jqueryui.org.cn/demo/5640.html";
Thread.Sleep(1000);
driver.Manage().Window.Maximize();
ReadOnlyCollection<IWebElement> items = driver.FindElements(By.XPath("//ol[@id='selectable']/li"));
Assert.AreEqual<int>(7, items.Count);
Actions actionsObj = new Actions(driver);
actionsObj.ClickAndHold(items[0]).ClickAndHold(items[4]).Release().Perform();

最後一行代碼連續調用了兩次ClickAndHold()方法,==第1次調用代表是起始控件,第2次調用代表是結束控件,注意裏面的下標是從0開始==

  • 鼠標單擊拖動前後的狀態
  • 11-2 鼠標單擊拖動前後的狀態_c2i.jpg

移動鼠標到指定控件

IWebElement eles=driver.FindElement(By.Id("id"));
Actions actionsObj = new Actions(driver);
actionsObj.MoveToElement(eles).Perform();

鼠標拖放

拖放操作因操作簡單易懂,在Web頁面中應用也越來越廣,特別是一些銀行頁面中。以下代碼演示如何通過Selenium API來完成操作。

  • 方法一:通過方法DragAndDrop()實現
driver.Url = "http://www.jqueryui.org.cn/demo/5622.html";
Thread.Sleep(1000);
driver.Manage().Window.Maximize();
IWebElement soureEle = driver.FindElement(By.Id("draggable"));
IWebElement targetEle = driver.FindElement(By.Id("droppable"));
Actions actionObj = new Actions(driver);
actionObj.DragAndDrop(soureEle, targetEle).Perform();
IWebElement textEle = driver.FindElement(By.XPath("//div[@id='droppable']/p"));
Assert.IsTrue(textEle.Text.Contains("Dropped!"));
  • 方法二:通過方法MoveToElement實現
driver.Url = "http://www.jqueryui.org.cn/demo/5622.html";
Thread.Sleep(1000);
driver.Manage().Window.Maximize();
IWebElement soureEle = driver.FindElement(By.Id("draggable"));
IWebElement targetEle = driver.FindElement(By.Id("droppable"));
Actions actionObj = new Actions(driver);
actionObj.ClickAndHold(soureEle).MoveToElement(targetEle).Release(targetEle).Perform();
IWebElement textEle = driver.FindElement(By.XPath("//div[@id='droppable']/p"));
Assert.IsTrue(textEle.Text.Contains("Dropped!"));
  • 鼠標拖放前後的狀態
  • 11-3 鼠標拖放前後狀態_c2i.jpg

拖動進度條或滑塊

進度條或滑塊一般都是通過JavaScript或JQuery實現,方便用戶直觀的調整值。一種使用JavaScript實現的滑塊如下所示:

11-4 滑塊示例_c2i.jpg

示例代碼如下:

driver.Url = "http://demo.lanrenzhijia.com/2015/drag1218/";
Thread.Sleep(1000);
driver.Manage().Window.Maximize();
IWebElement origianlValueEle = driver.FindElement(By.Id("title"));
Assert.AreEqual("0", origianlValueEle.Text);
IWebElement sliderEle = driver.FindElement(By.Id("btn"));
Actions actionsObj = new Actions(driver);
actionsObj.DragAndDropToOffset(sliderEle, 100, 0).Perform();
Assert.AreEqual("51%", origianlValueEle.Text);

滑塊移動前後的狀態

11-5 移動滑塊代碼實現_c2i.jpg

單擊鼠標右鍵菜單

在部分網站中會有一些操作需要通過右鍵菜單來實現,對於這種情況Selenium API也提供一種方法ContextClick()方法,詳細演示如下所示:

driver.Url = "http://www.helloweba.com/demo/2017/basicContext/";
Thread.Sleep(1000);
driver.Manage().Window.Maximize();
IWebElement btnEle = driver.FindElement(By.XPath("//button[contains(@class,'btn-success context')]"));
Actions actionsObj = new Actions(driver);
//點擊右鍵菜單
actionsObj.ContextClick(btnEle).Perform();
IWebElement rightMenu = driver.FindElement(By.XPath("//div[@class='basicContext']/table/tbody/tr[2]"));
rightMenu.Click();

IAlert alert = driver.SwitchTo().Alert();
string alertText = alert.Text;
alert.Accept();
Assert.AreEqual<string>("Item clicked!",alertText);

鍵盤操作

在之前的文章中我們經常使用SendKeys()方法向頁面中輸入文本,下面我們將演示,在百度搜索中輸入一段文字,然後通過鍵盤全選,再刪除的案例,代碼如下:

driver.Url = "https://www.baidu.com/";
Thread.Sleep(1000);
driver.Manage().Window.Maximize();
IWebElement searchBoxEle = driver.FindElement(By.Id("kw"));
searchBoxEle.SendKeys("在百度搜索框中輸入一段文字");
Thread.Sleep(1000);
Actions actionObj = new Actions(driver);
//使用Ctrl+A全選輸入的文字
actionObj.Click(searchBoxEle).KeyDown(Keys.Control).SendKeys("a").KeyUp(Keys.Control).Perform();
//使用鍵盤Backspace刪除剛纔輸入的文字
actionObj.SendKeys(Keys.Backspace).Perform();

注意上面代碼最後一行使用了Actions類中的SendKeys()方法,該方法與IWebElement接口中的SendKeys()方法不一樣,注意區別,如下所示:

11-6 SendKeys方法的區別.jpg



作者:Surpassme
鏈接:https://www.jianshu.com/p/310623afcde1
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯繫作者獲得授權並註明出處。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章