Last active
February 4, 2016 09:53
-
-
Save nawada/dd6b7250919df259299b to your computer and use it in GitHub Desktop.
Selenium Samples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const wd = require('wd'); | |
const chai = require('chai'); | |
const chaiAsPromised = require('chai-as-promised'); | |
chai.use(chaiAsPromised); // chaiのpluginとしてchaiAsPromiseを使用する | |
chai.should(); // AssertionとしてShouldを使用する | |
chaiAsPromised.transferPromiseness = wd.transferPromiseness; // chaiのメソッドチェーンを有効化 | |
describe('設定', function() { | |
this.timeout(10 * 1000); // テストのタイムアウト時間設定 | |
const driver = wd.promiseChainRemote(); // 初期化 | |
// テスト実行前に行われる | |
before(() => { | |
return driver | |
.init({'browserName': 'chrome'}) | |
.setWindowSize(1440, 900) | |
.maximize() | |
.get('http://www.seleniumhq.org/'); | |
}); | |
// テスト実行後に行われる | |
after(() => driver.quit()); | |
// テスト | |
it('H1確認', () => { | |
return driver | |
.waitForElementByCssSelector('h1 a') | |
.getAttribute('title').should.eventually.equal('Return to Selenium home page') | |
.elementByCssSelector('h1 a') | |
.text().should.eventually.equal('Browser Automation'); | |
}); | |
// テスト | |
it('H1確認(失敗)', () => { | |
return driver | |
.waitForElementByCssSelector('h1 a') | |
.getAttribute('title').should.eventually.equal('foobar') | |
.elementByCssSelector('h1 a') | |
.text().should.eventually.equal('Browser Automation'); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const wd = require('wd'); | |
// Q promises + chainingを使う | |
const driver = wd.promiseChainRemote(); | |
driver | |
.init({browserName: 'chrome'}) // ブラウザにchromeを使用する | |
.get('https://www.google.co.jp/') // Googleへ移動 | |
.elementById('lst-ib') // id="lst-ib"にフォーカス | |
.type('selenium') // "selenium"と打つ | |
.elementByCssSelector('button[name=btnG]') // name=btnGのボタンにフォーカス | |
.click() // ボタン押下 | |
.waitForElementByLinkText('Selenium - Web Browser Automation') // ←のリンク文字列が表示されるまで待ち、フォーカス | |
.click() // リンククリック | |
.sleep(1000) // 1秒待つ | |
.saveScreenshot('SampleScreenshot.png') // ScreenShotを撮る | |
.fin(() => driver.quit()) // 後処理 | |
.done(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const wd = require('wd'); | |
const driver = wd.promiseChainRemote(); | |
driver | |
.init({browserName: 'firefox'}) | |
.get('https://www.google.co.jp/') | |
.elementById('lst-ib').type('selenium') | |
.keys([wd.SPECIAL_KEYS.Enter]).keys([wd.SPECIAL_KEYS.NULL]) | |
.waitForElementByLinkText('Selenium - Web Browser Automation').click().sleep(1000) | |
.saveScreenshot('SampleScreenshot.png') | |
.fin(() => driver.quit()) | |
.done(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment