This Project is an attempt to wrap selenium to expose Api which is somewhere inbetween C# LINQ and Cypress
- query - commands that read the state of your application
- assertion - command which assert on a given state
- action - interact with your application as a user would
- other - any other command that is useful for writing tests
dotnet add package Shunya.Selenium --version 0.1.0
We first start by creating Instance of IChainable
i.e. SnSelenium
by passing Type of browser and Instance of ILogger.
This also created context which is then passed by reference to all subsequent commands.
SnSelenium snSel = new SnSelenium(Constants.SupportedBrowsers.CHROME,logger);
Then we can run commands on the snSel
as SnSelenium
implements IChainable
interface.
snSel.Visit("https://www.youtube.com/").Execute();
Here snSel is of type IChainable so we can use command on it.
All failable commands return instance of IRunnable which contains implementation of Execute
method.
Such commands can be executed by calling Execute()
on then.If you want to execute commands with retry meachanism
you can try .Execute(100,5)
which signifies try 5 times with duration of 500 miliseconds between tries.
for example
SnSelenium snSel = new SnSelenium(Constants.SupportedBrowsers.CHROME,logger);
snSel.Visit("https://hasare.com/").Execute().GetOne({provide x path}).Execute()...(next commmand)
as shown in above example we can chain commands together to accomplish the task.
Most Action are retryable by default
So if you want cetail action to execute atmost 3 times with time duration of 100 miliseconds between each rety till it succeds
then you can pass below parameters to Execute
Method
Execute(100,3) // wait for 100 miliseconds between each tries, try for almost 3 times
here 100 is time duration between retries and 3 iss max no of attempt
To get result of particular action we can call GetResult()
on Result returned by Execute
method
IWebElement el=snSel.Visit("https://hasare.com/").Execute().GetOne({provide By}).Execute().GetResult()
Point to note calling GetResult
breaks the chaining ability.
To save the result for later user but to continue execution chain please see below command As
As shown in diagram above Shunya initiates its own context which is used internally to store bunch of things.You can make use of same context to store result of action execution.
snSel.Visit("https://hasare.com/").Execute().GetOne({provide x path}).Execute().As("@elementone")... next actions
what this allows you do to is save the result of action and continue chaining to achieve objective.But when at later point you want to use the result you can simply call
var context = snSel.GetContext(); // Get context
IWebElement a=context.GetTypeValue<IWebElement>("@elementone"); // retrive element from context
Get saved IWebElement
!!! One Thing to note here is you should know the type of object you are retrieving fromm context !!!
!!! You could find the type of object by checking Return Type of Execute method before calling As on it !!!
Instantiate Shunya Selenium To begin
using Shunya.Selenium;
using Shunya.Selenium.Selenium;
SnSelenium snSel = new SnSelenium(Constants.SupportedBrowsers.CHROME,logger);
This is use to navigate top particular page
var foundElement=snSel.Visit("https://hasare.com").Execute();
Get First html element satisfying the requirements.
IWebElement foundElement=snSel.Visit("https://hasare.com").Execute()
.Get(By.XPath("/html/body/div[1]/div[1]/main/div/div[1]/div/h1")).Execute().GetResult();
Get all elements satisfying Selenium By object
ReadOnlyCollection<IWebElement> foundElement=snSel.Visit("https://www.selenium.dev").Execute()
.Get(By.XPath("/html/body/div/main/section[2]/div/div/div[2]/div/div[1]/h4")).Execute().GetResult();
Used to save result of earlier action in context.
Note- Use string starting with @ as input to As
var foundElement=snSel.Visit("https://www.selenium.dev").Execute()
.Get(By.XPath("/html/body/div/main/section[2]/div/div/div[2]/div/div[1]/h4")).Execute().As("@save");
var context = snSel.GetContext();
var a=context.GetTypeValue<IReadOnlyCollection<IWebDriver>>("@save");
This action is used to wait for certain time before executing next action ina action chain.
Below command will wait till 10 seconds have passed before getting the url.
var result = snSel.Visit("https://hasare.com/").Execute().Wait(10000).Location().GetResult();
This action pauses further execution of actions until user closes additional browser tab opened.
Use full for debugging purposes where you are not sure how long it will take to fix the issue.
// Pause takes in optional boolean parameter if you pass the params as True then Pause will be skipped
var result=snSel.Visit("https://hasare.com/").Execute().Pause().Location().GetResult(); // the pause will stop execution of next actrions till additrional browser window is closed.
Get the first DOM element that matches the selector (whether it be itself or one of its ancestors).
//Takes in ".className" or html tag like "div" as input.
var foundElement=snSel.Visit("https://hasare.com").Execute()
.GetOne(By.XPath("/html/body/div/div[1]/main/div/div[1]/div/h1")).Execute()
.Closest("div").Execute().GetResult();
Get A DOM element at a specific index in an array of elements.
var foundElement=snSel.Visit("https://hasare.com/").Execute()
.Get(By.XPath("//*[@id=\"products\"]/div/dl/div[*]/dd")).Execute().Eq(0);
Filters IWebElement collection based on delegate passed.
ReadOnlyCollection<IWebElement> foundElement=snSel.Visit("https://hasare.com/").Execute()
.Get(By.XPath("//*[@id=\"products\"]/div/dl/div[*]/dd")).Execute().Filter(el=>el.Text=="TEXT").GetResult();
Get the descendent DOM elements of a specific selector.
ReadOnlyCollection<IWebElement> foundElement = snSel.Visit("https://hasare.com/").Execute()
.GetOne(By.XPath("//*[@id=\"products\"]/div/dl")).Execute().Find("div").GetResult();
Get the first DOM element within a set of DOM elements.
IWebElement foundElement=snSel.Visit("https://hasare.com/").Execute()
.Get(By.XPath("//*[@id=\"products\"]/div/dl/div[*]/dd")).Execute().Filter(el=>el.Text=="TEXT").First().GetResult();
Get the last DOM element within a set of DOM elements.
var foundElement=snSel.Visit("https://hasare.com/").Execute()
.Get(By.XPath("//*[@id=\"products\"]/div/dl/div[*]/dd")).Execute().Last().GetResult();
Get Current browser Url
string result=snSel.Visit("https://hasare.com/").Execute().Location().GetResult();
For support raise an issue in this repository.
PR's are most welcome to add additional functionality.