繁体   English   中英

如何在 selenium c# 中声明 toast 错误消息中的文本

[英]How to assert the text inside toast error message in selenium c#

在我的应用程序中,当我尝试输入无效的手机号码并且我想断言该 toast 消息的文本时,会出现一条 toast 消息(弹出)。

下面是我的 HTML 的图像。我无法在此处复制粘贴。

在此处输入图像描述

以下是尝试过的代码,但没有发生此类元素错误。

String actual_error = driver.FindElement(By.XPath("//div[@class='toast-message']")).Text;
        String expected_error = "CellphoneNumber:Please enter a valid cellphone number. :";
        Assert.AreEqual(actual_error, expected_error);
        Console.WriteLine("Phone Number error message validated successfully");

请提出正确的方式来断言敬酒信息。

我认为你可以这样做:

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using Serilog;
using System;
using System.IO;
using System.Reflection;

namespace StackOverFlow.Answer.Selenium.AssertMessageText
{
    class AssertMessagePhone
    {
        public static IWebDriver driver;
        public static WebDriverWait wait;
        public static int timeOut = 30;

        [Test]
        [Category("ClickPartilLink")]
        public void AssertMessagePhoneTest()
        {
            string messageError = "CellphoneNumber:Please enter a valid cellphone number. :";

            Log.Information("Get instance Chrome Browser");
            driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), OptionsChrome());

            Console.WriteLine("Acess your url site");
            driver.Navigate().GoToUrl("http://YourUrlSite/index.aspx");

            // Steps until to fill and submit phone

            Log.Information("Wait notificantion message error");
            WaiElement(By.CssSelector(".toast-message"));

            Log.Information("Check message");
            Assert.True(GetText(By.CssSelector(".toast-message")).ToUpper().Contains(messageError.ToUpper()));

            Log.Information("Click on message to disaper");
            Click(By.CssSelector("div[class='noty_bar noty_type_success'] > div > span"));
        }

        /// <summary>
        /// Wait elements
        /// </summary>
        /// <param name="locator"></param>
        private void WaiElement(By locator)
        {
            wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeOut));
            wait.Until(condition: ExpectedConditions.PresenceOfAllElementsLocatedBy(locator));
        }      

        /// <summary>
        /// click on elements 
        /// </summary>
        /// <param name="locator"></param>
        private void Click(By locator)
        {
            WaiElement(locator);
            driver.FindElement(locator).Click();
        }

        /// <summary>
        /// return text from element
        /// </summary>
        /// <param name="locator"></param>
        /// <returns></returns>
        private string GetText(By locator)
        {
            WaiElement(locator);
            return driver.FindElement(locator).Text;
        }        
    }
}

您的验证似乎很好。 您需要确保:
- 如果页面在另一个页面中打开,您的测试顺序正确
- 确保等待所有元素,即使用方法:

ExpectedConditions.PresenceOfAllElementsLocatedBy(/*element to test*/)

我同意 lukbl 的观点,即我会创建一个 bool 来验证它存在的元素,但我认为问题在于您的文本位于 div 标签中而不是跨度中。

尝试这个:

String expected_error = driver.FindElement(By.XPath("//div[@class='toast-message']/text()[1]")).Text;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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