量角器 - 当元素不可见且不执行 "else" 条件时测试失败
Protractor - Test is failed when element is not visible and doesn't perform the "else" condition
我已经尝试 运行 一个流程 "if/else statement" 取决于某些元素是否可见。
但是如果这个元素不可见,它不会 运行 进入 "else condition" 并且所有测试用例都失败了。
请大家帮忙!谢谢。
测试用例:
it('New Client LiveSite - Payments - Perform a payment by PayPal', function () {
browser.wait(EC.visibilityOf(element(by.binding("buttonText"))), 30000);
element(by.binding("buttonText")).click();
var mainWindow;
browser.getAllWindowHandles().then(
function(handles) {
mainWindow = handles[0]; //at this point there should be only 1 window
}
);
browser.getAllWindowHandles().then(function (handles) {
handles.forEach(function(handle) {
if (handle !== mainWindow) {
browser.switchTo().window(handle)
var loadLogin = element(by.id("loadLogin"));
element.all(by.id('loadLogin')).then(function(){
expect(browser.driver.getCurrentUrl()).toContain('www.paypal.com');
browser.sleep(10000);
loadLogin.isDisplayed().then(function(result) {
if (result === true) {
loadLogin.click();
browser.driver.sleep(3000);
element(by.id("login_email")).sendKeys("username");
element(by.id("login_password")).sendKeys("pass");
browser.driver.sleep(1000);
element(by.id("submitLogin")).click();
browser.driver.sleep(5000);
element(by.id("submit.x")).click();
browser.driver.sleep(10000);
console.log("-=-=- first condition - I'm not logged-in -=-=-");
} else {
browser.driver.sleep(3000);
element(by.id("login_email")).clear();
browser.driver.sleep(1000);
element(by.id("login_email")).sendKeys("username");
element(by.id("login_password")).sendKeys("pass");
browser.driver.sleep(1000);
element(by.id("submitLogin")).click();
browser.driver.sleep(10000);
element(by.id("submit.x")).click();
browser.driver.sleep(10000);
console.log("-=-=- second condition - I'm already logged-in -=-=-")
}
});
});
}
});
});
browser.getAllWindowHandles().then(function (handles) {
browser.switchTo().window(handles[0]);
});
});
错误:[已编辑]
New Client LiveSite - Payments - Perform a payment by PayPal - fail
Error occurred in loadLogin.isDisplayed: { [NoSuchElementError: No element found
using locator: By.id("loadLogin")]
code: 7,
state: 'no such element',
message: 'No element found using locator: By.id("loadLogin")',
name: 'NoSuchElementError',
stack: 'NoSuchElementError: No element found using locator: By.id("loadLogin")
\n at new bot.Error (C:\Users\idan\AppData\Roaming\npm\node_modules\pr
otractor\node_modules\selenium-webdriver\lib\atoms\error.js:113:18)\n at
C:\Users\idan\AppData\Roaming\npm\node_modules\protractor\lib\element.
js:706:15\n at [object Object].promise.ControlFlow.runInFrame_ (C:\Users\id
an\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webd
river\lib\webdriver\promise.js:1877:20)\n at [object Object].promise.Callb
ack_.goog.defineClass.notify (C:\Users\idan\AppData\Roaming\npm\node_modul
es\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:246
4:25)\n at [object Object].promise.Promise.notify_ (C:\Users\idan\AppData\
\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\
webdriver\promise.js:563:12)\n at Array.forEach (native)\n at Object.goog
.array.forEach (C:\Users\idan\AppData\Roaming\npm\node_modules\protractor
\node_modules\selenium-webdriver\lib\goog\array\array.js:203:43)\n at [
object Object].promise.Promise.notifyAll_ (C:\Users\idan\AppData\Roaming\np
m\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\p
romise.js:552:16)\n at goog.async.run.processWorkQueue (C:\Users\idan\AppD
ata\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\l
ib\goog\async\run.js:125:21)\n at runMicrotasksCallback (node.js:337:7)\nE
rror\n at [object Object].ElementArrayFinder.applyAction_ (C:\Users\idan\A
ppData\Roaming\npm\node_modules\protractor\lib\element.js:403:21)\n at
[object Object].self.(anonymous function) [as isDisplayed] (C:\Users\idan\App
Data\Roaming\npm\node_modules\protractor\lib\element.js:76:19)\n at [ob
ject Object].self.(anonymous function) [as isDisplayed] (C:\Users\idan\AppDat
a\Roaming\npm\node_modules\protractor\lib\element.js:733:11)\n at c:\a
utomation\tests\paymentsNewClient.js:71:39\n at [object Object].promise.Con
trolFlow.runInFrame_ (C:\Users\idan\AppData\Roaming\npm\node_modules\prot
ractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:1877:20)\n
at [object Object].promise.Callback_.goog.defineClass.notify (C:\Users\idan
\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdri
ver\lib\webdriver\promise.js:2464:25)\n at [object Object].promise.Promise
.notify_ (C:\Users\idan\AppData\Roaming\npm\node_modules\protractor\node
_modules\selenium-webdriver\lib\webdriver\promise.js:563:12)\n at Array.f
orEach (native)\n at Object.goog.array.forEach (C:\Users\idan\AppData\Roa
ming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\goog
\array\array.js:203:43)\n at [object Object].promise.Promise.notifyAll_ (C:
\Users\idan\AppData\Roaming\npm\node_modules\protractor\node_modules\se
lenium-webdriver\lib\webdriver\promise.js:552:16)' }
错误提示未找到 loadLogin
元素。它并没有说它不可见。
NoSuchElementError: No element found using locator: By.id("loadLogin")
Stacktrace:
NoSuchElementError: No element found using locator: By.id("loadLogin")
您似乎打算在切换新的 window 后与 loadLogin
元素进行交互,但您似乎也在尝试在切换前找到元素 loadLogin
到 window。在测试开始时检查下面的代码。也许您需要切换到存在 loadLogin
元素的 window,然后查找它。
it('New Client LiveSite - Payments - Perform a payment by PayPal', function () {
var loadLogin = element(by.id("loadLogin"));
browser.sleep(2000);
编辑 1
当元素不可见时可能会抛出一些异常,这就是为什么您永远不会遇到 else 条件的原因。也许添加这样的东西,
loadLogin.isDisplayed().then(function(result) {
//keep your code here
}, function(err) {
console.log('Error occurred in loadLogin.isDisplayed:', err);
});
我已经尝试 运行 一个流程 "if/else statement" 取决于某些元素是否可见。
但是如果这个元素不可见,它不会 运行 进入 "else condition" 并且所有测试用例都失败了。
请大家帮忙!谢谢。
测试用例:
it('New Client LiveSite - Payments - Perform a payment by PayPal', function () {
browser.wait(EC.visibilityOf(element(by.binding("buttonText"))), 30000);
element(by.binding("buttonText")).click();
var mainWindow;
browser.getAllWindowHandles().then(
function(handles) {
mainWindow = handles[0]; //at this point there should be only 1 window
}
);
browser.getAllWindowHandles().then(function (handles) {
handles.forEach(function(handle) {
if (handle !== mainWindow) {
browser.switchTo().window(handle)
var loadLogin = element(by.id("loadLogin"));
element.all(by.id('loadLogin')).then(function(){
expect(browser.driver.getCurrentUrl()).toContain('www.paypal.com');
browser.sleep(10000);
loadLogin.isDisplayed().then(function(result) {
if (result === true) {
loadLogin.click();
browser.driver.sleep(3000);
element(by.id("login_email")).sendKeys("username");
element(by.id("login_password")).sendKeys("pass");
browser.driver.sleep(1000);
element(by.id("submitLogin")).click();
browser.driver.sleep(5000);
element(by.id("submit.x")).click();
browser.driver.sleep(10000);
console.log("-=-=- first condition - I'm not logged-in -=-=-");
} else {
browser.driver.sleep(3000);
element(by.id("login_email")).clear();
browser.driver.sleep(1000);
element(by.id("login_email")).sendKeys("username");
element(by.id("login_password")).sendKeys("pass");
browser.driver.sleep(1000);
element(by.id("submitLogin")).click();
browser.driver.sleep(10000);
element(by.id("submit.x")).click();
browser.driver.sleep(10000);
console.log("-=-=- second condition - I'm already logged-in -=-=-")
}
});
});
}
});
});
browser.getAllWindowHandles().then(function (handles) {
browser.switchTo().window(handles[0]);
});
});
错误:[已编辑]
New Client LiveSite - Payments - Perform a payment by PayPal - fail
Error occurred in loadLogin.isDisplayed: { [NoSuchElementError: No element found
using locator: By.id("loadLogin")]
code: 7,
state: 'no such element',
message: 'No element found using locator: By.id("loadLogin")',
name: 'NoSuchElementError',
stack: 'NoSuchElementError: No element found using locator: By.id("loadLogin")
\n at new bot.Error (C:\Users\idan\AppData\Roaming\npm\node_modules\pr
otractor\node_modules\selenium-webdriver\lib\atoms\error.js:113:18)\n at
C:\Users\idan\AppData\Roaming\npm\node_modules\protractor\lib\element.
js:706:15\n at [object Object].promise.ControlFlow.runInFrame_ (C:\Users\id
an\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webd
river\lib\webdriver\promise.js:1877:20)\n at [object Object].promise.Callb
ack_.goog.defineClass.notify (C:\Users\idan\AppData\Roaming\npm\node_modul
es\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:246
4:25)\n at [object Object].promise.Promise.notify_ (C:\Users\idan\AppData\
\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\
webdriver\promise.js:563:12)\n at Array.forEach (native)\n at Object.goog
.array.forEach (C:\Users\idan\AppData\Roaming\npm\node_modules\protractor
\node_modules\selenium-webdriver\lib\goog\array\array.js:203:43)\n at [
object Object].promise.Promise.notifyAll_ (C:\Users\idan\AppData\Roaming\np
m\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\p
romise.js:552:16)\n at goog.async.run.processWorkQueue (C:\Users\idan\AppD
ata\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\l
ib\goog\async\run.js:125:21)\n at runMicrotasksCallback (node.js:337:7)\nE
rror\n at [object Object].ElementArrayFinder.applyAction_ (C:\Users\idan\A
ppData\Roaming\npm\node_modules\protractor\lib\element.js:403:21)\n at
[object Object].self.(anonymous function) [as isDisplayed] (C:\Users\idan\App
Data\Roaming\npm\node_modules\protractor\lib\element.js:76:19)\n at [ob
ject Object].self.(anonymous function) [as isDisplayed] (C:\Users\idan\AppDat
a\Roaming\npm\node_modules\protractor\lib\element.js:733:11)\n at c:\a
utomation\tests\paymentsNewClient.js:71:39\n at [object Object].promise.Con
trolFlow.runInFrame_ (C:\Users\idan\AppData\Roaming\npm\node_modules\prot
ractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:1877:20)\n
at [object Object].promise.Callback_.goog.defineClass.notify (C:\Users\idan
\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdri
ver\lib\webdriver\promise.js:2464:25)\n at [object Object].promise.Promise
.notify_ (C:\Users\idan\AppData\Roaming\npm\node_modules\protractor\node
_modules\selenium-webdriver\lib\webdriver\promise.js:563:12)\n at Array.f
orEach (native)\n at Object.goog.array.forEach (C:\Users\idan\AppData\Roa
ming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\goog
\array\array.js:203:43)\n at [object Object].promise.Promise.notifyAll_ (C:
\Users\idan\AppData\Roaming\npm\node_modules\protractor\node_modules\se
lenium-webdriver\lib\webdriver\promise.js:552:16)' }
错误提示未找到 loadLogin
元素。它并没有说它不可见。
NoSuchElementError: No element found using locator: By.id("loadLogin")
Stacktrace:
NoSuchElementError: No element found using locator: By.id("loadLogin")
您似乎打算在切换新的 window 后与 loadLogin
元素进行交互,但您似乎也在尝试在切换前找到元素 loadLogin
到 window。在测试开始时检查下面的代码。也许您需要切换到存在 loadLogin
元素的 window,然后查找它。
it('New Client LiveSite - Payments - Perform a payment by PayPal', function () {
var loadLogin = element(by.id("loadLogin"));
browser.sleep(2000);
编辑 1
当元素不可见时可能会抛出一些异常,这就是为什么您永远不会遇到 else 条件的原因。也许添加这样的东西,
loadLogin.isDisplayed().then(function(result) {
//keep your code here
}, function(err) {
console.log('Error occurred in loadLogin.isDisplayed:', err);
});