HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Testing whether webapp-generated reports match expected output

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
reportsgeneratedmatchexpectedoutputwebapptestingwhether

Problem

I'm new to coding and have cobbled together some code to run an automated test against a web application. The code works but I have some complexity rule violations (CA1505, CA1502 and CA1506) and would be grateful to anyone with insight on how to go about improving the code.

The code logs into the UI, makes sure the user is on the correct account and the correct data set. Then goes through 4 different report tables and checks the table data against expected outcome files. I have made some changes already suggested which have improved the violation levels slightly but not enough to suppress them...

```
namespace ObservatoryAutomatedFunctional.Tests
{
[TestClass]
public class LineDetailAnalysisTests: IDisposable
{
private IWebDriver driver;
private FirefoxBinary binary;
private string baseUrl = Tests.Urls.BaseUrl;
private string firefoxPath = Tests.DriverSetup.FirefoxPath;
private string driverPath = Tests.DriverSetup.DriverPath;
private string actualData = Tests.OutcomeFileLocations.ActualOutcome + "Analyse_SpendBySubjective_Act.csv";
private string expectedData = Tests.OutcomeFileLocations.ExpectedOutcome + "Analyse_SpendBySubjective_Exp.csv";
private string actualData2 = Tests.OutcomeFileLocations.ActualOutcome + "Analyse_SpendByDirectorate_Act.csv";
private string expectedData2 = Tests.OutcomeFileLocations.ExpectedOutcome + "Analyse_SpendByDirectorate_Exp.csv";
private string actualData3 = Tests.OutcomeFileLocations.ActualOutcome + "Analyse_SpendByCostCentre_Act.csv";
private string expectedData3 = Tests.OutcomeFileLocations.ExpectedOutcome + "Analyse_SpendByCostCentre_Exp.csv";
private string actualData4 = Tests.OutcomeFileLocations.ActualOutcome + "Analyse_SpendByDepartment_Act.csv";
private string expectedData4 = Tests.OutcomeFileLocations.ExpectedOutcome + "Analyse_SpendByDepartment_Exp.csv";

[TestInitialize()]
//Set the browswer from a build
public void MyTestInitialize()
{

Solution

I see you're using Selenium automated testing, so I would recommend reviewing Selenium best practices.

There's several problems with the code you showed, most of which stem from it being essentially one long method. I would suggest refactoring, with the following objectives:

  • It is a standard approach to create your own automation framework tailored to the website you're testing.



  • Use Page Object pattern for modelling the UI.



  • Split the code into smaller modules with small responsibilities (e.g. finding a specific control, clicking a specific button etc) - having these as small building blocks would enable you to write (a) a more readable test and (b) more tests in the future (by reusing the framwork).



  • Consider implementing it as a fluent API to make the tests more readable by people not intimately familiar with the website.



The end results of a well-done framework are tests that look like this (simplifying somewhat):

LoginPage.WithUsername("foo").WithPassword("bar").Login();
MainPage.NavigateTo(SitePage.Reports);
ReportsPage.SpecifyDate("2013-04-17").ThenGenerate();
Assert.That(ReportsPage.TheReport.RowCount, Is.EqualTo(50));
////etc.

Code Snippets

LoginPage.WithUsername("foo").WithPassword("bar").Login();
MainPage.NavigateTo(SitePage.Reports);
ReportsPage.SpecifyDate("2013-04-17").ThenGenerate();
Assert.That(ReportsPage.TheReport.RowCount, Is.EqualTo(50));
////etc.

Context

StackExchange Code Review Q#146377, answer score: 3

Revisions (0)

No revisions yet.