简体   繁体   中英

Verifying error message outputs with Selenium and C#

I should preface this by saying that I'm new to Selenium/C# and automated testing in general. I'm trying to write a test case to verify that if a user enters incorrect credentials (username/password) then the system will throw an error stating "The UserName or Password is incorrect."

Would anyone be able to suggest how to verify the highlighted text in the image appears on the webpage?

element inspection img

This is what I have so far:

[TestMethod]
    public void CoordinatorLogin_Failure()
    {
        var driver = new ChromeDriver();
        driver.Navigate().GoToUrl(url);
        driver.Manage().Window.Maximize();
        driver.FindElement(By.Name("user.userName")).SendKeys("someguy@gmail.com");
        driver.FindElement(By.Name("user.password")).SendKeys("WrongPassword");
        driver.FindElement(By.Name("user.password")).SendKeys(Keys.Enter);

        string actual_Result = driver.FindElements(By.CssSelector("#wrapper span.ng-binding ng-scope xh-highlight")).ToString();

        string expected_Result = "The UserName or Password is incorrect.";

        Assert.AreEqual(actual_Result, expected_Result);            
    }

You might want to check the field below since you are clicking "enter" on a password field.

driver.FindElement(By.Name("user.password")).SendKeys(Keys.Enter); 

When you are looking for the field on the browser you want to get the "Text" value. It will look something like:

 string actual_Result =   driver.FindElements(By.CssSelector("#wrapper span.ng-binding ng-scope xh-highlight")).Text;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM