0

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]).

captainsac
  • 2,484
  • 3
  • 27
  • 48
Suk
  • 31
  • 4
  • Please show your HTML code and describe what are you trying to achieve. There could be a more simple approach. – alecxe Jun 02 '15 at 05:20
  • Hi alexce, Thanks for your reply. I added some comments in my code, which may help you to understand my struggle. I was able to move from non-angular pages to angular pages. My main aim is to compare getText(), form different pages and then perform the click operation. If i use the condition if(menu == 'String1'), it working fine , but when i try if(menu == pageName[menuIndex]) it dosent – Suk Jun 02 '15 at 06:01

1 Answers1

1

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.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195