Example #1
0
// 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
	})
}
Example #2
0
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
}
Example #3
0
		Context("when the client retrieves exactly one element", func() {
			It("should successfully return that element", func() {
				element := &api.Element{}
				client.GetElementsCall.ReturnElements = []*api.Element{element}
				Expect(repository.GetExactlyOne(target.Selectors{target.Selector{}})).To(Equal(element))
			})
		})
	})

	Describe("#Get", func() {
		var (
			firstParentBus     *mocks.Bus
			firstParent        *api.Element
			secondParentBus    *mocks.Bus
			secondParent       *api.Element
			children           []Element
			parentSelector     target.Selector
			parentSelectorJSON string
			childSelector      target.Selector
			childSelectorJSON  string
		)

		BeforeEach(func() {
			firstParentBus = &mocks.Bus{}
			firstParent = &api.Element{Session: &api.Session{firstParentBus}}
			secondParentBus = &mocks.Bus{}
			secondParent = &api.Element{Session: &api.Session{secondParentBus}}
			children = []Element{
				Element(&api.Element{ID: "first child", Session: &api.Session{firstParentBus}}),
				Element(&api.Element{ID: "second child", Session: &api.Session{firstParentBus}}),
				Element(&api.Element{ID: "third child", Session: &api.Session{secondParentBus}}),