WebDriver自动化测试高级技巧

自动化测试领域,WebDriver是一个强大的工具,它允许测试人员编写脚本来模拟用户与网页的交互。本文将分享一些高级的WebDriver使用技巧,帮助测试人员提高测试效率和质量。

1. 拖放操作

使用WebDriver的Actions类可以执行复杂的UI交互,例如拖放。通过设置期望的X和Y偏移量,可以使用DragAndDropToOffset方法来实现拖放。

[Test] public void DragAndDrop() { this.driver.Navigate().GoToUrl( "http://loopj.com/jquery-simple-slider/" ); IWebElement element = driver.FindElement(By.XPath( "//*[@id='project']/p[1]/div/div[2]" )); Actions move = new Actions(driver); move.DragAndDropToOffset(element, 30, 0).Perform(); }

2.文件上传

使用WebDriver上传文件是一个简单的任务。需要定位文件元素,并使用IWebElement的SendKeys方法来设置文件路径。

[Test] public void FileUpload() { this.driver.Navigate().GoToUrl( "https://demos.telerik.com/aspnet-ajax/ajaxpanel/application-scenarios/file-upload/defaultcs.aspx" ); IWebElement element = driver.FindElement(By.Id( "ctl00_ContentPlaceholder1_RadUpload1file0" )); string filePath = @"D:\Projects\PatternsInAutomation.Tests\WebDriver.Series.Tests\bin\Debug\WebDriver.xml"; element.SendKeys(filePath); }

3. 处理JavaScript弹窗

通过IWebDriver的ITargetLocator接口,可以定位JavaScript弹窗。然后可以使用IAlert接口的Accept和Dismiss方法。

[Test] public void JavaScripPopUps() { this.driver.Navigate().GoToUrl( "http://www.w3schools.com/js/tryit.asp?filename=tryjs_confirm" ); this.driver.SwitchTo().Frame("iframeResult"); IWebElement button = driver.FindElement(By.XPath("/html/body/button")); button.Click(); IAlert a = driver.SwitchTo().Alert(); if (a.Text.Equals("Press a button!")) { a.Accept(); } else { a.Dismiss(); } }

4. 在浏览器窗口或标签页之间切换

WebDriver在单个浏览器窗口的范围内驱动浏览器。但是,可以使用其SwitchTo方法来改变目标窗口或标签页。

[Test] public void MovingBetweenTabs() { this.driver.Navigate().GoToUrl( "http://automatetheplanet.com/compelling-sunday-14022016/" ); driver.FindElement(By.LinkText("10 Advanced WebDriver Tips and Tricks Part 1")).Click(); driver.FindElement(By.LinkText("The Ultimate Guide To Unit Testing in ASP.NET MVC")).Click(); ReadOnlyCollection windowHandles = driver.WindowHandles; string firstTab = windowHandles.First(); string lastTab = windowHandles.Last(); driver.SwitchTo().Window(lastTab); Assert.AreEqual( "The Ultimate Guide To Unit Testing in ASP.NET MVC", driver.Title); driver.SwitchTo().Window(firstTab); Assert.AreEqual( "Compelling Sunday – 19 Posts on Programming and Quality Assurance", driver.Title); }

WebDriver的INavigation接口包含有用的方法,用于前进和后退。此外,还可以刷新当前页面。

[Test] public void NavigationHistory() { this.driver.Navigate().GoToUrl( "http://www.codeproject.com/Articles/1078541/Advanced-WebDriver-Tips-and-Tricks-Part" ); this.driver.Navigate().GoToUrl( "http://www.codeproject.com/Articles/1017816/Speed-up-Selenium-Tests-through-RAM-Facts-and-Myth" ); driver.Navigate().Back(); Assert.AreEqual( "10 Advanced WebDriver Tips and Tricks - Part 1 - CodeProject", driver.Title); driver.Navigate().Refresh(); Assert.AreEqual( "10 Advanced WebDriver Tips and Tricks - Part 1 - CodeProject", driver.Title); driver.Navigate().Forward(); Assert.AreEqual( "Speed up Selenium Tests through RAM Facts and Myths - CodeProject", driver.Title); }

6. 更改用户代理

在之前的系列文章中,展示了如何创建一个新的自定义Firefox配置文件。可以设置其参数'general.useragent.override'为所需的用户代理字符串。

FirefoxProfileManager profileManager = new FirefoxProfileManager(); FirefoxProfile profile = new FirefoxProfile(); profile.SetPreference( "general.useragent.override", "Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.1.0.346 Mobile Safari/534.11+" ); this.driver = new FirefoxDriver(profile);

7. 设置HTTP代理

与用户代理配置类似,要为Firefox设置代理,只需要设置Firefox配置文件的几个参数。

FirefoxProfile firefoxProfile = new FirefoxProfile(); firefoxProfile.SetPreference( "network.proxy.type", 1 ); firefoxProfile.SetPreference( "network.proxy.http", "myproxy.com" ); firefoxProfile.SetPreference( "network.proxy.http_port", 3239 ); driver = new FirefoxDriver(firefoxProfile);

8. 处理SSL证书错误

8.1. 处理SSL证书错误FirefoxDriver

FirefoxProfile firefoxProfile = new FirefoxProfile(); firefoxProfile.AcceptUntrustedCertificates = true; firefoxProfile.AssumeUntrustedCertificateIssuer = false; driver = new FirefoxDriver(firefoxProfile);

8.2. 处理SSL证书错误ChromeDriver

DesiredCapabilities capability = DesiredCapabilities.Chrome(); Environment.SetEnvironmentVariable( "webdriver.chrome.driver", "C:\\Path\\To\\ChromeDriver.exe" ); capability.SetCapability(CapabilityType.AcceptSslCertificates, true); driver = new RemoteWebDriver(capability);

8.3. 处理SSL证书错误InternetExplorerDriver

DesiredCapabilities capability = DesiredCapabilities.InternetExplorer(); Environment.SetEnvironmentVariable( "webdriver.ie.driver", "C:\\Path\\To\\IEDriver.exe" ); capability.SetCapability(CapabilityType.AcceptSslCertificates, true); driver = new RemoteWebDriver(capability);

9. 滚动焦点到控件

WebDriver没有内置机制来滚动焦点到控件。但是,可以使用JavaScript的window.scroll方法。只需要传递目标元素的Y位置。

[Test] public void ScrollFocusToControl() { this.driver.Navigate().GoToUrl( "http://automatetheplanet.com/compelling-sunday-14022016/" ); IWebElement link = driver.FindElement(By.PartialLinkText("Previous post")); string jsToBeExecuted = string.Format( "window.scroll(0, {0});", link.Location.Y); ((IJavaScriptExecutor)driver).ExecuteScript(jsToBeExecuted); link.Click(); Assert.AreEqual( "10 Advanced WebDriver Tips and Tricks - Part 1", driver.Title); }

10. 控制焦点

有两种方法可以实现。第一种是使用IWebElement的SendKeys方法与空字符串。第二种是使用一点JavaScript代码。

[Test] public void FocusOnControl() { this.driver.Navigate().GoToUrl( "http://automatetheplanet.com/compelling-sunday-14022016/" ); IWebElement link = driver.FindElement(By.PartialLinkText("Previous post")); // Option 1. link.SendKeys(string.Empty); // Option 2. ((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].focus();", link); }

本文是“实用自动化与WebDriver”系列的一部分,以下是一些相关文章:

  • 10 Advanced WebDriver Tips and Tricks Part 2
  • 10 Advanced WebDriver Tips and Tricks Part 1
  • Create Custom Selenium IDE Export to WebDriver
  • Automate Telerik Kendo Grid with WebDriver and JavaScript
  • Speed up Selenium Tests through RAM Facts and Myths
  • Microsoft Edge WebDriver- What Everybody Ought to Know About
  • Test URL Redirects with WebDriver and HttpWebRequest
  • WebDriver Selenium Tor Integration C# Code
  • Most Underrated WebDriver Locator - XPath
  • Getting Started with WebDriver C# in 10 Minutes

如果喜欢文章,请订阅,也可以分享到社交网络。

沪ICP备2024098111号-1
上海秋旦网络科技中心:上海市奉贤区金大公路8218号1幢 联系电话:17898875485