2

I have written some code to verify that a user can login to a hotel booking platform & I also added a verification step. I get an error on the Assert.IsTrue() Method. Every other thing is fine in the code.

I did some research for a possible solution before stopping by.

Here is the solution I found on StackOverflow: https://stackoverflow.com/questions/8695595/assert-method-error

I have added Microsoft.VisualStudio.QualityTools.UnitTestFramework to my project references and also added

using Microsoft.VisualStudio.TestTools.UnitTesting; & using NUnit.Framework; to my Using section of the code.

I get the following errors:

Severity Code Description Project File Line Suppression State Error CS0234 The type or namespace name 'VisualStudio' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) PremierInn_Valid_User_Login c:\users\XXXX\onedrive\documents\visual studio 2015\Projects\PremierInn_Valid_User_Login\PremierInn_Valid_User_Login\Program.cs 5 Active

Severity Code Description Project File Line Suppression State Error CS0246 The type or namespace name 'NUnit' could not be found (are you missing a using directive or an assembly reference?) PremierInn_Valid_User_Login c:\users\XXXX\onedrive\documents\visual studio 2015\Projects\PremierInn_Valid_User_Login\PremierInn_Valid_User_Login\Program.cs 6 Active

I get the same error after removing the bindings -

Microsoft.VisualStudio.TestTools.UnitTesting; & using NUnit.Framework.

And, finally here is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using System.Threading.Tasks;

namespace PremierInn_Valid_User_Login {
 class Program {
  static void Main(string[] args) {

   //Instantiate Firefox Driver
   var driver = new FirefoxDriver();
   driver.Navigate().GoToUrl("https://secure.premierinn.com/en/mypremierinn/home.action");

   //Enter User Name - My Email Address
   var user = driver.FindElement(By.Id("loginForm.username"));
   user.SendKeys("abcdefg@gmail.com");

   //Enter Password - Account Pasword
   var pass = driver.FindElement(By.Id("loginForm.password"));
   pass.SendKeys("Testing123");

   //Click on Login button
   driver.FindElement(By.Id("loginForm.button")).Click();

   var loggedInHeader = driver.FindElement(By.Id("body-inner"));
   Assert.IsTrue(loggedInHeader.Displayed, "The user was able to successfully login.");

  }
 }
}

Thank you for your help.

  • 1
    formatted to make it readable. – Michael Durrant Jan 28 '17 at 22:53
  • are you sure abcdefg@gmail.com is a valid username? – Corey Goldberg Jan 28 '17 at 23:05
  • Thank you Corey for your response. I have tried with a different email address & I can confirm that that is not where the error is coming from. Thank very much once again. – OA345 Jan 30 '17 at 3:07
0

First decide on what testing framework to use either MSTest or NUnit. You cannot or should not use both at the same time.

Both test frameworks expect you use a test-class and not Main() as you are doing. If you follow the getting started guides you should end up with a full test-class in which you can use the Assert function.

Typical it would look something like this

using System;  
using Microsoft.VisualStudio.TestTools.UnitTesting;  

namespace Tests  
{  
    [TestClass]  
    public class YourTestClass  
    {  
        [TestMethod]  
        public void TestMethod1()  
        { 
          // Arrange
          // Act
          // Assert
          Assert.IsTrue(true);
        }  
    }  
}  

If you want to use Assert without a test-class suggested is to replace it with an exception. Exceptions is also what the Assert throws when it fails.

Other reads:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.