EnVisageOnline/Main/TestSystem/Prevu/Selenium/UsersPage.cs

132 lines
5.6 KiB
C#
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
namespace Prevu
{
public class UsersPage : BasePage
{
By btnPrimary = By.ClassName("btn-primary");
By txtUserName = By.Id("UserName");
By txtFirstName = By.Id("FirstName");
By txtLastName = By.Id("LastName");
By txtEmail = By.Id("Email");
By txtPhone = By.Id("Phone");
By btnSave = By.Id("btnsave");
By lblWarning = By.ClassName("panel-body");
By btnDelete = By.Id("btnDelete");
WebDriver _driver;
public UsersPage(WebDriver driver)
{
_driver = driver;
}
public bool OpenUsers()
{
_driver.OpenPage("User");
_driver.WaitElement(btnPrimary);
return _driver.GetElement(btnPrimary, "Add User") != null;
}
private bool SetRoles(string[] roles)
{
bool someChecked = false;
if (roles.Length > 0)
{
string pageSource = _driver.Driver.PageSource;
IWebElement gropRoles = _driver.GetGroup("Roles");
IList<IWebElement> elements = gropRoles.FindElement(By.XPath("div")).FindElement(By.XPath("div")).FindElements(By.XPath("input"));
foreach (string role in roles)
{
foreach (IWebElement element in elements)
{
string name = element.GetAttribute("name");
if ((!string.IsNullOrEmpty(name)) && (name == "roleitems"))
{
//value=\"0abed6d0-5fd7-4e06-b6e7-fc51324f8572\" name=\"roleitems\" /> 1     <input
string guidValue = element.GetAttribute("value");
string elementRole = _driver.Subs(pageSource, string.Format("value=\"{0}\"", guidValue), "input");
elementRole = _driver.Subs(elementRole, ">", "<").Trim();
if ((!string.IsNullOrEmpty(elementRole)) && (elementRole == role))
{
element.Click();
someChecked = true;
}
}
}
}
} else
{
someChecked = true;
}
return someChecked;
}
private bool CheckRoles(string[] roles)
{
//TODO
return true;
}
public bool CreateUser(string userName, string firstName, string lastName, string email, string phone, string[] roles, out string id)
{
_driver.ClickElement(btnPrimary, "Add User");
_driver.SendKeys(txtUserName, userName);
_driver.SendKeys(txtFirstName, firstName);
_driver.SendKeys(txtLastName, lastName);
_driver.SendKeys(txtEmail, email);
_driver.SendKeys(txtPhone, phone);
SetRoles(roles);
_driver.ClickElement(btnSave);
id = "";
return _driver.FindRecord("User", userName, out id);
}
public bool CheckUser(string id, string userName, string firstName, string lastName, string email, string phone, string[] roles)
{
_driver.OpenPage(string.Format("User/Edit/{0}", id));
return (_driver.GetElement(txtUserName).GetAttribute("value") == userName) &&
(_driver.GetElement(txtFirstName).GetAttribute("value") == firstName) &&
(_driver.GetElement(txtLastName).GetAttribute("value") == lastName) &&
(_driver.GetElement(txtEmail).GetAttribute("value") == email) &&
(_driver.GetElement(txtPhone).GetAttribute("value") == phone) &&
CheckRoles(roles);
}
public bool DeleteUser(string userId, string userName)
{
_driver.OpenPage(string.Format("User/Delete/{0}", userId));
bool result = _driver.GetElement(lblWarning).Text.IndexOf(string.Format("You are about to delete the {0}. Are you sure you want to delete it?", userName)) > -1;
_driver.ClickElement(btnDelete);
string id = "";
_driver.FindRecord("User", userName, out id);
return result && (id == "");
}
public bool EditUser(string id, string userName, string firstName, string lastName, string email, string phone, string[] roles)
{
_driver.OpenPage(string.Format("User/Edit/{0}", id));
_driver.SendKeys(txtUserName, userName);
_driver.SendKeys(txtFirstName, firstName);
_driver.SendKeys(txtLastName, lastName);
_driver.SendKeys(txtEmail, email);
_driver.SendKeys(txtPhone, phone);
SetRoles(roles);
_driver.ClickElement(btnSave);
return CheckUser(id, userName, firstName, lastName, email, phone, roles);
}
public bool FindUser(string userName, out string id)
{
return _driver.FindRecord("User", userName, out id);
}
public bool ResetUserPassword(string email)
{
string id = "";
Assert.IsTrue(FindUser(email, out id));
_driver.ClickElement(btnPrimary, "Reset password");
//return _driver.Driver.PageSource.IndexOf("Email with reset password link has been sent to user's email.") > -1;
return !string.IsNullOrEmpty(id);
}
}
}