Skip to content

Instantly share code, notes, and snippets.

@akimboyko
Last active December 18, 2015 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akimboyko/5766080 to your computer and use it in GitHub Desktop.
Save akimboyko/5766080 to your computer and use it in GitHub Desktop.
Selenium WebDriver integration testing using ScriptCs. FireFox and PhantomJs drivers are used
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Selenium.WebDriver" version="2.33.0" targetFramework="net40" />
<package id="Selenium.Support" version="2.33.0" targetFramework="net40" />
<package id="phantomjs.exe" version="1.8.1" targetFramework="net40" />
<package id="NUnit" version="2.6.2" targetFramework="net40" />
<package id="FluentAssertions" version="2.0.1" targetFramework="net45" />
</packages>
// load all require references
#r "System.Drawing"
#r "D:\work\Courses\MetaProgramming\Snippets\Scripting\ScriptCs\SeleniumWebDriver\bin\WebDriver.dll"
#r "D:\work\Courses\MetaProgramming\Snippets\Scripting\ScriptCs\SeleniumWebDriver\bin\WebDriver.Support.dll"
#r "D:\work\Courses\MetaProgramming\Snippets\Scripting\ScriptCs\SeleniumWebDriver\bin\nunit.framework.dll"
#r "D:\work\Courses\MetaProgramming\Snippets\Scripting\ScriptCs\SeleniumWebDriver\bin\FluentAssertions.dll"
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
using FluentAssertions;
// create WebDriver instance
using (var driver = new FirefoxDriver())
{
try
{
driver.Navigate().GoToUrl(@"http://localhost:8080/knockout.js/index.html");
var firstName = driver.FindElement(By.Id("firstName"));
firstName.Clear();
firstName.SendKeys("Ole");
firstName.SendKeys(Keys.Tab);
var lastName = driver.FindElement(By.Id("lastName"));
lastName.Clear();
lastName.SendKeys("Hansenn");
lastName.SendKeys(Keys.Tab);
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(3));
wait.Until(c => c.FindElement(By.Id("fullName")).Text.Contains("Ole Hansenn"));
driver.FindElement(By.Id("fullName")).Text.Should().Be("Ole Hansenn");
}
finally
{
// create screenshot
driver
.GetScreenshot()
.SaveAsFile("./test_firefox.png", System.Drawing.Imaging.ImageFormat.Png);
driver.Quit();
}
}
// load all require references
#r "System.Drawing"
#r "D:\work\Courses\MetaProgramming\Snippets\Scripting\ScriptCs\SeleniumWebDriver\bin\WebDriver.dll"
#r "D:\work\Courses\MetaProgramming\Snippets\Scripting\ScriptCs\SeleniumWebDriver\bin\WebDriver.Support.dll"
#r "D:\work\Courses\MetaProgramming\Snippets\Scripting\ScriptCs\SeleniumWebDriver\bin\nunit.framework.dll"
#r "D:\work\Courses\MetaProgramming\Snippets\Scripting\ScriptCs\SeleniumWebDriver\bin\FluentAssertions.dll"
using System;
using System.IO;
using OpenQA.Selenium;
using OpenQA.Selenium.PhantomJS;
using OpenQA.Selenium.Support.UI;
using FluentAssertions;
// hack to move drivers into bin so they can be located by Selenium (only prob on scriptcs atm)
foreach (var driver in Directory.GetFiles(Environment.CurrentDirectory, "*.exe", SearchOption.AllDirectories))
{
var newFileName = Path.Combine(Environment.CurrentDirectory, "bin", Path.GetFileName(driver));
File.Copy(driver, newFileName);
}
const string phantomjsExeFile = @".\bin\phantomjs.exe";
// create WebDriver instance
using (var driver = new PhantomJSDriver())
{
try
{
driver.Navigate().GoToUrl(@"http://localhost:8080/knockout.js/index.html");
var firstName = driver.FindElement(By.Id("firstName"));
firstName.Clear();
firstName.SendKeys("Ole");
firstName.SendKeys(Keys.Tab);
var lastName = driver.FindElement(By.Id("lastName"));
lastName.Clear();
lastName.SendKeys("Hansenn");
lastName.SendKeys(Keys.Tab);
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(3));
wait.Until(c => c.FindElement(By.Id("fullName")).Text.Contains("Ole Hansenn"));
driver.FindElement(By.Id("fullName")).Text.Should().Be("Ole Hansenn");
}
finally
{
// create screenshot
driver
.GetScreenshot()
.SaveAsFile("./test_phantomjs.png", System.Drawing.Imaging.ImageFormat.Png);
driver.Quit();
// cleanup phantomjs.exe binary
if (File.Exists(phantomjsExeFile)) File.Delete(phantomjsExeFile);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment