c# 断言检查按钮是否不再可用 - 测试

c# Assertion to check if button is not available anymore - test

我是断言新手。 如果 cookie 按钮不再在页面上,我想检查断言。 我正在使用 C# 与 selenium 和 NUnit 进行测试。我也在使用页面对象建模。

希望有人能帮助我。

这是我的反对页面。

    using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MakroTest
{
    class LandingPage
    {
        IWebDriver driver = new ChromeDriver();
        public IWebElement CookieButton => driver.FindElement(By.Id("cookie-bar-btn"));
        public IWebElement AlgemeneVoowaarden => driver.FindElement(By.LinkText("Algemene voorwaarden"));
        public IWebElement Contact => driver.FindElement(By.LinkText("Contact"));
        public IWebElement InlogCode => driver.FindElement(By.Id("FormModel_PromotionName"));
        public IWebElement Wachtwoord => driver.FindElement(By.Id("FormModel_Secret"));
        public IWebElement InlogButton => driver.FindElement(By.ClassName("button-secondary"));

        public void OpenWebsite()
        {
            driver.Url = DELETED THIS BECAUSE OF PRIVACY REASONS
            driver.Manage().Window.Maximize(); ;
        }

        public void ClickCookieButton()
        {
            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
            CookieButton.Click();
        }

        //Assert ClickCookieButton - geen button meer zichtbaar  WERKT NOG NIET
            public bool AssertCookieButtonDisplayed()
        {
            bool isDisplayed = CookieButton.Displayed;
            return isDisplayed;
        }


    }
}

这是我的测试页

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using NUnit.Framework;
using OpenQA.Selenium.Support.UI;


namespace MakroTest
{
    class LoginTest
    {
       IWebDriver driver = new ChromeDriver();

      //  [SetUp]



    [Test]
      public void ShouldBeAbleToClickCookies()
        {
            LandingPage home = new LandingPage();  //Initialize the page by calling its reference
            home.OpenWebsite();
            home.ClickCookieButton();
            // assert toevoegen  
            Assert.Null(home.AssertCookieButtonDisplayed());
            home.CloseBrowser();
         }

我知道有问题,但我看不出是什么。 还检查了 google 等。希望有人能帮助我。 感谢您的大力帮助。

如果元素在 dom 中不再存在,您将在尝试执行 CookieButton.Displayed 时收到 NoSuchElementException 错误。您可以使用 try/catch 并忽略如下错误:

public bool AssertCookieButtonDisplayed()
{
    bool isDisplayed = false;
    try {
        isDisplayed = CookieButton.Displayed;
    }
    catch{}

    return isDisplayed;
}
   public static bool existsElement(IWebDriver _driver)
       {


        try
        {

            _driver.FindElement(by);

        }

        catch (NoSuchElementException ex)
        {
            Console.WriteLine("Message : " + ex.Message);
            Console.WriteLine("StackTrace: " + ex.StackTrace);
            return false;
        }
        catch(Exception ex)
        {
            Console.WriteLine("Message : " + ex.Message);
            Console.WriteLine("StackTrace: " + ex.StackTrace);
            return false;
        }


        return true;
    }

您可以尝试用这个来检查元素,并让它 return 作为布尔值。