Use element by css to check if element exists in Protractor

前端 未结 3 743
一个人的身影
一个人的身影 2020-12-16 09:17

In a protractor end to end test, I want to check if an element exist using element(by.css(...)), my code:

var myElement = element(by.css(\'.elementClass\'));         


        
相关标签:
3条回答
  • 2020-12-16 09:35

    Truthy and falsie refer to values that are evaluated to true and false after being coerced to a boolean unless you want your function to return different types of values.

    var myElement = element(by.css('.elementClass'));
    myElement.isPresent().then(function (elm)
    {
        if (elm)
        {
            console.log("... Element was found")
            expect(myElement.getText()).toBeUndefined();
        } else {
            console.log("... Element was not found")
        }
    });
    
    0 讨论(0)
  • 2020-12-16 09:49

    You need to test if the element is not present:

    expect(element(by.css('.elementClass')).isPresent()).toBe(false);
    
    0 讨论(0)
  • 2020-12-16 09:53

    You can test whether the element is present with isPresent. Here are the protractor docs for the isPresent function.

    So, your code would be something like:

    var myElement = element(by.css('.elementClass'));
    expect(myElement.isPresent()).toBeFalsy();
    
    0 讨论(0)
提交回复
热议问题