CodeceptJS / Puppeteer не распознает оператор «если» - PullRequest
0 голосов
/ 19 октября 2019

не очень знаком с js / node.js. использование codeceptjs / puppeteer для некоторых автоматических тестов. сейчас пытаюсь отредактировать описание в тесте. но иногда описания там нет - для этого нет кнопки «редактировать описание» - вместо этого есть кнопка «добавить описание». поэтому я написал заявление, которое указывает. но код просто переворачивает его. не имеет значения, что это за утверждение, оно просто переходит на следующую строку. в настоящее время if (desc) и if(!desc) выполняют одну и ту же функцию - она ​​переходит к следующей строке в операторе if. это вызывает ошибку, потому что описание уже есть, и поэтому кнопка «добавить описание» недоступна. Я не уверен, что происходит.

Scenario('test something', (I, userLoginPage, FBPagePage) => {


    userLoginPage.validate();


    I.click('//*[@id="card_611"]');
       // clicks the card

    var desc = '//*[@id="show_card_description"]/section/button';
    // add description button
    // tried 'Add description' but the result was the same

    if (desc){
     // this is where the error happens. it simply looks for the add description button
     // no matter what button is there it decides to click the 'add description' button

        I.click('//*[@id="show_card_description"]/section/button');
    // click add desc button

        I.fillField('//*[@id="description_editor_container"]/div[2]/div[1]',
        'not admin user created this description thanks to automated testing');
    // types this stuff 
        I.click('//*[@id="description_editor_container"]/button[1]');
    // saves it 
        I.wait(1);
}

    I.click('//*[@id="show_card_description"]/section/h5/a');
    // click edit desc button if a description already exists
    I.fillField('//*[@id="description_editor_container"]/div[2]/div[1]', 'not admin user edited this description thanks to automated testing');
    I.click('//*[@id="description_editor_container"]/button[1]');


I.say('success!')
});

1 Ответ

1 голос
/ 21 октября 2019

Чтобы дать вам правильный контекст: вы задаете вопрос Node.js, а не CodeceptJS или Puppeteer.

desc всегда true, потому что вы объявляете его как строку, поэтомунезависимо от того, какой код внутри if будет запущен, как вы уже узнали.

Вы можете использовать что-то вроде:

const numOfElements = await I.grabNumberOfVisibleElements('#show_card_description section button'); // Use CSS locator instead of Xpath for easier readability

console.log(numOfElements); // Debug

if(numOfElements === 1) {
   …
}

См. также https://codecept.io/helpers/Puppeteer#grabnumberofvisibleelements

...