Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recipe for using AVA with Selenium #1023

Closed
sindresorhus opened this issue Aug 29, 2016 · 5 comments · Fixed by #2320
Closed

Recipe for using AVA with Selenium #1023

sindresorhus opened this issue Aug 29, 2016 · 5 comments · Fixed by #2320

Comments

@sindresorhus
Copy link
Member

It's a common need. We should have a recipe to make it easy for users to get started.

I think Webdriver.IO would be the best choice.

@rhubarbselleven
Copy link

rhubarbselleven commented Oct 21, 2016

This is quite straight forward:

import test from 'ava';

let webdriverio = require('webdriverio');
let client = webdriverio.remote({
    // just using a local chromedriver
    desiredCapabilities: {browserName: 'chrome'}
});

test.before(async t => {
    await client.init()
        .url('http://localhost');
});

test.after.always(async t => {
    await client.end();
});

test('has a body', t => {
    return client.isExisting('body').then(result => {
        t.true(result);
    });
});

I've only had success using AVA and webdriverio together when running tests serially

@sindresorhus
Copy link
Member Author

@rhubarbselleven Interested in submitting a recipe?

@nreijmersdal
Copy link

nreijmersdal commented Oct 21, 2016

I did have success running tests in parallel with selenium-webdriver package.
You do need to start a Selenium server, for example with the webdriver-manager package.

Here is my example code:

import test from 'ava';
import webdriver from 'selenium-webdriver';

test.beforeEach(t => {
  t.context.driver = new webdriver.Builder()
                        .forBrowser('chrome')
                        .usingServer('http://localhost:4444/wd/hub')
                        .build();
});

test('Search for webdriver', async t => {
      let driver = t.context.driver;
      await searchGoogle(driver, 'webdriver')
      t.is(await driver.getTitle(), "webdriver - Google Search");
      await driver.quit();
});

test('Search for avajs', async t => {
      let driver = t.context.driver;
      await searchGoogle(driver, 'avajs')
      t.is(await driver.getTitle(), "avajs - Google Search");
      await driver.quit();      
});

test('Search for concurrent', async t => {
      let driver = t.context.driver;
      await searchGoogle(driver, 'concurrent')
      t.is(await driver.getTitle(), "concurrent - Google Search");
      await driver.quit();      
});

async function searchGoogle(driver, keyword) {
      await driver.get('http://www.google.com/ncr');
      await driver.findElement(webdriver.By.name('q')).sendKeys(keyword);
      await driver.findElement(webdriver.By.name('btnG')).click();
      await driver.wait(webdriver.until.titleIs(keyword + ' - Google Search'), 1000);
}

@aptester
Copy link
Contributor

@nreijmersdal After running the above code, the tests are getting executed serially. Getting the below results with 'ava --verbose'

√ Search for webdriver (6.6s)
√ Search for avajs (10.7s)
√ Search for concurrent (14.9s)

I'm running [email protected], [email protected] and [email protected] which uses selenium-server-standalone-3.5.3.jar. Any ideas what change is now required to get the tests running in parallel?

@nreijmersdal
Copy link

@aptester Sorry no idea. Also I am not using Ava anymore. Running each test in parallel doesn't make a lot of sense anyways. Maybe it is easier to group sets of tests and create a couple of command-line jobs to start the groups in parallel. This is what I used to do when we used Java for Selenium. Goodluck.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants