-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebDriverExtensions.cs
70 lines (66 loc) · 2.51 KB
/
WebDriverExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using System.Collections.ObjectModel;
using System.Text.RegularExpressions;
namespace OLX_Selenium_Tests.Helpers
{
public static class WebDriverExtensions
{
public static void EnterText(this IWebElement webElement, string text)
{
webElement.Clear();
webElement.SendKeys(text);
}
public static IWebElement FindElement(this IWebDriver driver, By by, int timeoutInSeconds)
{
if (timeoutInSeconds > 0)
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
return wait.Until(drv => drv.FindElement(by));
}
return driver.FindElement(by);
}
public static IWebElement FindElementByDataTestID(this IWebDriver webDriver, string testID)
{
return webDriver.FindElement(By.XPath($"//*[@data-testid='{testID}']"));
}
public static IWebElement FindElementByDataTestID(this IWebElement webElement, string testID)
{
return webElement.FindElement(By.XPath($"//*[@data-testid='{testID}']"));
}
public static ReadOnlyCollection<IWebElement> FindElementsByDataTestID(this IWebDriver webDriver, string testID)
{
return webDriver.FindElements(By.XPath($"//*[@data-testid='{testID}']"));
}
public static ReadOnlyCollection<IWebElement> FindElementsByDataTestID(this IWebElement webElement, string testID)
{
return webElement.FindElements(By.XPath($"//*[@data-testid='{testID}']"));
}
public static string GetElementDetails(this IWebElement element)
{
try
{
return $"Tag: {element.TagName},\n Text: {element.Text},\n Class: {element.GetAttribute("class")}";
}
catch (Exception ex)
{
return $"Element not found. Exception: {ex.Message}";
}
}
public static int ConvertTextToInt(this IWebElement webElement)
{
var text = webElement.Text;
if (text.IndexOf(".") != -1)
{
text = text.Substring(0, text.IndexOf("."));
}
if (text.IndexOf(",") != -1)
{
text = text.Substring(0, text.IndexOf(","));
}
Regex regex = new Regex("[^0-9]");
text = regex.Replace(text, "");
return Convert.ToInt32(text);
}
}
}