Skip to content

Instantly share code, notes, and snippets.

@suparnashal
suparnashal / java_array_methods.js
Last active April 12, 2022 19:24
Javascript List methods
const items = [
{name:'Bike', price:100},
{name:'TV', price:200},
{name:'Album', price:10},
{name:'Book',price:5},
{name:'Phone',price:500},
{name:'Computer',price:1000},
{name:'Keyboard', price:25}
]
@suparnashal
suparnashal / gist:dd0dedce032077e490506319c4da6611
Created April 6, 2022 20:02
For every method that takes parameter we need to check if any of parameters are null
//Not needed for value types like bool and int
//null reference check example.
//Good practice to use such reference checks
private void SetDependency(string rowIndex, string columnName, string dependency)
{
if (rowIndex is null) throw new ArgumentNullException(nameof(rowIndex));
if (columnName is null) throw new ArgumentNullException(nameof(columnName));
if (dependency is null) throw new ArgumentNullException(nameof(dependency));
..
..}
@suparnashal
suparnashal / Driver.cs
Last active March 25, 2022 14:44
Driver and Listener classes from our Event based automation framework
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Appium.Android;
using OpenQA.Selenium.Appium.iOS;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Edge;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.Events;
using System;
@suparnashal
suparnashal / BasePage.cs
Last active March 12, 2022 20:32
Automation framework based on custom event handlers for webdriver operations
public abstract class BasePage : Driver
{
protected IWebElement LookUpElement(By locator) {}
protected IWebElement LookUpElements(By locator) {}
protected string[] GetTextsByLocator(By locator) {}
protected int GetCountOfElements(By locator) {}
protected int GetCountOfElementsUsingJS(string CssSelector) {}
protected string[] GetTextValuesByCssSelector(string CssSelector) {}
..
..
@suparnashal
suparnashal / gist:e5400859f0881b22bd455dbf33c52612
Created February 25, 2022 17:23
Javascript css selectors
//input is a child of td having a col index of 0
td[aria-colindex='0'] input
//Css selector using a pseudoelement :before :focus-within etc
//here we get the label having a pseudoelement focus-within
label:focus-within
@suparnashal
suparnashal / gist:c58ebb91e1e5968a4d358454cff8072a
Created February 25, 2022 16:23
This gets a list of strings using css selector
/// <summary>
/// Gets list of strings defined by given CssSelector
/// </summary>
/// <param name="CssSelector">Css selector to identify the group of elements</param>
/// <returns>array of strings</returns>
protected string[] GetTextValuesByCssSelector(string CssSelector)
{
if (CssSelector is null) throw new ArgumentNullException(nameof(CssSelector));
object obj = ((IJavaScriptExecutor)this.GetDriver).ExecuteScript($"return Array.from(document.querySelectorAll(\"{CssSelector}\"),x=>x.value)");
@suparnashal
suparnashal / gist:c0e534a97b0ef3698ffebc5d3290b298
Created February 25, 2022 16:19
This method gets a count of elements using CSS selector.
/// This method is useful when count can be 0 or is expected to be zero
/// </summary>
/// <param name="CssSelector"></param>
/// <returns>count of elements</returns>
protected int GetCountOfElementsUsingJS(string CssSelector)
{
if (CssSelector is null) throw new ArgumentNullException(nameof(CssSelector));
int count;
count = Convert.ToInt32(((IJavaScriptExecutor)this.GetDriver).ExecuteScript($"return document.querySelectorAll('{CssSelector}').length"));
Actions action = new Actions(webdriver);
WebElement we = webdriver.findElement(By.xpath("html/body/div[13]/ul/li[4]/a"));
action.moveToElement(we).moveToElement(webdriver.findElement(By.xpath("/expression-here"))).click().build().perform();
@suparnashal
suparnashal / gist:8066de454f5f4ccaac582420b39ce9eb
Created May 4, 2021 17:38
Use Conditional attributes instead of #if..#endif blocks
#define TRACE_ON
using System;
using System.Diagnostics;
namespace AttributeExamples
{
public class Trace
{
[Conditional("TRACE_ON")]
public static void Msg(string msg)
@suparnashal
suparnashal / gist:edc8bc8787c084e11afd7bb3f801ea67
Last active May 4, 2021 16:34
C#9.0 New syntax for null/ not null
if (e is not null)
{
// ...
}
if (input is null)
{
return;
}