Writing Automated UI tests in .NET using Playwright and SpecFlow

Writing Automated UI tests in .NET using Playwright and SpecFlowWhy are automated tests and why should you write themWhile writing tests isn’t a pleasant exercise for the majority of developers (me included) it is a fundamental part of software develop…


This content originally appeared on Level Up Coding - Medium and was authored by Ivan Stoev

Writing Automated UI tests in .NET using Playwright and SpecFlow

Why are automated tests and why should you write them

While writing tests isn’t a pleasant exercise for the majority of developers (me included) it is a fundamental part of software development. The high-quality software products that you use daily have enough tests underneath the hood to keep the errors to a minimum and give us flawless experience and that’s why you should do the same in the application you’re working on.

Photo by ThisisEngineering on Unsplash

Automated Testing

So what is automated testing you ask? It’s writing code that is separate from the software you want to test with the main purpose being to find any errors or bugs in an application before it has reached production.

Don’t get me wrong, even with tests your software will not be 100% bug-free, but the percentage of bugs will definitely go down.

Short term disadvantages of writing Automated Tests

There are a lot of people out there that would argue that writing a lot of tests could be costly because of the time it consumes as you have to write the actual application and then the test before you’re able to release any piece of software.

But let me ask a question. Do you want to release the best possible product you and your team can built with bugs limited to a minimum, or do you want to rush and release an application with a lot of bugs that will make all of the users, and parties involved unhappy?

Long term advantages

While it is true that you’d need to spend more time and money as a company to write tests that is not enough reason not to do them.

In the long term, if the tests are done right, the application will be of higher quality and nobody will need to spend time and money to fix the bugs in a rush, not to mention that manual tests are simply not scalable if your application is growing at a fast pace.

And while it might be a pain to write them in the first place. Automated tests are reusable — you will write them once and run them hundreds or thousands of times.

Playwright and SpecFlow

After all that is out of the way, let’s create some Automated UI tests. For us to do so we will need to use Playwright, which enables us to write end-to-end tests and is an open-source browser automation library. We will also use SpecFlow, a behaviour-driven development (BDD) framework for .NET.

And for those who are not familiar with BDD testing here is a code sample of a SpecFlow feature file.

As we can see it follows the behaviour that a normal user would have if he wanted to do something in the browser. And what Playwright does is helps us use the browser to test if this is what indeed happens.

Setting up Playwright

What this piece of code does is launch a browser before every Scenario (Test) we have and close it after the Scenario has finished.

Creating our Page Objects

First, we are going to create a BasePageObject class that has a NavigateAsync method navigating us to our page path.

Then we need an actual class for the page being tested.

This will have the initial page path and all the methods we need to complete the test. As mentioned above, we will need to:

  1. Fill in the details in the search field
  2. Click on the search button
  3. Click on the SpecFlow link

This is exactly what is happening in the example using the Page.ClickAsync and passing in the selector ( You can get the selector by inspecting a page a copying it directly from there).

The Page.RunAndWaitForNavigationAsync will force the method to wait for a navigation change before clicking therefore preventing the test from failing because the page was slow to load.

Writing the actual tests

This will go through all the steps coming from the .feature file above and navigate the way we want it to. Finally, it will check if where we are in the browser is actually the target URL that we passed in our .feature file and is https://specflow.org/. In the test itself, it’s represented as ‘(.*)’ in the Then case.

Summary

Tests might be a short term hell for developers but are a long term investment that will surely yield a nice return and once understand they are not so difficult to implement. So if you want to produce the best software possible be sure to dig into Automated Testing!


Writing Automated UI tests in .NET using Playwright and SpecFlow was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Ivan Stoev


Print Share Comment Cite Upload Translate Updates
APA

Ivan Stoev | Sciencx (2022-01-24T02:57:35+00:00) Writing Automated UI tests in .NET using Playwright and SpecFlow. Retrieved from https://www.scien.cx/2022/01/24/writing-automated-ui-tests-in-net-using-playwright-and-specflow/

MLA
" » Writing Automated UI tests in .NET using Playwright and SpecFlow." Ivan Stoev | Sciencx - Monday January 24, 2022, https://www.scien.cx/2022/01/24/writing-automated-ui-tests-in-net-using-playwright-and-specflow/
HARVARD
Ivan Stoev | Sciencx Monday January 24, 2022 » Writing Automated UI tests in .NET using Playwright and SpecFlow., viewed ,<https://www.scien.cx/2022/01/24/writing-automated-ui-tests-in-net-using-playwright-and-specflow/>
VANCOUVER
Ivan Stoev | Sciencx - » Writing Automated UI tests in .NET using Playwright and SpecFlow. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/01/24/writing-automated-ui-tests-in-net-using-playwright-and-specflow/
CHICAGO
" » Writing Automated UI tests in .NET using Playwright and SpecFlow." Ivan Stoev | Sciencx - Accessed . https://www.scien.cx/2022/01/24/writing-automated-ui-tests-in-net-using-playwright-and-specflow/
IEEE
" » Writing Automated UI tests in .NET using Playwright and SpecFlow." Ivan Stoev | Sciencx [Online]. Available: https://www.scien.cx/2022/01/24/writing-automated-ui-tests-in-net-using-playwright-and-specflow/. [Accessed: ]
rf:citation
» Writing Automated UI tests in .NET using Playwright and SpecFlow | Ivan Stoev | Sciencx | https://www.scien.cx/2022/01/24/writing-automated-ui-tests-in-net-using-playwright-and-specflow/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.