in Microsoft.Dynamics365.UIAutomation.Api/Pages/LoginDialog.cs [88:159]
private LoginResult Login(IWebDriver driver, Uri uri, SecureString username, SecureString password, Action<LoginRedirectEventArgs> redirectAction)
{
var redirect = false;
bool online = !(this.OnlineDomains != null && !this.OnlineDomains.Any(d => uri.Host.EndsWith(d)));
driver.Navigate().GoToUrl(uri);
if (online)
{
if (driver.IsVisible(By.Id("use_another_account_link")))
driver.ClickWhenAvailable(By.Id("use_another_account_link"));
driver.WaitUntilAvailable(By.XPath(Elements.Xpath[Reference.Login.UserId]),
$"The Office 365 sign in page did not return the expected result and the user '{username}' could not be signed in.");
driver.FindElement(By.XPath(Elements.Xpath[Reference.Login.UserId])).SendKeys(username.ToUnsecureString());
driver.FindElement(By.XPath(Elements.Xpath[Reference.Login.UserId])).SendKeys(Keys.Tab);
driver.FindElement(By.XPath(Elements.Xpath[Reference.Login.UserId])).SendKeys(Keys.Enter);
Thread.Sleep(1000);
if (driver.IsVisible(By.Id("aadTile")))
{
driver.FindElement(By.Id("aadTile")).Click(true);
}
Thread.Sleep(1000);
//If expecting redirect then wait for redirect to trigger
if (redirectAction != null)
{
//Wait for redirect to occur.
Thread.Sleep(3000);
redirectAction?.Invoke(new LoginRedirectEventArgs(username, password, driver));
redirect = true;
MarkOperation(driver);
}
else
{
driver.FindElement(By.XPath(Elements.Xpath[Reference.Login.LoginPassword])).SendKeys(password.ToUnsecureString());
driver.FindElement(By.XPath(Elements.Xpath[Reference.Login.LoginPassword])).SendKeys(Keys.Tab);
driver.FindElement(By.XPath(Elements.Xpath[Reference.Login.LoginPassword])).Submit();
Thread.Sleep(2000);
if (driver.IsVisible(By.XPath(Elements.Xpath[Reference.Login.StaySignedIn])))
{
driver.ClickWhenAvailable(By.XPath(Elements.Xpath[Reference.Login.StaySignedIn]));
//Click didn't work so use submit
if (driver.HasElement(By.XPath(Elements.Xpath[Reference.Login.StaySignedIn])))
driver.FindElement(By.XPath(Elements.Xpath[Reference.Login.StaySignedIn])).Submit();
}
driver.WaitUntilVisible(By.XPath(Elements.Xpath[Reference.Login.CrmMainPage])
, new TimeSpan(0, 0, 60),
e =>
{
driver.WaitForPageToLoad();
MarkOperation(driver);
driver.SwitchTo().Frame(0);
driver.WaitForPageToLoad();
},
"Login page failed."
);
}
}
return redirect ? LoginResult.Redirect : LoginResult.Success;
}