无法使用 Selenium WebDriver 自动登录到 Azure Active Directory

Stu*_*ord 5 c# selenium selenium-webdriver azure-active-directory

我正在尝试使用 Azure AD 测试登录到我们的 Web 应用程序,但“登录”按钮上的单击操作似乎未注册。这是代码:

\n\n
namespace 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}\n
Run 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.!#$%&amp;\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>\n
Run Code Online (Sandbox Code Playgroud)\n\n

在浏览器中一切看起来都正常:用户名和密码字段已填写,按钮看起来像是被单击了(小“工作”图标短暂出现在其上方),但没有任何反应。

\n\n
    \n
  • 我尝试过在单击“登录”按钮后等待更长的时间(最多 30 秒)才能显示主页。
  • \n
\n\n

wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));

\n\n
    \n
  • 我尝试Enter在表单中发送密钥,而不是单击按钮。
  • \n
\n\n

driver.FindElement(By.Id("password")).SendKeys(Keys.Enter);

\n\n
    \n
  • 我尝试执行一些 JavaScript 来调用按钮单击操作。
  • \n
\n\n

IJavaScriptExecutor jse = (IJavaScriptExecutor)driver;\njse.ExecuteScript("document.getElementById(\'next\').click();");

\n\n
    \n
  • 我尝试过使用 Chrome 和 Firefox 驱动程序。
  • \n
  • 我已经尝试过本地开发版本和托管测试环境。
  • \n
\n\n

到目前为止还没有任何效果。在此过程中,浏览器中不会显示任何错误/验证消息。

\n\n

我对这个有点不知所措。

\n\n

谢谢,\n斯图尔特。

\n

Mar*_*abo 6

截至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)