简体   繁体   English

使用if-else语句声明

[英]assertExists with if-else statement

I use this code: 我使用以下代码:

var x = require('casper').selectXPath;
...
casper.waitForSelector(x("//a[contains(@id,'cell_13_1')]"), function() {             
    this.test.assertExists(x("//a[contains(@id,'cell_13_1')]"), 'Clickable');
    this.click(x("//a[contains(@id,'cell_13_1')]"));
});

I am trying to use if-else with assertExists to click another element if the first is not there: 我试图将if-elseassertExists一起使用, if-else第一个不存在,请单击另一个元素:

casper.waitForSelector(x("//a[contains(@id,'cell_13_1')]"), function() {          
    if(this.test.assertExists(x("//a[contains(@id,'cell_13_1')]")==="PASS"){
        this.click(x("//a[contains(@id,'cell_11_1')]"));} 
    else{
        this.click(x("//a[contains(@id,'cell_22_1')]"));
    }         
});

But that does not seem to work. 但这似乎不起作用。 How would one do it correctly? 如何正确地做到这一点?

That's exactly what casper.exists() is for. 这正是casper.exists()目的。 You can also explicitly pass or fail some things: 您还可以显式地通过或失败某些事情:

casper.waitForSelector(x("//a[contains(@id,'cell_13_1')]"), function() {          
    if(this.exists(x("//a[contains(@id,'cell_13_1')]")){
        this.test.pass("Clickable");
        this.click(x("//a[contains(@id,'cell_11_1')]"));
    } else {
        this.test.fail("Clickable");
        //this.click(x("//a[contains(@id,'cell_22_1')]"));
    }       
});

This code is equivalent to your first snippet. 此代码等效于您的第一个代码段。 Comment the fail() call and uncomment the last click in order to get your "intended" behavior. 注释一下fail()调用,并取消对最后单击的注释,以获取“预期”行为。

Btw, it doesn't make sense to fail some assertion and still continue with the script. 顺便说一句,失败一些断言并继续执行脚本是没有意义的。 You have to think about what exactly you want to test and what part of your script is supposed to be navigation to the component under test. 您必须考虑要精确地测试什么,以及脚本的哪一部分应该导航到被测组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM