Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save roycollings/4196422 to your computer and use it in GitHub Desktop.
Save roycollings/4196422 to your computer and use it in GitHub Desktop.
AngularJS E2E: (futures) compare a value on page A with a value on page B.
//
// 2 pages:
// A: "Employee" page containing a link to an employee.
// B: "Person" page - click employee and it brings you here.
// There is an AngularJS binding called "person.name" which
// should match the Employee's name you clicked on.
//
describe('The link in the Employee screen', function() {
it('takes you to the correct "Person" page', function() {
element('a').query(function (selectedElements, done) {
element('a').click(); // Click this list item.
// The "selectedElements.text()" is getting this string "<a ..> HERE </a>"
// from the link.
//
// (NOTE: This is a JQuery ".text()", NOT an Angular one!)
expect(binding('person.name')).toBe(selectedElements.text());
done();
});
});
});
//
// If it's a list of links, then use $(this).text() ... or any jQuery method that works:
//
var elementSel = 'li a:eq(0)';
element(elementSel).query(function (selectedElements, done) {
selectedElements.each(function(idx,elm) {
var clickedHref = $(this).attr("href").replace(/./,'/'); // JQuery methods, NOT AngularJS.
element('li a:eq(' + idx + ')').click();
expect(browser().location().url()).toBe(clickedHref);
browser().navigateTo("/app/index.html"); // Prepare to test the next link in the loop.
});
done();
});
@roycollings
Copy link
Author

A possible alternative (but I can't get e2e components to work in the final setInterval function!):

    //
    // Wait for the list elements to resolve, then
    // assign the count to the variable "x".
    //
    var x = [];
    it('get list count into x', function() {
        var y = element('li a').count();
        var z = setInterval(function() {
            if (y.fulfilled) {
                clearInterval(z);
                console.log("pushing to x: " + y.value);
                x.push(y.value);
            }
        },200);
    });

    //
    // Now loop through each list item, clicking +
    // running tests etc... against each one, in it's
    // own "it" statement.
    //
    var z2 = setInterval(function() {
        if (x > 0) {
            clearInterval(z2);
            console.log("X: " + x);
            ... "x" is now usable from this point onwards because it contains the value you were waiting for ...

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