public void ScrollToBottom()
{
((IJavaScriptExecutor)_driver).ExecuteScript("window.scroll(0, document.body.scrollHeight)");
}
public void ScrollToTop()
{
((IJavaScriptExecutor)_driver).ExecuteScript("window.scroll(0, document.body.scrollTop)");
}
public void WaitExplicitely(IWebElement element)
{
DefaultWait<IWebElement> wait = new DefaultWait<IWebElement>(element);
wait.Timeout = TimeSpan.FromMinutes(1);
wait.PollingInterval = TimeSpan.FromMilliseconds(250);
Func<IWebElement, bool> condition = new Func<IWebElement, bool>((IWebElement ele) =>
{
if (element.Displayed && element.Enabled)
{
return true;
}
return false;
});
wait.Until(condition);
}
public bool IsAlertPresent()
{
try
{
_driver.SwitchTo().Alert();
return true;
}
catch (NoAlertPresentException)
{
return false;
}
}
public void CloseAlert()
{
IAlert alert = _driver.SwitchTo().Alert();
alert.Accept();
}
public void CancelAlert()
{
IAlert alert = _driver.SwitchTo().Alert();
alert.Dismiss();
}
public decimal CustomRoundOff(decimal x)
{
decimal gmr = Math.Truncate(x * 100) / 100;
decimal gmrDecPlaces = Math.Truncate(x * 1000) / 1000;
decimal t = (gmrDecPlaces * 1000) % 10;
if (t >= 6)
gmr += 0.01m;
return gmr;
}
public string ConvertToStringByCustomRoundOff(string value, int x = 3)
{
string result = decimal.Round(decimal.Parse(value), x).ToString();
return result;
}
public string GetDate(string Date, string Format)
{
string input = Date;
DateTime d;
if (DateTime.TryParseExact(input, "MMM dd,yy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out d))
{
return d.ToString("mm.dd.yy");
}
return d.ToString("mm.dd.yy");
}
public string ConvertDateFormat(string Date)
{
String[] s = Date.Split('/');
return string.Format("{0}.{1}.{2}", s[1].TrimStart('0').Trim(), s[0].TrimStart('0').Trim(), s[2].Trim());
}
public string GetDate(string ExpectedFormat)
{
try
{
DateTime d = DateTime.Parse(DateTime.Now.ToString());
return d.ToString(ExpectedFormat);
}
catch (Exception)
{
return DateTime.Parse(DateTime.Now.ToString()).ToString(ExpectedFormat);
}
}
public string GetNumericData(string text)
{
string value = Regex.Replace(text, "[A-Za-z ]", "");
return value.Trim();
}
public string GetAlphaChars(string text)
{
string value = Regex.Replace(text, "[^a-zA-Z]", "");
return value.Trim();
}
public void DeleteallCookies()
{
_driver.Manage().Cookies.DeleteAllCookies();
Thread.Sleep(5000);
RefreshPage();
}
public void SwitchToParent()
{
var windowids = _driver.WindowHandles;
for (int i = windowids.Count - 1; i > 0;)
{
_driver.Close();
i = i - 1;
ImplicitWait();
_driver.SwitchTo().Window(windowids[i]);
}
_driver.SwitchTo().Window(windowids[0]);
}
public void SwitchToParentWindow()
{
ImplicitWait(10);
var windowids = _driver.WindowHandles;
for (int i = windowids.Count - 1; i > 0;)
{
i = i - 1;
ImplicitWait();
_driver.SwitchTo().Window(windowids[i]);
}
_driver.SwitchTo().Window(windowids[0]);
}
public void SwitchToOtherWindow()
{
ImplicitWait(10);
IReadOnlyCollection<string> windowids = _driver.WindowHandles;
string Current = _driver.CurrentWindowHandle;
foreach (var item in windowids)
{
if (!item.Equals(Current))
{
_driver.SwitchTo().Window(item);
}
}
}
public void SwitchTotWindow(int w)
{
var windowids = _driver.WindowHandles;
_driver.SwitchTo().Window(windowids[w]);
}
public void SwitchToFrame(By locator)
{
_driver.SwitchTo().Frame(_driver.FindElement(locator));
}
public void OpenNewTab()
{
((IJavaScriptExecutor)_driver).ExecuteScript("window.open();");
_driver.SwitchTo().Window(_driver.WindowHandles.Last());
}
public string GetTPlus2DaysDate()
{
DateTime currentDate = DateTime.Now;
DateTime tPlus2Date = currentDate.AddDays(2);
// Check if the date is a weekend day
while (tPlus2Date.DayOfWeek == DayOfWeek.Saturday || tPlus2Date.DayOfWeek == DayOfWeek.Sunday)
{
// If it is a weekend day, add another day
tPlus2Date = tPlus2Date.AddDays(1);
}
return tPlus2Date.ToString("MMM dd yyyy");
}
public bool WaitForFileDownLoad(string fileName = "Confirmed.xlsx", string path = @"C:\Downloads", int timeout = 30)
{
//Wait for the file to download completely
bool isFileDownloaded = false;
while (timeout > 0 && !isFileDownloaded)
{
isFileDownloaded = Directory.GetFiles(path, fileName, SearchOption.AllDirectories).Length > 0;
timeout--;
Thread.Sleep(1000); //wait for 1 second
}
//Verify that the file has been downloaded successfully
return isFileDownloaded;
}
public int GetRowsCountFromExcel(string fileName, string downloadPath = @"C:\Downloads")
{
/* //Read the downloaded Excel file and count the rows
string filePath = Path.Combine(downloadPath, fileName);
ExcelPackage excelPackage = new ExcelPackage(new FileInfo(filePath));
ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets[0];
int rowCount = worksheet.Dimension.Rows; */
string filePath = Path.Combine(downloadPath, fileName);
XSSFWorkbook workBook = new XSSFWorkbook(File.Open(filePath, FileMode.Open));
var sheet = workBook.GetSheetAt(0);
int count = sheet.LastRowNum + 1;
//Clean up: delete the downloaded file
File.Delete(filePath);
return count;
}
public void WaitUntilVisible(By locator,int seconds=10)
{
DefaultWait<IWebDriver> fluentWait = new DefaultWait<IWebDriver>(_driver);
fluentWait.Timeout = TimeSpan.FromSeconds(seconds);
fluentWait.PollingInterval = TimeSpan.FromMilliseconds(250);
/* Ignore the exception - NoSuchElementException that indicates that the element is not present */
fluentWait.IgnoreExceptionTypes(typeof(NoSuchElementException));
fluentWait.Message = "Element to be searched not found";
fluentWait.Until(ExpectedConditions.ElementIsVisible(locator));
fluentWait.Until(ExpectedConditions.ElementToBeClickable(locator));
}
public void deleteAllCookiesAndRefresh()
{
_driver.Manage().Cookies.DeleteAllCookies();
_driver.Navigate().Refresh();
}