Memo

メモ > 技術 > 開発: Selenium > JavaScript版Selenium環境をWindowsに構築

JavaScript版Selenium環境をWindowsに構築
Seleniumで自動テスト - Qiita https://qiita.com/naka46/items/2587a5075d6920888648 上記のページを参考に、JavaScriptでテストを作成&実行する。 ■作業フォルダを作成 C:\Users\refirio\Selenium にSelenium用のフォルダを作成したとする。 ここに作業フォルダを作成するものとする。
>C:\Users\refirio\Selenium >mkdir sample >cd sample
■必要なライブラリをインストール
>npm install -D selenium-webdriver >npm install -D mocha >npm install -D expect.js
■package.json を作成
>npm init -y
■ChromeDriverをインストール 以下からChromeDriverをダウンロードし、解凍して作成される chromedriver.exe を package.json と同じ場所に配置。 (今回は「ChromeDriver 87.0.4280.88」をダウンロードしたが、利用中のChromeのバージョンに合ったものをダウンロードする。) https://sites.google.com/a/chromium.org/chromedriver/downloads ■プログラムを作成 sample.js
var webdriver = require('selenium-webdriver'); var expect = require('expect.js'); var assert = require('assert'); var By = webdriver.By; describe('test sample', function(){ this.timeout(15000); let browser; // テスト開始前に行う処理 before(() => { browser = new webdriver.Builder().forBrowser('chrome').build(); }); // テスト終了時に行う処理 after(() => { browser.quit(); }); // テスト内容(itを複数記述できる) it('web app test1 success', async function() { // googleホームページを起動する。 await browser.get('https://google.co.jp'); //テスト処理 let targetValue = await browser.getTitle(); expect(targetValue).to.be('Google'); }); it('web app test2 error', async function() { // googleホームページを起動する。 await browser.get('https://google.co.jp'); //テスト処理 let targetValue = await browser.getTitle(); expect(targetValue).to.be('Google111'); }); });
■プログラムを実行
>npm test sample.js
ブラウザが起動して処理され、完了すると以下のように結果が表示される。 今回は「1つのテストが成功して、1のテストが失敗する」という前提のテスト内容なので、「1 passing」「1 failing」のように表示されている。
> sample@1.0.0 test C:\Users\refirio\Selenium\sample > mocha "sample.js" test sample DevTools listening on ws://127.0.0.1:65214/devtools/browser/b3ffd2b5-a747-41d5-b01b-cd8e019e1522 √ web app test1 success (3824ms) 1) web app test2 error 1 passing (5s) 1 failing 1) test sample web app test2 error: Error: expected 'Google' to equal 'Google111' at Assertion.assert (node_modules\expect.js\index.js:96:13) at Assertion.be.Assertion.equal (node_modules\expect.js\index.js:216:10) at Assertion.<computed> [as be] (node_modules\expect.js\index.js:69:24) at Context.<anonymous> (sample.js:37:32) at processTicksAndRejections (internal/process/task_queues.js:93:5) npm ERR! Test failed. See above for more details.
■その他メモ テスト実行時、以下のエラーが表示されることがあった。
[15964:8524:1209/174355.103:ERROR:device_event_log_impl.cc(211)] [17:43:55.102] USB: usb_device_handle_win.cc:1020 Failed to read descriptor from node connection: システムに接続されたデバイスが機能していません。 (0x1F) [15964:8524:1209/174355.107:ERROR:device_event_log_impl.cc(211)] [17:43:55.107] USB: usb_device_handle_win.cc:1020 Failed to read descriptor from node connection: システムに接続されたデバイスが機能していません。 (0x1F)
以下のページを参照する限り無視して良さそうだが、また対策は考えたい。 Seleniumを使用しPythonでWebページのタイトルを取得する | 中小企業のIT相談窓口「Soo_Daaan」 - 論理的思考/課題解決/プログラミング https://laboratory.kazuuu.net/get-web-page-titles-in-python-using-selenium/

Advertisement