简体   繁体   中英

Cannot compare element.getText() values in if statement - in Protractor automation

I am new to protractor, and I am struggling to compare the values in if condition, I am not sure what I am missing. Please check my spec and let me know what I need to do.

Spec.js:

Non- Angular Page: Click the elements and getText() into an array

     browser.driver.findElements(by.css('classname')).then(function (list) {
     pageCounts = list.length;
     for (var pageIndex = 0; pageIndex < pageCounts; pageIndex++) 
     {
         pageName[pageIndex] = list[pageIndex].getText();
         list[pageIndex].click();
     }

Angular Page : Check for the title

if (pageCounts > 1) {
    element(by.xpath('')).getText().then(function (title) 
    {
         expect(title).toBe('Accounts Created!');

Non- Angular page :Once the the title is matched, it moves to another non - angular page and compare the all the getText() values in the above code with all the elements in the menu, and click the exact match

browser.driver.findElements(by.xpath('xpath')).then(function (totalPages) {
    for (var menulength = 1; menulength < totalPages.length; menulength++){
        menuPath[menulength] = browser.driver.findElement(by.xpath('xpath'));           
        menuPath[menulength].getText().then(function (menu) {
            for (var menuIndex = 0; menuIndex < pageCounts; menuIndex++) {

                if (menu === pageName[menuIndex]) {
                    browser.driver.findElement(by.xpath('')).click();                                            
                }
                else 
                {
                    browser.driver.findElement(by.xpath('')).click();
                }
            }
        }
    });

It never goes into the loop if (menu === pageName[menuIndex]) .

You can simplify the code by using each() function on an ElementArrayFinder :

var pages = element.all(by.css(".classname"));
var totalPages = element.all(by.xpath("xpath"));

pages.each(function (page) {
    totalPages.each(function (totalPage) {
        page.getText().then(function (pageText) {
            totalPage.getText().then(function (totalPageText) {
                if (pageText === totalPageText) {
                    element(by.xpath('xpath1')).click();
                } else {
                    element(by.xpath('xpath2')).click();
                }
            });
        })
    });
});

There is also a handy map() function that can be applied here too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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