480 lines
16 KiB
C#
480 lines
16 KiB
C#
using OpenQA.Selenium;
|
|
using OpenQA.Selenium.Chrome;
|
|
using OpenQA.Selenium.Edge;
|
|
using OpenQA.Selenium.Firefox;
|
|
using OpenQA.Selenium.IE;
|
|
using OpenQA.Selenium.Opera;
|
|
using OpenQA.Selenium.PhantomJS;
|
|
using OpenQA.Selenium.Remote;
|
|
using OpenQA.Selenium.Safari;
|
|
using OpenQA.Selenium.Support.UI;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Reflection;
|
|
using System.Threading;
|
|
|
|
namespace Prevu
|
|
{
|
|
public enum Browser
|
|
{
|
|
InternetExplorer,
|
|
Firefox,
|
|
Chrome,
|
|
Opera,
|
|
MSEdge,
|
|
Safari,
|
|
PhantomJS
|
|
}
|
|
public class WebDriver
|
|
{
|
|
private IWebDriver driver;
|
|
public string Domain;
|
|
|
|
public WebDriver(Browser browser, string domain)
|
|
{
|
|
Domain = domain;
|
|
Create(browser);
|
|
}
|
|
|
|
public IWebDriver Driver
|
|
{
|
|
get { return driver; }
|
|
set { driver = value; }
|
|
}
|
|
public void Close()
|
|
{
|
|
Driver.Quit();
|
|
}
|
|
private void Create(Browser browser)
|
|
{
|
|
switch (browser)
|
|
{
|
|
case Browser.Chrome:
|
|
driver = CreateChromeDriver();
|
|
break;
|
|
case Browser.Firefox:
|
|
driver = CreateFirefoxDriver();
|
|
break;
|
|
case Browser.InternetExplorer:
|
|
driver = CreateInternetExplorerDriver();
|
|
break;
|
|
case Browser.Opera:
|
|
driver = CreateOperaDriver();
|
|
break;
|
|
case Browser.MSEdge:
|
|
driver = CreateMSEdgeDriver();
|
|
break;
|
|
case Browser.Safari:
|
|
driver = CreateSafariDriver();
|
|
break;
|
|
case Browser.PhantomJS:
|
|
driver = CreatePhantomJSDriver();
|
|
break;
|
|
default:
|
|
driver = CreateFirefoxDriver();
|
|
break;
|
|
}
|
|
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(60 * 2));
|
|
driver.Manage().Timeouts().SetScriptTimeout(TimeSpan.FromSeconds(60 * 4));
|
|
driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(60 * 3));
|
|
driver.Manage().Window.Maximize();
|
|
driver.Manage().Cookies.DeleteAllCookies();
|
|
}
|
|
|
|
private IWebDriver CreateFirefoxDriver()
|
|
{
|
|
FirefoxProfile profile = new FirefoxProfile();
|
|
profile.Clean();
|
|
profile.SetPreference("browser.startup.homepage", "about:blank");
|
|
profile.SetPreference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", false);
|
|
profile.AcceptUntrustedCertificates = true;
|
|
profile.AssumeUntrustedCertificateIssuer = false;
|
|
profile.EnableNativeEvents = false;
|
|
profile.AlwaysLoadNoFocusLibrary = false;
|
|
profile.SetPreference("network.ntlm.send-lm-response", true);
|
|
return new FirefoxDriver(profile);
|
|
}
|
|
private static IWebDriver CreateInternetExplorerDriver()
|
|
{
|
|
return new InternetExplorerDriver();
|
|
}
|
|
private static IWebDriver CreateMSEdgeDriver()
|
|
{
|
|
return new EdgeDriver();
|
|
}
|
|
private static IWebDriver CreateChromeDriver()
|
|
{
|
|
return new ChromeDriver();
|
|
}
|
|
|
|
private static IWebDriver CreateOperaDriver()
|
|
{
|
|
return new OperaDriver();
|
|
}
|
|
|
|
private static IWebDriver CreatePhantomJSDriver()
|
|
{
|
|
return new PhantomJSDriver();
|
|
}
|
|
|
|
private static IWebDriver CreateSafariDriver()
|
|
{
|
|
return new SafariDriver();
|
|
}
|
|
|
|
|
|
public void OpenPage(string url)
|
|
{
|
|
Driver.Navigate().GoToUrl(string.Format("{0}{1}", Domain, url));
|
|
WaitAjax();
|
|
}
|
|
public void WaitElement(By locator, int timeout = 11000, int interval = 500)
|
|
{
|
|
WebDriverWait wait = new WebDriverWait(Driver, TimeSpan.FromMilliseconds(timeout));
|
|
wait.PollingInterval = new TimeSpan(0, 0, 0, 0, interval);
|
|
IWebElement waitElem;
|
|
waitElem = wait.Until(ExpectedConditions.ElementExists(locator));
|
|
}
|
|
public void WaitAjax(int timeout = 30, int interval = 1000)
|
|
{
|
|
float time = 0;
|
|
var ajaxIsComplete = false;
|
|
do
|
|
{
|
|
Thread.Sleep(interval);
|
|
time += interval / 1000;
|
|
try
|
|
{
|
|
ajaxIsComplete = (bool)(Driver as IJavaScriptExecutor).ExecuteScript("return jQuery.active == 0");
|
|
}
|
|
catch
|
|
{
|
|
ajaxIsComplete = true;
|
|
}
|
|
} while (!ajaxIsComplete && time < timeout);
|
|
}
|
|
|
|
public IWebElement GetElement(By locator)
|
|
{
|
|
return Driver.FindElement(locator);
|
|
}
|
|
public IWebElement GetElement(IWebElement container, By locator)
|
|
{
|
|
return container.FindElement(locator);
|
|
}
|
|
public IWebElement GetElement(By locator, string attributeName, string attributeValue)
|
|
{
|
|
IList<IWebElement> elements = Driver.FindElements(locator);
|
|
foreach (IWebElement element in elements)
|
|
{
|
|
string attribute = element.GetAttribute(attributeName);
|
|
if ((!string.IsNullOrEmpty(attribute)) && (attribute == attributeValue))
|
|
{
|
|
return element;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
public IWebElement GetElement(By locator, string attributeName, string attributeValue, string text, By containerLocator = null)
|
|
{
|
|
IList<IWebElement> elements;
|
|
if (containerLocator != null)
|
|
{
|
|
IWebElement container = Driver.FindElement(containerLocator);
|
|
elements = container.FindElements(locator);
|
|
}
|
|
else
|
|
{
|
|
elements = Driver.FindElements(locator);
|
|
}
|
|
foreach (IWebElement element in elements)
|
|
{
|
|
string attribute = element.GetAttribute(attributeName);
|
|
if ((!string.IsNullOrEmpty(attribute)) && (attribute == attributeValue) && (element.Text == text))
|
|
{
|
|
return element;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
public IWebElement GetElement(By locator, string text)
|
|
{
|
|
IList<IWebElement> elements = Driver.FindElements(locator);
|
|
foreach (IWebElement element in elements)
|
|
{
|
|
if ((!string.IsNullOrEmpty(element.Text)) && (element.Text.IndexOf(text) > -1))
|
|
{
|
|
return element;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
public IWebElement GetElementXPath1(By containerLocator, string xPath, string text)
|
|
{
|
|
IWebElement container = Driver.FindElement(containerLocator);
|
|
IList<IWebElement> elements = container.FindElements(By.XPath(xPath));
|
|
foreach (IWebElement element in elements)
|
|
{
|
|
if ((!string.IsNullOrEmpty(element.Text)) && (element.Text == text))
|
|
{
|
|
return element;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
public IWebElement GetElement(By containerLocator, string xPath1, string xPath2, string text)
|
|
{
|
|
IWebElement container1 = Driver.FindElement(containerLocator);
|
|
IWebElement container2 = container1.FindElement(By.XPath(xPath1));
|
|
IList<IWebElement> elements = container2.FindElements(By.XPath(xPath2));
|
|
foreach (IWebElement element in elements)
|
|
{
|
|
if ((!string.IsNullOrEmpty(element.Text)) && (element.Text == text))
|
|
{
|
|
return element;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
public IWebElement GetLinkElement(string text)
|
|
{
|
|
return Driver.FindElement(By.PartialLinkText(text));
|
|
}
|
|
public IList<IWebElement> GetElements(By locator, By containerLocator = null)
|
|
{
|
|
if (containerLocator != null)
|
|
{
|
|
IWebElement container = Driver.FindElement(containerLocator);
|
|
return container.FindElements(locator);
|
|
}
|
|
else
|
|
{
|
|
return Driver.FindElements(locator);
|
|
}
|
|
}
|
|
public IWebElement GetGroup(string text)
|
|
{
|
|
IList<IWebElement> elements = Driver.FindElements(By.ClassName("form-group"));
|
|
foreach (IWebElement element in elements)
|
|
{
|
|
if ((!string.IsNullOrEmpty(element.Text)) && (element.Text.IndexOf(text) > -1))
|
|
{
|
|
return element;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public void SendKeys(By locator, string text)
|
|
{
|
|
WaitElement(locator);
|
|
GetElement(locator).Clear();
|
|
GetElement(locator).SendKeys(text);
|
|
WaitAjax();
|
|
}
|
|
public void SendKeys(IWebElement container, By locator, string text)
|
|
{
|
|
WaitElement(locator);
|
|
GetElement(container, locator).Clear();
|
|
GetElement(container, locator).SendKeys(text);
|
|
WaitAjax();
|
|
}
|
|
public void SendKeys(By locator, string attributeName, string attributeValue, string text)
|
|
{
|
|
WaitElement(locator);
|
|
IWebElement elm = GetElement(locator, attributeName, attributeValue);
|
|
elm.Click();
|
|
elm.SendKeys(text);
|
|
WaitAjax();
|
|
}
|
|
|
|
public void ClickElement(By locator)
|
|
{
|
|
WaitElement(locator);
|
|
GetElement(locator).Click();
|
|
WaitAjax();
|
|
}
|
|
public void ClickElement(IWebElement container, By locator)
|
|
{
|
|
WaitElement(locator);
|
|
GetElement(container, locator).Click();
|
|
WaitAjax();
|
|
}
|
|
public void ClickElement(By locator, string text)
|
|
{
|
|
WaitElement(locator);
|
|
GetElement(locator, text).Click();
|
|
WaitAjax();
|
|
}
|
|
public void ClickElement(By locator, string attributeName, string attributeValue, string text, By containerLocator = null)
|
|
{
|
|
WaitElement(locator);
|
|
GetElement(locator, attributeName, attributeValue, text, containerLocator).Click();
|
|
WaitAjax();
|
|
}
|
|
public void ClickLink(string text)
|
|
{
|
|
GetLinkElement(text).Click();
|
|
WaitAjax();
|
|
}
|
|
public bool CheckElement(By locator, string attributeName, string attributeValue, string text)
|
|
{
|
|
WaitElement(locator);
|
|
IWebElement element = GetElement(locator, attributeName, attributeValue);
|
|
return element.GetAttribute("value") == text;
|
|
}
|
|
public void SelectDropdownValue(By locator, string value)
|
|
{
|
|
IWebElement dropdown = GetElement(locator);
|
|
SelectElement select = new SelectElement(dropdown);
|
|
int index = -1;
|
|
bool found = false;
|
|
foreach (IWebElement option in select.Options)
|
|
{
|
|
index++;
|
|
if (option.Text == value)
|
|
{
|
|
found = true;
|
|
break;
|
|
}
|
|
}
|
|
if ((found) && (index > -1))
|
|
{
|
|
select.SelectByIndex(index);
|
|
}
|
|
WaitAjax();
|
|
}
|
|
public void SelectDropdownValue(IWebElement container, By locator, string value)
|
|
{
|
|
IWebElement dropdown = GetElement(container, locator);
|
|
SelectElement select = new SelectElement(dropdown);
|
|
int index = -1;
|
|
bool found = false;
|
|
foreach (IWebElement option in select.Options)
|
|
{
|
|
index++;
|
|
if (option.Text == value)
|
|
{
|
|
found = true;
|
|
break;
|
|
}
|
|
}
|
|
if ((found) && (index > -1))
|
|
{
|
|
select.SelectByIndex(index);
|
|
}
|
|
WaitAjax();
|
|
}
|
|
public string SelectedDropdownValue(By locator)
|
|
{
|
|
IWebElement dropdown = GetElement(locator);
|
|
SelectElement select = new SelectElement(dropdown);
|
|
return select.SelectedOption.Text;
|
|
}
|
|
|
|
private bool ListContains(string[] list, string value)
|
|
{
|
|
foreach (string item in list)
|
|
{
|
|
if (item == value) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
public bool SetCheckboxList(string name, string[] list)
|
|
{
|
|
IWebElement group = GetGroup(name);
|
|
bool someChecked = false;
|
|
if (list.Length > 0)
|
|
{
|
|
string pageSource = Driver.PageSource;
|
|
IWebElement table = group.FindElement(By.Id("uom"));
|
|
IList<IWebElement> rows = table.FindElement(By.XPath("tbody")).FindElements(By.XPath("tr"));
|
|
int i = -1;
|
|
foreach (IWebElement row in rows)
|
|
{
|
|
i++;
|
|
if (i > 0)
|
|
{
|
|
IWebElement checkbox = row.FindElement(By.XPath("td")).FindElement(By.XPath("input"));
|
|
if (ListContains(list, row.Text))
|
|
{
|
|
checkbox.Click();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
someChecked = true;
|
|
}
|
|
return someChecked;
|
|
}
|
|
|
|
public string Subs(string source, string start, string end)
|
|
{
|
|
try
|
|
{
|
|
int indexStart = source.IndexOf(start) + start.Length;
|
|
int indexEnd = source.IndexOf(end, indexStart);
|
|
return source.Substring(indexStart, indexEnd - indexStart);
|
|
}
|
|
catch
|
|
{
|
|
return string.Empty;
|
|
}
|
|
}
|
|
|
|
public bool FindRecord(string objectType, string text, out string id, int index = 1)
|
|
{
|
|
id = "";
|
|
SendKeys(By.ClassName("form-control"), "type", "search", text);
|
|
IList<IWebElement> buttons = Driver.FindElements(By.ClassName("btn"));
|
|
int i = 0;
|
|
foreach (IWebElement button in buttons)
|
|
{
|
|
if (button.Text == "Edit")
|
|
{
|
|
i++;
|
|
if (i == index)
|
|
{
|
|
id = button.GetAttribute("href").Replace(string.Format("{0}{1}/Edit/", Domain, objectType), "");
|
|
return GetElementXPath1(By.ClassName("odd"), "td", text) != null;
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
/*IWebElement btnEdit = GetElement(By.ClassName("btn"), "Edit");
|
|
if (btnEdit != null)
|
|
{
|
|
id = btnEdit.GetAttribute("href").Replace(string.Format("{0}{1}/Edit/", Domain, objectType), "");
|
|
}
|
|
return GetElementXPath1(By.ClassName("odd"), "td", text) != null; */
|
|
}
|
|
public bool FindTableRecord(By tableLocator, string text, string start, string end, out string id)
|
|
{
|
|
id = "";
|
|
IWebElement table = GetElement(tableLocator);
|
|
IList<IWebElement> rows = table.FindElement(By.XPath("tbody")).FindElements(By.XPath("tr"));
|
|
foreach (IWebElement row in rows)
|
|
{
|
|
IList<IWebElement> columns = row.FindElements(By.XPath("td"));
|
|
foreach (IWebElement column in columns)
|
|
{
|
|
if (column.Text.IndexOf(text) > -1) {
|
|
if ((column.Text.IndexOf(start) > -1) && (column.Text.IndexOf(end) > -1)) {
|
|
id = Subs(column.Text, start, end);
|
|
return true;
|
|
} else {
|
|
id = Subs(column.FindElement(By.XPath("a")).GetAttribute("href"), start, end);
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
} |