title: Selenium: How do I write a synchronous Selenium automation?
body: <p>I am becoming more and more frustrated at the moment and I hope to find some help on stackexchange.</p>
  
  <p>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. </p>
  
  <p>Please, can anyone tell me, how the heck I can let my code being executed in a synchronous way from top to bottom?</p>
  
  <p>After lots and lots of hours googling, I have tryed the following so far:</p>
  
  <ul>
  <li>begin line 1 with <code>#! /usr/bin/env node</code> and start in terminal using <code>./app.js</code></li>
  <li>make every function() an <code>async</code> function()</li>
  <li>use <code>await</code> on all methods()</li>
  </ul>
  
  <p>But even now, when I run the script, it throws me a lot of <code>UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'isDisplayed' of undefined</code> 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.</p>
  
  <p>I am using:
  * selenium-webdriver 3.6.0
  * Firefox 60.0.2
  * node 8.10.0</p>
  
  <p>My code basicaly looks like this:</p>
  
  <pre><code>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();
  </code></pre>
  
  <p>So as far as I understand, I am starting the webdriver and firefox with my <code>async init()</code> 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).</p>
  
  <p>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) <code>UnhandledPromiseRejectionWarning</code>.. </p>
  
  <ul>
  <li><code>UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'sendKeys' of undefined</code></li>
  <li><code>UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'click' of undefined</code></li>
  <li><code>UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'isDisplayed' of undefined</code></li>
  </ul>
  
  <p>I would reallllyy appreciate some help here.  </p>