Created
November 30, 2012 22:35
-
-
Save margusmartsepp/4179201 to your computer and use it in GitHub Desktop.
Automated testing in c# using selenium
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; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using NUnit.Framework; | |
using OpenQA.Selenium; | |
using OpenQA.Selenium.Firefox; | |
using OpenQA.Selenium.Support.UI; | |
using System.Text.RegularExpressions; | |
using System.Globalization; | |
namespace Testimine | |
{ | |
[TestFixture] | |
public class Program | |
{ | |
IWebDriver browser; | |
WebDriverWait w; | |
[SetUp] | |
public void Init() | |
{ | |
browser = new FirefoxDriver(); | |
w = new WebDriverWait(browser, new TimeSpan(0, 0, 5)); | |
} | |
[TearDown] | |
public void Dispose() | |
{ | |
browser.Quit(); | |
} | |
public struct TableDateInfo | |
{ | |
public DateTime deadline; | |
public DateTime inserted; | |
public TableDateInfo(string txt) | |
{ | |
CultureInfo provider = CultureInfo.InvariantCulture; | |
var format = "dd.MM.yyyy"; | |
string[] s = Regex.Split(txt, "\r\n"); | |
deadline = DateTime.ParseExact(s[0].Split(' ')[1], format, provider); | |
inserted = DateTime.ParseExact(s[1].Split(' ')[1], format, provider); | |
} | |
public override string ToString() | |
{ | |
var sb = new StringBuilder(); | |
sb.Append("deadline: ").Append(deadline.ToShortDateString()).Append(" | "); | |
sb.Append("inserted: ").Append(inserted.ToShortDateString()); | |
return sb.ToString(); | |
} | |
} | |
/// <summary> | |
/// Action > search for jobs in Tallinn | |
/// <ol> | |
/// <li>When logged in click on the link Tööpakkumised.</li> | |
/// <li>Find the container and click to see all data.</li> | |
/// <li>Click on the tallinn-harjumaa filter</li> | |
/// <li>Click submit</li> | |
/// </ol> | |
/// </summary> | |
/// <param name="browser">Web browser used for testing *(ex. Firefox)</param> | |
public static void searchTallinnJobs(IWebDriver browser) | |
{ | |
var xpath1 = "//input[@name='filters[]' and @value='tallinn-harjumaa']"; | |
var xpath2 = "//input[@type='submit' and @value='Otsi']"; | |
browser.FindElement(By.LinkText("Tööpakkumised")).Click(); | |
var container = browser.FindElement(By.Id("filters_Asukoht")); | |
container.FindElement(By.LinkText("Rohkem")).Click(); | |
container.FindElement(By.XPath(xpath1)).Click(); | |
browser.FindElement(By.XPath(xpath2)).Click(); | |
} | |
/// <summary> | |
/// Action > get jobs list | |
/// <ol> | |
/// <li>When logged in and on Find the entery conteiner table.</li> | |
/// <li>Loop over it's fields and find the correct cells element</li> | |
/// <li>Create a new instance of TableDateInfo and add it to result list</li> | |
/// </ol> | |
/// </summary> | |
/// <param name="browser">Web browser used for testing *(ex. Firefox)</param> | |
/// <returns>list of table data info</returns> | |
public static List<TableDateInfo> getJobInfo(IWebDriver browser) | |
{ | |
var result = new List<TableDateInfo>(); | |
var container = browser.FindElement(By.Id("table_jobs")); | |
foreach (var row in container.FindElements(By.TagName("Tr"))) | |
result.Add(new TableDateInfo(row.FindElement(By.XPath("//td[3]//p[2]")).Text)); | |
return result; | |
} | |
/// <summary> | |
/// Action > log in | |
/// <ol> | |
/// <li>Navigate to login page http://www.cv.ee/login </li> | |
/// <li>Enter username: bing</li> | |
/// <li>Enter password: bing</li> | |
/// <li>Click submit</li> | |
/// </ol> | |
/// </summary> | |
/// <param name="browser">Web browser used for testing *(ex. Firefox)</param> | |
private static void logIn(IWebDriver browser) | |
{ | |
browser.Navigate().GoToUrl("http://www.cv.ee/login"); | |
browser.FindElement(By.Id("kasutajanimi")).SendKeys("bing"); | |
browser.FindElement(By.Id("parool")).SendKeys("bing"); | |
browser.FindElement(By.ClassName("btn_green_submit")).Click(); | |
} | |
/// <summary> | |
/// Action > delete cv | |
/// <ol> | |
/// <li>Find the cv container</li> | |
/// <li>Click on the "Kustuta" link inside the container</li> | |
/// <li>Accept the poppup confirmation</li> | |
/// </ol> | |
/// </summary> | |
/// <param name="browser">Web browser used for testing *(ex. Firefox)</param> | |
private static bool deleteEstonianCV(IWebDriver browser) | |
{ | |
var container = getEstonianCVContainer(browser); | |
if (container != null) | |
{ | |
container.FindElement(By.LinkText("Kustuta")).Click(); | |
browser.SwitchTo().Alert().Accept(); | |
return true; | |
} | |
return false; | |
} | |
/// <summary> | |
/// Action > create cv | |
/// <ol> | |
/// <li>When logged in click to "Minu CV-d" link</li> | |
/// <li>Loop over every table row and check if it's text matches "Keel eesti"</li> | |
/// <li>Click on the "Kustuta" link inside the container</li> | |
/// <li>Accept the poppup confirmation</li> | |
/// </ol> | |
/// </summary> | |
/// <param name="browser">Web browser used for testing *(ex. Firefox)</param> | |
private static void createEstonianCV(IWebDriver browser) | |
{ | |
browser.FindElement(By.LinkText("Minu CV-d")).Click(); | |
browser.FindElement(By.LinkText("Lisa uus CV")).Click(); | |
browser.FindElement(By.Name("cv_name")).SendKeys("bing"); | |
var education = browser.FindElement(By.Name("keel_id")); | |
var selection = new SelectElement(education); | |
selection.SelectByText("eesti"); | |
browser.FindElement(By.ClassName("blue_submit")).Click(); | |
} | |
/// <summary> | |
/// Action > return CV container, if it exists | |
/// <ol> | |
/// <li>When logged in click to "Minu CV-d" link</li> | |
/// <li>Loop over every table row and check if it's text matches "Keel eesti"</li> | |
/// <li>If it does, then return container</li> | |
/// </ol> | |
/// </summary> | |
/// <param name="browser">Web browser used for testing *(ex. Firefox)</param> | |
/// <returns>CV container or null</returns> | |
private static IWebElement getEstonianCVContainer(IWebDriver browser) | |
{ | |
browser.FindElement(By.LinkText("Minu CV-d")).Click(); | |
foreach (var container in browser.FindElements(By.TagName("Table"))) | |
foreach (var row in container.FindElements(By.TagName("Tr"))) | |
if (row.Text.CompareTo("Keel eesti") == 0) | |
return container; | |
return null; | |
} | |
/// <summary> | |
/// Test > create Estonian cv | |
///log in | |
///delete Estonian cv if it exists | |
///verify that Estonian cv does not exist | |
///create Estonian cv | |
///verify that Estonian cv exists | |
/// </summary> | |
[Test] | |
public void testCreateEstonianCV() | |
{ | |
logIn(browser); | |
deleteEstonianCV(browser); | |
Assert.Null(getEstonianCVContainer(browser)); | |
createEstonianCV(browser); | |
Assert.NotNull(getEstonianCVContainer(browser)); | |
} | |
/// <summary> | |
/// Test > create cv | |
/// </summary> | |
[Test] | |
public void testTallinnJobsSortedAscendingly() | |
{ | |
logIn(browser); | |
searchTallinnJobs(browser); | |
Assert.NotNull(browser.FindElement(By.TagName("body")).Text. | |
Contains("(rakendatud filter: \"Tallinn / Harjumaa\")")); | |
var resultList = getJobInfo(browser); | |
var expectedList = resultList.OrderBy(x => x.deadline); | |
Assert.IsTrue(expectedList.SequenceEqual(resultList)); | |
} | |
static void Main(string[] args) | |
{ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment