使用量角器js和mocha如何阅读网站上的所有链接?

时间:2013-09-26 17:00:42

标签: unit-testing selenium mocha protractor

我正在尝试使用量角器和mocha

读取网站上的所有HREF链接

我并不与任何技术“结婚”,但我认为这些是目前用于驱动硒的最佳技术。

我正在使用项目附带的量角器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

});

似乎正在发生的事情是“元素”列表立即返回而不是在页面加载后返回。

我如何处理硒+量角器?

1 个答案:

答案 0 :(得分: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"
            }
        };
    });
  });