Class: ClassifierClient

Inherits:
Object
  • Object
show all
Defined in:
lib/testai_classifier.rb

Instance Method Summary collapse

Constructor Details

#initialize(host, port) ⇒ ClassifierClient

Returns a new instance of ClassifierClient.



14
15
16
# File 'lib/testai_classifier.rb', line 14

def initialize(host, port)
    @stub = Classifier::Stub.new("#{host}:#{port}", :this_channel_is_insecure)
end

Instance Method Details

#classify_images(label, element_images, confidence = DEF_CONFIDENCE, allow_weaker_matches = false) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/testai_classifier.rb', line 18

def classify_images(label, element_images, confidence=DEF_CONFIDENCE,
                    allow_weaker_matches=false)
    ecr = ElementClassificationRequest
    req = ecr.new(labelHint: label, 
                  elementImages: element_images,
                  confidenceThreshold: confidence, 
                  allowWeakerMatches: allow_weaker_matches)
    resp = @stub.classify_elements(req)
    ret = {}
    resp.classifications.each do |id, cls|
        ret[id] = {
            :label => cls.label, 
            :confidence => cls.confidence,
            :confidence_for_hint => cls.confidenceForHint
        }
    end
    return ret
end

#find_elements_matching_label(driver, label, confidence = DEF_CONFIDENCE, allow_weaker_matches = false) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/testai_classifier.rb', line 37

def find_elements_matching_label(driver, label, confidence=DEF_CONFIDENCE, allow_weaker_matches=false)
    els = driver.find_elements(:xpath, QUERY)
    element_images = {}
    els.each do |el|
        begin
            element_images[el.ref] = el.screenshot
        rescue
        end
    end
    if element_images.size < 1
        raise "Could not find any screenshots for leaf node elements"
    end
    matched = self.classify_images(label, element_images, confidence,
                                   allow_weaker_matches)
    return els.select {|el| matched.has_key? el.ref}
end