Reputation: 21
I have a issue in my test e2e. I was reading a lot of protractor and selenium and don't find a solution.
My test e2e is :
it('Recorro tabla de MCSS, verificando que existan datos', function () {
browser.sleep(1000);
browser.ignoreSynchronization = true;
logger.info("Recorro tabla de MCSS, verificando que existan datos");
utils.awaitElement(element.all(by.repeater('item in alertsMCSS'))).then(function (rows) {
rows.forEach(function (row) {
row.all(by.tagName('td')).then(function (columns) {
utils.awaitElement(columns[0].getAttribute('innerText')).then(function (instancia) {
logger.info('MCSS: ' + instancia);
expect(instancia !== "");
});
utils.awaitElement(columns[1].getAttribute('innerText')).then(function (valor) {
logger.info('Valor: ' + valor);
expect(valor !== "");
});
});
});
});
browser.ignoreSynchronization = false;
});
The function utils.awaits (I found this function here ) but I don't know if really works or not:
var awaitElement = function (item) {
var EC = protractor.ExpectedConditions;
var ee = item;
browser.wait(EC.presenceOf(ee), 5000);
expect(ee.isPresent()).toBeTruthy();
return ee;
};
When I run the test, sometimes it's all okey, and sometimes the test fails. The description of failure is :
StaleElementReferenceError: stale element reference: element is not attached to the page document
at WebDriverError (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/error.js:27:5)
at StaleElementReferenceError (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/error.js:227:5)
at Object.checkLegacyResponse (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/error.js:505:15)
at parseHttpResponse (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/http.js:509:13)
at doSend.then.response (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/http.js:440:13)
at process._tickCallback (internal/process/next_tick.js:109:7)
From: Task: WebElement.getAttribute(innerText)
What's the issue ? How can I resolved?
Upvotes: 2
Views: 1073
Reputation: 5016
I would try to approach this problem with some of the built in Protractor functionality. My guess is that you are referencing elements that exist but has been lost by reference.
Below is my attempt to refactor the code above with some of the built in Protractor APIs.
it('Recorro tabla de MCSS, verificando que existan datos', function () {
browser.sleep(1000);
browser.ignoreSynchronization = true;
logger.info("Recorro tabla de MCSS, verificando que existan datos");
// element all by repeater should only be used for AngularJS and not Angular
// see element.all each methods link below.
element.all(by.repeater('item in alertsMCSS')).each(function(rowElement) {
// after we get each row, we could get all table data for that row
columnElements = row.all(by.tagName('td');
// use the element.all get method to get the first and second elements
instanciaElement = columnElements.get(0);
valorElement = columnElement.get(1);
// instead of using innerHtml, think about using getText
instanciaElement.getText().then(function(text) {
logger.info('MCSS: ' + text);
// use jasmine not tobe method. see link below.
// expect(text !== "") is not a valid jasmine statement
expect(text).not.toBe("");
});
valorElement.getText().then(function(text) {
logger.info('Valor: ' + text);
expect(text).not.toBe("");
});
});
browser.ignoreSynchronization = false;
});
Instead of doing a forEach
, think about using the element.all each method. After you cycle through each row, you can get a handle for all table data elements. Then you can use element.all get to a specific element by index. After we have the element we want to log and test, we could use the built in getText method. Since this returns a promise, we need to resolve the promise to get the text. From there we should consider using a valid jasmine statement. See expectations jasmine docs here.
Upvotes: 1