WebDriverIO is a progressive automation framework for web and mobile applications. It's built on Node.js and implements the WebDriver protocol, providing a simple and powerful API for browser automation.
# Node.js (version 14 or higher)
node --version
# npm (comes with Node.js)
npm --version
# Create new project directory
mkdir webdriverio-tests
cd webdriverio-tests
# Initialize npm project
npm init -y
# Install WebDriverIO CLI
npm install --save-dev @wdio/cli
# Initialize WebDriverIO configuration
npx wdio config
During configuration, you'll be prompted to choose:
exports.config = {
// Test runner configuration
runner: 'local',
// Specify test files
specs: [
'./test/specs/**/*.js'
],
// Browser capabilities
capabilities: [{
maxInstances: 5,
browserName: 'chrome',
acceptInsecureCerts: true
}],
// Test framework
framework: 'mocha',
// Mocha options
mochaOpts: {
ui: 'bdd',
timeout: 60000
},
// Services
services: ['chromedriver'],
// Reporters
reporters: ['spec']
};
// test/specs/basic.test.js
describe('My First WebDriverIO Test', () => {
it('should open Google and verify title', async () => {
// Navigate to Google
await browser.url('https://www.google.com');
// Verify page title
const title = await browser.getTitle();
expect(title).toBe('Google');
// Take a screenshot
await browser.saveScreenshot('./screenshots/google.png');
});
it('should perform a search', async () => {
// Navigate to Google
await browser.url('https://www.google.com');
// Find search input and enter text
const searchInput = await $('input[name="q"]');
await searchInput.setValue('WebDriverIO');
// Submit search
await browser.keys('Enter');
// Wait for results
await $('h3').waitForDisplayed();
// Verify results are displayed
const results = await $$('h3');
expect(results.length).toBeGreaterThan(0);
});
});
# Run all tests
npx wdio run wdio.conf.js
# Run specific test file
npx wdio run wdio.conf.js --spec test/specs/basic.test.js
# Run tests in headless mode
npx wdio run wdio.conf.js --headless
// Navigation
await browser.url('https://example.com');
await browser.back();
await browser.forward();
await browser.refresh();
// Window management
await browser.maximizeWindow();
await browser.setWindowSize(1024, 768);
const windowSize = await browser.getWindowSize();
// Page information
const title = await browser.getTitle();
const url = await browser.getUrl();
const source = await browser.getPageSource();
// Screenshots
await browser.saveScreenshot('./screenshot.png');
// Cookies
await browser.setCookies({
name: 'test',
value: 'value'
});
const cookies = await browser.getCookies();
// Single element
const element = await $('button');
const elementById = await $('#submit-btn');
const elementByClass = await $('.btn-primary');
// Multiple elements
const elements = await $$('button');
const listItems = await $$('li');
// Element within element
const form = await $('form');
const submitButton = await form.$('button[type="submit"]');
// Click actions
await element.click();
await element.doubleClick();
await element.rightClick();
// Input actions
await element.setValue('text');
await element.addValue('more text');
await element.clearValue();
// Element properties
const text = await element.getText();
const value = await element.getValue();
const isDisplayed = await element.isDisplayed();
const isEnabled = await element.isEnabled();
In the next module, we'll dive deeper into element selection strategies and advanced interaction patterns. You'll learn about different locator strategies and how to handle complex UI elements effectively.