在 Selenium C# 中獲取下拉選項的螢幕截圖。
我們可以使用 Selenium Webdriver 對下拉選項進行截圖。通常,截圖是針對失敗的測試用例進行捕獲的。這是藉助於 ITakesScreenshot 介面來實現的。
我們將藉助 GetScreenshot 方法來獲取螢幕截圖。最後,使用SaveAsFile方法,我們傳遞引數 - 檔案路徑和影像格式。
語法
((ITakesScreenshot)d). GetScreenshot().SaveAsFile("Screenshot.png",ScreenshotImageFormat.Png);
對於實現,我們將使用 NUnit 框架。
示例
using NUnit.Framework; using OpenQA.Selenium; using OpenQA.Selenium.Firefox; using System; namespace NUnitTestProject1{ public class Tests{ String u ="https://tutorialspoint.tw/selenium/selenium_automation_practice.htm"; IWebDriver d; [SetUp] public void Setup(){ //creating object of FirefoxDriver d = new FirefoxDriver(); } [Test] public void Test1(){ d.Navigate() .GoToUrl(u); d.Manage().Window.Maximize(); //identify dropdown IWebElement l = d.FindElement(By.Name("continents")); //scroll to dropdown ((IJavaScriptExecutor)d) .ExecuteScript("arguments[0].scrollIntoView(true);", l); l.Click(); //capture screenshot along file name ((ITakesScreenshot)d) .GetScreenshot().SaveAsFile("Screenshot.png", ScreenshotImageFormat.Png); } [TearDown] public void close_Browser(){ d.Quit(); } } }
輸出
在解決方案資源管理器中右鍵單擊專案。然後單擊在檔案資源管理器中開啟資料夾。
然後移動到資料夾bin->Debug,應該有名為 Screenshot.png 的檔案可用。
廣告