1

I have 4 container boxes on a page which are identical. I have taken all of them in an array. Now each container box contains some elements which are identical as well. I want to fetch 2nd element from 1st container. Below is what i used to fetch my container and it is working fine:

var pageContent = element(by.css('[class="main-content"]'));
expect(pageContent.all(by.css('.modularBoxContent')).count()).tobe(4);

Now if i want to fetch count of all elements with class as labelText insider modular box container, i am able to do that by using below:

 expect(pageContent.all(by.css('.modularBoxContent')).all(by.css('[class="labelText"]')).count()).tobe(10);

But this is giving me labelText present in all 4 containers. I want to get count of labelText only in first container or get text of first labelText in first container. I tried below code but it is not working and getting error message

TypeError: this.pageContent.get is not a function

expect(pageContent.get(0).all(by.css('.modularBoxContent')).all(by.css('[class="labelText"]')).count()).tobe(3);

I also tried below but that is not working as well. Getting same error as above for this as well.

 expect(pageContent.all(by.css('.modularBoxContent')).get(1).all(by.css('[class="labelText"]')).get(0).getText()).tobe(3);

Can someone please suggest correct usage?

4
  • Can you elaborate what you mean by "it's not working"? Is there an error? Are you retrieving the wrong elements? etc. Commented Oct 24, 2016 at 16:29
  • @Gunderson Added error message in my post. I am getting error stating get is not a function. Moreover i do not think this is right approach to retrieve element so i am not sure. Commented Oct 24, 2016 at 16:39
  • Ya you can't call .get() on a single element, only an element array. If it was var pageContent = element.all(by.css('[class="main-content"]')); it would work. So are the 4 main containers all with class main-content? Commented Oct 24, 2016 at 16:45
  • Yes. That is right. All of them are under main-content Commented Oct 24, 2016 at 16:54

1 Answer 1

2

I think this should work for you, let's try this. Since all containers have the same class, create an array with .all() and use .get() to retrieve the desired index.

var pageContent = element(by.css('.main-content'));
var firstContainer = pageContent.all(by.css('.modularBoxContent')).first(); // or could have used .get(0);

Once your first container is successfully identified, now you just chain locator calls to find all the children under it. Since you said you wanted the second element under the first container, we'll use .get(1) (1 is the index, so it's the second item in the array).

var secondChild = firstContainer.all(by.css('[class="labelText"]')).get(1);

And, just an FYI, a lot of this could probably be refactored to be shorter if you wanted. For example, you can chain your CSS calls, no need to separate them:

The above code should be the same as this:

var firstContainer = element.all(by.css('.main-content .modularBoxContent')).first();
var secondChild = firstContainer.all(by.css('.labelText')).get(1);
Sign up to request clarification or add additional context in comments.

8 Comments

I think there is some confusion. I just have 1 element with class = .main-content. Now under this i have different containers with same name and class as modularBoxContent and under this i have different labels with same class as .labelText I want to get count of .labelText under each container and also fetch 1st labelText from 1st container.
var firstContainer = element.all(by.css('.main-content')).first(); will not give me first container as container class is .modularBoxContent
I used below code but it is not working var firstcontainer = element.all(by.css('.modularBoxContent')).first(); expect(firstContainer.all(by.css('.labelText')).count()).tobe(10); Getting error stating Expected 0 to be 10
@NewWorld Oh ok, I slightly updated my answer (see the firstContainer locator again). If I'm understanding correctly, .modularBoxContent is all containers, so calling .all() and .first() on that should give you want you want?
Thanks. I think most of it worked but i am not sure how to access 2nd or 3rd container? is second() or third() valid functions as they are not working for me?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.