Skip to content

Instantly share code, notes, and snippets.

@johnmmoss
johnmmoss / gist:c3aa5390017c6c12c1f5
Last active August 29, 2015 14:16
Example GWT scenario for Specflow
Feature: UserAccountFeature
In order to be able to use the site
As a user
I want to be able to manage my account
Scenario: User can register to use the site
Given I am on the register page
And I have entered a Username of 'johnmmoss' and a password of 'P@ssword123'
When I press register
Then a new account is created with a username of a Username of 'johnmmoss'
@johnmmoss
johnmmoss / gist:311700a22e4b63df90ed
Last active August 29, 2015 14:16
Example Specflow steps
[Binding]
public class UserAccountSteps
{
[Given(@"I am on the register page")]
public void GivenIAmOnTheRegisterPage()
{
var homePage = ScenarioContext.Current.Get<HomePage>();
var registerPage = homePage.GoToRegisterPage();
ScenarioContext.Current.Set(registerPage);
}
@johnmmoss
johnmmoss / gist:13c00919752c06de252f
Created February 28, 2015 16:51
Initializing the Selenium FirefoxDriver
IWebDriver driver = new FirefoxDriver();
FeatureContext.Current.Set(driver);
driver.Navigate().GoToUrl(Url);
@johnmmoss
johnmmoss / gist:b74445923615d2ff99ad
Created February 28, 2015 16:55
Example Page Objects using Selenium WebDriver
public class HomePage
{
private readonly IWebDriver webDriver;
protected static string BaseUrl = ConfigurationManager.AppSettings["WebsiteUrl"];
public HomePage(IWebDriver webDriver)
{
this.webDriver = webDriver;
}
@johnmmoss
johnmmoss / gist:03e1c688de6257a448e6
Created February 28, 2015 17:13
Example Specflow feature hooks
[BeforeFeature()]
public static void BeforeFeature() // must be static
{
// Create the webdriver and store it in the feature context
IWebDriver driver = new FirefoxDriver();
FeatureContext.Current.Set(driver);
driver.Navigate().GoToUrl(Url);
// Empty the database ready for the tests
using (var context = new SpecflowTestContext())
@johnmmoss
johnmmoss / gist:c11bfe2abf078fc88c7a
Created March 13, 2015 16:15
Selenium API get the Rows of an HTML table
var trElements = webDriver.FindElements(By.ClassName("tr-content"));
var result = new List<List<string>>();
foreach (var trElement in trElements)
{
var row = new List<string>();
foreach (var tdElement in trElement.FindElements(By.ClassName("td-content")))
{
row.Add(tdElement.Text);
}
result.Add(row);
@johnmmoss
johnmmoss / gist:dbdcc6f9a986fa401787
Created March 13, 2015 16:20
Selenium API submitting a form
// Find the submit button and click it
webDriver.FindElement(By.Id("CreateButton")).Click();
// Find the form by its tag and submit it
webDriver.FindElement(By.TagName("form")).Submit();
// Call submit on the element, it automatically submits the form
var descriptionElement = webDriver.FindElement(By.Id("Name"));
descriptionElement.Clear();
descriptionElement.SendKeys(description);
@johnmmoss
johnmmoss / gist:734ab75cec857895b0b5
Created March 13, 2015 16:26
Selenium API Selecting a Button in a html column
// Click the a link of the tr where a td has a value of "Leeds", and the a link has the text "Edit"
var name = "Leeds";
var xpath = string.Format("//tr[td[normalize-space(text())='{0}']]/td/a[normalize-space(text()) = \"Edit\"]", name);
var editLink = webDriver.FindElement(By.XPath(xpath));
editLink.Click();
@johnmmoss
johnmmoss / gist:8ae6afe7260a42cf12d6
Created March 13, 2015 16:29
Selenium API Test if a component is visible
// Check if the provided value exists in an html table cell
public bool RowExists(string value)
{
try
{
var rowXPath = string.Format("//tr[td[normalize-space(text())='{0}']]", value);
webDriver.FindElement(By.XPath(rowXPath));
return true;
}
@johnmmoss
johnmmoss / gist:92d79a681ba8f38e8d2b
Created March 31, 2015 06:56
Entity Framework Mock DbContext
ITimesheetsContext _context = MockRepository.GenerateStub<ITimesheetsContext>();
_context.Departments = new TestDbSet<Department>();
_context.Timesheets = new TestDbSet<Timesheet>();
_context.Employees = new TestDbSet<Employee>();
_context.Stub(c => c.SaveChangesAsync()).Return(Task.FromResult(0));