Top 28 Selenium WebDriver Commands in NUnit For Test Automation

Image for post
Image for post

Note: the code samples may be displayed improperly because of markdown. I recommend continuing to read the original article on our blog to make sure all the examples are displayed properly.

What are Selenium Webdriver Commands?

Selenium Webdriver commands are the methods used to run your Selenium test automation scripts. You can use these browser commands to automate different browser actions to perform testing.

  1. Web Element Commands — Performs actions on the web elements in the current window (e.g. checking a button, entering text in a textbox, clicking a button, etc.)
  2. Drop Down Commands — Performs actions on the drop-down elements in the current window (e.g. selecting or deselecting a value from drop-down window).

Web Browser Commands

When C# is used along with the Selenium testing framework, the IWebDriver interface is used to perform actions on the web browser. To access the methods of the IWebDriver interface, an instance/driver object from IWebDriver should be created. The complete list of methods for iWebDriver comes up as soon as you press the dot key.

Image for post
Image for post

1. Url

Using this Selenium Webdriver command you can Get or Set the URL/web page that needs to be displayed.

string IWebDriver.Url { get; set;}

2. Title

This Selenium webdriver command gets the title of the current browser window.

string IWebDriver.Title{get;}

3. PageSource

You can get the source of the page that is loaded by the web browser with this Selenium Webdriver command.

string IWebDriver.PageSource{get;}

4. Quit

You can quit the driver and close every browser window associated with this Selenium Webdriver command in NUnit.

void IWebDriver.Quit()

5. Close

With close() Selenium Webdriver command, you can close the current window, quit the browser in case if it is the last window open. In case you want more detailed information on how to handle browser windows, you can refer to our blog.

void IWebDriver.Close()

6. Navigate

With this Selenium WebDriver command in NUnit you can navigate the browser to another location i.e. Refresh the current window/Go Back to the previous window/Go to the next page as per browsing history/Go to a particular URL.

Image for post
Image for post
INavigation IWebDriver.Navigate()

Back()

Move back a single entry as per the browser’s history.

void INavigation.Back()
driver.Navigate().Back();

Forward()

Move a single item forward as per the browser’s history

void INavigation.Forward()
driver.Navigate().Forward();

GoToUrl()

Navigate to a particular URL which is passed as a parameter to the Navigate() command

void INavigation.GoToUrl(string url)
driver.Navigate().GoToUrl(“https://www.lambdatest.com")

Refresh()

Refresh the current browser window

void INavigation.Refresh()
driver.Navigate().Refresh();

Web Element Commands

While performing Selenium Test automation with NUnit you’ll often have to rely on Selenium Webdriver command to perform operations on web elements (e.g. textboxes, radio buttons, hyperlinks, etc.) on a page, that are triggered via the IWebElement interface.

Image for post
Image for post
Image for post
Image for post

1. Click

Using the Click() Selenium Webdriver command, you can click on a particular web element present on the web page. The Click operation can be performed on hyperlinks, buttons, checkboxes, or any other element that is clickable on the page. The element is first located on the page, after which the Click operation is performed.

void IWebElement.Click()

2. Enabled

You can check whether a particular web element is enabled or not with the Enabled Selenium Webdriver command. It returns True if the web element on the page is enabled else it returns False.

Bool IWebElement.Enabled{get;}

3. Displayed

This Selenium Webdriver command checks whether a particular web element is displayed or not. It returns True if the web element on the page is displayed else it returns False.

Bool IWebElement.Displayed{get;}

4. Clear

The Clear() Selenium WebDriver command clears the content in the web element present on the page. It is used for clearing the pre-loaded contents in a text box.

void IWebElement.Clear()

5. SendKeys

This Selenium Webdriver command Inputs the values in an element like a textbox on the web page.

void IWebElement.SendKeys(string text)

6. GetAttribute

There may be attributes and properties associated to web elements present on a web page. These are added by the website developer. WIth GetAttribute you get the value of a certain attribute as a string of the webelement.

string IWebElement.GetAttribute(string attributename)

7. GetCssValue

Gets the value of a CSS property of a web element present on the web page. Color values are returned in the form of rgba string. For example, a “background-color” property set as “green” in the HTML source, will return “#008000” for its value.

string IWebElement.GetCssValue(string propertyname)
Image for post
Image for post

8. Submit

Performs a click on a button which is of the type submit.

void IWebElement.Submit()

9. Text

Gets the innerText of a web element, without any leading or trailing whitespace & other whitespaces collapsed.

string IWebElement.Text{get;}

10. TagName

Gets the tag name of a web element on the web page.

string IWebElement.TagName{get;}

11. Selected

Checks whether a web element is selected or not. The web element can be radio button, checkbox, etc.

bool IWebElement.Selected{get;}

12. Size

Gets the height & width associated with a web element or the current browser window. The return type is of type struct Size.

Size IWebElement.Size{get;}
Image for post
Image for post

1. SelectByIndex

Select a value based on the index value. The starting index in drop down is 0.

void SelectElement.SelectByIndex(int index)

2. DeselectByIndex

Deselect a selected value based on the index.

void SelectElement.DeselectByIndex(int index)

3. SelectByText

Select an option based on the text of the options available in the drop down. There is an option to do partial matching of the text and by default partial matching is False.

void SelectElement.SelectByText(string text, [bool partialMatch = false])

4. DeselectByText

Deselect an already selected option based on the text of the options available in the drop down.

void SelectElement.DeselectByText(string text)

5. SelectByValue

Select an option based on the value supplied as input.

void SelectElement.SelectByValue(string value)

6. DeselectByValue

Deselect an already selected option based on the value supplied as input.

void SelectElement.DeselectByValue(string value)

7. DeselectAll

Used to clear the selected options in multi select drop down menus.

void SelectElement.DeselectAll()

8. IsMultiple

This Selenium Webdriver command gets a value showing if the parent element supports multiple selections. If the drop down is multi select capable it returns True else otherwise it returns False.

bool SelectElement.IsMultiple{get;}

9. Options

You can get the list of options for the select element with this Selenium WebDriver command.

IList<IWebElement> SelectElement.Options{ get; }

10. AllSelectedOptions

Gets all of the selected options within the select element. It is similar to Options except that it should be used on a multi select drop down menu.

IList<IWebElement> AllSelectedOptions { get; }

It’s A Wrap!

The IWebDriver interface is the base on which the browser interactions for Selenium test automation happens. In this NUnit tutorial, I had a look at the most widely used Selenium WebDriver commands in Nunit for automated browser testing.

Written by

Product Growth at @lambdatesting (www.lambdatest.com)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store