2013-09-26 1 views
1

Я пытаюсь прочитать все HREF ссылки на сайте с помощью транспортира и моккоИспользование protractorjs и мокко, как я могу прочитать все ссылки на сайте?

Я не «брак» на любой из технологий, но я нахожусь под впечатлением, что они в настоящее время являются лучшими в своем классе технологии для вождения селена.

Я работаю с транспортиром Мокко пример файлом, который пришел с проектом, который я настроенный из примера кода для чтения:

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" 
      } 
     }; 
    }); 
    }); 
Смежные вопросы