gpt4 book ai didi

javascript - 无法使用 Promise 等待元素

转载 作者:行者123 更新时间:2023-12-01 00:48:29 32 4
gpt4 key购买 nike

无法使用 Promise 等待元素

我尝试过使用异步等待

仍然无法执行第二个 promise

import {browser, by, element, ElementFinder} from "protractor"
import {log4jsconfig} from '../../config/log4jsconfig'

describe('Angular Application', function() {
it("Verify if dynamic search is getting populated", function() {
console.log('calling before each');
log4jsconfig.Log().debug('calling before each')
browser.get('https://angular.io/');
element(by.xpath("//input[@placeholder='Search']")).sendKeys("kit");
element(by.xpath("//div[@class='search results']")).isPresent().then(function(val){
log4jsconfig.Log().debug("Expected Val : "+ val)
element(by.xpath("//li/a/span[text()='Press kit']")).getText().then(function(val1){
log4jsconfig.Log().debug("Expected Val1 : "+ val1)
expect(val1).toEqual('Press kit')
})
})
})
})

Config.ts

var HtmlReporter = require('protractor-beautiful-reporter');

exports.config={
framework:'jasmine',
/**
* The timeout in milliseconds for each script run on the browser. This
* should be longer than the maximum time your application needs to
* stabilize between tasks.
*/
allScriptsTimeout: 100000,

getPageTimeout:100000,
capabilities:{
'browserName':'chrome',
chromeOptions: {
args: ['--start-maximized'] // THIS!
}
},

specs:['**/Search/*.js'],

/**
* Use default globals: 'protractor', 'browser', '$', '$$', 'element',
'by'.
* These also exist as properties of the protractor namespace:
* 'protractor.browser', 'protractor.$', 'protractor.$$',
* 'protractor.element', 'protractor.by', and 'protractor.By'.
*
* When no globals is set to true, the only available global variable
will be
* 'protractor'.
*/
noglobal:false,
SELENIUM_PROMISE_MANAGER: false,

// Options to be passed to Jasmine.
jasmineNodeOpts: {
defaultTimeoutInterval: 30000
},

onPrepare: function() {
let folderName = "ReportFor_"+Date.now()
// Add a screenshot reporter and store screenshots to
`/tmp/screenshots`:
jasmine.getEnv().addReporter(new HtmlReporter({
baseDirectory: './../Reports/'+folderName
, screenshotsSubfolder: 'images'
, jsonsSubfolder: 'jsons'
}).getJasmine2Reporter());
}
}

我希望驱动程序等待元素“//div[@class='search-results']”出现。

我希望驱动程序等到 div 出现,但 promise 不起作用

最佳答案

您在 Protractor conf.js中设置了SELENIUM_PROMISE_MANAGER: false,因此您必须使用async/await 控制 Promise 执行顺序。

it("Verify if dynamic search is getting populated", function() {
console.log('calling before each');
log4jsconfig.Log().debug('calling before each')
await browser.get('https://angular.io/');
await element(by.xpath("//input[@placeholder='Search']")).sendKeys("kit");

let EC = protractor.ExpectedConditions;
let waitTarget = element(by.xpath("//div[@class='search results']");

await browser.wait(EC.presenceOf(waitTarget), 15000);
// wait appear until exceed 15 seconds, then throw timeout exception

let txt = await element(by.xpath("//li/a/span[text()='Press kit']")).getText();

log4jsconfig.Log().debug("txt : "+ txt);

expect(txt).toEqual('Press kit')
})

关于javascript - 无法使用 Promise 等待元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57182991/

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