Stu*_*ord 5 c# selenium selenium-webdriver azure-active-directory
我正在尝试使用 Azure AD 测试登录到我们的 Web 应用程序,但“登录”按钮上的单击操作似乎未注册。这是代码:
\n\nnamespace WebApp.Tests\n{\n using System;\n using System.IO;\n using System.Threading;\n using FluentAssertions;\n using Microsoft.VisualStudio.TestTools.UnitTesting;\n using OpenQA.Selenium;\n using OpenQA.Selenium.IE;\n using OpenQA.Selenium.Remote;\n using OpenQA.Selenium.Support.UI;\n\n [TestClass]\n public class LoginTests\n {\n [TestMethod]\n public void CanLogin()\n {\n string internetExplorerDriverServerDirectory = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory);\n IWebDriver driver = new InternetExplorerDriver(internetExplorerDriverServerDirectory)\n {\n Url = "https://localhost:44399"\n };\n\n try\n {\n driver.Manage().Cookies.DeleteAllCookies();\n\n WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));\n wait.Until(d => d.FindElement(By.Id("logonIdentifier")));\n\n driver.FindElement(By.Id("logonIdentifier")).SendKeys("username");\n driver.FindElement(By.Id("password")).SendKeys("password"); \n driver.FindElement(By.Id("next")).Click();\n\n wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));\n wait.Until(d => d.FindElement(By.ClassName("logo_title")));\n\n driver.FindElement(By.ClassName("logo_title")).Text.Should().Contain("HELLO!");\n }\n finally\n {\n driver.Close();\n driver.Quit();\n driver.Dispose();\n driver = null;\n }\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n这是 HTML 的相关部分:
\n\n<div class="entry">\n <div class="entry-item">\n <label for="logonIdentifier">Email Address</label>\n <div class="error itemLevel" aria-hidden="true" style="display: none;">\n <p role="alert"></p>\n </div>\n <input type="email" id="logonIdentifier" name="Username or email address" pattern="^[a-zA-Z0-9.!#$%&\xe2\x80\x99\'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$" placeholder="Email Address" value="" tabindex="1" autocomplete="off">\n </div>\n <div class="entry-item">\n <div class="password-label">\n <label for="password">Password</label>\n <a id="forgotPassword" tabindex="2" href="/redacted">Forgot your password?</a>\n </div>\n <div class="error itemLevel" aria-hidden="true" style="display: none;">\n <p role="alert"></p>\n </div>\n <input type="password" id="password" name="Password" placeholder="Password" tabindex="1" autocomplete="off">\n </div>\n <div class="working"></div>\n <div class="buttons">\n <button id="next" tabindex="1">Sign in</button>\n </div>\n</div>\nRun Code Online (Sandbox Code Playgroud)\n\n在浏览器中一切看起来都正常:用户名和密码字段已填写,按钮看起来像是被单击了(小“工作”图标短暂出现在其上方),但没有任何反应。
\n\nwait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
Enter在表单中发送密钥,而不是单击按钮。driver.FindElement(By.Id("password")).SendKeys(Keys.Enter);
IJavaScriptExecutor jse = (IJavaScriptExecutor)driver;\njse.ExecuteScript("document.getElementById(\'next\').click();");
到目前为止还没有任何效果。在此过程中,浏览器中不会显示任何错误/验证消息。
\n\n我对这个有点不知所措。
\n\n谢谢,\n斯图尔特。
\n截至2019-01-20,这对我有用:
[ClassInitialize]
public static void InitializeClass(TestContext testContext)
{
var options = new ChromeOptions();
options.AddArguments("--incognito");
driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), options);
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
}
[TestMethod]
public void Login()
{
driver.Navigate().GoToUrl("https://localhost:44399/");
driver.FindElement(By.Id("i0116")).Clear();
driver.FindElement(By.Id("i0116")).SendKeys("test@test.onmicrosoft.com");
driver.FindElement(By.Id("i0116")).SendKeys(Keys.Enter);
driver.FindElement(By.Id("i0118")).Clear();
driver.FindElement(By.Id("i0118")).SendKeys("password");
Thread.Sleep(500);
driver.FindElement(By.Id("i0118")).SendKeys(Keys.Enter);
driver.FindElement(By.Id("idSIButton9")).Click();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8355 次 |
| 最近记录: |