javascript - DART - exception in unit testing -


i wrote simple code demonstrate problem unit-testing. have prog.html , prog.dart program has tested. works ok. in same folder have test,html , test,dart represent unit test. here code:

prog.html

<!doctype html> <html>   <head>     <meta charset="utf-8">     <title>prog</title>     <link rel="stylesheet" href="prog.css">   </head>   <body>     <h1>test</h1>     <div id="mydiv">      see me     </div>     <script type="application/dart" src="prog.dart"></script>     <script src="packages/browser/dart.js"></script>   </body> </html> 

prog.dart

import 'dart:html';  var mydiv; void main() {   mydiv=queryselector("#mydiv")   ..text="now don't"; } 

test.html

<!doctype html>  <html>   <head>     <meta charset="utf-8">     <title>test</title>     <link rel="import" href="prog.html">     <script type="application/dart" src="test.dart"></script>     <script src="packages/browser/dart.js"></script>     </head> </html> 

test.dart:

import 'dart:html'; import 'package:unittest/unittest.dart'; import 'package:unittest/html_config.dart'; import 'prog.dart' prog;   main(){   usehtmlconfiguration();   prog.main();   test('test 1', (){     expect(prog.mydiv, haslength(greaterthan(0)), reason:"zero length");   }); } 

i include prog.html test.html using

<link rel="import" href="prog.html"> 

however, when run test.html in dartium, getting

breaking on exception: null object not have setter 'text='.

in

  ..text="now don't"; 

it seems prog.dart not see prog.html, mydiv null.

all files in same root level (web).

what doing wrong?

edited based on günter's suggestions:

i modified html file build dom before running dart code. somehow dart script runs before div appended body (i not see debug alert messages) , still getting null when accessing ..text:

<!doctype html>  <html>   <head>     <meta charset="utf-8">     <title>test</title>     <link rel="import" href="prog.html">     <script>     function getprog (){       var content = document.queryselector('link[rel="import"]').import;       var newbody = content.body;       alert('getting div');       var mydiv = content.getelementbyid("mydiv");        alert(mydiv);       document.body.appendchild(mydiv.clonenode(true));     }     </script>      </head>      <body onload="getprog();">     <!--      <body>          <div id="mydiv">             see me          </div>     -->      </body>     <script type="application/dart" src="test.dart"></script>     <script src="packages/browser/dart.js"></script> </html> 

with actual div in body uncommented runs normally.

edit:

here code works:  <!doctype html>  <html>   <head>     <meta charset="utf-8">     <title>test</title>     <link rel="import" href="prog.html">     <script>     function getprog (){       var content = document.queryselector('link[rel="import"]').import;       var newbody = content.body;       document.body.innerhtml = newbody.innerhtml;       var script1 = document.createelement('script');       script1.type = 'application/dart';       script1.src = 'test.dart';       document.body.appendchild(script1);       var script2 = document.createelement('script');       script2.src = 'packages/browser/dart.js';       document.body.appendchild(script2);     }     </script>   </head>   <body onload="getprog();"></body> </html> 

importing html file doesn't mean content included in page import tag.

http://www.html5rocks.com/en/tutorials/webcomponents/imports/ (see using content).

there several way can access content (for example using script).

edit

i don't think right attempt web app testing.

  • i suggest either https://pub.dartlang.org/packages/webdriver
  • or embed application polymer element can use in test.html file.
  • when build application of smaller polymer elements can test these smaller parts may reusable. testing whole application use webdriver (see first item).

there might better technologies out there testing web apps, didn't use of them myself yet.
use polymer elements easy (easier) test or angular has it's own testing story (also dart unit tests, designed testable beginning).


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -