1

我正在尝试使用量角器和摩卡阅读网站上的所有 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

});

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

我如何在硒+量角器中处理这个问题?

4

1 回答 1

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"
            }
        };
    });
  });
于 2013-09-26T17:56:40.303 回答