Created
May 18, 2017 12:20
-
-
Save jansabbe/ed77c7d686ef0918dcc3de69d0b07776 to your computer and use it in GitHub Desktop.
Basic webdriver example with page objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Linq; | |
using HtmlElements; | |
using HtmlElements.Elements; | |
using HtmlElements.Extensions; | |
using NUnit.Framework; | |
using OpenQA.Selenium; | |
using OpenQA.Selenium.Firefox; | |
using OpenQA.Selenium.Support.PageObjects; | |
namespace UiTest | |
{ | |
[TestFixture] | |
public class GoogleTests | |
{ | |
// NUGet: | |
// HtmlElements | |
// NUnit | |
// NUnit3TestAdapter | |
// Selenium.Firefox.WebDriver | |
// Selenium.Support | |
// Selenium.WebDriver | |
private IWebDriver _firefox; | |
private GoogleSearchPage _googleSearchPage; | |
[SetUp] | |
public void StartBrowser() | |
{ | |
_firefox = new FirefoxDriver(); | |
var pageObjectFactory = new PageObjectFactory(); | |
_firefox.Navigate().GoToUrl("https://www.google.be"); | |
_firefox.Manage().Window.Maximize(); | |
_googleSearchPage = pageObjectFactory.Create<GoogleSearchPage>(_firefox); | |
} | |
[TearDown] | |
public void StopBrowser() | |
{ | |
_firefox.Dispose(); | |
} | |
[Test] | |
public void GoogleCanFindSdWorx() | |
{ | |
var resultPage = _googleSearchPage.Search("SD Worx"); | |
resultPage.WaitTillResultsVisible(); | |
Assert.Contains("SD Worx: Payroll - Tax & Legal - HR", resultPage.GetResultTitles()); | |
} | |
} | |
public class GoogleSearchPage : HtmlPage | |
{ | |
[FindsBy(How = How.Id, Using = "lst-ib")] | |
private HtmlInput searchBox; | |
public GoogleSearchPage(ISearchContext webDriverOrWrapper) : base(webDriverOrWrapper) | |
{ | |
} | |
public GoogleResultPage Search(string query) | |
{ | |
searchBox.EnterText(query); | |
return PageObjectFactory.Create<GoogleResultPage>(WrappedDriver); | |
} | |
} | |
public class GoogleResultPage : HtmlPage | |
{ | |
[FindsBy(How = How.CssSelector, Using = "h3.r")] | |
private IList<HtmlElement> results; | |
[FindsBy(How = How.Id, Using = "rcnt")] | |
private HtmlElement resultPane; | |
public GoogleResultPage(ISearchContext webDriverOrWrapper) : base(webDriverOrWrapper) | |
{ | |
} | |
public List<string> GetResultTitles() | |
{ | |
return results.Select(t => t.Text.Trim()).ToList(); | |
} | |
public void WaitTillResultsVisible() | |
{ | |
resultPane.WaitForVisible(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment