// Select may be called on a selection of any number of <select> elements to select // any <option> elements under those <select> elements that match the provided text. func (s *Selection) Select(text string) error { return s.forEachElement(func(selectedElement element.Element) error { optionXPath := fmt.Sprintf(`./option[normalize-space()="%s"]`, text) optionToSelect := target.Selector{Type: target.XPath, Value: optionXPath} options, err := selectedElement.GetElements(optionToSelect.API()) if err != nil { return fmt.Errorf("failed to select specified option for %s: %s", s, err) } if len(options) == 0 { return fmt.Errorf(`no options with text "%s" found for %s`, text, s) } for _, option := range options { if err := option.Click(); err != nil { return fmt.Errorf(`failed to click on option with text "%s" for %s: %s`, text, s, err) } } return nil }) }
func retrieveElements(client Client, selector target.Selector) ([]Element, error) { if selector.Single { elements, err := client.GetElements(selector.API()) if err != nil { return nil, err } if len(elements) == 0 { return nil, errors.New("element not found") } else if len(elements) > 1 { return nil, errors.New("ambiguous find") } return []Element{Element(elements[0])}, nil } if selector.Indexed && selector.Index > 0 { elements, err := client.GetElements(selector.API()) if err != nil { return nil, err } if selector.Index >= len(elements) { return nil, errors.New("element index out of range") } return []Element{Element(elements[selector.Index])}, nil } if selector.Indexed && selector.Index == 0 { element, err := client.GetElement(selector.API()) if err != nil { return nil, err } return []Element{Element(element)}, nil } elements, err := client.GetElements(selector.API()) if err != nil { return nil, err } newElements := []Element{} for _, element := range elements { newElements = append(newElements, element) } return newElements, nil }
Element(&api.Element{ID: "third child", Session: &api.Session{secondParentBus}}), Element(&api.Element{ID: "fourth child", Session: &api.Session{secondParentBus}}), } firstParentBus.SendCall.Result = `[{"ELEMENT": "first child"}, {"ELEMENT": "second child"}]` secondParentBus.SendCall.Result = `[{"ELEMENT": "third child"}, {"ELEMENT": "fourth child"}]` client.GetElementsCall.ReturnElements = []*api.Element{firstParent, secondParent} parentSelector = target.Selector{Type: target.CSS, Value: "parents"} parentSelectorJSON = `{"using": "css selector", "value": "parents"}` childSelector = target.Selector{Type: target.XPath, Value: "children"} childSelectorJSON = `{"using": "xpath", "value": "children"}` }) Context("when all unindexed elements are successfully retrieved", func() { It("should retrieve the parent elements using the client", func() { repository.Get(target.Selectors{parentSelector, childSelector}) Expect(client.GetElementsCall.Selector).To(Equal(parentSelector.API())) }) It("should retrieve the child elements of the parent selector", func() { repository.Get(target.Selectors{parentSelector, childSelector}) Expect(firstParentBus.SendCall.BodyJSON).To(MatchJSON(childSelectorJSON)) Expect(secondParentBus.SendCall.BodyJSON).To(MatchJSON(childSelectorJSON)) }) It("should successfully return all of the children", func() { Expect(repository.Get(target.Selectors{parentSelector, childSelector})).To(Equal(children)) }) }) Context("when all non-zero-indexed elements are successfully retrieved", func() { BeforeEach(func() {