PixnBits / karma-selenium-webdriver-launcher

A Karma plugin. Run tests in an instance of a Selenium Webdriver.
MIT License
5 stars 8 forks source link

Need doc on how to access webdriver driver from jasmine test #1

Closed ilbmiller closed 9 years ago

ilbmiller commented 9 years ago

I'm using Karma with Jasmine, and it is unclear how to access the driver that is created by the karma-selenium-webdriver-launcher from within the Jasmine test. As an example, in the below jasmine test, how do I obtain the driver? The readme.md directions for configuring the karma.conf.js were clear, so I just need the missing piece as to how to access the resulting driver from within the jasmine tests.

describe('basic test', function () {
  var driver = getDriver();  // Bombs!  How do I get a handle to the webdriver?

    it('should be on correct page', function (done) {
        driver.get('http://www.wingify.com');
        driver.getTitle().then(function(title) {
            expect(title).toBe('Wingify');
            done();
        });
    });
});

And thanks for writing this! If this doesn't work, I'm off to try Protractor, which seems like overkill for my non-AngularJS app.

PixnBits commented 9 years ago

TL;DR: I didn't write that part yet :frowning: Sorry

I would have used Protractor from the beginning, but it doesn't allow low-level access to the browser (say, to load an extension).

Now that I look again, it turns out Karma (understandably) loads the tests via a special Kama-HTML page in the browser. A simple test, like driver.get(...) would change the page and disrupt the CLI Karma from the webpage Karma (test fail?). So now I'm pretty sure using Karma is a dead-end. :frowning:

Ideally, I want to be able to use Protractor tests, but the browser object would have further controls. Perhaps all of these should be features for Protractor, instead of a separate project?

ilbmiller commented 9 years ago

Thanks Nick - I had a hunch that Karma+Selenium-webdriver might be incompatible (both want to drive the browser), but nice to see it confirmed.

On to Protractor, which has (some) support for non-AngularJS apps (or maybe more accurately, has additional features for AngularJS apps that I just won't use).

Thanks again, Brad

From: Nick Oliver notifications@github.com To: PixnBits/karma-selenium-webdriver-launcher karma-selenium-webdriver-launcher@noreply.github.com, Cc: ilbmiller bmiller@abinitio.com Date: 02/24/2015 09:18 PM Subject: Re: [karma-selenium-webdriver-launcher] Need doc on how to access webdriver driver from jasmine test (#1)

TL;DR: I didn't write that part yet Sorry I would have used Protractor from the beginning, but it doesn't allow low-level access to the browser (say, to load an extension). Now that I look again, it turns out Karma (understandably) loads the tests via a special Kama-HTML page in the browser. A simple test, like driver.get(...) would change the page and disrupt the CLI Karma from the webpage Karma (test fail?). So now I'm pretty sure using Karma is a dead-end. Ideally, I want to be able to use Protractor tests, but the browser object would have further controls. Perhaps all of these should be features for Protractor, instead of a separate project? — Reply to this email directly or view it on GitHub.

NOTICE from Ab Initio: This email (including any attachments) may contain information that is subject to confidentiality obligations or is legally privileged, and sender does not waive confidentiality or privilege. If received in error, please notify the sender, delete this email, and make no further use, disclosure, or distribution.

PixnBits commented 9 years ago

Not that it helps @ilbmiller :frowning: but as a note FWIW Protractor may give access to the browser instance in a test https://github.com/angular/protractor/blob/master/lib/protractor.js#L118 and https://github.com/angular/protractor/blob/master/lib/runner.js#L154

ilbmiller commented 9 years ago

Thanks Nick.

Protractor does give access directly to the webdriver via the 'browser.driver' variable in global scope (but you can also use 'browser' directly). Only tricky part is making sure that you invoke ' browser.ignoreSynchronization = true' so that it doesn't wait (forever) for angular js app to initialize.

Below is a simple protractor example that worked for me (this is the javascript produced by the typescript compiler). A helpful link that got me going was: http://ng-learn.org/2014/02/Protractor_Testing_With_Angular_And_Non_Angular_Sites/

Note that 'browser', by/By, and 'element' variables are automagically put in the test scope by protractor (and there are more).

cheers, Brad

*** EchoSpec.js ** ///<reference path="../../../../../../typings/angular-protractor/angular-protractor.d.ts" /> ///

"use strict"; describe('basic echo test', function () { var echoInputEl; var echoResultEl; var echoBtnEl; beforeAll(function () { // Otherwise waits for angular js app to fully initialize, which never happens as my app isn't angular... browser.ignoreSynchronization = true; }); beforeEach(function () { // Best practice: isolate page-specific html bindings needed by tests (aka page object) browser.get('http://localhost:8080/cc/js/cv4/cv4.html#/mainTab/3' ); echoInputEl = element(by.id('echoInput')); echoInputEl.clear(); echoResultEl = element(by.id('echoResult')); echoBtnEl = element(by.id('echoBtn')); }); it('verify echo submit button', function (done) { var msg = 'foo'; echoInputEl.sendKeys(msg); echoBtnEl.click(); // Now wait for response, and check echoResult field echoResultEl.getText().then(function (txt) { expect(txt).toBe(msg); done(); }); }); it('verify enter will submit echo request', function (done) { // This will enter 'bar' and hit enter, thereby causing page to async submit var msg = 'bar'; echoInputEl.sendKeys(msg + '\n'); // Now wait for response, and check echoResult field echoResultEl.getText().then(function (txt) { expect(txt).toBe(msg); done(); }); }); });

From: Nick Oliver notifications@github.com To: PixnBits/karma-selenium-webdriver-launcher karma-selenium-webdriver-launcher@noreply.github.com, Cc: ilbmiller bmiller@abinitio.com Date: 02/25/2015 11:59 AM Subject: Re: [karma-selenium-webdriver-launcher] Need doc on how to access webdriver driver from jasmine test (#1)

Not that it helps @ilbmiller but as a note FWIW Protractor may give access to the browser instance in a test https://github.com/angular/protractor/blob/master/lib/protractor.js#L118 and https://github.com/angular/protractor/blob/master/lib/runner.js#L154 — Reply to this email directly or view it on GitHub.

NOTICE from Ab Initio: This email (including any attachments) may contain information that is subject to confidentiality obligations or is legally privileged, and sender does not waive confidentiality or privilege. If received in error, please notify the sender, delete this email, and make no further use, disclosure, or distribution.