|
| 1 | +import*asfsfrom'fs'; |
| 2 | +import*aspathfrom'path'; |
| 3 | +import{expect}from'chai'; |
| 4 | +import{generateProjectSync}from'./util/yeoman'; |
| 5 | +import{AspNetProcess,AspNetCoreEnviroment,defaultUrl,publishProjectSync}from'./util/aspnet'; |
| 6 | +import{getValue,getCssPropertyValue}from'./util/webdriverio'; |
| 7 | + |
| 8 | +// First, generate a new project using the locally-built generator-aspnetcore-spa |
| 9 | +// Do this outside the Mocha fixture, otherwise Mocha will time out |
| 10 | +constappDir=path.resolve(__dirname,'../generated/angular'); |
| 11 | +constpublishedAppDir=path.resolve(appDir,'./bin/Release/published'); |
| 12 | +if(!process.env.SKIP_PROJECT_GENERATION){ |
| 13 | +generateProjectSync(appDir,{framework: 'angular-2',name: 'Test App',tests: false}); |
| 14 | +publishProjectSync(appDir,publishedAppDir); |
| 15 | +} |
| 16 | + |
| 17 | +functiontestBasicNavigation(){ |
| 18 | +describe('Basic navigation',()=>{ |
| 19 | +beforeEach(()=>browser.url(defaultUrl)); |
| 20 | + |
| 21 | +it('should initially display the home page',()=>{ |
| 22 | +expect(browser.getText('h1')).to.eq('Hello, world!'); |
| 23 | +expect(browser.getText('li a[href="https://angular.io/"]')).to.eq('Angular 2'); |
| 24 | +}); |
| 25 | + |
| 26 | +it('should be able to show the counter page',()=>{ |
| 27 | +browser.click('a[href="/counter"]'); |
| 28 | +expect(browser.getText('h1')).to.eq('Counter'); |
| 29 | + |
| 30 | +// Test clicking the 'increment' button |
| 31 | +expect(browser.getText('counter strong')).to.eq('0'); |
| 32 | +browser.click('counter button'); |
| 33 | +expect(browser.getText('counter strong')).to.eq('1'); |
| 34 | +}); |
| 35 | + |
| 36 | +it('should be able to show the fetchdata page',()=>{ |
| 37 | +browser.click('a[href="/fetch-data"]'); |
| 38 | +expect(browser.getText('h1')).to.eq('Weather forecast'); |
| 39 | + |
| 40 | +browser.waitForExist('fetchdata table'); |
| 41 | +expect(getValue(browser.elements('fetchdata table tbody tr')).length).to.eq(5); |
| 42 | +}); |
| 43 | +}); |
| 44 | +} |
| 45 | + |
| 46 | +functiontestHotModuleReplacement(){ |
| 47 | +describe('Hot module replacement',()=>{ |
| 48 | +beforeEach(()=>browser.url(defaultUrl)); |
| 49 | + |
| 50 | +it('should update when HTML is changed',()=>{ |
| 51 | +expect(browser.getText('h1')).to.eq('Hello, world!'); |
| 52 | + |
| 53 | +constfilePath=path.resolve(appDir,'./ClientApp/app/components/home/home.component.html'); |
| 54 | +constorigFileContents=fs.readFileSync(filePath,'utf8'); |
| 55 | + |
| 56 | +try{ |
| 57 | +constnewFileContents=origFileContents.replace('<h1>Hello, world!</h1>','<h1>HMR is working</h1>'); |
| 58 | +fs.writeFileSync(filePath,newFileContents,{encoding: 'utf8'}); |
| 59 | + |
| 60 | +browser.waitUntil(()=>browser.getText('h1').toString()==='HMR is working'); |
| 61 | +}finally{ |
| 62 | +// Restore old contents so that other tests don't have to account for this |
| 63 | +fs.writeFileSync(filePath,origFileContents,{encoding: 'utf8'}); |
| 64 | +} |
| 65 | +}); |
| 66 | + |
| 67 | +it('should update when CSS is changed',()=>{ |
| 68 | +expect(getCssPropertyValue(browser,'li.link-active a','color')).to.eq('rgba(255,255,255,1)'); |
| 69 | + |
| 70 | +constfilePath=path.resolve(appDir,'./ClientApp/app/components/navmenu/navmenu.component.css'); |
| 71 | +constorigFileContents=fs.readFileSync(filePath,'utf8'); |
| 72 | + |
| 73 | +try{ |
| 74 | +constnewFileContents=origFileContents.replace('color: white;','color: purple;'); |
| 75 | +fs.writeFileSync(filePath,newFileContents,{encoding: 'utf8'}); |
| 76 | + |
| 77 | +browser.waitUntil(()=>getCssPropertyValue(browser,'li.link-active a','color')==='rgba(128,0,128,1)'); |
| 78 | +}finally{ |
| 79 | +// Restore old contents so that other tests don't have to account for this |
| 80 | +fs.writeFileSync(filePath,origFileContents,{encoding: 'utf8'}); |
| 81 | +} |
| 82 | +}); |
| 83 | +}); |
| 84 | +} |
| 85 | + |
| 86 | +// Now launch dotnet and use selenium to perform tests |
| 87 | +describe('Angular template: dev mode',()=>{ |
| 88 | +AspNetProcess.RunInMochaContext(appDir,AspNetCoreEnviroment.development); |
| 89 | +testBasicNavigation(); |
| 90 | +testHotModuleReplacement(); |
| 91 | +}); |
| 92 | + |
| 93 | +describe('Angular template: production mode',()=>{ |
| 94 | +AspNetProcess.RunInMochaContext(publishedAppDir,AspNetCoreEnviroment.production,'angular.dll'); |
| 95 | +testBasicNavigation(); |
| 96 | +}); |
0 commit comments