Selenium

WebdriverIO 覚書き

Getting started WebdriverIO Automation

package.json
"scripts": {
  "start": "node main.js",
  "repl": "./node_modules/.bin/wdio repl chrome"
},
main.js
const webdriverio = require('webdriverio');

const options = {
  desiredCapabilities: {
    browserName: 'chrome'
  }
};

webdriverio
  .remote(options)
  .init()
  .windowHandleSize({width: 1000, height: 700})
  .url('https://www.google.co.jp/')
  .getTitle().then(title => {
    console.log(title);
  })
  .setValue('input[name="q"]', 'webdriverio')
  .submitForm('form[role="search"]')
  // .end();
shell
$ npm start

options

webdriver.io

WebdriverIO - Configuration

デバイス・OS・ブラウザ・バージョンを指定して生成する便利ツール

Platform Configurator - The Sauce Labs Cookbook

const options = {
  desiredCapabilities: {
    browserName: 'internet explorer',
    platform: 'Windows 10',
    version: '11.103',
    screenResolution: '1920x1080'
  }
};

waitForUrl

/**
 * @alias browser.waitForUrl
 * @param {string|RegExp|Function} value
 * @param {number} timeout — ms
 * @param {number} revert
 * @returns {boolean}
 */
browser.addCommand('waitForUrl', function (value, timeout, revert) {
  let url, actual;

  try {
    return browser.waitUntil(() => {
      url = browser.getUrl();
      actual = value === url;

      // This slash is added by Selenium
      if (typeof value === 'string' && !value.endsWith('/')) {
        url = url.replace(/\/$/, '');
      }

      if (typeof value === 'function') {
        actual = value(url);
      } else if (value[Symbol.match]) {
        actual = value.test(url);
      }

      if (revert) {
        actual = !actual;
      }

      return value && actual;
    }, timeout, '');
  } catch (error) {
    let message = 'Could not wait for required url:';
      message += `\n\tActual: ${url}`;
      message += `\n\tExpected: ${value}`;

    throw new Error(message);
  }
});
Usage
browser.waitForUrl('https://example.com');
browser.waitForUrl(/example/);
browser.waitForUrl(url => /example/.test(url));