2013-09-26 19 views
1

我試圖閱讀使用量角器和摩卡的網站上的所有HREF鏈接使用protractorjs和mocha如何閱讀網站上的所有鏈接?

我不是「已婚」的任何技術,但我的印象是,這些是目前最好的技術用於驅動硒。

我有與我從示例代碼調整讀取項目來到量角器摩卡示例文件的工作:這似乎是發生

before(function() { 
    driver = new webdriver.Builder(). 
    usingServer('http://localhost:4444/wd/hub'). 
    withCapabilities(webdriver.Capabilities.chrome()).build(); 
    driver.manage().timeouts().setScriptTimeout(10000); 
    ptor = protractor.wrapDriver(driver); 
}); 

function Log(obj){ 
    console.log(JSON.stringify(obj)); 
} 

it.only('should read all HREFS', function(done){ 
    ptor.get('http://www.angularjs.org'); 

    var elements = ptor.findElements(protractor.By.tagName('a')); 
    Log(protractor.By.tagName('a')); 
    // {"using":"tag name","value":"a"} 
    Log(elements); 
    // Result: {} 
    // Expected: a full list of every 'a' tag element on the angularjs homepage 

}); 

是「元素」列表返回立即而不是頁面加載後。

我該如何處理硒+量角器?

回答

0

好的 - 我發現Async讓我有些困擾。

我已經調整了代碼,使這做我以上要求。

it.only('should read all HREFS', function(done){ 
    ptor.get('http://www.angularjs.org'); 

    var elements = ptor.findElements(protractor.By.tagName('a')); 
    Log(protractor.By.tagName('a')); 
    Log(elements); 

    var a = driver.findElements(webdriver.By.tagName('a')); 
    for (var element in a) { 
     Log(element); 
    } 

// NOTE *********************************** 
// this here is the "answer" the asynchonous nature of javascript means that I 
// do not have have access to the contents of the request until I am inside the "then" 
// response 
// 
// from there I have to use the same structure when executing the getAttribute method 
    a.then(function(elements){ 

     for (var i = 0; i < elements.length; i++){ 
      var link = elements[i]; 
      link.getAttribute('href') 
       .then(function(value){ 
        Log(value); 
       }); 
     } 

     for (e in elements){ 
      var link = elements[e]; 
      Log(link.getTagName()); 
     // I left this in my debugging code for the stackoverflow reader who might 
     // want to know what other functions they can execute on the LINK object 
      for (attribute in link) { 
       Log(attribute); 
       // "then" 
       // "cancel" 
       // "isPending" 
       // "errback" 
       // "driver_" 
       // "id_" 
       // "constructor" 
       // "getDriver" 
       // "toWireValue" 
       // "schedule_" 
       // "findElement" 
       // "isElementPresent" 
       // "findElements" 
       // "click" 
       // "sendKeys" 
       // "getTagName" 
       // "getCssValue" 
       // "getAttribute" 
       // "getText" 
       // "getSize" 
       // "getLocation" 
       // "isEnabled" 
       // "isSelected" 
       // "submit" 
       // "clear" 
       // "isDisplayed" 
       // "getOuterHtml" 
       // "getInnerHtml" 
       // "addCallback" 
       // "addErrback" 
       // "addBoth" 
       // "addCallbacks" 
      } 
     }; 
    }); 
    }); 
相關問題