10 Advanced WebDriver Tips and Tricks Part 2





5.00/5 (6 votes)
Find some advanced WebDriver tips and tricks how to use the framework for changing user agent or dealing with SSL certificates.The post 10 Advanced WebDriver Tips and Tricks Part 2 appeared first on Automate The Planet.
Introduction
As you probably know I am developing a series posts called- Pragmatic Automation with WebDriver. They consist of tons of practical information how to start writing automation tests with WebDriver. Also, contain a lot of more advanced topics such as automation strategies, benchmarks and researches. In the next couple of publications, I am going to share with you some Advanced WebDriver usages in tests. Without further ado, here are the today's advanced WebDriver Automation tips and trips.
1. Drag and Drop
2. Upload a File
It is a straightforward task to upload a file using WebDriver. You need to locate the file element and use the IWebElement's SendKeys method to set the path to your file.
3. Handle JavaScript Pop-ups
Through the ITargetLocator interface of the IWebDriver, you can locate the JavaScript alert. Then you can use the Accept and Dismiss methods of the IAlert interface.

4. Switch Between Browser Windows or Tabs
WebDriver drives the browser within a scope of one browser window. However, we can use its SwitchTo method to change the target window or tab.
The WindowHandles property returns all open browser windows. You can pass the name of the desired tab/window to the Window method of the ITargetLocator interface (returned by the SwitchTo method) to change the current target.

5. Navigation History
WebDriver's INavigation interface contains handy methods for going forward and backward. Also, you can refresh the current page.
6. Change User Agent
In my previous post from the series, I showed you how to create a new custom Firefox profile. You can set its argument 'general.useragent.override' to the desired user agent string.
7. Set HTTP Proxy for Browser
Similar to the user agent configuration, to set a proxy for Firefox, you only need to set a few arguments of the Firefox' profile.
8. Handle SSL Certicate Error
8.1. Handle SSL Certicate Error FirefoxDriver
8.2. Handle SSL Certicate Error ChromeDriver
You can use the special SetEnvironmentVariable method to create an environment variable in Windows pointing the path to your driver's executable. This way you do not need to specify the path to it every time you initialize the driver's instances.
DesiredCapabilities capability = DesiredCapabilities.Chrome(); Environment.SetEnvironmentVariable("webdriver.ie.driver", "C:\\Path\\To\\ChromeDriver.exe"); capability.SetCapability(CapabilityType.AcceptSslCertificates, true); driver = new RemoteWebDriver(capability);
8.3. Handle SSL Certicate Error ChromeDriver

9. Scroll Focus to Control
There isn't a built-in mechanism in WebDriver to scroll focus to a control. However, you can use the JavaScript's method window.scroll. You only need to pass the Y location of the desired element.
10. Focus on a Control
There are two ways to do the job. The first one is to use the IWebElement's SendKeys method with empty string. The second is to use a little bit of JavaScript code.
So Far in the 'Pragmatic Automation with WebDriver' Series
- 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
If you enjoy my publications, feel free to SUBSCRIBE
Also, hit these share buttons. Thank you!
Source Code
The post 10 Advanced WebDriver Tips and Tricks Part 2 appeared first on Automate The Planet.
All images are purchased from DepositPhotos.com and cannot be downloaded and used for free.
License Agreement