I am becoming more and more frustrated at the moment and I hope to find some help on stackexchange.
First things first: I am not an experienced Javascript dev, probably not an experienced dev at all, but I guess I know how to work with basic scripting - I know a little bit of C#, Java and co.. For my current web automation script I thought it would be a good idea to catch up and try writing in Javascript, but right now I am at a point, where I consider to start from scratch with a different not so confusing language.
Please, can anyone tell me, how the heck I can let my code being executed in a synchronous way from top to bottom?
After lots and lots of hours googling, I have tryed the following so far:
- begin line 1 with
#! /usr/bin/env node
and start in terminal using./app.js
- make every function() an
async
function() - use
await
on all methods()
But even now, when I run the script, it throws me a lot of UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'isDisplayed' of undefined
and similar things, which let me guess, that node is running some of my methods/functions asynchronous. Those exceptions pop up in console well before the browser windows is even up and loaded.
I am using: * selenium-webdriver 3.6.0 * Firefox 60.0.2 * node 8.10.0
My code basicaly looks like this:
const {Builder, By, Key, until, manage} = require('selenium-webdriver');
const firefox = require('selenium-webdriver/firefox');
const webdriver = require('selenium-webdriver');
const fs = require('fs');
//
// declaring lots of global variables, I need in my functions
//
async function init(){
let options = await new firefox.Options()
.setProfile('/home/ruphus/.mozilla/firefox/selenium_profile.backtesting');
let driver = await new webdriver.Builder()
.forBrowser('firefox')
.setFirefoxOptions(options)
.build();
await driver.get('https://www.someurl.com/')
.then(openStrategySettings())
.then(btnStrategySettings.click());
// ... defining webelements/locations to the previously created objects - using xpath
inputPeriod = await driver.findElement(By.xpath("//div[@id='header-toolbar-intervals']/div/div/div"));
}
async function openStrategySettings() {
if (!someWebelement.isDisplayed()){
await tabStrategyTester.click();
}
}
async function inputValue(element, value) {
await element.sendKeys(Key.BACK_SPACE + Key.BACK_SPACE + value.toString());
}
async function run(){
// this is the main function with a couple of for loops and array.forEach(function()... things
}
init();
run();
So as far as I understand, I am starting the webdriver and firefox with my async init()
function. Here I use await for all those methods. After starting webdriver/firefox I define the Object variables to the locations (I want that to happen, when the browser is up and loaded).
But somehow, I do not get why, the script seems to run all my functions and all code it can find straight after starting the script. Actually it seems that it waits to load the browser the last. Before it has finally loaded I get several(8) UnhandledPromiseRejectionWarning
..
UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'sendKeys' of undefined
UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'click' of undefined
UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'isDisplayed' of undefined
I would reallllyy appreciate some help here.