The following tutorial is a complete guide for Creating a basic Selenium test automation framework with C#, Selenium WebDriver 3, Page Object Pattern and support for multiple browsers and data driven. In the last tutorial, we learned how to inspect elements. Now, let’s learn how to find elements with Selenium WebDriver 3, in order to interact with them.
Tutorial Overview:
Class 1 – Creating an Automated Test Using Selenium WebDriver 3 and C#
- Setting Up the Development Environment for Your Selenium Automation Framework
- How to Inspect Web Elements & Methods to Locate Them with Chrome Devtools
- You’re here→How to Find Web Elements with Selenium WebDriver
Class 2 – How to Create a Test Automation Framework Architecture with Selenium WebDriver 3
- How to Create a Cross Browser Compatible Testing Framework
- Steps to Develop a Report Module in a Testing Framework
- Implementing a Report Module in an Automated Framework
- How to Write a Functional Test with a Basic Selenium Automation Framework
Class 3 – Utilizing Test Automation Framework with Advanced Capabilities
- Page Object Pattern: Advantages & Implementation
- Read Data From CSV File in C#
- How to Create a Test Suite in Selenium WebDriver and C#
- How to Use Data Driven with Selenium Test Suite
We’ll be opening our test we created in Visual Studio in the first tutorial:
[TestFixture] public class AutomationCore { IWebDriver driver; [TestFixtureSetUp] public void startTest() { driver = new ChromeDriver(); driver.Url = "https://blog.testproject.io/"; } [TestFixtureTearDown] public void endtest() { driver.Quit(); } [Test] public void blogTest() { Assert.IsTrue(Pages.contactUs.isAt()); } }
Finding elements on a web page before interacting with it, can be done on the WebDriver Instance(driver) itself or on a WebElement. WebDriver gives us the Find Element and Find Elements methods to find elements on the web page.
In order to reach the element, first we’ll find it through Selenium with the function FindElement which receives the method we’ll choose to find the element with.
Our example will be focusing on the field: ‘Your Name’. Considering we are willing to perform a certain action on this element, the element will be found via Chrome as we learned in the previous tutorial: Copy the value of the attribute – ‘Name’.
The final command:
driver.FindElement(By.Name(“your-name”))
After finding the element with Selenium, we are able to perform various actions on it. In our case, we’ll be sending text into the field. Thus, we’ll choose the function: SendKeys , as shown in the example below:
[TestFixture] public class AutomationCore { IWebDriver driver; [TestFixtureSetUp] public void startTest() { driver = new ChromeDriver(); driver.Url = "https://blog.testproject.io/"; } [TestFixtureTearDown] public void endtest() { driver.Quit(); } [Test] public void blogTest() { Assert.IsTrue(Pages.contactUs.isAt()); driver.FindElement(By.Name("your-name")).SendKeys("Test"); } }
FindElement is the most basic way to find elements, although not very convenient for code maintenance. Assuming the fact that there are hundreds of tests all using the same Selector and in case one day, the Selector would change, we’ll have to go over each line of code and change it. To solve this issue, the recommendation is to build the test automation framework by implementing the Page Object Model, discussed in this tutorial.
Continue to the next step in creating a test automation framework: How to Create a Cross Browser Compatible Test Automation Framework.
-I’m curious to see what you think, please leave your questions and thoughts!
Thanks. Page Object Model “this tutorial” link gives 404 – page not found. please fix it.
Hiten
Hi Hiten,
Thanks for the update! It’s fixed 🙂
Hi,
Managed to muddle through errors on previous tutorials, but on this one I’m getting:
Error CS0229 Ambiguity between ‘AutomationCore.driver’ and ‘AutomationCore.driver’ NUnitTestProject1
What am I missing here?
Note, this currently what my program looks like:
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using NUnit.Framework;
namespace Test
{
[TestFixture]
public class AutomationCore
{
IWebDriver driver;
[SetUp]
public void startTest()
{
driver = new ChromeDriver(@”C:\Users\Magoo\Downloads”);
driver.Url = “https://blog.testproject.io/”;
}
[TearDown]
public void endtest()
{
driver.Quit();
}
[Test]
public void blogTest()
{
Assert.IsTrue(driver.Title.Contains(“Blog”));
driver.FindElement(By.Name(“your-name”)).SendKeys(“Test”);
}
}
}
Had to change the Assert.IsTrue line as it wouldn’t recognise the Pages thing from a previous test, and had to include the file location for chromedriver I was getting an error saying it couldn’t find the file unless I specified the file location.