import time
import os

from behave import *
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

use_step_matcher("re")

options = Options()
if "GOOGLE_CHROME_SHIM" in os.environ:
    options.binary_location = os.environ.get("GOOGLE_CHROME_SHIM")
    driver = webdriver.Chrome(chrome_options=options, executable_path=options.binary_location)
else:
    driver = webdriver.Chrome()

# focus window on top
primary = driver.window_handles[0]
driver.switch_to.window(primary)

pause = 1


def before_all(context):
    pass


@given("I am on the login page")
def step_impl(context):
    driver.get('http://localhost:5000/login')
    time.sleep(pause)


@when("I enter a valid username and password")
def step_impl(context):
    driver.switch_to.window(primary)
    username = driver.find_element_by_id('username')
    username.send_keys("user")
    time.sleep(pause)
    password = driver.find_element_by_id('password')
    password.send_keys("pass")
    time.sleep(pause)
    driver.find_element_by_id('submit').click()


@then("I should see the welcome page")
def step_impl(context):
    assert driver.title == "Scuba Portugal - Welcome"


@when("I enter an invalid username or password")
def step_impl(context):
    driver.switch_to.window(primary)
    username = driver.find_element_by_id('username')
    username.send_keys("wronguser")
    time.sleep(pause)
    password = driver.find_element_by_id('password')
    password.send_keys("wrongpass")
    time.sleep(pause)
    driver.find_element_by_id('submit').click()


@then("I expect to be on the login page")
def step_impl(context):
    assert driver.title == "Scuba Portugal - Login"


@step("I expect to see an error message")
def step_impl(context):
    driver.switch_to.window(primary)
    message = driver.find_element_by_id('message').text
    assert message == "Incorrect Username/Password. Please try again."


@when("I am missing a username")
def step_impl(context):
    driver.switch_to.window(primary)
    username = driver.find_element_by_id('username')
    username.send_keys("")
    time.sleep(pause)
    password = driver.find_element_by_id('password')
    password.send_keys("wrongpass")
    time.sleep(pause)
    driver.find_element_by_id('submit').click()


@step("I expect to see an missing field error")
def step_impl(context):
    driver.switch_to.window(primary)
    message = driver.find_element_by_id('message').text
    assert message == "Missing Username"



def after_all(context):
    driver.close()

import time
import os

from behave import *
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

use_step_matcher("re")

options = Options()
if "GOOGLE_CHROME_SHIM" in os.environ:
    options.binary_location = os.environ.get("GOOGLE_CHROME_SHIM")
    driver = webdriver.Chrome(chrome_options=options, executable_path=options.binary_location)
else:
    driver = webdriver.Chrome()

port = 80
if "PORT" in os.environ:
    port = os.environ.get("PORT")

# focus window on top
primary = driver.window_handles[0]
driver.switch_to.window(primary)

pause = 1


def before_all(context):
    pass


@given("I am on the login page")
def step_impl(context):
    driver.get('http://localhost:' + port + '/login')
    time.sleep(pause)


@when("I enter a valid username and password")
def step_impl(context):
    driver.switch_to.window(primary)
    username = driver.find_element_by_id('username')
    username.send_keys("user")
    time.sleep(pause)
    password = driver.find_element_by_id('password')
    password.send_keys("pass")
    time.sleep(pause)
    driver.find_element_by_id('submit').click()


@then("I should see the welcome page")
def step_impl(context):
    assert driver.title == "Scuba Portugal - Welcome"


@when("I enter an invalid username or password")
def step_impl(context):
    driver.switch_to.window(primary)
    username = driver.find_element_by_id('username')
    username.send_keys("wronguser")
    time.sleep(pause)
    password = driver.find_element_by_id('password')
    password.send_keys("wrongpass")
    time.sleep(pause)
    driver.find_element_by_id('submit').click()


@then("I expect to be on the login page")
def step_impl(context):
    assert driver.title == "Scuba Portugal - Login"


@step("I expect to see an error message")
def step_impl(context):
    driver.switch_to.window(primary)
    message = driver.find_element_by_id('message').text
    assert message == "Incorrect Username/Password. Please try again."


@when("I am missing a username")
def step_impl(context):
    driver.switch_to.window(primary)
    username = driver.find_element_by_id('username')
    username.send_keys("")
    time.sleep(pause)
    password = driver.find_element_by_id('password')
    password.send_keys("wrongpass")
    time.sleep(pause)
    driver.find_element_by_id('submit').click()


@step("I expect to see an missing field error")
def step_impl(context):
    driver.switch_to.window(primary)
    message = driver.find_element_by_id('message').text
    assert message == "Missing Username"



def after_all(context):
    driver.close()

Use port 80 by default