Pritam C.
Pritam C.

Reputation: 21

unable to wait for element using promises

Unable to wait for the element using promises

I have tried using async wait

Still, unable to execute the second promise that is

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());
 }
}

I want the driver to wait for the element "//div[@class='search-results']" to be present.

I want driver to wait till the div appears but the promise is not working

Upvotes: 0

Views: 132

Answers (2)

Manoela
Manoela

Reputation: 1

You can use protractor ExpectedConditions to wait the element appear:

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

Upvotes: 0

yong
yong

Reputation: 13712

You set SELENIUM_PROMISE_MANAGER: false in protractor conf.js, therefor you have to use async/await to control promise execution order.

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

Upvotes: 2

Related Questions