我尝试使用“if / else语句”运行一个流,这取决于某个元素是否可见。
但是在这个元素不可见的情况下,它不会遇到“else条件”,并且所有测试用例都失败了。
请帮忙!感谢。
测试用例:
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)' }
答案 0 :(得分:2)
错误表示找不到loadLogin
元素。它没有说它不可见。
NoSuchElementError: No element found using locator: By.id("loadLogin")
Stacktrace:
NoSuchElementError: No element found using locator: By.id("loadLogin")
在切换到新窗口后,您似乎打算与loadLogin
元素进行交互,但在切换到窗口之前,您似乎也在尝试查找元素loadLogin
。在测试开始时检查下面的代码。也许您需要切换到存在loadLogin
元素的窗口,然后查找它。
it('New Client LiveSite - Payments - Perform a payment by PayPal', function () {
var loadLogin = element(by.id("loadLogin"));
browser.sleep(2000);
当元素不可见时,可能会抛出一些异常,这就是为什么你永远不会遇到else条件。也许添加这样的东西,
loadLogin.isDisplayed().then(function(result) {
//keep your code here
}, function(err) {
console.log('Error occurred in loadLogin.isDisplayed:', err);
});