nightwatchjs元标记检查是否为空

时间:2017-09-11 08:32:07

标签: javascript nightwatch.js e2e-testing

我想检查描述元标记是否有任何值但不确定如何操作。

试过这个但没有运气:

browser.verify.element("meta[name=description]").attribute('content').not.equals('');

任何帮助表示感谢,谢谢!

1 个答案:

答案 0 :(得分:0)

使用Selenium protocol "element"

browser.element('css selector', '#advanced-search', function(result){
    if(result.status != -1){
        //Element exists, do something
    } else{
        //Element does not exist, do something else
    }
});

简单的Javascript解决方案:



if(document.querySelector("meta[name=description]")){

  if(document.querySelector("meta[name=description]").getAttribute("content") === "Description of the webpage")

    console.log("meta description with content 'Description of the webpage' found");
    // do something

}
else{

  console.log("meta description not found");
  // do something

}

<meta name="description" content="Description of the webpage"/>
&#13;
&#13;
&#13;

来源:Nightwatchjs: how to check if element exists without creating an error/failure/exceptionHow to check for existing meta tag before generating a new one with javascript (or jquery)?