Class: Playwright::Selectors

Inherits:
PlaywrightApi show all
Defined in:
lib/playwright_api/selectors.rb

Overview

Selectors can be used to install custom selector engines. See [Working with selectors](../selectors.md) for more information.

Instance Method Summary collapse

Methods inherited from PlaywrightApi

#initialize, unwrap, wrap

Constructor Details

This class inherits a constructor from Playwright::PlaywrightApi

Instance Method Details

#off(event, callback) ⇒ Object

– inherited from EventEmitter –



59
60
61
# File 'lib/playwright_api/selectors.rb', line 59

def off(event, callback)
  event_emitter_proxy.off(event, callback)
end

#on(event, callback) ⇒ Object

– inherited from EventEmitter –



71
72
73
# File 'lib/playwright_api/selectors.rb', line 71

def on(event, callback)
  event_emitter_proxy.on(event, callback)
end

#once(event, callback) ⇒ Object

– inherited from EventEmitter –



65
66
67
# File 'lib/playwright_api/selectors.rb', line 65

def once(event, callback)
  event_emitter_proxy.once(event, callback)
end

#register(name, contentScript: nil, path: nil, script: nil) ⇒ Object

An example of registering selector engine that queries elements based on a tag name:

“‘python sync from playwright.sync_api import sync_playwright

def run(playwright):

tag_selector = """
  {
      // Returns the first element matching given selector in the root's subtree.
      query(root, selector) {
          return root.querySelector(selector);
      },
      // Returns all elements matching given selector in the root's subtree.
      queryAll(root, selector) {
          return Array.from(root.querySelectorAll(selector));
      }
  }"""

# Register the engine. Selectors will be prefixed with "tag=".
playwright.selectors.register("tag", tag_selector)
browser = playwright.chromium.launch()
page = browser.new_page()
page.set_content('<div><button>Click me</button></div>')

# Use the selector prefixed with its name.
button = page.locator('tag=button')
# Combine it with other selector engines.
page.locator('tag=div >> text="Click me"').click()
# Can use it in any methods supporting selectors.
button_count = page.locator('tag=button').count()
print(button_count)
browser.close()

with sync_playwright() as playwright:

run(playwright)

“‘



42
43
44
# File 'lib/playwright_api/selectors.rb', line 42

def register(name, contentScript: nil, path: nil, script: nil)
  wrap_impl(@impl.register(unwrap_impl(name), contentScript: unwrap_impl(contentScript), path: unwrap_impl(path), script: unwrap_impl(script)))
end

#set_test_id_attribute(attributeName) ⇒ Object Also known as: test_id_attribute=

Defines custom attribute name to be used in [‘method: Page.getByTestId`]. `data-testid` is used by default.

Raises:

  • (NotImplementedError)


47
48
49
# File 'lib/playwright_api/selectors.rb', line 47

def set_test_id_attribute(attributeName)
  raise NotImplementedError.new('set_test_id_attribute is not implemented yet.')
end

#text_id_attribute=(attribute_name) ⇒ Object



53
54
55
# File 'lib/playwright_api/selectors.rb', line 53

def text_id_attribute=(attribute_name)
  wrap_impl(@impl.text_id_attribute=(unwrap_impl(attribute_name)))
end