简体   繁体   English

如何编写 selenium c# x 单元测试以执行 crud 操作而不更改实际数据库?

[英]How to write a selenium c# x unit test for performing crud operations without making changes to the actual database?

My program has a page that displays a table as well as buttons to add/edit/delete users.我的程序有一个显示表格的页面以及添加/编辑/删除用户的按钮。 I wrote selenium tests that simulate clicking those buttons and entering in user info and submitting the form.我编写了 selenium 测试来模拟单击这些按钮并输入用户信息并提交表单。 The problem with this is I am making changes to my database as well as I have to keep changing the values of the keys I am sending in each time I run my tests.问题是我正在对我的数据库进行更改,并且我必须在每次运行测试时不断更改我发送的键的值。 I don't know how to simulate/test these cases without doing so.如果不这样做,我不知道如何模拟/测试这些案例。 This is my test for adding a user:这是我添加用户的测试:

[Fact]
        public void AddUserSuccessfully()
        {
            var email = "anotheradd@gmail.com";

            using IWebDriver driver = new ChromeDriver();
            driver.Manage().Window.Maximize();
            driver.Navigate().GoToUrl(BaseUrl.testUserUrl);

            var login = new Login(driver);
            login.EnterEmail();
            login.EnterPassword();
            login.HitSubmit();

            var users = new Users(driver);
            users.ClickOnAddUser();
            users.EnterFirstName("Anita");
            users.EnterLastName("Work");
            users.EnterEmail(email);
            users.EnterPassword("P*$sW0rd");
            users.ConfirmPassword("P*$sW0rd");
            users.EnterRole("Developer");
            Pause.SimulatePause();
            var modal = new Modal(driver);
            modal.SubmitForm();

            driver.Navigate().Refresh();

            WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
            var emailFound = wait.Until((d) => d.PageSource.Contains(email));
            Assert.True(emailFound);

            driver.Quit();
        }

If anyone has any suggestions, I'd appreciate it.如果有人有任何建议,我将不胜感激。 Thank you,谢谢,

Hope you are not running this on PROD, but as others have suggested and if you have read/write access to you test database write you logic to delete user from table in your clean up code.希望您没有在 PROD 上运行它,但正如其他人所建议的那样,如果您对测试数据库具有读/写访问权限,请在清理代码中编写逻辑以从表中删除用户。

  • Moq library is useful for unit testing but wouldn't be good with Selenium, as it seems that you are trying to validate a Post-back to ensure that the user you had created does appear on the page. Moq 库对于单元测试很有用,但不适用于 Selenium,因为您似乎正在尝试验证回发以确保您创建的用户确实出现在页面上。

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

相关问题 如何正确地对存储库中的CRUD操作进行单元测试? - How To Properly Unit-Test CRUD Operations on a Repository? 如何在C#中单元测试数据库方法? - How to unit test database methods in C#? 如何在不使用Operation上下文的情况下用c#编写单元测试用例? - How to write a Unit Test case in c# without using the Operation context? .NET C#CRUD操作有效,但永远不会在数据库中更新 - .NET C# CRUD operations work but never gets updated in the Database 如何使用 C# 中的数据库优先方法生成具有所有 CRUD 操作的存储库和模型/实体类 - how to generate the Repository and model/entity classes with all CRUD operations using database-first apporach in C# 如何在单元测试中伪造数据库操作? - How to fake database operations in Unit test? 如何将外部javascript文件嵌入到ac#硒测试单元中? - how to embed an external javascript file to a c# selenium test unit? 如何为C#Azure功能编写单元测试? - How do I write Unit Test for C# Azure Function? 如何在C#中编写Memory Stream的单元测试? - How to write unit test for Memory Stream in c#? 如何使用 C# 为 Snowflake 编写 MS 单元测试 - How to write MS unit test for Snowflake using C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM