{"id":13711966,"url":"https://github.com/crossbrowsertesting/selenium-webdriverio","last_synced_at":"2025-05-06T21:32:53.562Z","repository":{"id":47071907,"uuid":"71587343","full_name":"crossbrowsertesting/selenium-webdriverio","owner":"crossbrowsertesting","description":null,"archived":true,"fork":false,"pushed_at":"2021-09-14T19:33:43.000Z","size":88,"stargazers_count":3,"open_issues_count":2,"forks_count":5,"subscribers_count":8,"default_branch":"master","last_synced_at":"2024-11-13T22:35:23.350Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/crossbrowsertesting.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-10-21T18:17:42.000Z","updated_at":"2023-12-13T18:42:36.000Z","dependencies_parsed_at":"2022-09-03T01:10:35.377Z","dependency_job_id":null,"html_url":"https://github.com/crossbrowsertesting/selenium-webdriverio","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crossbrowsertesting%2Fselenium-webdriverio","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crossbrowsertesting%2Fselenium-webdriverio/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crossbrowsertesting%2Fselenium-webdriverio/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crossbrowsertesting%2Fselenium-webdriverio/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/crossbrowsertesting","download_url":"https://codeload.github.com/crossbrowsertesting/selenium-webdriverio/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252772247,"owners_count":21801888,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T23:01:13.432Z","updated_at":"2025-05-06T21:32:53.279Z","avatar_url":"https://github.com/crossbrowsertesting.png","language":"JavaScript","funding_links":[],"categories":["By Technology"],"sub_categories":["JavaScript"],"readme":"# Getting Started with WebDriverIO and CrossBrowserTesting\n\n* [Simple Test with Mocha](#test-with-mocha)\n* [Converting Existing Test Suites](#converting-existing-test-suites)\n* [Using the Local Connection](#using-the-local-connection)\n\nWant a powerful and easy to use command line tool for running Selenium-JS tests? Like to shy away from asynchronous\ncommands and race conditions? [WebDriverIO](http://webdriver.io/) might be the option for you. WebDriverIO provides\nlanguage-bindings for the powerful browser-driving tool [Selenium](http://www.seleniumhq.org/docs/). Its test runner\nallows you to write your tests in a synchronous way so that you're not constantly in the world of asynchronous coding.\nLuckily WebDriverIO integrates easily with the CrossBrowserTesting platform, so you can perform tests on a wide variety\nof OS/Device/Browser combinations, all from one test. Let's walk through getting WebDriverIO tests running with\nCrossBrowserTesting.\n\n## Install and run app locally\n\n#### Clone a repo\n\n```https://github.com/crossbrowsertesting/selenium-webdriverio.git```\n\n#### Install the node modules\n\n```npm install```\n\n#### Add a username and auth key\n\nReplace **process.env.CBT_USERNAME** and **process.env.CBT_AUTHKEY** with your username (CBT email) and secret auth\nkey (find this under the \"Manage Account\")\n\n#### Run the test\n\n```wdio or ./node_modules/.bin/wdio```\n\n## OR\n\n## Starting from Scratch\n\nYou can configure WebDriverIO to use many different testing frameworks, such as [Chai](http://chaijs.com/)\nor [Mocha](https://mochajs.org/), and you can [read more on that here](https://webdriver.io/docs/gettingstarted). For\nour purposes, we'll start by writing a simple test using WebDriverIO with Mocha.\n\n### Simple Test\n\nFirst, create a folder for your test. From inside that folder, run the following commands (the `npm init` command will\nprompt for information about your project; if you are unsure, it is fine to leave those fields blank):\n\n```\nnpm init -y\nnpm install --save-dev @wdio/cli\nnpm install --save-dev @wdio/local-runner\nnpm install --save-dev @wdio/spec-reporter\nnpm install --save-dev request\n```\n`npm init -y` starts your project, `npm install --save-dev @wdio/cli` installs WebDriverIO into your project, `npm install --save-dev @wdio/local-runner` allows local running of tests, `npm install --save-dev @wdio/spec-reporter` formats test output in spec style and `npm install --save-dev request` installs the request module.\n\n### Test with Mocha\n\nThe process of using WebDriverIO with other frameworks will vary slightly with each framework. Several frameworks are supported with the configuration tool, which can be started up with the command `./node_modules/.bin/wdio config`. This will give a series of prompts and create a configuration file with the information you provide. We'll be manually creating our configuration file for this example.\n\nFirst, you will need to install the WebDriverIO/Mocha adapter and the CBT WDIO service:\n\n```bash\nnpm install --save-dev @wdio/mocha-framework\nnpm install --save-dev @wdio/crossbrowsertesting-service\n```\n\nNext, you'll need to make a configuration file. We're going to manually make one in this guide, but if you'd like to use WebDriverIO's configuration tool, you can run it with `./node_modules/.bin/wdio config`.\n\nSave the following code as \"wdio.conf.js\" in the root directory of your project:\n\n```javascript\nexports.config = {\n    runner: 'local',\n\n    hostname: 'hub.crossbrowsertesting.com',\n    port: 80,\n    path: '/wd/hub',\n\n    services: ['crossbrowsertesting'],\n    user: process.env.CBT_USERNAME,\n    key: process.env.CBT_AUTHKEY,\n    cbtTunnel: false, //set to true if a local connection is needed\n\n    specs: [\n        './test/specs/**/*.js'\n    ],\n    exclude: [\n        // 'path/to/excluded/files'\n    ],\n\n    maxInstances: 10,\n\n    capabilities: [{\n        maxInstances: 5,\n        platformName: 'Windows 10',\n        browserName: 'chrome',\n        browserVersion: 93,\n        'cbt:options': {\n            'name': 'WDIO Selenium Test Example',\n            'screenResolution': '1366x768',\n            'record_video': 'true',\n            'record_network': 'false',\n        }\n    }],\n\n    logLevel: 'info',\n\n    bail: 0,\n\n    baseUrl: 'http://localhost',\n\n    waitforTimeout: 10000,\n\n    connectionRetryTimeout: 90000,\n\n    connectionRetryCount: 3,\n\n    framework: 'mocha',\n\n    reporters: ['spec'],\n\n    mochaOpts: {\n        ui: 'bdd',\n        timeout: 60000\n    }\n}\n```\n\nAll that's left to do now is add your tests! The directory we specified in the config file is `/test/specs`, so place your tests there. Here is the sample test we'll use saved in a file named login.js:\n\n```javascript\nconst request = require('request');\nconst assert = require('assert');\n\ndescribe(\"Login form\", function () {\n    //this.timeout(5 * 1000 * 60);\n\n    it('should have the right title', async () =\u003e {\n        await browser.url(\"https://crossbrowsertesting.github.io/login-form.html\")\n\n        //Enter the username\n        let username = browser.$(\"#username\");\n        await username.click();\n        await username.setValue(\"tester@crossbrowsertesting.com\");\n\n        //Enter the password\n        let password = browser.$(\"#password\");\n        await password.click();\n        await password.setValue(\"test123\");\n\n        // Click \"Login\"\n        await browser.$(\"div.form-actions\u003ebutton\").click();\n\n        // Get the title\n        let title = await (browser.getTitle());\n        try {\n            //Check title\n            assert.deepStrictEqual(title, \"Login Form - CrossBrowserTesting.com\");\n        } catch (e) {\n            //Set score and throw exception so WebDriverIO detects it\n            setScore(\"fail\");\n            throw(e);\n        }\n        setScore(\"pass\");\n    });\n});\n\n//Helper to set the score\nfunction setScore(score) {\n\n    const result = {error: false, message: null};\n\n    if (browser.sessionId) {\n\n        request({\n                method: 'PUT',\n                uri: 'https://crossbrowsertesting.com/api/v3/selenium/' + browser.sessionId,\n                body: {'action': 'set_score', 'score': score},\n                json: true\n            },\n            function (error, response, body) {\n                if (error) {\n                    result.error = true;\n                    result.message = error;\n                } else if (response.statusCode !== 200) {\n                    result.error = true;\n                    result.message = body;\n                } else {\n                    result.error = false;\n                    result.message = 'success';\n                }\n\n            })\n            .auth(process.env.CBT_USERNAME, process.env.CBT_AUTHKEY);\n    } else {\n        result.error = true;\n        result.message = 'Session Id was not defined';\n    }\n}\n```\n\n\n## Converting Existing Test Suites\n\nIf you're already a WebDriverIO user, you can quickly change your current tests by making the following changes to your webdriver configuration:\n\nBefore:\n\n```javascript\nvar options = {\n    desiredCapabilities: {\n        browserName: 'firefox'\n    }\n};\n```\n\nNow:\n\n```javascript\nexports.config = {\n    runner: 'local',\n    hostname: \"hub.crossbrowsertesting.com\",\n    port: 80,\n    path: '/wd/hub',\n    user: 'you@yourdomain.com',\t\t// the email address associated with your CBT account\n    key: 'yourauthkey',      \t\t\t\t\t// find this under the \"Manage Account page of our app\"\n    capabilities: [{\n        maxInstances: 5,\n        platformName: 'Windows 10',\n        browserName: 'chrome',\n        browserVersion: 93,\n        'cbt:options': {\n            'name': 'WDIO Selenium Test Example',\n            'build': '1.0',\n            'screenResolution': '1366x768',\n            'record_video': 'true',\n            'record_network': 'false',\n        }\n    }],\n}\n```\nRun your test using the command:\n\n```\n./node_modules/.bin/wdio\n```\nAs you can see, we're now pointing the test at our hub rather than a local driver instance. \n\n## Using the Local Connection\n\nIf you would like to test behind your firewall or access non-public sites, you can use our local connection tool through our WebdriverIO service. Simply install our service by running the command:\n\n```bash\nnpm install --save-dev @wdio/crossbrowsertesting-service\n```\n\nThen add the following to your wdio.conf.js file :\n\n```js\nexports.config = {\n  // ...\n  services: ['crossbrowsertesting'],\n  cbtTunnel: true,\n  // ...\n};\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrossbrowsertesting%2Fselenium-webdriverio","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcrossbrowsertesting%2Fselenium-webdriverio","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrossbrowsertesting%2Fselenium-webdriverio/lists"}