相关疑难解决方法(0)

如何使用赛普拉斯检查可能不存在的元素

我正在写一个赛普拉斯测试来登录一个网站.有用户名和密码字段以及提交按钮.大多数登录都很简单,但有时会出现一个必须被解除的警告对话框.

我试过这个:

    cy.get('#login-username').type('username');
    cy.get('#login-password').type(`password{enter}`);

    // Check for a possible warning dialog and dismiss it
    if (cy.get('.warning')) {
        cy.get('#warn-dialog-submit').click();
    }
Run Code Online (Sandbox Code Playgroud)

哪个工作正常,但如果没有出现警告则测试失败:

CypressError: Timed out retrying: Expected to find element: '.warning', but never found it.
Run Code Online (Sandbox Code Playgroud)

然后我尝试了这个,因为警告不够快,所以赛普拉斯.$没有找到任何东西:

    cy.get('#login-username').type('username');
    cy.get('#login-password').type(`password{enter}`);

    // Check for a possible warning dialog and dismiss it
    if (Cypress.$('.warning').length > 0) {
        cy.get('#warn-dialog-submit').click();
    }
Run Code Online (Sandbox Code Playgroud)

检查元素是否存在的正确方法是什么?我需要像cy.get这样的东西,如果找不到元素就不会抱怨.

automated-tests cypress

12
推荐指数
3
解决办法
2万
查看次数

如何等待dojo的元素存在?

在dojo中,有没有办法在创建某个类(或包含某些文本)的元素时收到通知?

这里提出的jQuery 几乎完全相同.但是我想知道dojo是否有类似的解决方案.谢谢!

javascript dojo

7
推荐指数
1
解决办法
1128
查看次数

如何等待元素存在于 JavaScript 中?

我正在使用一个proxy object检测对象值更改然后通过 AJAX 加载新内容的方法,我使用一个setInterval函数来等待 AJAX 请求中出现的元素存在,然后执行一段代码。我这样做是因为我的情况需要它。我做了一个简短的片段示例:

var handler = {
    makeThings: 0,
    otherStuff: 0
};
var globalHandler = new Proxy(handler, {
    set: function(obj, prop, value) {
        obj[prop] = value
        if (prop == "makeThings") {
            var clearTimeSearchProxy = setInterval(function() {
                if ($("p").length) {
                    console.log("The element finally exist and we execute code");
                    clearTimeout(clearTimeSearchProxy);
                }
            }, 100);
        }
        return true;
    }
});

$(document).ready(function() {
    $("button").on("click", function() {
        globalHandler.makeThings = 1;
        //This element comes with ajax but I use a …
Run Code Online (Sandbox Code Playgroud)

javascript jquery promise ecmascript-6 es6-promise

2
推荐指数
2
解决办法
1万
查看次数

MutationObserver错误:我怎么能等待元素的创建?

我正在尝试制作Greasemonkey脚本以在Facebook页面中添加按钮.但我需要等待创建一个元素来添加按钮.
我正在尝试使用MutationObserver(如此处所示)来执行操作,但存在错误.

function init() {
    console.log('Launched : ' + launched);
    if (!launched) {
        launched = true;
        main();
    }
}
console.log('barfoo');

var observer = new MutationObserver(function(mutations) {
    mutations.forEach(function(mutation) {
        if (!mutation.addedNodes) return;
        console.log(mutation.addedNodes.type());
        for (var i = 0; i < mutation.addedNodes.length; i++) {
            if (mutation.addedNodes[i].matches('div[class="pam _5shk uiBoxWhite bottomborder"]')) {
                console.log('init');
                init();
            }
        }
    })
});
console.log('bar');

try {
    observer.observe(document.body, {
        childlist: true,
        subtree: true,
        attributes: false,
        characterData: false,
    });
} catch (error) {
    console.log(error);
} …
Run Code Online (Sandbox Code Playgroud)

javascript firefox greasemonkey mutation-observers

0
推荐指数
1
解决办法
4766
查看次数