gpt4 book ai didi

javascript - Protractor - 当元素不可见且不执行 "else"条件时测试失败

转载 作者:行者123 更新时间:2023-11-30 16:45:47 25 4
gpt4 key购买 nike

我尝试使用“if/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)' }

最佳答案

错误提示未找到 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);

编辑 1

当元素不可见时,可能会抛出一些异常,这就是为什么您永远不会遇到 else 条件的原因。也许添加这样的东西,

loadLogin.isDisplayed().then(function(result) {
//keep your code here
}, function(err) {
console.log('Error occurred in loadLogin.isDisplayed:', err);
});

关于javascript - Protractor - 当元素不可见且不执行 "else"条件时测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31262409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com