Skip to content

myamyu/selenimum

Repository files navigation

Selenimum

Selenimum is inspired by dom96/webdriver.

1. About

WebDriver for Selenium(selenium-hub) by Nim.

2. Usage

2.1. launch selenium hub

save docker-compose.yaml to your woriking directory.
and up by docker-compose.

version: '3'

services:
  selenium-hub:
    image: selenium/hub:3.141.59-20210830
    container_name: selenium-hub
    ports:
    - 4444:4444
    environment:
    - GRID_MAX_SESSION=10

  firefox: &node
    image: selenium/node-firefox:3.141.59-20210830
    container_name: selenium-firefox
    shm_size: 2gb
    depends_on:
    - selenium-hub
    environment:
    - HUB_HOST=selenium-hub
    - HUB_PORT=4444
    - NODE_MAX_SESSION=5

  chrome:
    <<: *node
    image: selenium/node-chrome:3.141.59-20210830
    container_name: selenium-chrome
docker-compose up -d --remove-orphans

2.2. control selenium example

import selenimum, strformat

proc main() =
  let
    driver = newSeleniumWebDriver()
    session = driver.newSession()
  defer:
    session.deleteSession()

  try:
    # navigate to Yahoo!JAPAN
    session.navigateTo("https://www.yahoo.co.jp/")
    let title = session.getTitle()
    echo(&"page title:{title}")
    session.saveScreenshot("yahoo-japan.png")
    echo("screenshot saved.")
  except Exception as e:
    echo("ERROR!!", e.getStackTrace())

main()

if you want to use firefox.

import selenimum, json

let
  capabilities = %*{
    "desiredCapabilities": {
      "browserName": "firefox"
    },
    "requiredCapabilities": {}
  }
  session = driver.newSession(capabilities=capabilities)

2.3. control selenium example by macro

import selenimum, strformat

proc main() =
  selenium "http://selenium-hub:4444/wd/hub":
    chrome:
      # navigate to Yahoo!JAPAN
      navigateTo "https://www.yahoo.co.jp/"
      let title = session.getTitle()
      echo(&"page title:{title}")
      session.saveScreenshot("yahoo-japan.png")
      echo("screenshot saved.")

try:
  main()
except Exception as e:
  echo("ERROR!!", e.getStackTrace())

if you want to use firefox.

import selenimum, strformat

proc main() =
  selenium "http://selenium-hub:4444/wd/hub":
    firefox:
      # navigate to Yahoo!JAPAN
      navigateTo "https://www.yahoo.co.jp/"
      let title = session.getTitle()
      echo(&"page title:{title}")
      session.saveScreenshot("yahoo-japan.png")
      echo("screenshot saved.")

try:
  main()
except Exception as e:
  echo("ERROR!!", e.getStackTrace())

3. Docs