Protractor can't get value from executescript it returns a promise

Asked
Active3 hr before
Viewed126 times

9 Answers

executescriptreturnspromiseprotractorvalue
90%

Example_snippet/controller/utility/_executescript.js/ constructor(){ navigateT. . .
 constructor() {
    navigateTo('/');
    browser.executeScript('window.localStorage.clear();');
    browser.executeScript('window.stop();');
    browser.sleep(1000);
    browser.waitForAngularEnabled(false);
 }
load more v
88%

sleep(millis: number): Promise<void> , Returns Protractor configuration parameter at path,, Pause the actor flow for a specified number of milliseconds

Example_snippet/controller/utility/_executescript.js/ import {BrowseTheWeb} from '@s. . .
import {
   BrowseTheWeb
} from '@serenity-js/protractor/lib/screenplay/abilities'
load more v
72%

This method returns a WebElement if the javascript command results in an HTML element,,A Double value is returned, if the javascript command results in Decimal value

Example_snippet/controller/utility/_executescript.js/ // access the methods browser. . .
// access the methods
browser.executeScript("javascript command", "arguments");
browser.executeAsyncScript("javascript command");
browser.executeScriptWithDescription("javascript command", "description message", "arguments")
load more v
65%

I'm trying to use protractor to test an angularjs controller function so I can do some stress testing, I'm trying to test $http

Example_snippet/controller/utility/_executescript.js/ $scope.testfunc = -> 42 $. . .
$scope.testfunc = - >
   42

$scope.testasyncfunc = - >
   $http.get("/current_user_id.json").success(data) - >
   43 # Hard - coded value
for debugging our tests
load more v
75%

string protractor,Key

Example_snippet/controller/utility/_executescript.js/ n/a. . .
n / a
load more v
40%

What is Promise in Protractor?, What is Promise in Protractor? ,There is no guarantee that tests will be executed sequentially, so by the time it reaches the expect statement name variable might not have any value,,A promise is an object that represents the eventual completion (success or failure) of an asynchronous operation and its resulting value

Example_snippet/controller/utility/_returns.js/ // spec.ts describe('Promise D. . .
// spec.ts
describe('Promise Demo', () => {
   it('Sample', () => {
      browser.get('https://material.angularjs.org/latest/demo/autocomplete')
      element(by.name('basic-usage')).click();
      element(by.id('input-99')).sendKeys('Test');
      const name = element(by.id('input-99')).getAttribute("value");
      expect(name).toBe("Test");
   });
});
load more v
22%

You can also pass a regular function to Protractor:,Notice here that our function has a return value, browser

Example_snippet/controller/utility/_returns.js/ browser.executeScript('1+1') . . .
browser.executeScript('1+1')
load more v
60%

The site configured at this address does not contain the requested file, , Read the full documentation for more information about using GitHub Pages

Example_snippet/controller/utility/_returns.js/ http://example.com/. . .
http: //example.com/
load more v
48%

The first one returns a promise and I don't know how to get the value from a promise,, your second example seems to miss return before browser

Example_snippet/controller/utility/_returns.js/ async function getText(): Prom. . .
async function getText(): Promise<string> {
  const text = browser.executeScript(`return 'some text';`);
  return text;
});

...
...

let text = await getText();
load more v