Top Banner
COPYRIGHT, TECHTALK - WWW.TECHTALK.AT Lessons Learned Web Application Testing in .NET GÁSPÁR NAGY [email protected] http://gasparnagy.com NDC Oslo, 6/6/2014
29

Lessons Learned Web Application Testing ingasparnagy.com/media/resources/ndc2014/20140606... · . 3 Is UI automation bad? costly (implementation, maintenance) senseless (not testing

May 02, 2018

Download

Documents

dinhhanh
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lessons Learned Web Application Testing ingasparnagy.com/media/resources/ndc2014/20140606... · . 3 Is UI automation bad? costly (implementation, maintenance) senseless (not testing

COPYRIGHT, TECHTALK - WWW.TECHTALK.AT

Lessons Learned

Web Application Testing in .NETGÁSPÁR [email protected]

http://gasparnagy.com

NDC Oslo, 6/6/2014

Page 2: Lessons Learned Web Application Testing ingasparnagy.com/media/resources/ndc2014/20140606... · . 3 Is UI automation bad? costly (implementation, maintenance) senseless (not testing

COPYRIGHT, TECHTALK - WWW.TECHTALK.AT

http://specflow.org/plus

Page 3: Lessons Learned Web Application Testing ingasparnagy.com/media/resources/ndc2014/20140606... · . 3 Is UI automation bad? costly (implementation, maintenance) senseless (not testing

3

Is UI automation bad?

costly(implementation, maintenance)

senseless(not testing app behavior)

Page 4: Lessons Learned Web Application Testing ingasparnagy.com/media/resources/ndc2014/20140606... · . 3 Is UI automation bad? costly (implementation, maintenance) senseless (not testing

4

Google Trends for web automation technologies

WebDriverWatir+WatinSelenium WebHtmlUnitPhantomJS

Source: http://bit.ly/1jacnQ4

Page 5: Lessons Learned Web Application Testing ingasparnagy.com/media/resources/ndc2014/20140606... · . 3 Is UI automation bad? costly (implementation, maintenance) senseless (not testing

5

Moving target

•Web technology is changing• HTML5/CSS3, REST, JSON, jQuery, WebSocket, etc.

•Web applications are changing• responsive, SPA, js-heavy, offline, desktop-like UX, touch, etc.

•Web automation is changing• WebDriver W3C specification, browser as IDE, headless

browsers, cloud testing

Page 6: Lessons Learned Web Application Testing ingasparnagy.com/media/resources/ndc2014/20140606... · . 3 Is UI automation bad? costly (implementation, maintenance) senseless (not testing

6

Web Automation in .NET

Web application testing principles should not depend on the technology you use…

But it does.

The current version of ASP.NET is tightly coupled to IIS.

ASP.NET vNext – we wait for you desperately!

Page 7: Lessons Learned Web Application Testing ingasparnagy.com/media/resources/ndc2014/20140606... · . 3 Is UI automation bad? costly (implementation, maintenance) senseless (not testing

7

Goals for today

Scope: ASP.NET MVC “classic” business apps with automated functional tests (BDD/ATDD)

1. Discover possibilities of doing test-first web development

2. See options how to ease the pain caused by the out-proc testing nature of ASP.NET MVC web testing

3. Enumerate useful tools & resources

Page 8: Lessons Learned Web Application Testing ingasparnagy.com/media/resources/ndc2014/20140606... · . 3 Is UI automation bad? costly (implementation, maintenance) senseless (not testing

COPYRIGHT, TECHTALK - WWW.TECHTALK.AT

Test-first web development

Page 9: Lessons Learned Web Application Testing ingasparnagy.com/media/resources/ndc2014/20140606... · . 3 Is UI automation bad? costly (implementation, maintenance) senseless (not testing

9

Test-first development

Failing

end-to-end Tests

Passing

end-to-end Tests

RefactorDeploy

Page 10: Lessons Learned Web Application Testing ingasparnagy.com/media/resources/ndc2014/20140606... · . 3 Is UI automation bad? costly (implementation, maintenance) senseless (not testing

10

Test-first web development

•Outside-in approach• You model & design your application based on the actual required

outcome

• Ensures clean and consistent domain model• In modern web applications, HTML is more part of the model

than the presentation (which is in CSS/js)

• Ensures fast and stable browser automation from the beginning• Better than fixing it later with a huge automation code base

Page 11: Lessons Learned Web Application Testing ingasparnagy.com/media/resources/ndc2014/20140606... · . 3 Is UI automation bad? costly (implementation, maintenance) senseless (not testing

11

Demo: Test-first web development

Controller-level

intergrationtesting

Pure Selenium

WebDriver

with FireFox

Coypu

SpecsFor.MVC

Page Objects

Page 12: Lessons Learned Web Application Testing ingasparnagy.com/media/resources/ndc2014/20140606... · . 3 Is UI automation bad? costly (implementation, maintenance) senseless (not testing

12

Demo: Test-first web development

Controller-level

intergrationtesting

Pure Selenium

WebDriver

with FireFox

Coypu

SpecsFor.MVC

Page Objects

driver.Navigate().GoToUrl("http://localhost/Home/Index");

↓app.NavigateTo<HomeController>(c => c.Index());

Page 13: Lessons Learned Web Application Testing ingasparnagy.com/media/resources/ndc2014/20140606... · . 3 Is UI automation bad? costly (implementation, maintenance) senseless (not testing

13

Expressing tests – Display caseController-level SpecsFor.MVC Pure WebDriver

var controller = new HomeController();

var app = new MvcWebApp();

var driver = new FirefoxDriver();

var result = controller.Index();

app.NavigateTo<HomeController>(c =>c.Index());

driver.Navigate().GoToUrl("http:...");

var list = app.FindDisplayFor<HomePageModel>().DisplayFor(m => m.Questions);

var list = driver

.FindElement(By

.Id("questions"));

var questions = list

.FindElements(By

.TagName("li"));

var questions = list

.FindElements(By

.TagName("li"));

Assert.AreEqual(3,result.Model.Count);

Assert.AreEqual(3,questions.Count);

Assert.AreEqual(3,questions.Count);

Arra

nge

Act

Asse

rt

Page 14: Lessons Learned Web Application Testing ingasparnagy.com/media/resources/ndc2014/20140606... · . 3 Is UI automation bad? costly (implementation, maintenance) senseless (not testing

14

Controller-level SpecsFor.MVC Pure WebDriver

... ... ...

app.NavigateTo<HomeController>(c =>c.Ask());

driver.Navigate().GoToUrl("http:...");

var result = controller.Ask(new QuestionModel { Title = "foo" });

app.FindFormFor<QuestionModel>()

.Field(qm => qm.Title).SetValueTo("foo")

driver.FindElement(By.Id("Title"))

.SendKeys("foo");

.Submit(); driver.FindElement(By.Id("askform"))

.Submit();

// "follow redirect”result = controller.Index();

// check model // grab question list from page and check

Arra

nge

Act

Asse

rtExpressing tests – Edit case

Page 15: Lessons Learned Web Application Testing ingasparnagy.com/media/resources/ndc2014/20140606... · . 3 Is UI automation bad? costly (implementation, maintenance) senseless (not testing

15

Controller-level SpecsFor.MVC Pure WebDriver

... ... ...

app.NavigateTo<HomeController>(...

driver.Navigate().GoToUrl("http:...");

var q = new QuestionModel{ Title = "" });

//TODO: model validationvar result = controller.Ask(q);

app.FindFormFor<QuestionModel>()

.Field(qm => qm.Title).SetValueTo("")

.Submit();

driver.FindElement(By.Id("Title"))

.SendKeys("");driver.FindElement(By

.Id("askform")).Submit();

var errors = result.

ViewData.ModelState

["Title"].Errors;

Assert.Greater(0,

errors.Count);

app.FindFormFor<QuestionModel>()

.Field(qm => qm.Title).ShouldBeInvalid();

Assert.IsNotNull(driver.FindElements(By.CssSelector("span.field-

validation-error[data-valmsg-for=Title]")).SingleOrDefault);

Arra

nge

Act

Asse

rtExpressing tests – Validation case

Page 16: Lessons Learned Web Application Testing ingasparnagy.com/media/resources/ndc2014/20140606... · . 3 Is UI automation bad? costly (implementation, maintenance) senseless (not testing

16

Expressing tests – A subjective comparison

Controller-level SpecsFor.MVC Pure WebDriver

Dev. order Test – Controller/BL – View Test – (Controller/BL | View) Test – View – Controller/BL

Good design ♥ (not driven by outcomes) ♥ ♥ ♥ (driven by domain) ♥ ♥ (mixed domain)

Clean test ♥ ♥ ♥ (except infr. hack) ♥ ♥ (needs switching to WD) ♥ (selectors as string)

Clean HTML ♥ (no design help) ♥ ♥ ♥ (~stays with domain) ♥ ♥ (mixed domain)

Maturity ♥ ♥ ♥ (~your code) ♥ (3rd party framework) ♥ ♥ (W3C-driven framework)

8 ♥ 9 ♥ 7 ♥

Page 17: Lessons Learned Web Application Testing ingasparnagy.com/media/resources/ndc2014/20140606... · . 3 Is UI automation bad? costly (implementation, maintenance) senseless (not testing

COPYRIGHT, TECHTALK - WWW.TECHTALK.AT

Out-proc vs. in-proc testing

Page 18: Lessons Learned Web Application Testing ingasparnagy.com/media/resources/ndc2014/20140606... · . 3 Is UI automation bad? costly (implementation, maintenance) senseless (not testing

18

Is mocking bad?

• The automation trade-off also appears at the component level• Some components cannot be tested without mocks/stubs• E.g: e-mail sending, time, authentication, etc.

• Mock/stub components can improve the efficiency

•Demo: Let’s try to use in-memory database access to our tests!

Page 19: Lessons Learned Web Application Testing ingasparnagy.com/media/resources/ndc2014/20140606... · . 3 Is UI automation bad? costly (implementation, maintenance) senseless (not testing

19

Test Process

Testing through controller in-proc

ApplicationTests

DB

Page 20: Lessons Learned Web Application Testing ingasparnagy.com/media/resources/ndc2014/20140606... · . 3 Is UI automation bad? costly (implementation, maintenance) senseless (not testing

20

Browser

IIS

Testing through browser out-proc

Test Process

ApplicationTests

DB

Page 21: Lessons Learned Web Application Testing ingasparnagy.com/media/resources/ndc2014/20140606... · . 3 Is UI automation bad? costly (implementation, maintenance) senseless (not testing

21

Test Process

Testing through browser in-proc (wish)

ApplicationTests

DBBrowser

headless

Possible for:• WebAPI• NancyFX• vNext

Still a wish:• ASP.NET MVC• WebAPI used in

a web project• ASP.NET

WebForms

Page 22: Lessons Learned Web Application Testing ingasparnagy.com/media/resources/ndc2014/20140606... · . 3 Is UI automation bad? costly (implementation, maintenance) senseless (not testing

22

IIS

NUnit inside IIS

ApplicationTests

DBBrowser

Test Process

Test

Co

ntr

olle

r

Page 23: Lessons Learned Web Application Testing ingasparnagy.com/media/resources/ndc2014/20140606... · . 3 Is UI automation bad? costly (implementation, maintenance) senseless (not testing

23

Test Process

IISTest Process IIS-Like AppDomain

Testing through browser in-proc with “MvcIntegrationTesting” (Steven Sanderson)

ApplicationTests

DBBrowser

Test AppDomain

Test

Co

ntr

olle

r

ASP.NET

remoting

Page 24: Lessons Learned Web Application Testing ingasparnagy.com/media/resources/ndc2014/20140606... · . 3 Is UI automation bad? costly (implementation, maintenance) senseless (not testing

24

SpecRun Process

SR AppDomain IIS-Like Test AppDomain

Testing through browser in-proc with SpecFlow+ Web (alpha)

ApplicationTests

DBBrowser

Spec

Ru

n

Test

Co

ntr

olle

r

ASP.NET

Page 25: Lessons Learned Web Application Testing ingasparnagy.com/media/resources/ndc2014/20140606... · . 3 Is UI automation bad? costly (implementation, maintenance) senseless (not testing

25

Out-proc vs. in-proc testing

• Subbing and mocking are important tools to fight for better efficiency• The out-proc nature of ASP.NET web testing makes

this hard• Frameworks can help to host your tests in the same

AppDomain• If this is not possible, you can implement backdoor interfaces

to your application (e.g. WebAPI/REST)• OWIN improves this, but for MVC only in vNext

Page 26: Lessons Learned Web Application Testing ingasparnagy.com/media/resources/ndc2014/20140606... · . 3 Is UI automation bad? costly (implementation, maintenance) senseless (not testing

26

Our journey…

SpecFlow+ Web

Coypu

SpecsFor.MVCPage Objects

Selenium WebDriver

NUnit inside IIS

MvcIntegrationTest

Page 27: Lessons Learned Web Application Testing ingasparnagy.com/media/resources/ndc2014/20140606... · . 3 Is UI automation bad? costly (implementation, maintenance) senseless (not testing

27

Conclusions

• Reevaluate web testing strategy, as the circumstances rapidly changing

•Define a testing strategy based on test-first

• Be the master of the tools – you need to carefully pick and combine them to get the best result

Page 28: Lessons Learned Web Application Testing ingasparnagy.com/media/resources/ndc2014/20140606... · . 3 Is UI automation bad? costly (implementation, maintenance) senseless (not testing

COPYRIGHT, TECHTALK - WWW.TECHTALK.AT

Questions?

GÁSPÁR [email protected]

http://gasparnagy.com

Resources: http://bit.ly/ndc2014gn

• WebDriver W3C Draft• Coypu• Selenium Page Object Framework• SpecsFor.MVC (good intro post)• Phantomjs (NuGet package)• SimpleBrowser, SimpleBrowser.WebDriver• MvcIntegrationTestFramework (Steven

Sanderson)• FakeHost – recent fork

• ASP.NET vNext• SpecFlow+ Web

Page 29: Lessons Learned Web Application Testing ingasparnagy.com/media/resources/ndc2014/20140606... · . 3 Is UI automation bad? costly (implementation, maintenance) senseless (not testing

COPYRIGHT, TECHTALK - WWW.TECHTALK.AT

GÁSPÁR NAGY

NDC Oslo, 6/6/2014

Thank you!

[email protected]

http://gasparnagy.com

twitter: @gasparnagy