c#中的Selenium不读取javascript警报

本文关键字:javascript 警报 读取 中的 Selenium | 更新日期: 2023-09-27 18:02:42

我使用selenium ide来测试我的。net网站。我正在使用硒测试登录网站。我记录了步骤,当我在visual studio中测试代码时,测试用例失败了。我的测试用例是:

selenium.Open("//login.aspx");
            selenium.Type("fldUsername", "abc");
            selenium.Type("fldPassword", "abc");
            selenium.Click("btnLogin");
            bool log1 = selenium.IsPromptPresent();
            Assert.IsNull(log1);
            selenium.WaitForPageToLoad("80000");

这里的用户名和密码不正确,并给出警告。但使用硒时,它就不会得到警报。

c#中的Selenium不读取javascript警报

您使用了错误的方法。selenium.IsPromptPresent()查找JavaScript window.prompt()调用。对于window.alert(),需要使用selenium.IsAlertPresent()

为额外加分,将这两行替换为:

Assert.True(selenium.IsAlertPresent());
Assert.AreEqual("your expected alert text", selenium.GetAlert());

这样你就可以确保你得到"错误的用户名/密码"警报,而不是"没有指定密码"警报或其他东西