Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expect two elements to be equal

I want to test if two elements in two different pages are equal. The reason for this is that I need to check a "copy" function that already works in my page, so both elements (divs in this case) have to be indentical:

I found that there's a method in protractor for element objects called "clone" but doesn't explains its purpose that much. Anyway I tried this:

// In the first page:
browser.get("/page1");
var clone1 = element(by.id("firstElem")).clone();

// then navigating to the other page
browser.get("/page2");
var clone2 = element(by.id("secondElem")).clone();

// then the expectation of them to be equal
expect(clone1).toEqual(clone2);

but the expectation fails with a very heavy stacktrace. Also tried comparing:

expect(clone1 == clone2).toBeTruthy();

which fails again.

  • What is "clone()" supposed to be used for? and,

  • How do I compare two divs in two separate pages for being identical?

like image 809
Cyberdelphos Avatar asked Sep 13 '26 12:09

Cyberdelphos


1 Answers

What is "clone()" supposed to be used for?

I've recently created a closely related question, you can follow the updates there:

  • Cloning element finders

How do I compare two divs in two separate pages for being identical?

Depending on your end goal, you may compare "outer HTML" representations of the elements using getOuterHtml() , example:

browser.get("/page1");
element(by.id("firstElem")).getOuterHtml().then(function(value) {
    browser.get("/page2");
    expect(element(by.id("secondElem")).getOuterHtml()).toEqual(value);
});
like image 199
alecxe Avatar answered Sep 16 '26 08:09

alecxe