无法使用承诺等待元素



无法使用 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='搜索结果']"存在。

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

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

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')
})

您可以使用量角器预期条件来等待元素出现:

browser.wait(protractor.ExpectedConditions.visibilityOf(element(by.xpath("//div[@class='search results']"))), 5000); // you can set the time to wait until element visibility

最新更新