Introduction
As you probably know, I am developing a series of posts called Pragmatic Automation with WebDriver. They consist of tons of practical information how to start writing automation tests with WebDriver. Also, they 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 today's advanced WebDriver Automation tips and tricks.
1. Start FirefoxDriver with Plugins
Just use the AddExtension
of the FirefoxProfile
class to load the desired extension.
FirefoxProfile profile = new FirefoxProfile();
profile.AddExtension(@"C:\extensionsLocation\extension.xpi");
IWebDriver driver = new FirefoxDriver(profile);
2. Set HTTP Proxy ChromeDriver
The configuration of a proxy for ChromeDriver
is a little bit different from the one for FirefoxDriver
. You need to use the special Proxy
class in combination with ChromeOptions
.
ChromeOptions options = new ChromeOptions();
var proxy = new Proxy();
proxy.Kind = ProxyKind.Manual;
proxy.IsAutoDetect = false;
proxy.HttpProxy =
proxy.SslProxy = "127.0.0.1:3239";
options.Proxy = proxy;
options.AddArgument("ignore-certificate-errors");
IWebDriver driver = new ChromeDriver(options);
3. Set HTTP Proxy with Authentication ChromeDriver
The only difference from the previous example is the configuration of the --proxy-server argument.
ChromeOptions options = new ChromeOptions();
var proxy = new Proxy();
proxy.Kind = ProxyKind.Manual;
proxy.IsAutoDetect = false;
proxy.HttpProxy =
proxy.SslProxy = "127.0.0.1:3239";
options.Proxy = proxy;
options.AddArguments("--proxy-server=http://user:password@127.0.0.1:3239");
options.AddArgument("ignore-certificate-errors");
IWebDriver driver = new ChromeDriver(options);
4. Start ChromeDriver with an Unpacked Extension
Chrome extensions can be either packed or unpacked. Packed extensions are a single file with a .crx extension. Unpacked Extensions are a directory containing the extension, including a manifest.json file. To load an unpacked extension, you need to set the load-extension
argument.
ChromeOptions options = new ChromeOptions();
options.AddArguments("load-extension=/pathTo/extension");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.SetCapability(ChromeOptions.Capability, options);
DesiredCapabilities dc = DesiredCapabilities.Chrome();
dc.SetCapability(ChromeOptions.Capability, options);
IWebDriver driver = new RemoteWebDriver(dc);
5. Start ChromeDriver with an Packed Extension
Instead of setting the load-extension
argument, you need to use the AddExtension
method of the ChromeOptions
class to set the path to your packed extension.
ChromeOptions options = new ChromeOptions();
options.AddExtension(Path.GetFullPath("local/path/to/extension.crx"));
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.SetCapability(ChromeOptions.Capability, options);
DesiredCapabilities dc = DesiredCapabilities.Chrome();
dc.SetCapability(ChromeOptions.Capability, options);
IWebDriver driver = new RemoteWebDriver(dc);
6. Assert a Button Enabled or Disabled
You can check if an element is disabled through the Enabled
property of the IWebElement
interface.
[TestMethod]
public void AssertButtonEnabledDisabled()
{
this.driver.Navigate().GoToUrl
(@"http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_button_disabled");
this.driver.SwitchTo().Frame("iframeResult");
IWebElement button = driver.FindElement(By.XPath("/html/body/button"));
Assert.IsFalse(button.Enabled);
}
7. Set and Assert the Value of a Hidden Field
You can get the value of a hidden field through the GetAttribute
method, part of the IWebElement
interface. Get the value attribute of the element. You can set the same attribute with a little bit of a JavaScript code.
[TestMethod]
public void AssertButtonEnabledDisabled()
{
this.driver.Navigate().GoToUrl
(@"http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_button_disabled");
this.driver.SwitchTo().Frame("iframeResult");
IWebElement button = driver.FindElement(By.XPath("/html/body/button"));
Assert.IsFalse(button.Enabled);
}
8. Wait AJAX Call to Complete Using JQuery
jQuery.active
is a variable JQuery uses internally to track the number of simultaneous AJAX requests. Wait until the value of jQuery.active
is zero. Then, continue with the next operation.
public void WaitForAjaxComplete(int maxSeconds)
{
bool isAjaxCallComplete = false;
for (int i = 1; i <= maxSeconds; i++)
{
isAjaxCallComplete = (bool)((IJavaScriptExecutor)driver).
ExecuteScript("return window.jQuery != undefined && jQuery.active == 0");
if (isAjaxCallComplete)
{
return;
}
Thread.Sleep(1000);
}
throw new Exception(string.Format("Timed out after {0} seconds", maxSeconds));
}
9. Verify File Downloaded ChromeDriver
To change the default download directory of the current Chrome instance, set the download.default_directory
argument. When we initiate the file download, we use the WebDriverWait
to wait until the file exists on the file system. Finally, we assert the file size. The whole code is surrounded with a try
-finally
block. In the finally
, we delete the downloaded file so that our test will be consistent every time.
[TestMethod]
public void VerifyFileDownloadChrome()
{
string expectedFilePath =
@"c:\temp\Testing_Framework_2015_3_1314_2_Free.exe";
try
{
String downloadFolderPath = @"c:\temp\";
var options = new ChromeOptions();
options.AddUserProfilePreference
("download.default_directory", downloadFolderPath);
driver = new ChromeDriver(options);
driver.Navigate().GoToUrl
("https://www.telerik.com/download-trial-file/v2/telerik-testing-framework");
WebDriverWait wait =
new WebDriverWait(driver, TimeSpan.FromSeconds(30));
wait.Until((x) =>
{
return File.Exists(expectedFilePath);
});
FileInfo fileInfo = new FileInfo(expectedFilePath);
long fileSize = fileInfo.Length;
Assert.AreEqual(4326192, fileSize);
}
finally
{
if (File.Exists(expectedFilePath))
{
File.Delete(expectedFilePath);
}
}
}
10. Verify File Downloaded FirefoxDriver
The code for FirefoxDriver
is similar to the one for ChromeDriver
. We need to set a few arguments so that the browser does not ask us every time where to save the specified file.
[TestMethod]
public void VerifyFileDownloadFirefox()
{
string expectedFilePath = @"c:\temp\Testing_Framework_2015_3_1314_2_Free.exe";
try
{
String downloadFolderPath = @"c:\temp\";
FirefoxProfile profile = new FirefoxProfile();
profile.SetPreference("browser.download.folderList", 2);
profile.SetPreference("browser.download.dir", downloadFolderPath);
profile.SetPreference("browser.download.manager.alertOnEXEOpen", false);
profile.SetPreference("browser.helperApps.neverAsk.saveToDisk",
"application/msword, application/binary, application/ris, text/csv,
image/png, application/pdf, text/html, text/plain, application/zip,
application/x-zip, application/x-zip-compressed,
application/download, application/octet-stream");
this.driver = new FirefoxDriver(profile);
driver.Navigate().GoToUrl
("https://www.telerik.com/download-trial-file/v2/telerik-testing-framework");
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
wait.Until((x) =>
{
return File.Exists(expectedFilePath);
});
FileInfo fileInfo = new FileInfo(expectedFilePath);
long fileSize = fileInfo.Length;
Assert.AreEqual(4326192, fileSize);
}
finally
{
if (File.Exists(expectedFilePath))
{
File.Delete(expectedFilePath);
}
}
}
So Far in the 'Pragmatic Automation with WebDriver' Series
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 3 appeared first on Automate The Planet.
All images are purchased from DepositPhotos.com and cannot be downloaded and used for free.
License Agreement