Help us understand the problem. What is going on with this article?

GitHub Actions で Windows IE11 と Mac Safari を selenium-webdriver で動かす

最近得た天啓で、 「GitHub Actions はコンテナを windows / mac / ubuntu から選べるということは、 本物の safari と ie11 を selenium-webdriver で動かすことができるのでは?」 と思ってガチャガチャやってみたら、なんとできてしまったので、紹介します。

今回は node で。

npm install selenium-webdriver --save-dev
.github/workflows/xbrowser.yaml
name: xbrowser

on: [push]

jobs:
  e2e-ie:
    runs-on: windows-latest
    steps:
      - uses: actions/checkout@v1
      - uses: warrenbuckley/Setup-Nuget@v1
      - uses: actions/setup-node@v1
        with:
          node-version: 12
      - run: yarn install
      - run: nuget install Selenium.WebDriver.IEDriver -Version 3.150.0
      - run: node scratch.js
        env:
          BROWSER: ie
  e2e-safari:
    runs-on: macos-latest
    steps:
      - uses: actions/checkout@v1
      - uses: actions/setup-node@v1
        with:
          node-version: 12
      - run: yarn install
      - name: Run safari driver
        run: |
          sudo safaridriver --enable
          safaridriver -p 0 &
      - run: node scratch.js
        env:
          BROWSER: safari

今回動かしてるスクリプト。 youtube にアクセスして、title を取得するだけ。

scratch.js
const webdriver = require("selenium-webdriver");

function buildCapabilities() {
  switch (process.env.BROWSER) {
    case "ie": {
      process.env.PATH = `${process.env.PATH};${__dirname}/Selenium.WebDriver.IEDriver.3.150.0/driver/;`;
      const capabilities = webdriver.Capabilities.ie();
      capabilities.set("ignoreProtectedModeSettings", true);
      capabilities.set("ignoreZoomSetting", true);
      return capabilities;
    }
    case "safari": {
      return webdriver.Capabilities.safari();
    }
  }
}

async function main() {
  let driver;
  try {
    const capabilities = await buildCapabilities();
    driver = await new webdriver.Builder()
      .withCapabilities(capabilities)
      .build();

    await driver.get("https://www.youtube.com/");
    const title = await driver.getTitle();
    console.log("title", title);
  } catch (e) {
    console.error(e);
    process.exit(1);
  }
  driver && (await driver.quit());
}

main();

パスの書き換えのところがちょっと Hacky なんですが、もっときれいに書けるかも

これが動いてる風景

https://github.com/mizchi/frontend-gh-action-playground/pull/4/checks?check_run_id=229164948

感想

えっこれ最高では。

(もうちょっと頑張れば Android Chrome と iOS Emulator も動かせる気がする…)

plaid
CXプラットフォーム「KARTE」の開発・運営、EC特化型メディア「Shopping Tribe」の企画・運営、CX特化型メディア「XD(クロスディー)」の企画・運営
https://plaid.co.jp/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした