简体   繁体   English

如何等待直到通过C#中的硒自动化测试验证了登录名

[英]how to wait until login is verified with selenium automation testing in C#

I have to do frontend automation testing with selenium in C#. 我必须在C#中使用硒进行前端自动化测试。 I have started learning and started with filling parameters in textbox and clicking login button. 我已经开始学习,并开始在文本框中填充参数并单击“登录”按钮。

But requirement is that, after clicking login button, I need to wait to check for login is success or not and only if success , need to proceed to next test. 但要求是,单击登录按钮后,我需要等待以检查登录是否成功,并且只有在成功的情况下,才需要进行下一个测试。 I am using VisualStudio.TestTools.UnitTesting reference. 我正在使用VisualStudio.TestTools.UnitTesting参考。 This is the starting method and only after successful login I need to proceed further testing methods. 这是启动方法,只有成功登录后,我才需要继续进行其他测试方法。

I am stuck here on how to wait to verify and proceed to next method, following is my code 我在这里停留在如何等待验证并继续执行下一个方法,以下是我的代码

    [TestMethod]
    public void LoginWithParameters()
    {
        IWebDriver driver = new ChromeDriver();
        driver.Navigate().GoToUrl(ConfigurationManager.AppSettings["TestURL"]);
        var userName = driver.FindElement(By.Id("UserName"));
        var passWord = driver.FindElement(By.Id("Password"));
        var logOn = driver.FindElement(By.XPath("//input[@value='Log On']"));
        userName.SendKeys(ConfigurationManager.AppSettings["UserName"]);
        passWord.SendKeys(ConfigurationManager.AppSettings["Password"]);
        logOn.Click();


        var str = driver.PageSource;

        Assert.IsTrue(str.Contains("Login was unsuccessful"));


    } 

Please help with this or any proper tutorials . 请帮助这个或任何适当的教程。 Thanks. 谢谢。

Most probably the success message is displayed in DIV or something similar. 成功消息很可能以DIV或类似方式显示。 So you can use WebDriverWait. 因此,您可以使用WebDriverWait。

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
wait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(
By.Id("successDivId")));
// code after successful login...

You can also check here, a summarised sheet- https://automatetheplanet.com/selenium-webdriver-csharp-cheat-sheet/ 您也可以在此处查看摘要表-https : //automatetheplanet.com/selenium-webdriver-csharp-cheat-sheet/

You don't need conditional code in your tests, just create two different tests for the successful login and for the unsuccessful. 您不需要在测试中使用条件代码,只需为成功登录和失败创建两个不同的测试。

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

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