LoginSignup
0
0

More than 3 years have passed since last update.

EC2上にSelenium環境構築&Node.jsでテスト実装

Last updated at Posted at 2019-06-20

EC2上にSelenium環境を構築し、Node.jsでテストを実装した際の備忘録。

手順

ec2インスタンス作成

AMI:amzn2-ami-hvm-2.0.20190508-x86_64-gp2 (ami-0c6b1d09930fac512)

chromeインストール

sudo yum -y install http://orion.lcg.ufrj.br/RPMS/myrpms/google/google-chrome-stable-71.0.3578.98-1.x86_64.rpm

ChromeDriverインストール

wget https://chromedriver.storage.googleapis.com/2.45/chromedriver_linux64.zip
unzip chromedriver_linux64.zip -d bin/

注意:chromeとChromeDriverは対応したバージョンでないと動作しないことがあります。
こちらを参考にしてください。
http://chromedriver.chromium.org/

Node.jsインストール

sudo curl -sL https://rpm.nodesource.com/setup_8.x | sudo bash -
sudo yum install nodejs

npmモジュールインストール

sudo npm -g install selenium-webdriver mocha expect.js selenium-webdriver

test.js作成

qiitaトップページの「Hello hackers !」文字列を取得して検査。


const webdriver = require('selenium-webdriver');
const { Builder, By, until } = webdriver;
const assert = require("assert");

const capabilities = webdriver.Capabilities.chrome();
capabilities.set('chromeOptions', {
    args: [
        '--headless',
        '--no-sandbox',
        '--disable-gpu',
        `--window-size=1980,1200`
    ]
});

describe("top page test", () => {
    it("top page h1 value check", async () => {
        const driver = await new Builder().withCapabilities(capabilities).build();
        await driver.get('https://qiita.com/');
        let element = await driver.findElement({ css: '.nl-Hero_title' }).getText();
        assert.equal("Hello hackers !", element);
        await driver.quit();
    });
})

実行

mocha test.js

タイムアウトで失敗した場合は、-t 5000のようにタイムアウト時間を指定してください。デフォルトは2000ミリ秒なので、それよりも大きい値を指定してください。

参考

https://chida09.com/selenium-nodejs-dev/
https://oliversi.com/2019/01/06/python-aws-selenium-chrome/

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0