I have been teaching myself Selenium over the past few weeks and have started writing my own tests, I can get all my happy flow tests to work fine but my first attempt at writing a test to check an error message is not working.
As an overview the test is really simple:
- Enter an invalid postcode into a search box
- click search
- Assert the screen shows an error message below the search box.
I know the logic of my code works as when I run the positive flow (enter postcode, click search, new page opens) the automated test is working fine. Also when I run the test in debug mode and step through the failed Assert the test passes with the error message picked up.
My Test code
[TestClass]
public class invalidSearch
{
[TestInitialize]
public void Init()
{
driver.Initialize();
}
[TestMethod]
public void Invalid_Location_Returns_Error()
{
Homepage.GoTO_HomePage();
SearchPage.enterSearch("CFYUGHGYHYTDFD").Search();
Assert.IsTrue(SearchErrorMessage.IsInValidLocation("Please enter a valid location or postcode", "Validation Fails"));
}
[TestCleanup]
public void Cleanup()
{
driver.Close();
}
My assert class
public class SearchErrorMessage
{
public static bool IsInValidLocation(string InvalidLocation)
{
var ErrorMessage = driver.Instance.FindElement(By.XPath("/html/body/header/div/div[4]/div/div/div[2]/div[1]/form/div[2]/span[2]"));
bool result = ErrorMessage.Text.Contains(InvalidLocation);
return result;
}
My driver
Class
public class driver
{
public static IWebDriver Instance { get; set; }
public static void Initialize()
{
Instance = new ChromeDriver(@"C:\Users\xxxxxx.xxxxxx\Documents\Visual Studio 2015\Drivers\Chrome");
Instance.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
}
public static void Close()
{
Instance.Close();
}