Created
September 12, 2019 14:54
-
-
Save safebear/a550c4094811993f3c223e1d2f8a8eb5 to your computer and use it in GitHub Desktop.
Cheatsheet of Selenium Commands for C# / .NET
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using OpenQA.Selenium; | |
using OpenQA.Selenium.Interactions; | |
using OpenQA.Selenium.Support.UI; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace ToolsList.Pages | |
{ | |
class CheatSheet : Waits | |
{ | |
/** | |
* Drop-down selectors | |
*/ | |
public void DropDownSelectors() | |
{ | |
/********************************* | |
* Dropdown selectors example | |
*********************************/ | |
// Creats a new 'select' object by using the 'name' of the dropdown box | |
SelectElement drpCountry = new SelectElement(Browser.FindElement(By.Name("country"))); | |
// Selects an item in the dropdown by visible text | |
drpCountry.SelectByText("ANTARCTICA"); | |
/********************************* | |
* Selecting Items in a Multiple SELECT elements | |
*********************************/ | |
// Creates the 'select' object, this time using the ID of the multiselect to identify it. | |
SelectElement fruits = new SelectElement(Browser.FindElement(By.Id("fruits"))); | |
// Select an item in the multiselect by visible text | |
fruits.SelectByText("Banana"); | |
// Select another item in the multiselect by Index number. The first item is always index '0'. | |
fruits.SelectByIndex(1); | |
} | |
/** | |
* Mouse keys - e.g. hover | |
*/ | |
public void MouseKeys() | |
{ | |
/********************************* | |
* Using mousekeys with 'action' class | |
*********************************/ | |
// We're going to hover over this element | |
IWebElement link_Home = Browser.FindElement(By.Id("id")); | |
// We're going to see this element change colour when we hover over it | |
IWebElement td_Home = Browser.FindElement(By.Id("id")); | |
// We need to build a 'move to element' action to hover over something. | |
Actions builder = new Actions(Browser); | |
IAction mouseOverHome = builder | |
.MoveToElement(link_Home) | |
.Build(); | |
// Get the colour of 'td home' element | |
String bgColor = td_Home.GetCssValue("background-color"); | |
Console.WriteLine("Before hover: " + bgColor); | |
// Perform the action we built earlier. | |
mouseOverHome.Perform(); | |
// Check the colour again - it's changed! | |
bgColor = td_Home.GetCssValue("background-color"); | |
Console.WriteLine("After hover: " + bgColor); | |
} | |
/** | |
* Alert message | |
*/ | |
public void AlertMessage() | |
{ | |
/********************************* | |
* Alert Message handling | |
*********************************/ | |
// Switching to Alert | |
IAlert alert = Browser.SwitchTo().Alert(); | |
// Capturing alert message. | |
String alertMessage = Browser.SwitchTo().Alert().Text; | |
// Displaying alert message | |
Console.WriteLine(alertMessage); | |
// Accepting alert | |
alert.Accept(); | |
} | |
/** | |
* Pop-up windows | |
*/ | |
public void PopUpWindows() | |
{ | |
/********************************* | |
* Pop-up windows | |
*********************************/ | |
// Get window handle of current window | |
String MainWindow = Browser.CurrentWindowHandle; | |
// To handle all new opened window. | |
ICollection<String> windows = Browser.WindowHandles; | |
// Check that a new window has opened | |
windows.ToList().ForEach(window => | |
{ | |
String ChildWindow = window; | |
if (!MainWindow.Equals(ChildWindow)) | |
{ | |
// Switching to Child / pop-up window | |
Browser.SwitchTo().Window(ChildWindow); | |
// Enter email into login form (if there is one on the pop-up window!) or do whatever test you want | |
Browser.FindElement(By.Name("emailid")).SendKeys("gaurav.3n@gmail.com"); | |
// Click on the login button | |
Browser.FindElement(By.Name("btnLogin")).Click(); | |
// Closing the Child / pop-up Window. | |
Browser.Close(); | |
} | |
}); | |
// Switching to Parent window i.e Main Window. | |
Browser.SwitchTo().Window(MainWindow); | |
} | |
/** | |
* Date Picker | |
*/ | |
public void DatePicker() | |
{ | |
//Date and Time to be set in textbox | |
String dateTime = "12/07/2014 2:00 PM"; | |
//button to open calendar | |
IWebElement selectDate = Browser.FindElement(By.XPath("//span[@aria-controls='datetimepicker_dateview']")); | |
selectDate.Click(); | |
//button to move next in calendar | |
IWebElement nextLink = Browser.FindElement(By.XPath("//div[@id='datetimepicker_dateview']//div[@class='k-header']//a[contains(@class,'k-nav-next')]")); | |
//button to click in center of calendar header | |
IWebElement midLink = Browser.FindElement(By.XPath("//div[@id='datetimepicker_dateview']//div[@class='k-header']//a[contains(@class,'k-nav-fast')]")); | |
//button to move previous month in calendar | |
IWebElement previousLink = Browser.FindElement(By.XPath("//div[@id='datetimepicker_dateview']//div[@class='k-header']//a[contains(@class,'k-nav-prev')]")); | |
//Get all months from calendar to select correct one | |
ICollection<IWebElement> list_AllMonthToBook = Browser.FindElements(By.XPath("//div[@id='datetimepicker_dateview']//table//tbody//td[not(contains(@class,'k-other-month'))]")); | |
} | |
/** | |
* Switching frames | |
*/ | |
public void switchingFrames() | |
{ | |
Browser.SwitchTo().Frame("a077aa5e"); //switching the frame by ID | |
Browser.SwitchTo().ParentFrame(); // Switch to parent | |
Browser.SwitchTo().DefaultContent(); // switch to main frame | |
// Find number of iframes on the page: | |
int size = Browser.FindElements(By.TagName("iframe")).Count; | |
// Set index for iframe | |
int indexOfIFrame = 0; | |
// Find the frame that contains our element: | |
for (int i = 0; i <= size; i++) | |
{ | |
Browser.SwitchTo().Frame(i); | |
int total = Browser.FindElements(By.XPath("html/body/a/img")).Count; | |
if (total == 1) | |
{ | |
indexOfIFrame = i; | |
}; | |
Browser.SwitchTo().DefaultContent(); | |
} | |
Browser.SwitchTo().Frame(indexOfIFrame); //Switching to the frame | |
Browser.FindElement(By.XPath("html/body/a/img")).Click(); | |
} | |
/** | |
* Drag and Drop | |
* @param source WebElement | |
* @param target WebElement | |
* @throws IOException Reads from a JavaScript file | |
*/ | |
public void DragAndDrop(IWebElement source, IWebElement target) | |
{ | |
// Through Actions - may not work in Chrome! | |
Actions act = new Actions(Browser); | |
act.DragAndDrop(source, target); | |
act.Build().Perform(); | |
} | |
/** | |
* Scrolling | |
*/ | |
public void scrolling(IWebElement element) | |
{ | |
IJavaScriptExecutor js = (IJavaScriptExecutor)Browser; | |
// Until Element is visible | |
js.ExecuteScript("arguments[0].scrollIntoView();", element); | |
// To the bottom of the page | |
//This will scroll the web page till end. | |
js.ExecuteScript("window.scrollTo(0, document.body.scrollHeight)"); | |
} | |
/** | |
* Double and right click | |
*/ | |
public void doubleAndRightClick() | |
{ | |
Actions actions = new Actions(Browser); | |
IWebElement elementLocator = Browser.FindElement(By.Id("ID")); | |
// Double click | |
actions.DoubleClick(elementLocator).Perform(); | |
// Right click | |
actions.ContextClick(elementLocator).Perform(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment