我正在尝试使用Azure AD测试登录到我们的Web应用程序,但是“登录”按钮上的单击动作似乎未注册。这是代码:
namespace WebApp.Tests
{
using System;
using System.IO;
using System.Threading;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.UI;
[TestClass]
public class LoginTests
{
[TestMethod]
public void CanLogin()
{
string internetExplorerDriverServerDirectory = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory);
IWebDriver driver = new InternetExplorerDriver(internetExplorerDriverServerDirectory)
{
Url = "https://localhost:44399"
};
try
{
driver.Manage().Cookies.DeleteAllCookies();
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));
wait.Until(d => d.FindElement(By.Id("logonIdentifier")));
driver.FindElement(By.Id("logonIdentifier")).SendKeys("username");
driver.FindElement(By.Id("password")).SendKeys("password");
driver.FindElement(By.Id("next")).Click();
wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
wait.Until(d => d.FindElement(By.ClassName("logo_title")));
driver.FindElement(By.ClassName("logo_title")).Text.Should().Contain("HELLO!");
}
finally
{
driver.Close();
driver.Quit();
driver.Dispose();
driver = null;
}
}
}
}
以下是HTML的相关内容:
<div class="entry">
<div class="entry-item">
<label for="logonIdentifier">Email Address</label>
<div class="error itemLevel" aria-hidden="true" style="display: none;">
<p role="alert"></p>
</div>
<input type="email" id="logonIdentifier" name="Username or email address" pattern="^[a-zA-Z0-9.!#$%&’'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$" placeholder="Email Address" value="" tabindex="1" autocomplete="off">
</div>
<div class="entry-item">
<div class="password-label">
<label for="password">Password</label>
<a id="forgotPassword" tabindex="2" href="/redacted">Forgot your password?</a>
</div>
<div class="error itemLevel" aria-hidden="true" style="display: none;">
<p role="alert"></p>
</div>
<input type="password" id="password" name="Password" placeholder="Password" tabindex="1" autocomplete="off">
</div>
<div class="working"></div>
<div class="buttons">
<button id="next" tabindex="1">Sign in</button>
</div>
</div>
在浏览器中看起来一切正常:用户名和密码字段已填写,按钮看起来像是被单击了(上方的小“工作”图标短暂出现),但没有任何反应。
wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
Enter
键,而不是单击
按钮。 driver.FindElement(By.Id("password")).SendKeys(Keys.Enter);
IJavaScriptExecutor jse = (IJavaScriptExecutor)driver;
jse.ExecuteScript("document.getElementById('next').click();");
到目前为止没有任何工作。在此过程中,浏览器中没有显示错误/验证消息。
我对此感到有些茫然。
谢谢, 斯图尔特。
答案 0 :(得分:1)
从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();
}
答案 1 :(得分:-1)
我已经通过使用CSS选择器作为元素定位器解决了同样的问题。
SeleniumLibrary.Input Text css:#i0118 your_password
在每次迭代中都可以正常工作。